125 lines
4.7 KiB
PHP
125 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace Zoomyboy\LaravelNami\Tests\Unit;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Zoomyboy\LaravelNami\Authentication\MainCookie;
|
|
use Zoomyboy\LaravelNami\Fakes\CourseFake;
|
|
use Zoomyboy\LaravelNami\LoginException;
|
|
use Zoomyboy\LaravelNami\Nami;
|
|
use Zoomyboy\LaravelNami\Tests\TestCase;
|
|
|
|
class LoginTest extends TestCase
|
|
{
|
|
public function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
Carbon::setTestNow(Carbon::parse('2024-02-23 23:48:00'));
|
|
}
|
|
|
|
public function testItLoggsInAndSavesCookie(): void
|
|
{
|
|
Http::fake([
|
|
'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');
|
|
|
|
Http::assertSentCount(1);
|
|
$cookie = file_get_contents(__DIR__ . '/../../.cookies_test/' . now()->timestamp . '.txt');
|
|
$this->assertEquals('lala-testsession.srv-nami06', $cookie);
|
|
}
|
|
|
|
public function testItDoesntSaveCookieWhenLoginFails(): void
|
|
{
|
|
Http::fake([
|
|
'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 {
|
|
app(MainCookie::class)->login(12345, 'secret');
|
|
} catch (LoginException $e) {
|
|
}
|
|
|
|
Http::assertSentCount(1);
|
|
$this->assertEmpty(glob(__DIR__ . '/../../.cookies_test/*'));
|
|
}
|
|
|
|
public function testItUsesSavedCookieForLogin(): void
|
|
{
|
|
app(CourseFake::class)->fetches(103, []);
|
|
file_put_contents(__DIR__ . '/../../.cookies_test/' . now()->subMinutes(5)->timestamp . '.txt', 'cook-testsession.srv-nami06');
|
|
|
|
Nami::coursesOf(103);
|
|
|
|
Http::assertSentCount(1);
|
|
Http::assertSent(fn ($request) => $request->header('Cookie')[0] === 'JSESSIONID=cook-testsession.srv-nami06');
|
|
}
|
|
|
|
public function testItDoesntResetCookieWhenAlreadyLoggedIn(): void
|
|
{
|
|
app(CourseFake::class)->fetches(103, []);
|
|
file_put_contents(__DIR__ . '/../../.cookies_test/' . now()->subMinutes(5)->timestamp . '.txt', 'cook-testsession.srv-nami06');
|
|
|
|
Nami::login(90100, 'secret');
|
|
|
|
Http::assertSentCount(0);
|
|
$this->assertFileExists(__DIR__ . '/../../.cookies_test/' . now()->subMinutes(5)->timestamp . '.txt');
|
|
$this->assertCount(1, glob(__DIR__ . '/../../.cookies_test/*'));
|
|
}
|
|
|
|
public function testItRefreshesExpiredCookie(): void
|
|
{
|
|
app(CourseFake::class)->fetches(103, []);
|
|
Http::fake([
|
|
'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');
|
|
|
|
Nami::login(90100, 'secret');
|
|
|
|
Http::assertSentCount(1);
|
|
$cookie = file_get_contents(__DIR__ . '/../../.cookies_test/' . now()->timestamp . '.txt');
|
|
$this->assertEquals('newlogin.srv-nami06', $cookie);
|
|
$this->assertFileExists(__DIR__ . '/../../.cookies_test/' . now()->timestamp . '.txt');
|
|
$this->assertCount(1, glob(__DIR__ . '/../../.cookies_test/*'));
|
|
}
|
|
}
|