2FAuth/tests/Unit/TwoFAccountTest.php

168 lines
3.9 KiB
PHP
Raw Normal View History

2019-05-20 07:37:41 +02:00
<?php
namespace Tests\Unit;
use Tests\TestCase;
2019-05-25 23:51:20 +02:00
use App\TwoFAccount;
2019-05-23 21:27:27 +02:00
use Illuminate\Support\Facades\Artisan;
2019-05-20 07:37:41 +02:00
use Illuminate\Foundation\Testing\WithFaker;
2019-05-23 21:27:27 +02:00
use Illuminate\Foundation\Testing\DatabaseTransactions;
2019-05-22 00:49:27 +02:00
use Illuminate\Auth\Authenticatable;
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-05-23 14:02:29 +02:00
2019-05-23 21:27:27 +02:00
/**
* Rollback and execute migrations for each test.
*/
use DatabaseTransactions;
/**
* set up fresh db
*/
public function setUp(): void
{
parent::setUp();
Artisan::call('migrate', ['--seed' => true]);
Artisan::call('passport:install',['--verbose' => 2]);
}
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
*
* @return void
*/
2019-05-25 23:51:20 +02:00
public function testTwoFAccountCreation()
2019-05-20 07:37:41 +02:00
{
2019-05-25 23:51:20 +02:00
$user = \App\User::find(1);
2019-05-20 07:37:41 +02:00
2019-05-25 23:51:20 +02:00
$response = $this->actingAs($user, 'api')
->json('POST', '/api/twofaccounts', [
'name' => 'testCreation',
'secret' => 'test',
])
->assertStatus(201)
->assertJson([
'name' => 'testCreation',
'secret' => 'test',
]);
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
*
* @return void
*/
public function testTOTPgeneration()
{
$user = \App\User::find(1);
$twofaccount = TwoFAccount::create([
'name' => 'testTOTP',
'secret' => 'otpauth://totp/test@test.com?secret=A4GRFHVVRBGY7UIW&issuer=test'
]);
$response = $this->actingAs($user, 'api')
->json('POST', '/api/twofaccounts/' . $twofaccount->id . '/totp')
->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
*
* @return void
*/
2019-05-25 23:51:20 +02:00
public function testTwoFAccountUpdate()
2019-05-22 00:49:27 +02:00
{
$user = \App\User::find(1);
$response = $this->actingAs($user, 'api')
2019-05-25 23:51:20 +02:00
->json('PUT', '/api/twofaccounts/1', [
'name' => 'testUpdate',
'secret' => 'testUpdate',
])
->assertStatus(200)
->assertJson([
'id' => 1,
'name' => 'testUpdate',
'secret' => 'testUpdate',
'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
*
* @return void
*/
2019-05-25 23:51:20 +02:00
public function testTwoFAccountIndexListing()
2019-05-20 07:37:41 +02:00
{
$user = \App\User::find(1);
$response = $this->actingAs($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',
'name',
'secret',
'icon',
'created_at',
'updated_at',
'deleted_at'
]
]
);
}
/**
* test TwoFAccount deletion via API
2019-05-20 07:37:41 +02:00
* @return [type] [description]
*/
public function testTwoFAccountDeletion()
2019-05-20 07:37:41 +02:00
{
$user = \App\User::find(1);
2019-05-25 23:51:20 +02:00
$twofaccount = TwoFAccount::create([
2019-05-23 14:02:29 +02:00
'name' => 'testDelete',
'secret' => 'test'
2019-05-20 07:37:41 +02:00
]);
$response = $this->actingAs($user, 'api')
2019-05-25 23:51:20 +02:00
->json('DELETE', '/api/twofaccounts/' . $twofaccount->id)
->assertStatus(204);
}
/**
* test TwoFAccount permanent deletion via API
* @return [type] [description]
*/
public function testTwoFAccountPermanentDeletion()
{
$user = \App\User::find(1);
$twofaccount = TwoFAccount::create([
'name' => 'testHardDelete',
'secret' => 'test'
]);
$twofaccount->delete();
$response = $this->actingAs($user, 'api')
->json('DELETE', '/api/twofaccounts/force/' . $twofaccount->id)
->assertStatus(204);
2019-05-20 07:37:41 +02:00
}
}