133 lines
6.7 KiB
PHP
133 lines
6.7 KiB
PHP
<?php
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use IriksIT\Manager\Models\Group;
|
|
use IriksIT\Manager\Tests\Fixtures\User;
|
|
use IriksIT\Manager\Tests\TestCase;
|
|
|
|
uses(TestCase::class);
|
|
|
|
describe('groups', function () {
|
|
beforeEach(fn () => $this->actingAs(makeUser(superAdmin: true)));
|
|
|
|
it('lists the groups with what they allow and everything that can be allowed', function () {
|
|
$group = Group::create(['name' => 'Writers', 'permissions' => ['posts.write']]);
|
|
makeUser()->managerGroups()->attach($group);
|
|
|
|
$this->getJson('/manager/groups')->assertOk()
|
|
->assertJsonPath('groups.0.name', 'Writers')
|
|
->assertJsonPath('groups.0.permissions', ['posts.view', 'posts.write'])
|
|
->assertJsonPath('groups.0.accounts', 1);
|
|
|
|
$values = collect($this->getJson('/manager/groups')->json('catalogue'))->flatMap(fn ($s) => collect($s['permissions'])->pluck('value'))->all();
|
|
expect($values)->toContain('accounts.manage', 'media.upload')->not->toContain('groups.manage', 'reviews.manage');
|
|
});
|
|
|
|
it('creates a group, with what its permissions need', function () {
|
|
$this->postJson('/manager/groups', ['name' => ' Editors ', 'description' => 'Publish', 'permissions' => ['posts.publish', 'media.upload']])->assertCreated();
|
|
|
|
expect(Group::sole()->permissions)->toBe(['media.view', 'media.upload', 'posts.view', 'posts.write', 'posts.publish']);
|
|
});
|
|
|
|
it('refuses a name that is taken, a permission that does not exist and groups.manage', function () {
|
|
Group::create(['name' => 'Writers']);
|
|
|
|
$this->postJson('/manager/groups', ['name' => 'Writers'])->assertJsonValidationErrors('name');
|
|
$this->postJson('/manager/groups', ['name' => 'Others', 'permissions' => ['groups.manage']])->assertJsonValidationErrors('permissions.0');
|
|
expect(Group::count())->toBe(1);
|
|
});
|
|
|
|
it('changes what a group allows, which its accounts feel at once', function () {
|
|
$group = Group::create(['name' => 'Writers', 'permissions' => ['posts.write']]);
|
|
$writer = makeUser();
|
|
$writer->managerGroups()->attach($group);
|
|
|
|
$this->putJson("/manager/groups/{$group->id}", ['name' => 'Writers', 'permissions' => ['pages.edit']])->assertOk();
|
|
|
|
$this->actingAs($writer->refresh())->getJson('/manager/posts')->assertForbidden();
|
|
$this->getJson('/manager/pages')->assertOk();
|
|
});
|
|
|
|
it('deletes a group and leaves its accounts, without what it allowed', function () {
|
|
$group = Group::create(['name' => 'Writers', 'permissions' => ['posts.write']]);
|
|
$writer = makeUser();
|
|
$writer->managerGroups()->attach($group);
|
|
|
|
$this->deleteJson("/manager/groups/{$group->id}")->assertOk();
|
|
|
|
expect(Group::count())->toBe(0)->and($writer->refresh()->managerPermissions())->toBe([]);
|
|
});
|
|
|
|
it('is not for someone who manages accounts but is no super admin', function () {
|
|
$this->actingAs(makeUser(['accounts.manage']));
|
|
|
|
$this->getJson('/manager/groups')->assertForbidden();
|
|
$this->postJson('/manager/groups', ['name' => 'Mine'])->assertForbidden();
|
|
expect(Group::where('name', 'Mine')->exists())->toBeFalse();
|
|
});
|
|
});
|
|
|
|
describe('accounts', function () {
|
|
it('creates an account with a hashed password, never as super admin', function () {
|
|
$this->actingAs(makeUser(superAdmin: true));
|
|
$group = Group::create(['name' => 'Writers', 'permissions' => ['posts.write']]);
|
|
|
|
$this->postJson('/manager/accounts', ['name' => 'Ann', 'email' => ' [email protected] ', 'password' => 'a-long-password-1', 'groups' => [$group->id], 'is_super_admin' => true])->assertCreated();
|
|
|
|
$ann = User::where('email', '[email protected]')->sole();
|
|
expect(Hash::check('a-long-password-1', $ann->password))->toBeTrue()
|
|
->and($ann->isManagerSuperAdmin())->toBeFalse()
|
|
->and($ann->managerGroups->pluck('name')->all())->toBe(['Writers']);
|
|
});
|
|
|
|
it('refuses a short password and an address that is in use', function () {
|
|
$me = makeUser(superAdmin: true);
|
|
$this->actingAs($me);
|
|
|
|
$this->postJson('/manager/accounts', ['name' => 'A', 'email' => '[email protected]', 'password' => 'short'])->assertJsonValidationErrors('password');
|
|
$this->postJson('/manager/accounts', ['name' => 'A', 'email' => $me->email, 'password' => 'a-long-password-1'])->assertJsonValidationErrors('email');
|
|
});
|
|
|
|
it('does not let someone hand out more than they have', function () {
|
|
$this->actingAs(makeUser(['accounts.manage', 'posts.write']));
|
|
$big = Group::create(['name' => 'Big', 'permissions' => ['pages.edit']]);
|
|
|
|
$this->postJson('/manager/accounts', ['name' => 'B', 'email' => '[email protected]', 'password' => 'a-long-password-1', 'groups' => [$big->id]])->assertJsonValidationErrors('groups');
|
|
expect(User::where('email', '[email protected]')->exists())->toBeFalse();
|
|
});
|
|
|
|
it('does not let someone change or delete a mightier account, or themselves', function () {
|
|
$me = makeUser(['accounts.manage']);
|
|
$mighty = makeUser(['accounts.manage', 'posts.write']);
|
|
$boss = makeUser(superAdmin: true);
|
|
$this->actingAs($me);
|
|
|
|
$this->putJson("/manager/accounts/{$mighty->id}", ['name' => 'X', 'email' => '[email protected]', 'password' => 'a-long-password-1'])->assertForbidden();
|
|
$this->deleteJson("/manager/accounts/{$boss->id}")->assertForbidden();
|
|
$this->deleteJson("/manager/accounts/{$me->id}")->assertStatus(422);
|
|
});
|
|
|
|
it('keeps the password when it is left empty, and removes the group links on delete', function () {
|
|
$this->actingAs(makeUser(superAdmin: true));
|
|
$user = makeUser(['posts.view']);
|
|
$hash = $user->password;
|
|
|
|
$this->putJson("/manager/accounts/{$user->id}", ['name' => 'New name', 'email' => $user->email, 'password' => ''])->assertOk();
|
|
expect($user->refresh()->name)->toBe('New name')->and($user->password)->toBe($hash);
|
|
|
|
$this->deleteJson("/manager/accounts/{$user->id}")->assertOk();
|
|
expect(DB::table('manager_group_user')->count())->toBe(0);
|
|
});
|
|
});
|
|
|
|
it('makes a super admin only with the command', function () {
|
|
$this->artisan('manager:create-user', ['--name' => 'Root', '--email' => '[email protected]', '--password' => 'a-long-password-1'])->assertSuccessful();
|
|
|
|
$root = User::where('email', '[email protected]')->sole();
|
|
expect($root->isManagerSuperAdmin())->toBeTrue()->and(Hash::check('a-long-password-1', $root->password))->toBeTrue();
|
|
|
|
$this->artisan('manager:create-user', ['--name' => 'Root', '--email' => '[email protected]', '--password' => 'a-long-password-1'])->assertFailed();
|
|
$this->artisan('manager:create-user', ['--name' => 'Root', '--email' => '[email protected]', '--password' => 'short'])->assertFailed();
|
|
});
|