Fixed: Change cookie path in tests

This commit is contained in:
philipp lang 2022-02-19 18:41:51 +01:00
parent 63cef42385
commit 53f7537dbc
6 changed files with 22 additions and 12 deletions

0
.cookies_test/.gitkeep Normal file
View File

1
.gitignore vendored
View File

@ -4,4 +4,5 @@
*.swm
tags
.cookies
.cookies_test
/.phpunit.result.cache

View File

@ -9,5 +9,11 @@ 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';
public static function setPath(string $path): void
{
static::$path = $path;
}
}

View File

@ -10,7 +10,6 @@ use Zoomyboy\LaravelNami\LoginException;
class MainCookie extends Authenticator {
private string $path = __DIR__.'/../../.cookies';
private FileCookieJar $cookie;
private string $url = 'https://nami.dpsg.de';
@ -59,7 +58,7 @@ class MainCookie extends Authenticator {
private function newFileName(): string
{
return $this->path.'/'.time().'.txt';
return parent::$path.'/'.time().'.txt';
}
private function isExpired(): bool
@ -76,7 +75,7 @@ class MainCookie extends Authenticator {
*/
private function file(): ?string
{
$files = glob($this->path.'/*');
$files = glob(parent::$path.'/*');
if (!count($files)) {
return null;

View File

@ -4,7 +4,9 @@ namespace Zoomyboy\LaravelNami\Tests;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Zoomyboy\LaravelNami\Api;
use Zoomyboy\LaravelNami\Authentication\Authenticator;
use Zoomyboy\LaravelNami\Cookies\Cookie;
use Zoomyboy\LaravelNami\Cookies\FakeCookie;
use Zoomyboy\LaravelNami\Nami;
@ -17,7 +19,7 @@ class TestCase extends \Orchestra\Testbench\TestCase
public function setUp(): void {
parent::setUp();
$this->clearCookies();
$this->setupCookies();
}
protected function getPackageProviders($app)
@ -37,14 +39,16 @@ class TestCase extends \Orchestra\Testbench\TestCase
public function login(): Api
{
touch (__DIR__.'/../.cookies/'.time().'.txt');
touch (__DIR__.'/../.cookies_test/'.time().'.txt');
return Nami::login(123, 'secret');
}
private function clearCookies(): void
private function setupCookies(): void
{
foreach (glob(__DIR__.'/../.cookies/*') as $file) {
Authenticator::setPath(__DIR__.'/../.cookies_test');
foreach (glob(__DIR__.'/../.cookies_test/*') as $file) {
unlink($file);
}
}

View File

@ -57,12 +57,12 @@ class LoginTest extends TestCase
Nami::login(12345, 'secret');
$this->assertFileExists(__DIR__.'/../../.cookies/'.time().'.txt');
$this->assertFileExists(__DIR__.'/../../.cookies_test/'.time().'.txt');
}
public function test_dont_login_if_cookie_exists(): void
{
touch(__DIR__.'/../../.cookies/'.time().'.txt');
touch(__DIR__.'/../../.cookies_test/'.time().'.txt');
Nami::login(12345, 'secret');
@ -72,19 +72,19 @@ class LoginTest extends TestCase
public function test_delete_expired_cookie_before_login(): void
{
$lastLogin = now()->subHours(2)->timestamp;
touch(__DIR__."/../../.cookies/{$lastLogin}.txt");
touch(__DIR__."/../../.cookies_test/{$lastLogin}.txt");
Http::fake($this->fakeSuccessfulLogin());
Nami::login(12345, 'secret');
Http::assertSentCount(2);
$this->assertFileDoesNotExist(__DIR__."/../../.cookies/{$lastLogin}.txt");
$this->assertFileDoesNotExist(__DIR__."/../../.cookies_test/{$lastLogin}.txt");
}
public function test_login_once_if_cookie_is_expired(): void
{
$lastLogin = now()->subHour()->subMinutes(10)->timestamp;
touch(__DIR__."/../../.cookies/{$lastLogin}.txt");
touch(__DIR__."/../../.cookies_test/{$lastLogin}.txt");
Http::fake($this->fakeSuccessfulLogin());
Nami::login(12345, 'secret');