From 219c54411bb765bebd7813ad3e49ab05acf0b150 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Sun, 28 Jul 2019 17:14:42 +0200 Subject: Permissions: Added models and migrations --- tests/Unit/HasDatabase.php | 1 + tests/Unit/Helpers/AuthenticatorTest.php | 2 +- tests/Unit/Models/Auth/AuthModelTest.php | 109 ++++++++++++++++++++++++++++++ tests/Unit/Models/Auth/PermissionTest.php | 50 ++++++++++++++ tests/Unit/Models/Auth/RoleTest.php | 68 +++++++++++++++++++ tests/Unit/Models/TeamTest.php | 71 +++++++++++++++++++ tests/Unit/Models/User/UserTest.php | 72 +++++++++++++++++++- 7 files changed, 369 insertions(+), 4 deletions(-) create mode 100644 tests/Unit/Models/Auth/AuthModelTest.php create mode 100644 tests/Unit/Models/Auth/PermissionTest.php create mode 100644 tests/Unit/Models/Auth/RoleTest.php create mode 100644 tests/Unit/Models/TeamTest.php (limited to 'tests') diff --git a/tests/Unit/HasDatabase.php b/tests/Unit/HasDatabase.php index 7fd42f96..85b59435 100644 --- a/tests/Unit/HasDatabase.php +++ b/tests/Unit/HasDatabase.php @@ -46,6 +46,7 @@ trait HasDatabase ['migration' => '2018_01_01_000005_add_angel_supporter_permissions'], ['migration' => '2018_12_27_000000_fix_missing_arrival_dates'], ['migration' => '2019_07_21_000000_fix_old_character_set'], + ['migration' => '2019_07_21_000001_fix_old_groups_table_id_and_name'], ]); $migration->run(__DIR__ . '/../../db/migrations'); diff --git a/tests/Unit/Helpers/AuthenticatorTest.php b/tests/Unit/Helpers/AuthenticatorTest.php index d601a77f..8dc14370 100644 --- a/tests/Unit/Helpers/AuthenticatorTest.php +++ b/tests/Unit/Helpers/AuthenticatorTest.php @@ -132,7 +132,7 @@ class AuthenticatorTest extends ServiceProviderTest ->getMock(); $auth->expects($this->exactly(1)) ->method('getPermissionsByGroup') - ->with(10) + ->with(1) ->willReturn([]); $auth->expects($this->exactly(1)) ->method('getPermissionsByUser') diff --git a/tests/Unit/Models/Auth/AuthModelTest.php b/tests/Unit/Models/Auth/AuthModelTest.php new file mode 100644 index 00000000..754e120a --- /dev/null +++ b/tests/Unit/Models/Auth/AuthModelTest.php @@ -0,0 +1,109 @@ + 'Test User', + 'password' => 'nope', + 'email' => 'foo@bar.batz', + 'api_key' => 'Test123', + ]; + + /** @var array */ + protected $team = [ + 'name' => 'Test Team', + ]; + + /** @var array */ + protected $role = [ + 'name' => 'Test Role', + ]; + + /** @var array */ + protected $permission = [ + 'name' => 'foo.bar', + ]; + + /** + * @param array $data + * @return Permission + */ + protected function getPermission($data = []) + { + /** @var Permission $model */ + $model = $this->getModel(Permission::class, array_merge($this->permission, $data)); + + return $model; + } + + /** + * @param Permission|Permission[] $permissions + * @param array $data + * @return Role + */ + protected function getRole($permissions = [], $data = []) + { + /** @var Role $model */ + $model = $this->getModel(Role::class, array_merge($this->role, $data), ['permissions' => $permissions]); + + return $model; + } + + /** + * @param Role|Role[] $roles + * @param array $data + * @return Team + */ + protected function getTeam($roles = [], $data = []) + { + /** @var Team $model */ + $model = $this->getModel(Team::class, array_merge($this->team, $data), ['roles' => $roles]); + + return $model; + } + + /** + * @param Team|Team[] $teams + * @param array $data + * @return User + */ + protected function getUser($teams = [], $data = []) + { + /** @var User $model */ + $model = $this->getModel(User::class, array_merge($this->user, $data), ['teams' => $teams]); + + return $model; + } + + /** + * @param string $class BaseModel implementation name + * @param array $data + * @param array $attachments + * @return BaseModel + */ + protected function getModel($class, $data = [], $attachments = []) + { + /** @var BaseModel $instance */ + $instance = new $class($data); + $instance->save(); + + foreach ($attachments as $type => $attachment) { + $objects = is_object($attachment) ? [$attachment] : $attachment; + foreach ($objects as $object) { + $instance->$type()->attach($object); + } + } + + return $instance; + } +} diff --git a/tests/Unit/Models/Auth/PermissionTest.php b/tests/Unit/Models/Auth/PermissionTest.php new file mode 100644 index 00000000..bb5f7605 --- /dev/null +++ b/tests/Unit/Models/Auth/PermissionTest.php @@ -0,0 +1,50 @@ + 'foo.bar', + 'description' => 'Allows bar in foo', + ]; + + /** + * @covers \Engelsystem\Models\Auth\Permission::roles + */ + public function testPermissions() + { + $role = $this->getRole(); + $role2 = $this->getRole([], ['name' => 'Example Role']); + + $permission = new Permission($this->data); + $permission->save(); + $permission->roles()->attach($role); + $permission->roles()->attach($role2); + + $this->assertCount(2, $permission->roles); + $this->assertEquals('Test Role', $permission->roles()->get()->first()->name); + } + + /** + * @covers \Engelsystem\Models\Auth\Permission::users + */ + public function testUsers() + { + $role = $this->getRole(); + $team = $this->getTeam($role); + $this->getUser($team); + + $permission = new Permission($this->data); + $permission->save(); + $permission->roles()->attach($role); + + $this->assertEquals('Test User', $permission->users()->get()->first()->name); + } +} diff --git a/tests/Unit/Models/Auth/RoleTest.php b/tests/Unit/Models/Auth/RoleTest.php new file mode 100644 index 00000000..10c22f9c --- /dev/null +++ b/tests/Unit/Models/Auth/RoleTest.php @@ -0,0 +1,68 @@ + 'Test Role', + 'description' => 'Lorem Ipsum', + ]; + + /** + * @covers \Engelsystem\Models\Auth\Role::permissions + */ + public function testPermissions() + { + $permission = $this->getPermission(); + $permission2 = $this->getPermission(['name' => 'Another Permission']); + + $role = new Role($this->data); + $role->save(); + $role->permissions()->attach($permission); + $role->permissions()->attach($permission2); + + $this->assertCount(2, $role->permissions); + $this->assertEquals('foo.bar', $role->permissions()->get()->first()->name); + } + + /** + * @covers \Engelsystem\Models\Auth\Role::teams + */ + public function testTeams() + { + $team = $this->getTeam(); + + $role = new Role($this->data); + $role->save(); + $role->teams()->attach($team); + + $this->assertEquals('Test Team', $role->teams()->get()->first()->name); + } + + /** + * @covers \Engelsystem\Models\Auth\Role::users + */ + public function testUsers() + { + $team = $this->getTeam(); + $team2 = $this->getTeam([], ['name' => 'Dev']); + $this->getUser($team); + $this->getUser($team, ['name' => 'Foo', 'email' => 'foo@xample.com']); + $this->getUser($team2, ['name' => 'Bar', 'email' => 'bar@xample.com']); + + $role = new Role($this->data); + $role->save(); + $role->teams()->attach($team); + $role->teams()->attach($team2); + + $this->assertCount(3, $role->users()->get()); + $this->assertEquals('Test User', $role->users()->get()->first()->name); + } +} diff --git a/tests/Unit/Models/TeamTest.php b/tests/Unit/Models/TeamTest.php new file mode 100644 index 00000000..fdd5be44 --- /dev/null +++ b/tests/Unit/Models/TeamTest.php @@ -0,0 +1,71 @@ + 'Example team', + 'description' => 'Lorem Ipsum', + 'contact_name' => 'Con Tact', + 'contact_dect' => '1337', + 'contact_email' => 'foo@bar.batz', + 'restricted' => false, + 'self_signup' => true, + 'requires_drivers_license' => false, + 'show_on_frontend' => true, + 'show_on_dashboard' => true, + ]; + + /** + * @covers \Engelsystem\Models\Team::roles + */ + public function testRoles() + { + $role = $this->getRole(); + $role2 = $this->getRole([], ['name' => 'Another Role']); + + $team = new Team($this->data); + $team->save(); + $team->roles()->attach($role); + $team->roles()->attach($role2); + + $this->assertCount(2, $team->roles); + $this->assertEquals('Test Role', $team->roles()->get()->first()->name); + } + + /** + * @covers \Engelsystem\Models\Team::supporters + */ + public function testSupporters() + { + $user = $this->getUser(); + + $team = new Team($this->data); + $team->save(); + $team->supporters()->attach($user); + + $this->assertEquals('Test User', $team->supporters()->get()->first()->name); + } + + /** + * @covers \Engelsystem\Models\Team::users + */ + public function testUsers() + { + $user = $this->getUser(); + + $team = new Team($this->data); + $team->save(); + $team->users()->attach($user); + + $this->assertEquals('Test User', $team->users()->get()->first()->name); + } +} diff --git a/tests/Unit/Models/User/UserTest.php b/tests/Unit/Models/User/UserTest.php index a0d20bc2..4696d56a 100644 --- a/tests/Unit/Models/User/UserTest.php +++ b/tests/Unit/Models/User/UserTest.php @@ -1,6 +1,6 @@ 'lorem', 'password' => '', @@ -87,4 +88,69 @@ class UserTest extends ModelTest $this->assertArraySubset($data, (array)$user->{$name}->attributesToArray()); } + + /** + * @covers \Engelsystem\Models\User\User::permissions + */ + public function testPermissions() + { + $permission = $this->getPermission(); + $role = $this->getRole($permission); + $team = $this->getTeam($role); + + $user = new User($this->data); + $user->save(); + $user->teams()->attach($team); + + $this->assertEquals('foo.bar', $user->permissions()->first()->name); + } + + /** + * @covers \Engelsystem\Models\User\User::roles + */ + public function testRoles() + { + $role = $this->getRole(); + $role2 = $this->getRole([], ['name' => 'Foo Role']); + $team = $this->getTeam([$role, $role2]); + + $user = new User($this->data); + $user->save(); + $user->teams()->attach($team); + + $this->assertCount(2, $user->roles); + $this->assertEquals('Test Role', $user->roles()->get()->first()->name); + } + + /** + * @covers \Engelsystem\Models\User\User::supports + */ + public function testSupports() + { + $team = $this->getTeam(); + + $user = new User($this->data); + $user->save(); + $user->teams()->attach($team); + $user->supports()->attach($team); + + $this->assertEquals('Test Team', $user->supports()->get()->first()->name); + } + + /** + * @covers \Engelsystem\Models\User\User::teams + */ + public function testTeams() + { + $team = $this->getTeam(); + $team2 = $this->getTeam([], ['name' => 'Another Team']); + + $user = new User($this->data); + $user->save(); + $user->teams()->attach($team); + $user->teams()->attach($team2); + + $this->assertCount(2, $user->teams); + $this->assertEquals('Test Team', $user->teams()->get()->first()->name); + } } -- cgit v1.2.3-54-g00ecf