2FAuth/tests/Unit/TwoFAccountTest.php

139 lines
3.3 KiB
PHP
Raw Normal View History

2019-05-20 07:37:41 +02:00
<?php
namespace Tests\Unit;
2019-06-06 13:40:06 +02:00
use App\User;
2019-05-20 07:37:41 +02:00
use Tests\TestCase;
2019-05-25 23:51:20 +02:00
use App\TwoFAccount;
2019-05-20 07:37:41 +02:00
2019-05-25 23:51:20 +02:00
class TwoFAccountTest extends TestCase
2019-05-20 07:37:41 +02:00
{
2019-06-06 13:40:06 +02:00
/** @var \App\User */
protected $user;
/**
* @test
*/
public function setUp(): void
{
parent::setUp();
$this->user = factory(User::class)->create();
}
2019-05-20 07:37:41 +02:00
/**
2019-05-25 23:51:20 +02:00
* test TwoFAccount creation via API
2019-05-20 07:37:41 +02:00
*
2019-06-06 13:40:06 +02:00
* @test
2019-05-20 07:37:41 +02:00
*/
2019-05-25 23:51:20 +02:00
public function testTwoFAccountCreation()
2019-05-20 07:37:41 +02:00
{
2019-06-06 13:40:06 +02:00
$response = $this->actingAs($this->user, 'api')
2019-05-25 23:51:20 +02:00
->json('POST', '/api/twofaccounts', [
'service' => 'testCreation',
'account' => 'test@example.org',
'uri' => 'test',
2019-05-25 23:51:20 +02:00
])
->assertStatus(201)
->assertJson([
'service' => 'testCreation',
'account' => 'test@example.org',
'uri' => 'test',
2019-05-25 23:51:20 +02:00
]);
2019-05-20 07:37:41 +02:00
}
2019-05-25 23:51:20 +02:00
2019-05-26 16:42:09 +02:00
/**
* test TOTP generation via API
*
2019-06-06 13:40:06 +02:00
* @test
2019-05-26 16:42:09 +02:00
*/
public function testTOTPgeneration()
{
2019-06-06 13:40:06 +02:00
$twofaccount = factory(TwoFAccount::class)->create([
'service' => 'testTOTP',
'account' => 'test@test.com',
'uri' => 'otpauth://totp/test@test.com?secret=A4GRFHVVRBGY7UIW&issuer=test'
2019-05-26 16:42:09 +02:00
]);
2019-06-06 13:40:06 +02:00
$response = $this->actingAs($this->user, 'api')
->json('GET', '/api/twofaccounts/' . $twofaccount->id . '/totp')
2019-05-26 16:42:09 +02:00
->assertStatus(200)
->assertJsonStructure([
'totp',
]);
}
2019-05-22 00:49:27 +02:00
/**
2019-05-25 23:51:20 +02:00
* test TwoFAccount update via API
2019-05-22 00:49:27 +02:00
*
2019-06-06 13:40:06 +02:00
* @test
2019-05-22 00:49:27 +02:00
*/
2019-05-25 23:51:20 +02:00
public function testTwoFAccountUpdate()
2019-05-22 00:49:27 +02:00
{
2019-06-06 13:40:06 +02:00
$twofaccount = factory(TwoFAccount::class)->create();
2019-05-22 00:49:27 +02:00
2019-06-06 13:40:06 +02:00
$response = $this->actingAs($this->user, 'api')
->json('PUT', '/api/twofaccounts/' . $twofaccount->id, [
'service' => 'testUpdate',
'account' => 'testUpdate@test.com',
'uri' => 'testUpdate',
2019-05-25 23:51:20 +02:00
])
->assertStatus(200)
->assertJson([
'id' => 1,
'service' => 'testUpdate',
'account' => 'testUpdate@test.com',
'uri' => 'testUpdate',
2019-05-25 23:51:20 +02:00
'icon' => null,
]);
2019-05-22 00:49:27 +02:00
}
2019-05-20 07:37:41 +02:00
/**
* test TwoFAccount index fetching via API
2019-05-20 07:37:41 +02:00
*
2019-06-06 13:40:06 +02:00
* @test
2019-05-20 07:37:41 +02:00
*/
2019-05-25 23:51:20 +02:00
public function testTwoFAccountIndexListing()
2019-05-20 07:37:41 +02:00
{
2019-06-06 13:40:06 +02:00
$twofaccount = factory(TwoFAccount::class, 3)->create();
2019-05-20 07:37:41 +02:00
2019-06-06 13:40:06 +02:00
$response = $this->actingAs($this->user, 'api')
->json('GET', '/api/twofaccounts')
2019-05-25 23:51:20 +02:00
->assertStatus(200)
->assertJsonStructure([
2019-05-20 07:37:41 +02:00
'*' => [
'id',
'service',
'account',
'uri',
2019-05-20 07:37:41 +02:00
'icon',
'created_at',
'updated_at',
'deleted_at'
]
]
);
}
/**
* test TwoFAccount deletion via API
2019-06-06 13:40:06 +02:00
*
* @test
2019-05-20 07:37:41 +02:00
*/
public function testTwoFAccountDeletion()
2019-05-20 07:37:41 +02:00
{
2019-06-06 13:40:06 +02:00
$twofaccount = factory(TwoFAccount::class)->create();
2019-05-20 07:37:41 +02:00
2019-06-06 13:40:06 +02:00
$response = $this->actingAs($this->user, 'api')
2019-05-25 23:51:20 +02:00
->json('DELETE', '/api/twofaccounts/' . $twofaccount->id)
->assertStatus(204);
}
2019-05-20 07:37:41 +02:00
}