2FAuth/tests/Unit/TwoFAccountTest.php

239 lines
6.2 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;
2020-01-10 13:43:36 +01:00
use Illuminate\Support\Facades\Storage;
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();
}
2020-01-10 13:43:36 +01:00
/**
* test TwoFAccount display via API
*
* @test
*/
public function testTwoFAccountDisplay()
{
Storage::put('test.png', 'emptied to prevent missing resource replaced by null by the model getter');
$twofaccount = factory(TwoFAccount::class)->create([
'service' => 'testTOTP',
'account' => 'test@test.com',
'uri' => 'otpauth://totp/test@test.com?secret=A4GRFHVVRBGY7UIW&issuer=test',
'icon' => 'test.png',
]);
$response = $this->actingAs($this->user, 'api')
->json('GET', '/api/twofaccounts/' . $twofaccount->id)
->assertStatus(200)
->assertJson([
'service' => 'testTOTP',
'account' => 'test@test.com',
'uri' => 'otpauth://totp/test@test.com?secret=A4GRFHVVRBGY7UIW&issuer=test',
'icon' => 'test.png',
]);
}
/**
* test missing TwoFAccount display via API
*
* @test
*/
public function testMissingTwoFAccountDisplay()
{
$response = $this->actingAs($this->user, 'api')
->json('GET', '/api/twofaccounts/1000')
->assertStatus(404);
}
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',
2020-01-10 13:43:36 +01:00
'uri' => 'otpauth://totp/test@test.com?secret=A4GRFHZVRBGY7UIW&issuer=test',
'icon' => 'test.png',
2019-05-25 23:51:20 +02:00
])
->assertStatus(201)
->assertJson([
'service' => 'testCreation',
'account' => 'test@example.org',
2020-01-10 13:43:36 +01:00
'uri' => 'otpauth://totp/test@test.com?secret=A4GRFHZVRBGY7UIW&issuer=test',
'icon' => 'test.png',
2019-05-25 23:51:20 +02:00
]);
2019-05-20 07:37:41 +02:00
}
2019-05-25 23:51:20 +02:00
2020-01-10 13:43:36 +01:00
/**
* test TwoFAccount creation when fiels are empty via API
*
* @test
*/
public function testTwoFAccountCreationWithEmptyRequest()
{
$response = $this->actingAs($this->user, 'api')
->json('POST', '/api/twofaccounts', [
'service' => '',
'account' => '',
'uri' => '',
'icon' => '',
])
2020-01-20 14:23:31 +01:00
->assertStatus(422);
2020-01-10 13:43:36 +01:00
}
/**
* test TwoFAccount creation with an invalid TOTP uri via API
*
* @test
*/
public function testTwoFAccountCreationWithInvalidTOTP()
{
$response = $this->actingAs($this->user, 'api')
->json('POST', '/api/twofaccounts', [
'service' => 'testCreation',
'account' => 'test@example.org',
'uri' => 'invalidTOTP',
'icon' => 'test.png',
])
2020-01-20 14:23:31 +01:00
->assertStatus(422);
2020-01-10 13:43:36 +01: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')
2020-01-27 21:44:29 +01:00
->json('GET', '/api/twofaccounts/' . $twofaccount->id . '/otp')
2019-05-26 16:42:09 +02:00
->assertStatus(200)
->assertJsonStructure([
2020-01-27 21:44:29 +01:00
'otp',
2019-05-26 16:42:09 +02:00
]);
}
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',
2020-01-10 13:43:36 +01:00
'icon' => 'testUpdate.png',
2019-05-25 23:51:20 +02:00
])
->assertStatus(200)
->assertJson([
'id' => 1,
'service' => 'testUpdate',
'account' => 'testUpdate@test.com',
2020-01-27 21:44:29 +01:00
'uri' => $twofaccount->uri,
2020-01-10 13:43:36 +01:00
'icon' => 'testUpdate.png',
2019-05-25 23:51:20 +02:00
]);
2019-05-22 00:49:27 +02:00
}
2020-01-10 13:43:36 +01:00
/**
* test TwoFAccount update via API
*
* @test
*/
public function testTwoFAccountUpdateOfMissingTwoFAccount()
{
$twofaccount = factory(TwoFAccount::class)->create();
$id = $twofaccount->id;
$twofaccount->delete();
$response = $this->actingAs($this->user, 'api')
->json('PUT', '/api/twofaccounts/' . $id, [
'service' => 'testUpdate'
])
->assertStatus(404);
}
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)
2020-01-10 13:43:36 +01:00
->assertJsonCount(3, $key = null)
2019-05-25 23:51:20 +02:00
->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'
2019-05-20 07:37:41 +02:00
]
]
);
}
/**
* 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
}