105 lines
3.7 KiB
PHP
105 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace Zoomyboy\LaravelNami\Tests\Unit;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Zoomyboy\LaravelNami\Authentication\Authenticator;
|
|
use Zoomyboy\LaravelNami\Authentication\MainCookie;
|
|
use Zoomyboy\LaravelNami\Fakes\CourseFake;
|
|
use Zoomyboy\LaravelNami\Fakes\LoginFake;
|
|
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
|
|
{
|
|
app(LoginFake::class)->succeeds('lala-testsession');
|
|
|
|
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);
|
|
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
|
|
{
|
|
app(LoginFake::class)->fails('::token::');
|
|
|
|
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 testItIgnoresExpiredCookie(): void
|
|
{
|
|
app(CourseFake::class)->fetches(103, []);
|
|
app(LoginFake::class)->succeeds('newlogin');
|
|
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/*'));
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|