mirror of
https://github.com/Bubka/2FAuth.git
synced 2024-11-22 00:03:09 +01:00
Better API responses without envelope
This commit is contained in:
parent
6cdaa12e21
commit
482c01ca2c
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -58,6 +58,9 @@
|
||||
],
|
||||
"post-create-project-cmd": [
|
||||
"@php artisan key:generate --ansi"
|
||||
],
|
||||
"test" : [
|
||||
"vendor/bin/phpunit"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
@ -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()
|
||||
{
|
||||
TwoFAccount::create([
|
||||
'name' => 'Firefly',
|
||||
'secret' => '3G2I2P36J57LS',
|
||||
]);
|
||||
TwoFAccount::create([
|
||||
'name' => 'Facebook',
|
||||
'secret' => '3GB2I2P365J5S',
|
||||
]);
|
||||
TwoFAccount::create([
|
||||
'name' => 'Twitter',
|
||||
'secret' => '3GB2I25J575LS',
|
||||
]);
|
||||
TwoFAccount::create([
|
||||
'name' => 'Google',
|
||||
'secret' => '3GB2I25J575LS',
|
||||
]);
|
||||
$faker = \Faker\Factory::create();
|
||||
|
||||
TwoFAccount::create([
|
||||
'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' => $faker->unique()->domainName,
|
||||
'secret' => $faker->password,
|
||||
]);
|
||||
TwoFAccount::create([
|
||||
'name' => $faker->unique()->domainName,
|
||||
'secret' => $faker->password,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
*
|
||||
@ -78,15 +42,38 @@ public function testTwoFAccountCreation()
|
||||
|
||||
$response = $this->actingAs($user, 'api')
|
||||
->json('POST', '/api/twofaccounts', [
|
||||
'name' => 'testCreation',
|
||||
'secret' => 'test',
|
||||
]);
|
||||
'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
67
tests/Unit/UserTest.php
Normal 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']
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user