Fixed: Refresh login when it is expired
This commit is contained in:
parent
899264db51
commit
32c1eaf377
|
@ -438,6 +438,8 @@ class Api {
|
|||
|
||||
private function assertLoggedIn(): void
|
||||
{
|
||||
$this->authenticator->refresh();
|
||||
|
||||
if (!$this->isLoggedIn()) {
|
||||
throw new NotAuthenticatedException('You need to login first');
|
||||
}
|
||||
|
|
|
@ -9,7 +9,9 @@ abstract class Authenticator {
|
|||
abstract public function login(int $mglnr, string $password): self;
|
||||
abstract public function http(): PendingRequest;
|
||||
abstract public function isLoggedIn(): bool;
|
||||
protected static $path = __DIR__.'/../../.cookies';
|
||||
abstract public function refresh(): void;
|
||||
|
||||
protected static string $path = __DIR__.'/../../.cookies';
|
||||
|
||||
public static function setPath(string $path): void
|
||||
{
|
||||
|
|
|
@ -85,6 +85,11 @@ class FakeCookie extends Authenticator {
|
|||
);
|
||||
}
|
||||
|
||||
public function refresh(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function assertNotLoggedIn(): void
|
||||
{
|
||||
Assert::assertFalse(
|
||||
|
|
|
@ -12,6 +12,8 @@ class MainCookie extends Authenticator {
|
|||
|
||||
private FileCookieJar $cookie;
|
||||
private string $url = 'https://nami.dpsg.de';
|
||||
private ?int $mglnr = null;
|
||||
private ?string $password = null;
|
||||
|
||||
public function login(int $mglnr, string $password): self
|
||||
{
|
||||
|
@ -19,6 +21,9 @@ class MainCookie extends Authenticator {
|
|||
return $this;
|
||||
}
|
||||
|
||||
$this->mglnr = $mglnr;
|
||||
$this->password = $password;
|
||||
|
||||
while ($file = $this->file()) {
|
||||
unlink($file);
|
||||
}
|
||||
|
@ -52,6 +57,13 @@ class MainCookie extends Authenticator {
|
|||
return ! $this->isExpired();
|
||||
}
|
||||
|
||||
public function refresh(): void
|
||||
{
|
||||
if ($this->mglnr && $this->password) {
|
||||
$this->login($this->mglnr, $this->password);
|
||||
}
|
||||
}
|
||||
|
||||
public function http(): PendingRequest
|
||||
{
|
||||
return Http::withOptions(['cookies' => $this->load()]);
|
||||
|
|
|
@ -44,13 +44,17 @@ class TestCase extends \Orchestra\Testbench\TestCase
|
|||
return Nami::login(123, 'secret');
|
||||
}
|
||||
|
||||
private function setupCookies(): void
|
||||
protected function clearCookies(): void
|
||||
{
|
||||
Authenticator::setPath(__DIR__.'/../.cookies_test');
|
||||
|
||||
foreach (glob(__DIR__.'/../.cookies_test/*') as $file) {
|
||||
unlink($file);
|
||||
}
|
||||
}
|
||||
|
||||
private function setupCookies(): void
|
||||
{
|
||||
Authenticator::setPath(__DIR__.'/../.cookies_test');
|
||||
$this->clearCookies();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -91,7 +91,7 @@ class CourseTest extends TestCase
|
|||
{
|
||||
$this->expectException(NotAuthenticatedException::class);
|
||||
|
||||
$courses = Nami::coursesFor(11111);
|
||||
Nami::coursesFor(11111);
|
||||
}
|
||||
|
||||
public function test_parses_failed_login(): void
|
||||
|
|
|
@ -4,6 +4,7 @@ namespace Zoomyboy\LaravelNami\Tests\Unit;
|
|||
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Zoomyboy\LaravelNami\Authentication\Auth;
|
||||
use Zoomyboy\LaravelNami\Fakes\CourseFake;
|
||||
use Zoomyboy\LaravelNami\LoginException;
|
||||
use Zoomyboy\LaravelNami\Nami;
|
||||
use Zoomyboy\LaravelNami\Tests\TestCase;
|
||||
|
@ -93,6 +94,19 @@ class LoginTest extends TestCase
|
|||
Http::assertSentCount(2);
|
||||
}
|
||||
|
||||
public function test_it_refreshes_login_when_a_cookie_of_an_existing_api_is_expired(): void
|
||||
{
|
||||
app(CourseFake::class)->forMember(11111, []);
|
||||
Http::fake($this->fakeSuccessfulLoginForever());
|
||||
$api = Nami::login(12345, 'secret');
|
||||
$this->clearCookies();
|
||||
$lastLogin = now()->subHours(2)->timestamp;
|
||||
touch(__DIR__."/../../.cookies_test/{$lastLogin}.txt");
|
||||
|
||||
$courses = $api->coursesFor(11111);
|
||||
$this->assertCount(0, $courses);
|
||||
}
|
||||
|
||||
public function test_it_fakes_login(): void
|
||||
{
|
||||
Auth::fake();
|
||||
|
@ -138,6 +152,14 @@ class LoginTest extends TestCase
|
|||
];
|
||||
}
|
||||
|
||||
private function fakeSuccessfulLoginForever(): array
|
||||
{
|
||||
return [
|
||||
'https://nami.dpsg.de/ica/pages/login.jsp' => Http::response('<html></html>', 200),
|
||||
'https://nami.dpsg.de/ica/rest/nami/auth/manual/sessionStartup' => Http::response($this->successJson, 200),
|
||||
];
|
||||
}
|
||||
|
||||
private function fakeFailedLogin(): array
|
||||
{
|
||||
return [
|
||||
|
|
Loading…
Reference in New Issue