Add login test
This commit is contained in:
parent
5802ccf332
commit
cf50705d5a
|
@ -0,0 +1,53 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Zoomyboy\LaravelNami\Fakes;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Http;
|
||||||
|
|
||||||
|
class LoginFake extends Fake
|
||||||
|
{
|
||||||
|
public function succeeds(string $token): void
|
||||||
|
{
|
||||||
|
$this->fakeLogin($token, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function fails(string $token): void
|
||||||
|
{
|
||||||
|
$this->fakeLogin($token, 500);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function fakeLogin(string $token, int $statusCode): void
|
||||||
|
{
|
||||||
|
Http::fake(function ($request) use ($token, $statusCode) {
|
||||||
|
if ($request->url() !== 'https://nami.dpsg.de/ica/rest/nami/auth/manual/sessionStartup') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Http::response(json_encode([
|
||||||
|
"servicePrefix" => null,
|
||||||
|
"methodCall" => null,
|
||||||
|
"response" => null,
|
||||||
|
"statusCode" => $statusCode,
|
||||||
|
"statusMessage" => "",
|
||||||
|
"apiSessionName" => "JSESSIONID",
|
||||||
|
"apiSessionToken" => $token,
|
||||||
|
"minorNumber" => 2,
|
||||||
|
"majorNumber" => 1,
|
||||||
|
]), 200, [
|
||||||
|
'Set-Cookie' => 'JSESSIONID=' . $token . '.srv-nami06; path=/ica; secure; HttpOnly; Max-Age=9000; Expires=Sat, 24-Feb-2024 01:18:00 GMT'
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function assertSent(int $mglnr, string $password): void
|
||||||
|
{
|
||||||
|
Http::assertSent(function ($request) use ($mglnr, $password) {
|
||||||
|
return $request->url() === 'https://nami.dpsg.de/ica/rest/nami/auth/manual/sessionStartup'
|
||||||
|
&& $request->header('Content-Type')[0] === 'application/x-www-form-urlencoded'
|
||||||
|
&& $request['Login'] === 'API'
|
||||||
|
&& $request['redirectTo'] === './app.jsp'
|
||||||
|
&& $request['username'] === $mglnr
|
||||||
|
&& $request['password'] === $password;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,8 +4,10 @@ namespace Zoomyboy\LaravelNami\Tests\Unit;
|
||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Illuminate\Support\Facades\Http;
|
use Illuminate\Support\Facades\Http;
|
||||||
|
use Zoomyboy\LaravelNami\Authentication\Authenticator;
|
||||||
use Zoomyboy\LaravelNami\Authentication\MainCookie;
|
use Zoomyboy\LaravelNami\Authentication\MainCookie;
|
||||||
use Zoomyboy\LaravelNami\Fakes\CourseFake;
|
use Zoomyboy\LaravelNami\Fakes\CourseFake;
|
||||||
|
use Zoomyboy\LaravelNami\Fakes\LoginFake;
|
||||||
use Zoomyboy\LaravelNami\LoginException;
|
use Zoomyboy\LaravelNami\LoginException;
|
||||||
use Zoomyboy\LaravelNami\Nami;
|
use Zoomyboy\LaravelNami\Nami;
|
||||||
use Zoomyboy\LaravelNami\Tests\TestCase;
|
use Zoomyboy\LaravelNami\Tests\TestCase;
|
||||||
|
@ -20,46 +22,26 @@ class LoginTest extends TestCase
|
||||||
|
|
||||||
public function testItLoggsInAndSavesCookie(): void
|
public function testItLoggsInAndSavesCookie(): void
|
||||||
{
|
{
|
||||||
Http::fake([
|
app(LoginFake::class)->succeeds('lala-testsession');
|
||||||
'https://nami.dpsg.de/ica/rest/nami/auth/manual/sessionStartup' => Http::response(json_encode([
|
|
||||||
"servicePrefix" => null,
|
|
||||||
"methodCall" => null,
|
|
||||||
"response" => null,
|
|
||||||
"statusCode" => 0,
|
|
||||||
"statusMessage" => "",
|
|
||||||
"apiSessionName" => "JSESSIONID",
|
|
||||||
"apiSessionToken" => "lala-testsession",
|
|
||||||
"minorNumber" => 2,
|
|
||||||
"majorNumber" => 1,
|
|
||||||
]), 200, [
|
|
||||||
'Set-Cookie' => 'JSESSIONID=lala-testsession.srv-nami06; path=/ica; secure; HttpOnly; Max-Age=9000; Expires=Sat, 24-Feb-2024 01:18:00 GMT'
|
|
||||||
]),
|
|
||||||
]);
|
|
||||||
|
|
||||||
app(MainCookie::class)->login(12345, 'secret');
|
app(MainCookie::class)->login(12345, 'secret');
|
||||||
|
|
||||||
Http::assertSentCount(1);
|
Http::assertSentCount(1);
|
||||||
$cookie = file_get_contents(__DIR__ . '/../../.cookies_test/' . now()->timestamp . '.txt');
|
$cookie = file_get_contents(__DIR__ . '/../../.cookies_test/' . now()->timestamp . '.txt');
|
||||||
$this->assertEquals('lala-testsession.srv-nami06', $cookie);
|
$this->assertEquals('lala-testsession.srv-nami06', $cookie);
|
||||||
|
app(LoginFake::class)->assertSent(12345, 'secret');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItThrowsExceptionWhenLoginFails(): void
|
||||||
|
{
|
||||||
|
app(LoginFake::class)->fails('::token::');
|
||||||
|
$this->expectException(LoginException::class);
|
||||||
|
app(MainCookie::class)->login(12345, 'secret');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testItDoesntSaveCookieWhenLoginFails(): void
|
public function testItDoesntSaveCookieWhenLoginFails(): void
|
||||||
{
|
{
|
||||||
Http::fake([
|
app(LoginFake::class)->fails('::token::');
|
||||||
'https://nami.dpsg.de/ica/rest/nami/auth/manual/sessionStartup' => Http::response(json_encode([
|
|
||||||
"servicePrefix" => null,
|
|
||||||
"methodCall" => null,
|
|
||||||
"response" => null,
|
|
||||||
"statusCode" => 500,
|
|
||||||
"statusMessage" => "",
|
|
||||||
"apiSessionName" => "JSESSIONID",
|
|
||||||
"apiSessionToken" => "lala-testsession",
|
|
||||||
"minorNumber" => 2,
|
|
||||||
"majorNumber" => 1,
|
|
||||||
]), 200, [
|
|
||||||
'Set-Cookie' => 'JSESSIONID=lala-testsession.srv-nami06; path=/ica; secure; HttpOnly; Max-Age=9000; Expires=Sat, 24-Feb-2024 01:18:00 GMT'
|
|
||||||
]),
|
|
||||||
]);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
app(MainCookie::class)->login(12345, 'secret');
|
app(MainCookie::class)->login(12345, 'secret');
|
||||||
|
@ -93,24 +75,10 @@ class LoginTest extends TestCase
|
||||||
$this->assertCount(1, glob(__DIR__ . '/../../.cookies_test/*'));
|
$this->assertCount(1, glob(__DIR__ . '/../../.cookies_test/*'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testItRefreshesExpiredCookie(): void
|
public function testItIgnoresExpiredCookie(): void
|
||||||
{
|
{
|
||||||
app(CourseFake::class)->fetches(103, []);
|
app(CourseFake::class)->fetches(103, []);
|
||||||
Http::fake([
|
app(LoginFake::class)->succeeds('newlogin');
|
||||||
'https://nami.dpsg.de/ica/rest/nami/auth/manual/sessionStartup' => Http::response(json_encode([
|
|
||||||
"servicePrefix" => null,
|
|
||||||
"methodCall" => null,
|
|
||||||
"response" => null,
|
|
||||||
"statusCode" => 0,
|
|
||||||
"statusMessage" => "",
|
|
||||||
"apiSessionName" => "JSESSIONID",
|
|
||||||
"apiSessionToken" => "lala-testsession",
|
|
||||||
"minorNumber" => 2,
|
|
||||||
"majorNumber" => 1,
|
|
||||||
]), 200, [
|
|
||||||
'Set-Cookie' => 'JSESSIONID=newlogin.srv-nami06; path=/ica; secure; HttpOnly; Max-Age=9000; Expires=Sat, 24-Feb-2024 01:18:00 GMT'
|
|
||||||
]),
|
|
||||||
]);
|
|
||||||
file_put_contents(__DIR__ . '/../../.cookies_test/' . now()->subHour()->timestamp . '.txt', 'oldlogin-testsession.srv-nami06');
|
file_put_contents(__DIR__ . '/../../.cookies_test/' . now()->subHour()->timestamp . '.txt', 'oldlogin-testsession.srv-nami06');
|
||||||
|
|
||||||
Nami::login(90100, 'secret');
|
Nami::login(90100, 'secret');
|
||||||
|
@ -121,4 +89,16 @@ class LoginTest extends TestCase
|
||||||
$this->assertFileExists(__DIR__ . '/../../.cookies_test/' . now()->timestamp . '.txt');
|
$this->assertFileExists(__DIR__ . '/../../.cookies_test/' . now()->timestamp . '.txt');
|
||||||
$this->assertCount(1, glob(__DIR__ . '/../../.cookies_test/*'));
|
$this->assertCount(1, glob(__DIR__ . '/../../.cookies_test/*'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testItRefreshesLogin(): void
|
||||||
|
{
|
||||||
|
app(LoginFake::class)->succeeds('newlogin');
|
||||||
|
$auth = app(Authenticator::class)->login(12345, 'secret');
|
||||||
|
rename(
|
||||||
|
__DIR__ . '/../../.cookies_test/' . now()->timestamp . '.txt',
|
||||||
|
__DIR__ . '/../../.cookies_test/' . now()->subMinutes(51)->timestamp . '.txt'
|
||||||
|
);
|
||||||
|
$auth->refresh();
|
||||||
|
Http::assertSentCount(2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue