Tests: Rely on admin user, groups, permissions from test setup script

This commit is contained in:
Franz Liedke 2020-05-20 00:18:00 +02:00 committed by Alexander Skvortsov
parent a47187462d
commit a08fd3e475
27 changed files with 8 additions and 269 deletions

View File

@ -11,50 +11,6 @@ namespace Flarum\Tests\integration;
trait RetrievesAuthorizedUsers
{
protected function adminGroup(): array
{
return [
'id' => 1,
'name_singular' => 'Admin',
'name_plural' => 'Admins',
'color' => '#B72A2A',
'icon' => 'fas fa-wrench',
];
}
protected function guestGroup(): array
{
return [
'id' => 2,
'name_singular' => 'Guest',
'name_plural' => 'Guests',
'color' => null,
'icon' => null,
];
}
protected function memberGroup(): array
{
return [
'id' => 3,
'name_singular' => 'Member',
'name_plural' => 'Members',
'color' => null,
'icon' => null,
];
}
protected function adminUser(): array
{
return [
'id' => 1,
'username' => 'admin',
'password' => '$2y$10$HMOAe.XaQjOimA778VmFue1OCt7tj5j0wk5vfoL/CMSJq2BQlfBV2', // BCrypt hash for "password"
'email' => 'admin@machine.local',
'is_email_confirmed' => 1,
];
}
protected function normalUser(): array
{
return [

View File

@ -25,7 +25,6 @@ class WithApiKeyTest extends TestCase
$this->prepareDatabase([
'users' => [
$this->adminUser(),
$this->normalUser(),
],
]);

View File

@ -21,18 +21,6 @@ class RequireCsrfTokenTest extends TestCase
parent::setUp();
$this->prepareDatabase([
'users' => [
$this->adminUser(),
],
'groups' => [
$this->adminGroup(),
],
'group_user' => [
['user_id' => 1, 'group_id' => 1],
],
'group_permission' => [
['permission' => 'viewUserList', 'group_id' => 3],
],
'api_keys' => [
['user_id' => 1, 'key' => 'superadmin'],
],

View File

@ -18,30 +18,6 @@ class CreateTest extends TestCase
{
use RetrievesAuthorizedUsers;
protected function setUp(): void
{
parent::setUp();
$this->prepareDatabase([
'users' => [
$this->adminUser(),
$this->normalUser(),
],
'groups' => [
$this->adminGroup(),
$this->memberGroup(),
],
'group_user' => [
['user_id' => 1, 'group_id' => 1],
['user_id' => 2, 'group_id' => 3],
],
'group_permission' => [
['permission' => 'viewDiscussions', 'group_id' => 3],
['permission' => 'startDiscussion', 'group_id' => 3],
]
]);
}
/**
* @test
*/

View File

@ -29,15 +29,8 @@ class DeletionTest extends TestCase
['id' => 1, 'discussion_id' => 1, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => '<t><p>foo bar</p></t>'],
],
'users' => [
$this->adminUser(),
$this->normalUser(),
],
'groups' => [
$this->adminGroup(),
],
'group_user' => [
['user_id' => 1, 'group_id' => 1],
],
]);
}

View File

@ -30,13 +30,6 @@ class ListTest extends TestCase
],
'users' => [
$this->normalUser(),
],
'groups' => [
$this->memberGroup(),
$this->guestGroup(),
],
'group_permission' => [
['permission' => 'viewDiscussions', 'group_id' => 2],
]
]);
}

View File

@ -37,17 +37,6 @@ class ShowTest extends TestCase
],
'users' => [
$this->normalUser(),
],
'groups' => [
$this->guestGroup(),
$this->memberGroup(),
],
'group_user' => [
['user_id' => 2, 'group_id' => 3],
],
'group_permission' => [
['permission' => 'viewDiscussions', 'group_id' => 2],
['permission' => 'viewDiscussions', 'group_id' => 3],
]
]);
}

View File

@ -23,17 +23,8 @@ class ShowTest extends TestCase
$this->prepareDatabase([
'users' => [
$this->adminUser(),
$this->normalUser(),
],
'groups' => [
$this->adminGroup(),
$this->memberGroup(),
],
'group_user' => [
['user_id' => 1, 'group_id' => 1],
['user_id' => 2, 'group_id' => 3],
],
]
]);
}

View File

@ -24,15 +24,8 @@ class CreateTest extends TestCase
$this->prepareDatabase([
'users' => [
$this->adminUser(),
$this->normalUser(),
],
'groups' => [
$this->adminGroup(),
],
'group_user' => [
['user_id' => 1, 'group_id' => 1],
],
]);
}

View File

@ -22,16 +22,8 @@ class ListTest extends TestCase
parent::setUp();
$this->prepareDatabase([
'users' => [
$this->adminUser(),
$this->normalUser(),
],
'groups' => [
$this->adminGroup(),
$this->hiddenGroup()
],
'group_user' => [
['user_id' => 1, 'group_id' => 1],
$this->hiddenGroup(),
],
]);
}
@ -48,7 +40,8 @@ class ListTest extends TestCase
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true);
$this->assertEquals(['1'], Arr::pluck($data['data'], 'id'));
// The four default groups created by the installer
$this->assertEquals(['1', '2', '3', '4'], Arr::pluck($data['data'], 'id'));
}
/**
@ -65,7 +58,8 @@ class ListTest extends TestCase
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true);
$this->assertEquals(['1', '10'], Arr::pluck($data['data'], 'id'));
// The four default groups created by the installer and our hidden group
$this->assertEquals(['1', '2', '3', '4', '10'], Arr::pluck($data['data'], 'id'));
}
protected function hiddenGroup(): array

View File

@ -27,15 +27,6 @@ class CreateTest extends TestCase
],
'users' => [
$this->normalUser(),
],
'groups' => [
$this->memberGroup(),
],
'group_user' => [
['user_id' => 2, 'group_id' => 3],
],
'group_permission' => [
['permission' => 'viewDiscussions', 'group_id' => 3],
]
]);
}

View File

@ -23,16 +23,6 @@ class CreateTest extends TestCase
parent::setUp();
$this->prepareDatabase([
'users' => [
$this->adminUser(),
$this->normalUser(),
],
'groups' => [
$this->adminGroup()
],
'group_user' => [
['user_id' => 1, 'group_id' => 1],
],
'settings' => [
['key' => 'mail_driver', 'value' => 'log'],
],

View File

@ -9,7 +9,6 @@
namespace Flarum\Tests\integration\api\users;
use Flarum\Group\Permission;
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
use Flarum\Tests\integration\TestCase;
@ -17,24 +16,6 @@ class ListTest extends TestCase
{
use RetrievesAuthorizedUsers;
protected function setUp(): void
{
parent::setUp();
$this->prepareDatabase([
'users' => [
$this->adminUser(),
],
'groups' => [
$this->adminGroup(),
$this->guestGroup(),
],
'group_user' => [
['user_id' => 1, 'group_id' => 1],
],
]);
}
/**
* @test
*/

View File

@ -22,18 +22,8 @@ class ShowTest extends TestCase
$this->prepareDatabase([
'users' => [
$this->adminUser(),
$this->normalUser(),
],
'groups' => [
$this->adminGroup()
],
'group_user' => [
['user_id' => 1, 'group_id' => 1],
],
'settings' => [
['key' => 'mail_driver', 'value' => 'log'],
],
]);
}

View File

@ -22,20 +22,7 @@ class UpdateTest extends TestCase
$this->prepareDatabase([
'users' => [
$this->adminUser(),
$this->normalUser(),
],
'groups' => [
$this->adminGroup(),
$this->memberGroup(),
],
'group_user' => [
['user_id' => 1, 'group_id' => 1],
['user_id' => 2, 'group_id' => 3],
],
'group_permission' => [
['permission' => 'viewUserList', 'group_id' => 3],
['permission' => 'viewDiscussions', 'group_id' => 3]
]
]);
}

View File

@ -35,13 +35,8 @@ class ApiControllerTest extends TestCase
{
$this->prepareDatabase([
'users' => [
$this->adminUser(),
$this->normalUser()
],
'groups' => [
$this->adminGroup(),
$this->memberGroup()
],
'discussions' => [
['id' => 1, 'title' => 'Custom Discussion Title', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'first_post_id' => 0, 'comment_count' => 1, 'is_private' => 0],
['id' => 2, 'title' => 'Custom Discussion Title', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 3, 'first_post_id' => 0, 'comment_count' => 1, 'is_private' => 0],

View File

@ -31,7 +31,6 @@ class ApiSerializerTest extends TestCase
{
$this->prepareDatabase([
'users' => [
$this->adminUser(),
$this->normalUser()
],
'discussions' => [

View File

@ -24,15 +24,6 @@ class EventTest extends TestCase
protected function buildGroup()
{
$this->prepareDatabase([
'groups' => [
$this->adminGroup(),
],
'users' => [
$this->adminUser(),
],
]);
$bus = $this->app()->getContainer()->make(Dispatcher::class);
return $bus->dispatch(

View File

@ -23,28 +23,11 @@ class MailTest extends TestCase
{
use RetrievesAuthorizedUsers;
protected function prepDb()
{
$this->prepareDatabase([
'users' => [
$this->adminUser(),
],
'groups' => [
$this->adminGroup(),
],
'group_user' => [
['user_id' => 1, 'group_id' => 1],
],
]);
}
/**
* @test
*/
public function drivers_are_unchanged_by_default()
{
$this->prepDb();
$response = $this->send(
$this->request('GET', '/api/mail/settings', [
'authenticatedAs' => 1,
@ -76,8 +59,6 @@ class MailTest extends TestCase
->driver('custom', CustomDriver::class)
);
$this->prepDb();
$response = $this->send(
$this->request('GET', '/api/mail/settings', [
'authenticatedAs' => 1,
@ -100,8 +81,6 @@ class MailTest extends TestCase
->driver('smtp', CustomDriver::class)
);
$this->prepDb();
$response = $this->send(
$this->request('GET', '/api/mail/settings', [
'authenticatedAs' => 1,

View File

@ -29,7 +29,6 @@ class ModelTest extends TestCase
{
$this->prepareDatabase([
'users' => [
$this->adminUser(),
$this->normalUser(),
],
]);
@ -245,11 +244,6 @@ class ModelTest extends TestCase
);
$this->prepDB();
$this->prepareDatabase([
'groups' => [
$this->adminGroup()
]
]);
$group = Group::find(1);

View File

@ -26,7 +26,6 @@ class ModelUrlTest extends TestCase
$userClass = User::class;
$this->prepareDatabase([
'users' => [
$this->adminUser(),
$this->normalUser(),
],
'settings' => [

View File

@ -37,17 +37,6 @@ class ModelVisibilityTest extends TestCase
],
'users' => [
$this->normalUser(),
],
'groups' => [
$this->guestGroup(),
$this->memberGroup(),
],
'group_user' => [
['user_id' => 2, 'group_id' => 3],
],
'group_permission' => [
['permission' => 'viewDiscussions', 'group_id' => 2],
['permission' => 'viewDiscussions', 'group_id' => 3],
]
]);
}

View File

@ -32,7 +32,6 @@ class PolicyTest extends TestCase
{
$this->prepareDatabase([
'users' => [
$this->adminUser(),
$this->normalUser(),
],
'discussions' => [

View File

@ -21,7 +21,6 @@ class SettingsTest extends TestCase
{
$this->prepareDatabase([
'users' => [
$this->adminUser(),
$this->normalUser()
],
'settings' => [

View File

@ -22,15 +22,6 @@ class ThrottleApiTest extends TestCase
$this->prepareDatabase([
'users' => [
$this->normalUser(),
],
'groups' => [
$this->memberGroup(),
],
'group_user' => [
['user_id' => 2, 'group_id' => 3],
],
'group_permission' => [
['permission' => 'viewDiscussions', 'group_id' => 3],
]
]);
}

View File

@ -24,17 +24,10 @@ class UserTest extends TestCase
{
$this->prepareDatabase([
'users' => [
$this->adminUser(),
$this->normalUser(),
],
'group_permission' => [
['permission' => 'viewUserList', 'group_id' => 3]
],
'settings' => [
['key' => 'display_name_driver', 'value' => 'custom'],
],
'group_permission' => [
['permission' => 'viewUserList', 'group_id' => 3],
]
]);
}

View File

@ -56,8 +56,8 @@ $pipeline = $installation
)
->adminUser(new AdminUser(
'admin',
'secret',
'admin@flarum.email'
'password',
'admin@machine.local'
))
->settings(['mail_driver' => 'log'])
->build();