2021-11-30 17:39:33 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature\Services;
|
|
|
|
|
2022-11-22 15:15:52 +01:00
|
|
|
use App\Facades\Groups;
|
2021-12-02 13:15:53 +01:00
|
|
|
use App\Models\Group;
|
|
|
|
use App\Models\TwoFAccount;
|
2023-02-27 00:32:49 +01:00
|
|
|
use App\Models\User;
|
|
|
|
use App\Policies\GroupPolicy;
|
|
|
|
use Illuminate\Auth\Access\AuthorizationException;
|
|
|
|
use Mockery\MockInterface;
|
2021-11-30 17:39:33 +01:00
|
|
|
use Tests\FeatureTestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \App\Services\GroupService
|
2022-12-09 10:52:17 +01:00
|
|
|
* @covers \App\Facades\Groups
|
2021-11-30 17:39:33 +01:00
|
|
|
*/
|
|
|
|
class GroupServiceTest extends FeatureTestCase
|
|
|
|
{
|
2023-02-27 00:32:49 +01:00
|
|
|
/**
|
|
|
|
* @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
|
|
|
|
*/
|
|
|
|
protected $user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
|
|
|
|
*/
|
|
|
|
protected $admin;
|
|
|
|
|
2021-11-30 17:39:33 +01:00
|
|
|
/**
|
2021-12-02 13:15:53 +01:00
|
|
|
* App\Models\Group $groupOne, $groupTwo
|
2021-11-30 17:39:33 +01:00
|
|
|
*/
|
2022-11-22 15:15:52 +01:00
|
|
|
protected $groupOne;
|
2021-11-30 17:39:33 +01:00
|
|
|
|
2022-11-22 15:15:52 +01:00
|
|
|
protected $groupTwo;
|
2021-11-30 17:39:33 +01:00
|
|
|
|
|
|
|
/**
|
2021-12-02 13:15:53 +01:00
|
|
|
* App\Models\Group $twofaccountOne, $twofaccountTwo
|
2021-11-30 17:39:33 +01:00
|
|
|
*/
|
2022-11-22 15:15:52 +01:00
|
|
|
protected $twofaccountOne;
|
|
|
|
|
|
|
|
protected $twofaccountTwo;
|
2021-11-30 17:39:33 +01:00
|
|
|
|
|
|
|
private const NEW_GROUP_NAME = 'MyNewGroup';
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function setUp() : void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
$this->user = User::factory()->create();
|
|
|
|
$this->admin = User::factory()->administrator()->create();
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
$this->groupOne = Group::factory()->for($this->user)->create();
|
|
|
|
$this->groupTwo = Group::factory()->for($this->user)->create();
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
Group::factory()->count(3)->for($this->admin)->create();
|
2021-11-30 17:39:33 +01:00
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
$this->twofaccountOne = TwoFAccount::factory()->for($this->user)->create([
|
|
|
|
'group_id' => $this->groupOne->id,
|
|
|
|
]);
|
|
|
|
$this->twofaccountTwo = TwoFAccount::factory()->for($this->user)->create([
|
|
|
|
'group_id' => $this->groupTwo->id,
|
|
|
|
]);
|
2021-11-30 17:39:33 +01:00
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
TwoFAccount::factory()->for($this->admin)->create();
|
|
|
|
}
|
2021-11-30 17:39:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2023-02-27 00:32:49 +01:00
|
|
|
public function test_getAll_returns_pseudo_group_on_top_of_user_groups_only()
|
2021-11-30 17:39:33 +01:00
|
|
|
{
|
2023-02-27 00:32:49 +01:00
|
|
|
$groups = Groups::getAll($this->user);
|
2021-11-30 17:39:33 +01:00
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
$this->assertCount(3, $groups);
|
|
|
|
$this->assertEquals(0, $groups->first()->id);
|
|
|
|
$this->assertEquals(__('commons.all'), $groups->first()->name);
|
|
|
|
$this->assertEquals($this->groupOne->user_id, $groups[1]->user_id);
|
|
|
|
$this->assertEquals($this->groupTwo->user_id, $groups[2]->user_id);
|
2021-11-30 17:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2023-02-27 00:32:49 +01:00
|
|
|
public function test_getAll_returns_groups_with_count()
|
2021-11-30 17:39:33 +01:00
|
|
|
{
|
2023-02-27 00:32:49 +01:00
|
|
|
$groups = Groups::getAll($this->user);
|
|
|
|
|
|
|
|
$this->assertEquals(2, $groups->first()->twofaccounts_count);
|
|
|
|
$this->assertEquals(1, $groups[1]->twofaccounts_count);
|
|
|
|
$this->assertEquals(1, $groups[2]->twofaccounts_count);
|
2021-11-30 17:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2023-02-27 00:32:49 +01:00
|
|
|
public function test_create_persists_and_returns_created_group()
|
2021-11-30 17:39:33 +01:00
|
|
|
{
|
2023-02-27 00:32:49 +01:00
|
|
|
$newGroup = Groups::create(['name' => self::NEW_GROUP_NAME], $this->user);
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
$this->assertDatabaseHas('groups', [
|
|
|
|
'name' => self::NEW_GROUP_NAME,
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
]);
|
|
|
|
$this->assertInstanceOf(Group::class, $newGroup);
|
|
|
|
$this->assertEquals(self::NEW_GROUP_NAME, $newGroup->name);
|
2021-11-30 17:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2023-02-27 00:32:49 +01:00
|
|
|
public function test_create_authorization()
|
2021-11-30 17:39:33 +01:00
|
|
|
{
|
2023-02-27 00:32:49 +01:00
|
|
|
$this->mock(GroupPolicy::class, function (MockInterface $groupPolicy) {
|
|
|
|
$groupPolicy->shouldReceive('create')
|
|
|
|
->andReturn(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->expectException(AuthorizationException::class);
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
Groups::create(['name' => 'lorem'], $this->user);
|
2021-11-30 17:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2023-02-27 00:32:49 +01:00
|
|
|
public function test_update_persists_and_returns_updated_group()
|
2021-11-30 17:39:33 +01:00
|
|
|
{
|
2023-02-27 00:32:49 +01:00
|
|
|
$this->groupOne = Groups::update($this->groupOne, ['name' => self::NEW_GROUP_NAME], $this->user);
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2021-11-30 17:39:33 +01:00
|
|
|
$this->assertDatabaseHas('groups', ['name' => self::NEW_GROUP_NAME]);
|
2023-02-27 00:32:49 +01:00
|
|
|
$this->assertInstanceOf(Group::class, $this->groupOne);
|
|
|
|
$this->assertEquals(self::NEW_GROUP_NAME, $this->groupOne->name);
|
2021-11-30 17:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2023-02-27 00:32:49 +01:00
|
|
|
public function test_update_fails_when_user_does_not_own_the_group()
|
2021-11-30 17:39:33 +01:00
|
|
|
{
|
2023-02-27 00:32:49 +01:00
|
|
|
$this->expectException(AuthorizationException::class);
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
Groups::update($this->groupOne, ['name' => self::NEW_GROUP_NAME], $this->admin);
|
2021-11-30 17:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_delete_a_groupId_clear_db_and_returns_deleted_count()
|
|
|
|
{
|
2023-02-27 00:32:49 +01:00
|
|
|
$deleted = Groups::delete($this->groupOne->id, $this->user);
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2021-11-30 17:39:33 +01:00
|
|
|
$this->assertDatabaseMissing('groups', ['id' => $this->groupOne->id]);
|
|
|
|
$this->assertEquals(1, $deleted);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_delete_an_array_of_ids_clear_db_and_returns_deleted_count()
|
|
|
|
{
|
2023-02-27 00:32:49 +01:00
|
|
|
$deleted = Groups::delete([$this->groupOne->id, $this->groupTwo->id], $this->user);
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2021-11-30 17:39:33 +01:00
|
|
|
$this->assertDatabaseMissing('groups', ['id' => $this->groupOne->id]);
|
|
|
|
$this->assertDatabaseMissing('groups', ['id' => $this->groupTwo->id]);
|
|
|
|
$this->assertEquals(2, $deleted);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2023-02-27 00:32:49 +01:00
|
|
|
public function test_delete_missing_id_does_not_fail_and_returns_deleted_count()
|
2021-11-30 17:39:33 +01:00
|
|
|
{
|
2023-02-27 00:32:49 +01:00
|
|
|
$this->assertDatabaseMissing('groups', ['id' => 1000]);
|
2021-11-30 17:39:33 +01:00
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
$deleted = Groups::delete([$this->groupOne->id, 1000], $this->user);
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
$this->assertDatabaseMissing('groups', ['id' => $this->groupOne->id]);
|
|
|
|
$this->assertEquals(1, $deleted);
|
2021-11-30 17:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2023-02-27 00:32:49 +01:00
|
|
|
public function test_delete_default_group_reset_defaultGroup_preference()
|
2021-11-30 17:39:33 +01:00
|
|
|
{
|
2023-02-27 00:32:49 +01:00
|
|
|
$this->user['preferences->defaultGroup'] = $this->groupOne->id;
|
|
|
|
$this->user->save();
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
Groups::delete($this->groupOne->id, $this->user);
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
$this->user->refresh();
|
|
|
|
$this->assertEquals(0, $this->user->preferences['defaultGroup']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_delete_active_group_reset_activeGroup_preference()
|
|
|
|
{
|
|
|
|
$this->user['preferences->rememberActiveGroup'] = true;
|
|
|
|
$this->user['preferences->activeGroup'] = $this->groupOne->id;
|
|
|
|
$this->user->save();
|
|
|
|
|
|
|
|
Groups::delete($this->groupOne->id, $this->user);
|
|
|
|
|
|
|
|
$this->user->refresh();
|
|
|
|
$this->assertEquals(0, $this->user->preferences['activeGroup']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_delete_fails_when_user_does_not_own_one_of_the_groups()
|
|
|
|
{
|
|
|
|
$this->expectException(AuthorizationException::class);
|
|
|
|
|
|
|
|
Groups::delete($this->groupOne->id, $this->admin);
|
2021-11-30 17:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_assign_a_twofaccountid_to_a_specified_group_persists_the_relation()
|
|
|
|
{
|
2023-02-27 00:32:49 +01:00
|
|
|
Groups::assign($this->twofaccountOne->id, $this->user, $this->groupTwo);
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2021-11-30 17:39:33 +01:00
|
|
|
$this->assertDatabaseHas('twofaccounts', [
|
2022-11-22 15:15:52 +01:00
|
|
|
'id' => $this->twofaccountOne->id,
|
2023-02-27 00:32:49 +01:00
|
|
|
'group_id' => $this->groupTwo->id,
|
2021-11-30 17:39:33 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_assign_multiple_twofaccountid_to_a_specified_group_persists_the_relation()
|
|
|
|
{
|
2023-02-27 00:32:49 +01:00
|
|
|
Groups::assign([$this->twofaccountOne->id, $this->twofaccountTwo->id], $this->user, $this->groupTwo);
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2021-11-30 17:39:33 +01:00
|
|
|
$this->assertDatabaseHas('twofaccounts', [
|
2022-11-22 15:15:52 +01:00
|
|
|
'id' => $this->twofaccountOne->id,
|
2023-02-27 00:32:49 +01:00
|
|
|
'group_id' => $this->groupTwo->id,
|
2021-11-30 17:39:33 +01:00
|
|
|
]);
|
|
|
|
$this->assertDatabaseHas('twofaccounts', [
|
2022-11-22 15:15:52 +01:00
|
|
|
'id' => $this->twofaccountTwo->id,
|
2023-02-27 00:32:49 +01:00
|
|
|
'group_id' => $this->groupTwo->id,
|
2021-11-30 17:39:33 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_assign_a_twofaccountid_to_no_group_assigns_to_default_group()
|
|
|
|
{
|
2023-02-27 00:32:49 +01:00
|
|
|
$this->user['preferences->defaultGroup'] = $this->groupTwo->id;
|
|
|
|
$this->user->save();
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
Groups::assign($this->twofaccountOne->id, $this->user);
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2021-11-30 17:39:33 +01:00
|
|
|
$this->assertDatabaseHas('twofaccounts', [
|
2022-11-22 15:15:52 +01:00
|
|
|
'id' => $this->twofaccountOne->id,
|
2021-11-30 17:39:33 +01:00
|
|
|
'group_id' => $this->groupTwo->id,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_assign_a_twofaccountid_to_no_group_assigns_to_active_group()
|
|
|
|
{
|
2023-02-27 00:32:49 +01:00
|
|
|
$this->user['preferences->defaultGroup'] = -1;
|
|
|
|
$this->user['preferences->activeGroup'] = $this->groupTwo->id;
|
|
|
|
$this->user->save();
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
Groups::assign($this->twofaccountOne->id, $this->user);
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2021-11-30 17:39:33 +01:00
|
|
|
$this->assertDatabaseHas('twofaccounts', [
|
2022-11-22 15:15:52 +01:00
|
|
|
'id' => $this->twofaccountOne->id,
|
2021-11-30 17:39:33 +01:00
|
|
|
'group_id' => $this->groupTwo->id,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2023-02-27 00:32:49 +01:00
|
|
|
public function test_assign_a_twofaccountid_to_missing_active_group_returns_not_found()
|
2021-11-30 17:39:33 +01:00
|
|
|
{
|
2023-02-27 00:32:49 +01:00
|
|
|
$orginalGroup = $this->twofaccountOne->group_id;
|
|
|
|
|
|
|
|
$this->user['preferences->defaultGroup'] = -1;
|
|
|
|
$this->user['preferences->activeGroup'] = 1000;
|
|
|
|
$this->user->save();
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
Groups::assign($this->twofaccountOne->id, $this->user);
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2021-11-30 17:39:33 +01:00
|
|
|
$this->assertDatabaseHas('twofaccounts', [
|
2022-11-22 15:15:52 +01:00
|
|
|
'id' => $this->twofaccountOne->id,
|
2023-02-27 00:32:49 +01:00
|
|
|
'group_id' => $orginalGroup,
|
2021-11-30 17:39:33 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2023-02-27 00:32:49 +01:00
|
|
|
public function test_assign_fails_when_user_does_not_own_the_group()
|
|
|
|
{
|
|
|
|
$this->expectException(AuthorizationException::class);
|
|
|
|
|
|
|
|
Groups::assign($this->twofaccountOne->id, $this->user, $this->admin->groups()->first());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_assign_fails_when_user_does_not_own_one_of_the_accounts()
|
2021-11-30 17:39:33 +01:00
|
|
|
{
|
2023-02-27 00:32:49 +01:00
|
|
|
$this->expectException(AuthorizationException::class);
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2023-02-27 00:32:49 +01:00
|
|
|
Groups::assign([$this->twofaccountOne->id, $this->admin->twofaccounts()->first()->id], $this->user, $this->groupTwo);
|
2021-11-30 17:39:33 +01:00
|
|
|
}
|
2022-11-22 15:15:52 +01:00
|
|
|
}
|