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

289 lines
8.4 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;
use App\Facades\Settings;
2021-12-02 13:15:53 +01:00
use App\Models\Group;
use App\Models\TwoFAccount;
2021-11-30 17:39:33 +01:00
use Tests\FeatureTestCase;
/**
* @covers \App\Services\GroupService
*/
class GroupServiceTest extends FeatureTestCase
{
/**
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
2021-11-30 17:39:33 +01:00
private const TWOFACCOUNT_COUNT = 2;
2022-11-22 15:15:52 +01:00
2021-11-30 17:39:33 +01:00
private const ACCOUNT = 'account';
2022-11-22 15:15:52 +01:00
2021-11-30 17:39:33 +01:00
private const SERVICE = 'service';
2022-11-22 15:15:52 +01:00
2021-11-30 17:39:33 +01:00
private const SECRET = 'A4GRFHVVRBGY7UIW';
2022-11-22 15:15:52 +01:00
2021-11-30 17:39:33 +01:00
private const ALGORITHM_CUSTOM = 'sha256';
2022-11-22 15:15:52 +01:00
2021-11-30 17:39:33 +01:00
private const DIGITS_CUSTOM = 7;
2022-11-22 15:15:52 +01:00
2021-11-30 17:39:33 +01:00
private const PERIOD_CUSTOM = 40;
2022-11-22 15:15:52 +01:00
2021-11-30 17:39:33 +01:00
private const IMAGE = 'https%3A%2F%2Fen.opensuse.org%2Fimages%2F4%2F44%2FButton-filled-colour.png';
2022-11-22 15:15:52 +01:00
private const ICON = 'test.png';
2021-11-30 17:39:33 +01:00
2022-11-22 15:15:52 +01:00
private const TOTP_FULL_CUSTOM_URI = 'otpauth://totp/' . self::SERVICE . ':' . self::ACCOUNT . '?secret=' . self::SECRET . '&issuer=' . self::SERVICE . '&digits=' . self::DIGITS_CUSTOM . '&period=' . self::PERIOD_CUSTOM . '&algorithm=' . self::ALGORITHM_CUSTOM . '&image=' . self::IMAGE;
2021-11-30 17:39:33 +01:00
/**
* @test
*/
public function setUp() : void
{
parent::setUp();
2022-11-22 15:15:52 +01:00
$this->groupOne = new Group;
2021-11-30 17:39:33 +01:00
$this->groupOne->name = 'MyGroupOne';
$this->groupOne->save();
2022-11-22 15:15:52 +01:00
$this->groupTwo = new Group;
2021-11-30 17:39:33 +01:00
$this->groupTwo->name = 'MyGroupTwo';
$this->groupTwo->save();
2022-11-22 15:15:52 +01:00
$this->twofaccountOne = new TwoFAccount;
2021-11-30 17:39:33 +01:00
$this->twofaccountOne->legacy_uri = self::TOTP_FULL_CUSTOM_URI;
2022-11-22 15:15:52 +01:00
$this->twofaccountOne->service = self::SERVICE;
$this->twofaccountOne->account = self::ACCOUNT;
$this->twofaccountOne->icon = self::ICON;
$this->twofaccountOne->otp_type = 'totp';
$this->twofaccountOne->secret = self::SECRET;
$this->twofaccountOne->digits = self::DIGITS_CUSTOM;
$this->twofaccountOne->algorithm = self::ALGORITHM_CUSTOM;
$this->twofaccountOne->period = self::PERIOD_CUSTOM;
$this->twofaccountOne->counter = null;
2021-11-30 17:39:33 +01:00
$this->twofaccountOne->save();
2022-11-22 15:15:52 +01:00
$this->twofaccountTwo = new TwoFAccount;
2021-11-30 17:39:33 +01:00
$this->twofaccountTwo->legacy_uri = self::TOTP_FULL_CUSTOM_URI;
2022-11-22 15:15:52 +01:00
$this->twofaccountTwo->service = self::SERVICE;
$this->twofaccountTwo->account = self::ACCOUNT;
$this->twofaccountTwo->icon = self::ICON;
$this->twofaccountTwo->otp_type = 'totp';
$this->twofaccountTwo->secret = self::SECRET;
$this->twofaccountTwo->digits = self::DIGITS_CUSTOM;
$this->twofaccountTwo->algorithm = self::ALGORITHM_CUSTOM;
$this->twofaccountTwo->period = self::PERIOD_CUSTOM;
$this->twofaccountTwo->counter = null;
2021-11-30 17:39:33 +01:00
$this->twofaccountTwo->save();
}
/**
* @test
*/
public function test_getAll_returns_a_collection()
{
$this->assertInstanceOf(\Illuminate\Database\Eloquent\Collection::class, Groups::getAll());
2021-11-30 17:39:33 +01:00
}
/**
* @test
*/
public function test_getAll_adds_pseudo_group_on_top_of_user_groups()
{
$groups = Groups::getAll();
2022-11-22 15:15:52 +01:00
2021-11-30 17:39:33 +01:00
$this->assertEquals(0, $groups->first()->id);
$this->assertEquals(__('commons.all'), $groups->first()->name);
}
/**
* @test
*/
public function test_getAll_returns_pseudo_group_with_all_twofaccounts_count()
{
$groups = Groups::getAll();
2022-11-22 15:15:52 +01:00
2021-11-30 17:39:33 +01:00
$this->assertEquals(self::TWOFACCOUNT_COUNT, $groups->first()->twofaccounts_count);
}
/**
* @test
*/
public function test_create_persists_and_returns_created_group()
{
$newGroup = Groups::create(['name' => self::NEW_GROUP_NAME]);
2022-11-22 15:15:52 +01:00
2021-11-30 17:39:33 +01:00
$this->assertDatabaseHas('groups', ['name' => self::NEW_GROUP_NAME]);
2021-12-02 13:15:53 +01:00
$this->assertInstanceOf(\App\Models\Group::class, $newGroup);
2021-11-30 17:39:33 +01:00
$this->assertEquals(self::NEW_GROUP_NAME, $newGroup->name);
}
/**
* @test
*/
public function test_update_persists_and_returns_updated_group()
{
$this->groupOne = Groups::update($this->groupOne, ['name' => self::NEW_GROUP_NAME]);
2022-11-22 15:15:52 +01:00
2021-11-30 17:39:33 +01:00
$this->assertDatabaseHas('groups', ['name' => self::NEW_GROUP_NAME]);
2021-12-02 13:15:53 +01:00
$this->assertInstanceOf(\App\Models\Group::class, $this->groupOne);
2021-11-30 17:39:33 +01:00
$this->assertEquals(self::NEW_GROUP_NAME, $this->groupOne->name);
}
/**
* @test
*/
public function test_delete_a_groupId_clear_db_and_returns_deleted_count()
{
$deleted = Groups::delete($this->groupOne->id);
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()
{
$deleted = Groups::delete([$this->groupOne->id, $this->groupTwo->id]);
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
*/
public function test_delete_default_group_reset_defaultGroup_setting()
{
2022-07-30 17:51:02 +02:00
Settings::set('defaultGroup', $this->groupOne->id);
2021-11-30 17:39:33 +01:00
$deleted = Groups::delete($this->groupOne->id);
2022-11-22 15:15:52 +01:00
2021-11-30 17:39:33 +01:00
$this->assertDatabaseHas('options', [
2022-11-22 15:15:52 +01:00
'key' => 'defaultGroup',
'value' => 0,
2021-11-30 17:39:33 +01:00
]);
}
/**
* @test
*/
public function test_delete_active_group_reset_activeGroup_setting()
{
2022-07-30 17:51:02 +02:00
Settings::set('rememberActiveGroup', true);
Settings::set('activeGroup', $this->groupOne->id);
2022-11-22 15:15:52 +01:00
$deleted = Groups::delete($this->groupOne->id);
2022-11-22 15:15:52 +01:00
2021-11-30 17:39:33 +01:00
$this->assertDatabaseHas('options', [
2022-11-22 15:15:52 +01:00
'key' => 'activeGroup',
'value' => 0,
2021-11-30 17:39:33 +01:00
]);
}
/**
* @test
*/
public function test_assign_a_twofaccountid_to_a_specified_group_persists_the_relation()
{
Groups::assign($this->twofaccountOne->id, $this->groupOne);
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->groupOne->id,
]);
}
/**
* @test
*/
public function test_assign_multiple_twofaccountid_to_a_specified_group_persists_the_relation()
{
Groups::assign([$this->twofaccountOne->id, $this->twofaccountTwo->id], $this->groupOne);
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->groupOne->id,
]);
$this->assertDatabaseHas('twofaccounts', [
2022-11-22 15:15:52 +01:00
'id' => $this->twofaccountTwo->id,
2021-11-30 17:39:33 +01:00
'group_id' => $this->groupOne->id,
]);
}
/**
* @test
*/
public function test_assign_a_twofaccountid_to_no_group_assigns_to_default_group()
{
2022-07-30 17:51:02 +02:00
Settings::set('defaultGroup', $this->groupTwo->id);
2022-11-22 15:15:52 +01:00
Groups::assign($this->twofaccountOne->id);
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()
{
2022-07-30 17:51:02 +02:00
Settings::set('defaultGroup', -1);
Settings::set('activeGroup', $this->groupTwo->id);
2022-11-22 15:15:52 +01:00
Groups::assign($this->twofaccountOne->id);
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_missing_active_group_does_not_fails()
{
2022-07-30 17:51:02 +02:00
Settings::set('defaultGroup', -1);
Settings::set('activeGroup', 100000);
2022-11-22 15:15:52 +01:00
Groups::assign($this->twofaccountOne->id);
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' => null,
]);
}
/**
* @test
*/
public function test_getAccounts_returns_accounts()
{
Groups::assign([$this->twofaccountOne->id, $this->twofaccountTwo->id], $this->groupOne);
$accounts = Groups::getAccounts($this->groupOne);
2022-11-22 15:15:52 +01:00
2021-11-30 17:39:33 +01:00
$this->assertEquals(2, $accounts->count());
}
2022-11-22 15:15:52 +01:00
}