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/Models/Auth/AuthModelTest.php | 109 ++++++++++++++++++++++++++++++ tests/Unit/Models/Auth/PermissionTest.php | 50 ++++++++++++++ tests/Unit/Models/Auth/RoleTest.php | 68 +++++++++++++++++++ 3 files changed, 227 insertions(+) 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 (limited to 'tests/Unit/Models/Auth') 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); + } +} -- cgit v1.2.3-54-g00ecf