2FAuth/tests/Unit/APITest.php

141 lines
3.1 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 Account creation via API
*
* @return void
*/
public function testAccountCreation()
{
//$this->withoutMiddleware();
$user = \App\User::find(1);
$response = $this->actingAs($user, 'api')
->json('POST', '/api/account', [
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 Account index fetching via API
*
* @return void
*/
public function testAccountFetch()
{
$user = \App\User::find(1);
$response = $this->actingAs($user, 'api')
->json('GET', '/api/account')
->assertStatus(200)->assertJsonStructure([
'*' => [
'id',
'name',
'secret',
'icon',
'created_at',
'updated_at',
'deleted_at'
]
]
);
}
/**
* test Account deletion via API
* @return [type] [description]
*/
public function testAccountDeletion()
{
$user = \App\User::find(1);
2019-05-22 00:49:27 +02:00
$account = \App\Account::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-22 00:49:27 +02:00
->json('DELETE', "/api/account/{$account->id}")
2019-05-20 07:37:41 +02:00
->assertStatus(200)->assertJson([
'status' => true,
'message' => 'Account Deleted'
]);
}
}