Better API responses without envelope

This commit is contained in:
Bubka 2019-05-25 23:51:20 +02:00
parent 6cdaa12e21
commit 482c01ca2c
5 changed files with 164 additions and 96 deletions

View File

@ -31,13 +31,7 @@ public function store(Request $request)
'secret' => $request->secret
]);
$data = [
'data' => $twofaccount,
'status' => (bool) $twofaccount,
'message' => $twofaccount ? 'Account Created' : 'Error Creating Account',
];
return response()->json($data);
return response()->json($twofaccount, 201);
}
@ -49,7 +43,7 @@ public function store(Request $request)
*/
public function show(TwoFAccount $twofaccount)
{
return response()->json($twofaccount);
return response()->json($twofaccount, 200);
}
@ -62,14 +56,9 @@ public function show(TwoFAccount $twofaccount)
*/
public function update(Request $request, TwoFAccount $twofaccount)
{
$status = $twofaccount->update($request->all());
$twofaccount->update($request->all());
return response()->json([
'status' => $status,
'message' => $status ? 'Account Updated' : 'Error Updating Account'
]);
//return response()->json($request, 200);
return response()->json($twofaccount, 200);
}
@ -81,12 +70,9 @@ public function update(Request $request, TwoFAccount $twofaccount)
*/
public function destroy(TwoFAccount $twofaccount)
{
$status = $twofaccount->delete();
$twofaccount->delete();
return response()->json([
'status' => $status,
'message' => $status ? 'Account Deleted' : 'Error Deleting Account'
]);
return response()->json(null, 204);
}
@ -99,13 +85,9 @@ public function destroy(TwoFAccount $twofaccount)
public function forceDestroy($id)
{
$twofaccount = TwoFAccount::onlyTrashed()->findOrFail($id);
$twofaccount->forceDelete();
$status = $twofaccount->forceDelete();
return response()->json([
'status' => $status,
'message' => $status ? 'Account Deleted permanently' : 'Error Deleting Account'
]);
return response()->json(null, 204);
}
}

View File

@ -58,6 +58,9 @@
],
"post-create-project-cmd": [
"@php artisan key:generate --ansi"
],
"test" : [
"vendor/bin/phpunit"
]
}
}

View File

@ -2,6 +2,7 @@
use App\TwoFAccount;
use Illuminate\Database\Seeder;
use Illuminate\Foundation\Testing\WithFaker;
class TwoFAccountsTableSeeder extends Seeder
{
@ -12,21 +13,30 @@ class TwoFAccountsTableSeeder extends Seeder
*/
public function run()
{
$faker = \Faker\Factory::create();
TwoFAccount::create([
'name' => 'Firefly',
'secret' => '3G2I2P36J57LS',
'name' => $faker->unique()->domainName,
'secret' => $faker->password,
]);
$deletedResource = TwoFAccount::create([
'name' => $faker->unique()->domainName,
'secret' => $faker->password,
]);
$deletedResource->delete();
TwoFAccount::create([
'name' => $faker->unique()->domainName,
'secret' => $faker->password,
]);
TwoFAccount::create([
'name' => 'Facebook',
'secret' => '3GB2I2P365J5S',
'name' => $faker->unique()->domainName,
'secret' => $faker->password,
]);
TwoFAccount::create([
'name' => 'Twitter',
'secret' => '3GB2I25J575LS',
]);
TwoFAccount::create([
'name' => 'Google',
'secret' => '3GB2I25J575LS',
'name' => $faker->unique()->domainName,
'secret' => $faker->password,
]);
}
}

View File

@ -3,12 +3,13 @@
namespace Tests\Unit;
use Tests\TestCase;
use App\TwoFAccount;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Auth\Authenticatable;
class APITest extends TestCase
class TwoFAccountTest extends TestCase
{
/**
@ -23,48 +24,11 @@ class APITest extends TestCase
public function setUp(): void
{
parent::setUp();
// Artisan::call('make:migrate', ['--force' => true]);
Artisan::call('migrate', ['--seed' => true]);
Artisan::call('passport:install',['--verbose' => 2]);
}
/**
* test User creation via API
*
* @return void
*/
public function testUserCreation()
{
$response = $this->json('POST', '/api/register', [
'name' => 'testCreate',
'email' => str_random(10) . '@test.com',
'password' => 'test',
]);
$response->assertStatus(200)->assertJsonStructure([
'success' => ['token', 'name']
]);
}
/**
* test User login via API
*
* @return void
*/
public function testUserLogin()
{
$response = $this->json('POST', '/api/login', [
'email' => 'test@test.com',
'password' => 'test'
]);
$response->assertStatus(200)->assertJsonStructure([
'success' => ['token']
]);
}
/**
* test TwoFAccount creation via API
*
@ -80,12 +44,35 @@ public function testTwoFAccountCreation()
->json('POST', '/api/twofaccounts', [
'name' => 'testCreation',
'secret' => 'test',
])
->assertStatus(201)
->assertJson([
'name' => 'testCreation',
'secret' => 'test',
]);
}
$response->assertStatus(200)->assertJson([
// 'data' => $response->data,
'status' => true,
'message' => 'Account Created'
/**
* test TwoFAccount update via API
*
* @return void
*/
public function testTwoFAccountUpdate()
{
$user = \App\User::find(1);
$response = $this->actingAs($user, 'api')
->json('PUT', '/api/twofaccounts/1', [
'name' => 'testUpdate',
'secret' => 'testUpdate',
])
->assertStatus(200)
->assertJson([
'id' => 1,
'name' => 'testUpdate',
'secret' => 'testUpdate',
'icon' => null,
]);
}
@ -95,13 +82,14 @@ public function testTwoFAccountCreation()
*
* @return void
*/
public function testTwoFAccountFetch()
public function testTwoFAccountIndexListing()
{
$user = \App\User::find(1);
$response = $this->actingAs($user, 'api')
->json('GET', '/api/twofaccounts')
->assertStatus(200)->assertJsonStructure([
->assertStatus(200)
->assertJsonStructure([
'*' => [
'id',
'name',
@ -124,17 +112,35 @@ public function testTwoFAccountDeletion()
{
$user = \App\User::find(1);
$twoFAccount = \App\TwoFAccount::create([
$twofaccount = TwoFAccount::create([
'name' => 'testDelete',
'secret' => 'test'
]);
$response = $this->actingAs($user, 'api')
->json('DELETE', "/api/twofaccounts/{$twoFAccount->id}")
->assertStatus(200)->assertJson([
'status' => true,
'message' => 'Account Deleted'
->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);
}
}

67
tests/Unit/UserTest.php Normal file
View File

@ -0,0 +1,67 @@
<?php
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Auth\Authenticatable;
class UserTest extends TestCase
{
/**
* 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]);
}
/**
* test User creation via API
*
* @return void
*/
public function testUserCreation()
{
$response = $this->json('POST', '/api/register', [
'name' => 'testCreate',
'email' => str_random(10) . '@test.com',
'password' => 'test',
]);
$response->assertStatus(200)->assertJsonStructure([
'success' => ['token', 'name']
]);
}
/**
* test User login via API
*
* @return void
*/
public function testUserLogin()
{
$response = $this->json('POST', '/api/login', [
'email' => 'test@test.com',
'password' => 'test'
]);
$response->assertStatus(200)->assertJsonStructure([
'success' => ['token']
]);
}
}