123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- <?php
- namespace Tests\Feature;
- use App\User;
- use App\Group;
- use Tests\TestCase;
- use App\TwoFAccount;
- class AccountsGroupTest extends TestCase
- {
- /** @var \App\User, \App\TwoFAccount, \App\Group */
- protected $user, $twofaccounts, $group;
- /**
- * @test
- */
- public function setUp(): void
- {
- parent::setUp();
- $this->user = factory(User::class)->create();
- $this->twofaccounts = factory(Twofaccount::class, 3)->create();
- $this->group = factory(Group::class)->create();
- }
- /**
- * test 2FAccounts creation associated to a user group via API
- *
- * @test
- */
- public function testCreateAccountWhenDefaultGroupIsASpecificOne()
- {
- // Set the default group to the existing one
- $response = $this->actingAs($this->user, 'api')
- ->json('POST', '/api/settings/options', [
- 'defaultGroup' => $this->group->id,
- ])
- ->assertStatus(200);
- // Create the account
- $response = $this->actingAs($this->user, 'api')
- ->json('POST', '/api/twofaccounts', [
- 'service' => 'testCreation',
- 'account' => 'test@example.org',
- 'uri' => 'otpauth://totp/test@test.com?secret=A4GRFHZVRBGY7UIW&issuer=test',
- 'icon' => 'test.png',
- ])
- ->assertStatus(201)
- ->assertJsonFragment([
- 'group_id' => $this->group->id
- ]);
- }
- /**
- * test 2FAccounts creation associated to a user group via API
- *
- * @test
- */
- public function testCreateAccountWhenDefaultGroupIsSetToActiveOne()
- {
- // Set the default group as the active one
- $response = $this->actingAs($this->user, 'api')
- ->json('POST', '/api/settings/options', [
- 'defaultGroup' => -1,
- ])
- ->assertStatus(200);
- // Set the active group
- $response = $this->actingAs($this->user, 'api')
- ->json('POST', '/api/settings/options', [
- 'activeGroup' => 1,
- ])
- ->assertStatus(200);
- // Create the account
- $response = $this->actingAs($this->user, 'api')
- ->json('POST', '/api/twofaccounts', [
- 'service' => 'testCreation',
- 'account' => 'test@example.org',
- 'uri' => 'otpauth://totp/test@test.com?secret=A4GRFHZVRBGY7UIW&issuer=test',
- 'icon' => 'test.png',
- ])
- ->assertStatus(201)
- ->assertJsonFragment([
- 'group_id' => 1
- ]);
- }
- /**
- * test 2FAccounts creation associated to a user group via API
- *
- * @test
- */
- public function testCreateAccountWhenDefaultIsNoGroup()
- {
- // Set the default group to No group
- $response = $this->actingAs($this->user, 'api')
- ->json('POST', '/api/settings/options', [
- 'defaultGroup' => 0,
- ])
- ->assertStatus(200);
- // Create the account
- $response = $this->actingAs($this->user, 'api')
- ->json('POST', '/api/twofaccounts', [
- 'service' => 'testCreation',
- 'account' => 'test@example.org',
- 'uri' => 'otpauth://totp/test@test.com?secret=A4GRFHZVRBGY7UIW&issuer=test',
- 'icon' => 'test.png',
- ])
- ->assertStatus(201)
- ->assertJsonMissing([
- 'group_id' => null
- ]);
- }
- /**
- * test 2FAccounts creation associated to a user group via API
- *
- * @test
- */
- public function testCreateAccountWhenDefaultGroupDoNotExists()
- {
- // Set the default group to a non existing one
- $response = $this->actingAs($this->user, 'api')
- ->json('POST', '/api/settings/options', [
- 'defaultGroup' => 1000,
- ])
- ->assertStatus(200);
- // Create the account
- $response = $this->actingAs($this->user, 'api')
- ->json('POST', '/api/twofaccounts', [
- 'service' => 'testCreation',
- 'account' => 'test@example.org',
- 'uri' => 'otpauth://totp/test@test.com?secret=A4GRFHZVRBGY7UIW&issuer=test',
- 'icon' => 'test.png',
- ])
- ->assertStatus(201)
- ->assertJsonMissing([
- 'group_id' => null
- ]);
- }
- /**
- * test 2FAccounts association with a user group via API
- *
- * @test
- */
- public function testMoveAccountsToGroup()
- {
- // We associate all 3 accounts to the user group
- $response = $this->actingAs($this->user, 'api')
- ->json('PATCH', '/api/group/accounts/', [
- 'groupId' => $this->group->id,
- 'accountsIds' => [1,2,3]
- ])
- ->assertJsonFragment([
- 'id' => $this->group->id,
- 'name' => $this->group->name
- ])
- ->assertStatus(200);
- // test if the accounts have the correct foreign key
- $response = $this->actingAs($this->user, 'api')
- ->json('GET', '/api/twofaccounts/1')
- ->assertJsonFragment([
- 'group_id' => $this->group->id
- ]);
- $response = $this->actingAs($this->user, 'api')
- ->json('GET', '/api/twofaccounts/2')
- ->assertJsonFragment([
- 'group_id' => $this->group->id
- ]);
- $response = $this->actingAs($this->user, 'api')
- ->json('GET', '/api/twofaccounts/3')
- ->assertJsonFragment([
- 'group_id' => $this->group->id
- ]);
- // test the accounts count of the user group
- $response = $this->actingAs($this->user, 'api')
- ->json('GET', '/api/groups')
- ->assertJsonFragment([
- 'twofaccounts_count' => 3
- ]
- );
- }
- /**
- * test 2FAccounts association with a missing group via API
- *
- * @test
- */
- public function testMoveAccountsToMissingGroup()
- {
- $response = $this->actingAs($this->user, 'api')
- ->json('PATCH', '/api/group/accounts/', [
- 'groupId' => '1000',
- 'accountsIds' => $this->twofaccounts->keys()
- ])
- ->assertStatus(404);
- }
- /**
- * test 2FAccounts association with the pseudo group via API
- *
- * @test
- */
- public function testMoveAccountsToPseudoGroup()
- {
- $response = $this->actingAs($this->user, 'api')
- ->json('PATCH', '/api/group/accounts/', [
- 'groupId' => $this->group->id,
- 'accountsIds' => [1,2,3]
- ]);
- // We associate the first account to the pseudo group
- $response = $this->actingAs($this->user, 'api')
- ->json('PATCH', '/api/group/accounts/', [
- 'groupId' => 0,
- 'accountsIds' => [1]
- ])
- ->assertStatus(200);
- // test if the forein keys are set to NULL
- $response = $this->actingAs($this->user, 'api')
- ->json('GET', '/api/twofaccounts/1')
- ->assertJsonFragment([
- 'group_id' => null
- ]);
- // test the accounts count of the group
- $response = $this->actingAs($this->user, 'api')
- ->json('GET', '/api/groups')
- ->assertJsonFragment([
- 'twofaccounts_count' => 3, // the 3 accounts for 'all'
- 'twofaccounts_count' => 2 // the 2 accounts that remain in the user group
- ]
- );
- }
- }
|