2FAuth/tests/Feature/Services/GroupServiceTest.php

193 lines
5.3 KiB
PHP
Raw Normal View History

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;
use App\Models\User;
2023-08-01 11:28:27 +02:00
use App\Services\GroupService;
use Illuminate\Auth\Access\AuthorizationException;
2023-08-01 11:28:27 +02:00
use PHPUnit\Framework\Attributes\CoversClass;
2021-11-30 17:39:33 +01:00
use Tests\FeatureTestCase;
/**
2023-08-01 11:28:27 +02:00
* GroupServiceTest test class
2021-11-30 17:39:33 +01:00
*/
2023-08-01 11:28:27 +02:00
#[CoversClass(GroupService::class)]
#[CoversClass(Groups::class)]
2021-11-30 17:39:33 +01:00
class GroupServiceTest extends FeatureTestCase
{
/**
* @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
*/
protected $user;
/**
* @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
*/
protected $otherUser;
2021-11-30 17:39:33 +01:00
/**
* App\Models\Group $groupOne, $groupTwo, $groupThree
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
protected $groupThree;
2021-11-30 17:39:33 +01:00
/**
* App\Models\Group $twofaccountOne, $twofaccountTwo, $twofaccountThree
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
protected $twofaccountThree;
2021-11-30 17:39:33 +01:00
private const NEW_GROUP_NAME = 'MyNewGroup';
2022-11-22 15:15:52 +01:00
/**
* @test
*/
public function setUp() : void
{
parent::setUp();
2022-11-22 15:15:52 +01:00
2023-03-10 22:59:46 +01:00
$this->user = User::factory()->create();
$this->otherUser = User::factory()->create();
2022-11-22 15:15:52 +01:00
2023-03-10 22:59:46 +01:00
$this->groupOne = Group::factory()->for($this->user)->create();
$this->groupTwo = Group::factory()->for($this->user)->create();
$this->groupThree = Group::factory()->for($this->otherUser)->create();
2022-11-22 15:15:52 +01:00
Group::factory()->count(2)->for($this->otherUser)->create();
2021-11-30 17:39:33 +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
$this->twofaccountThree = TwoFAccount::factory()->for($this->otherUser)->create([
'group_id' => $this->groupThree->id,
]);
}
/**
* @test
*/
public function test_assign_a_twofaccount_to_a_group_persists_the_relation()
{
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,
'group_id' => $this->groupTwo->id,
2021-11-30 17:39:33 +01:00
]);
}
/**
* @test
*/
public function test_assign_multiple_twofaccounts_to_group_persists_the_relation()
2021-11-30 17:39:33 +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,
'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,
'group_id' => $this->groupTwo->id,
2021-11-30 17:39:33 +01:00
]);
}
/**
* @test
*/
public function test_assign_a_twofaccount_to_no_group_assigns_to_user_default_group()
2021-11-30 17:39:33 +01:00
{
$this->user['preferences->defaultGroup'] = $this->groupTwo->id;
$this->user->save();
2022-11-22 15:15:52 +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_twofaccount_to_no_group_assigns_to_user_active_group()
2021-11-30 17:39:33 +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
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_twofaccount_to_missing_active_group_returns_not_found()
2021-11-30 17:39:33 +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
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,
'group_id' => $orginalGroup,
2021-11-30 17:39:33 +01:00
]);
}
/**
* @test
*/
public function test_user_can_assign_an_account()
{
$this->expectException(AuthorizationException::class);
Groups::assign($this->twofaccountThree->id, $this->user, $this->user->groups()->first());
}
/**
* @test
*/
public function test_user_can_assign_multiple_accounts()
{
$this->expectException(AuthorizationException::class);
Groups::assign([$this->twofaccountOne->id, $this->twofaccountThree->id], $this->user, $this->user->groups()->first());
}
/**
* @test
*/
public function test_prependTheAllGroup_add_the_group_on_top_of_groups()
{
$groups = Groups::prependTheAllGroup($this->user->groups, $this->user);
2022-11-22 15:15:52 +01:00
$this->assertCount(3, $groups);
$this->assertEquals(0, $groups->first()->id);
$this->assertEquals(2, $groups->first()->twofaccounts_count);
2021-11-30 17:39:33 +01:00
}
2022-11-22 15:15:52 +01:00
}