2FAuth/tests/Unit/APITest.php

141 lines
3.2 KiB
PHP
Raw Normal View History

2019-05-20 07:37:41 +02:00
<?php
namespace Tests\Unit;
use Tests\TestCase;
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
class APITest extends TestCase
{
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('make:migrate', ['--force' => true]);
Artisan::call('migrate', ['--seed' => true]);
Artisan::call('passport:install',['--verbose' => 2]);
}
2019-05-20 07:37:41 +02:00
/**
2019-05-22 00:49:27 +02:00
* test User creation via API
2019-05-20 07:37:41 +02:00
*
* @return void
*/
2019-05-22 00:49:27 +02:00
public function testUserCreation()
2019-05-20 07:37:41 +02:00
{
2019-05-22 00:49:27 +02:00
$response = $this->json('POST', '/api/register', [
2019-05-23 14:02:29 +02:00
'name' => 'testCreate',
'email' => str_random(10) . '@test.com',
'password' => 'test',
2019-05-20 07:37:41 +02:00
]);
$response->assertStatus(200)->assertJsonStructure([
'success' => ['token', 'name']
]);
}
2019-05-22 00:49:27 +02:00
2019-05-20 07:37:41 +02:00
/**
* test User login via API
*
* @return void
*/
public function testUserLogin()
{
$response = $this->json('POST', '/api/login', [
2019-05-23 14:02:29 +02:00
'email' => 'test@test.com',
'password' => 'test'
2019-05-20 07:37:41 +02:00
]);
$response->assertStatus(200)->assertJsonStructure([
'success' => ['token']
]);
}
2019-05-22 00:49:27 +02:00
/**
* test TwoFAccount creation via API
2019-05-22 00:49:27 +02:00
*
* @return void
*/
public function testTwoFAccountCreation()
2019-05-22 00:49:27 +02:00
{
//$this->withoutMiddleware();
$user = \App\User::find(1);
$response = $this->actingAs($user, 'api')
->json('POST', '/api/twofaccounts', [
2019-05-23 14:02:29 +02:00
'name' => 'testCreation',
'secret' => 'test',
2019-05-22 00:49:27 +02:00
]);
$response->assertStatus(200)->assertJson([
// 'data' => $response->data,
'status' => true,
'message' => 'Account Created'
]);
}
2019-05-20 07:37:41 +02:00
/**
* test TwoFAccount index fetching via API
2019-05-20 07:37:41 +02:00
*
* @return void
*/
public function testTwoFAccountFetch()
2019-05-20 07:37:41 +02:00
{
$user = \App\User::find(1);
$response = $this->actingAs($user, 'api')
->json('GET', '/api/twofaccounts')
2019-05-20 07:37:41 +02:00
->assertStatus(200)->assertJsonStructure([
'*' => [
'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);
$twoFAccount = \App\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')
->json('DELETE', "/api/twofaccounts/{$twoFAccount->id}")
2019-05-20 07:37:41 +02:00
->assertStatus(200)->assertJson([
'status' => true,
'message' => 'Account Deleted'
]);
}
}