mirror of
https://github.com/flarum/core.git
synced 2025-07-30 21:20:24 +02:00
Tests: Stop using Eloquent models for seeding data
This commit is contained in:
committed by
Alexander Skvortsov
parent
a08fd3e475
commit
31765388c1
@@ -27,20 +27,13 @@ class WithApiKeyTest extends TestCase
|
||||
'users' => [
|
||||
$this->normalUser(),
|
||||
],
|
||||
'api_keys' => [
|
||||
['key' => 'mastertoken', 'user_id' => null, 'created_at' => Carbon::now()->toDateTimeString()],
|
||||
['key' => 'personaltoken', 'user_id' => 2, 'created_at' => Carbon::now()->toDateTimeString()],
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
protected function key(int $user_id = null): ApiKey
|
||||
{
|
||||
return ApiKey::unguarded(function () use ($user_id) {
|
||||
return ApiKey::query()->firstOrCreate([
|
||||
'key' => Str::random(),
|
||||
'user_id' => $user_id,
|
||||
'created_at' => Carbon::now()
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
@@ -59,18 +52,16 @@ class WithApiKeyTest extends TestCase
|
||||
*/
|
||||
public function master_token_can_authenticate_as_anyone()
|
||||
{
|
||||
$key = $this->key();
|
||||
|
||||
$response = $this->send(
|
||||
$this->request('GET', '/api')
|
||||
->withAddedHeader('Authorization', "Token {$key->key}; userId=1")
|
||||
->withAddedHeader('Authorization', 'Token mastertoken; userId=1')
|
||||
);
|
||||
|
||||
$data = json_decode($response->getBody(), true);
|
||||
$this->assertTrue($data['data']['attributes']['canViewUserList']);
|
||||
$this->assertArrayHasKey('adminUrl', $data['data']['attributes']);
|
||||
|
||||
$key->refresh();
|
||||
$key = ApiKey::where('key', 'mastertoken')->first();
|
||||
|
||||
$this->assertNotNull($key->last_activity_at);
|
||||
}
|
||||
@@ -80,18 +71,16 @@ class WithApiKeyTest extends TestCase
|
||||
*/
|
||||
public function personal_api_token_cannot_authenticate_as_anyone()
|
||||
{
|
||||
$key = $this->key(2);
|
||||
|
||||
$response = $this->send(
|
||||
$this->request('GET', '/api')
|
||||
->withAddedHeader('Authorization', "Token {$key->key}; userId=1")
|
||||
->withAddedHeader('Authorization', 'Token personaltoken; userId=1')
|
||||
);
|
||||
|
||||
$data = json_decode($response->getBody(), true);
|
||||
$this->assertTrue($data['data']['attributes']['canViewUserList']);
|
||||
$this->assertArrayNotHasKey('adminUrl', $data['data']['attributes']);
|
||||
|
||||
$key->refresh();
|
||||
$key = ApiKey::where('key', 'personaltoken')->first();
|
||||
|
||||
$this->assertNotNull($key->last_activity_at);
|
||||
}
|
||||
@@ -101,18 +90,16 @@ class WithApiKeyTest extends TestCase
|
||||
*/
|
||||
public function personal_api_token_authenticates_user()
|
||||
{
|
||||
$key = $this->key(2);
|
||||
|
||||
$response = $this->send(
|
||||
$this->request('GET', '/api')
|
||||
->withAddedHeader('Authorization', "Token {$key->key}")
|
||||
->withAddedHeader('Authorization', 'Token personaltoken')
|
||||
);
|
||||
|
||||
$data = json_decode($response->getBody(), true);
|
||||
$this->assertTrue($data['data']['attributes']['canViewUserList']);
|
||||
$this->assertArrayNotHasKey('adminUrl', $data['data']['attributes']);
|
||||
|
||||
$key->refresh();
|
||||
$key = ApiKey::where('key', 'personaltoken')->first();
|
||||
|
||||
$this->assertNotNull($key->last_activity_at);
|
||||
}
|
||||
|
Reference in New Issue
Block a user