Fix and complete unit tests

This commit is contained in:
Bubka 2020-01-10 13:43:36 +01:00
parent e5f040e066
commit 35a6b0d87c
5 changed files with 190 additions and 10 deletions

View File

@ -40,7 +40,7 @@ public function login(Request $request)
$success['token'] = Auth::user()->createToken('MyApp')->accessToken; $success['token'] = Auth::user()->createToken('MyApp')->accessToken;
$success['name'] = Auth::user()->name; $success['name'] = Auth::user()->name;
return response()->json(['success' => $success]); return response()->json(['success' => $success], 200);
} }
return response()->json(['error' => 'Unauthorised'], 401); return response()->json(['error' => 'Unauthorised'], 401);
@ -75,7 +75,7 @@ public function register(Request $request)
]); ]);
if ($validator->fails()) { if ($validator->fails()) {
return response()->json(['error' => $validator->errors()], 401); return response()->json(['error' => $validator->errors()], 400);
} }
$input = $request->all(); $input = $request->all();

View File

@ -7,8 +7,12 @@
class TwoFAccount extends Model class TwoFAccount extends Model
{ {
/**
protected $fillable = ['service', 'account', 'uri', 'icon']; * model's array form.
*
* @var array
*/
protected $fillable = ['service', 'account', 'uri', 'icon'];
/** /**
@ -27,10 +31,11 @@ class TwoFAccount extends Model
*/ */
public function getIconAttribute($value) public function getIconAttribute($value)
{ {
if (\App::environment('testing') == false) {
if( !Storage::exists('public/icons/' . pathinfo($value)['basename']) ) {
if( !Storage::exists('public/icons/' . pathinfo($value)['basename']) ) { return '';
}
return '';
} }
return $value; return $value;

View File

@ -22,5 +22,6 @@
'service' => $faker->unique()->domainName, 'service' => $faker->unique()->domainName,
'account' => $faker->safeEmail, 'account' => $faker->safeEmail,
'uri' => 'otpauth://totp/' . $faker->email . '?secret=' . $faker->regexify('[A-Z0-9]{16}') . '&issuer=test', 'uri' => 'otpauth://totp/' . $faker->email . '?secret=' . $faker->regexify('[A-Z0-9]{16}') . '&issuer=test',
'icon' => '',
]; ];
}); });

View File

@ -5,6 +5,7 @@
use App\User; use App\User;
use Tests\TestCase; use Tests\TestCase;
use App\TwoFAccount; use App\TwoFAccount;
use Illuminate\Support\Facades\Storage;
class TwoFAccountTest extends TestCase class TwoFAccountTest extends TestCase
{ {
@ -23,6 +24,48 @@ public function setUp(): void
} }
/**
* 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);
}
/** /**
* test TwoFAccount creation via API * test TwoFAccount creation via API
* *
@ -34,17 +77,55 @@ public function testTwoFAccountCreation()
->json('POST', '/api/twofaccounts', [ ->json('POST', '/api/twofaccounts', [
'service' => 'testCreation', 'service' => 'testCreation',
'account' => 'test@example.org', 'account' => 'test@example.org',
'uri' => 'test', 'uri' => 'otpauth://totp/test@test.com?secret=A4GRFHZVRBGY7UIW&issuer=test',
'icon' => 'test.png',
]) ])
->assertStatus(201) ->assertStatus(201)
->assertJson([ ->assertJson([
'service' => 'testCreation', 'service' => 'testCreation',
'account' => 'test@example.org', 'account' => 'test@example.org',
'uri' => 'test', 'uri' => 'otpauth://totp/test@test.com?secret=A4GRFHZVRBGY7UIW&issuer=test',
'icon' => 'test.png',
]); ]);
} }
/**
* 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' => '',
])
->assertStatus(400);
}
/**
* 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',
])
->assertStatus(400);
}
/** /**
* test TOTP generation via API * test TOTP generation via API
* *
@ -81,6 +162,7 @@ public function testTwoFAccountUpdate()
'service' => 'testUpdate', 'service' => 'testUpdate',
'account' => 'testUpdate@test.com', 'account' => 'testUpdate@test.com',
'uri' => 'testUpdate', 'uri' => 'testUpdate',
'icon' => 'testUpdate.png',
]) ])
->assertStatus(200) ->assertStatus(200)
->assertJson([ ->assertJson([
@ -88,11 +170,30 @@ public function testTwoFAccountUpdate()
'service' => 'testUpdate', 'service' => 'testUpdate',
'account' => 'testUpdate@test.com', 'account' => 'testUpdate@test.com',
'uri' => 'testUpdate', 'uri' => 'testUpdate',
'icon' => null, 'icon' => 'testUpdate.png',
]); ]);
} }
/**
* 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);
}
/** /**
* test TwoFAccount index fetching via API * test TwoFAccount index fetching via API
* *
@ -105,6 +206,7 @@ public function testTwoFAccountIndexListing()
$response = $this->actingAs($this->user, 'api') $response = $this->actingAs($this->user, 'api')
->json('GET', '/api/twofaccounts') ->json('GET', '/api/twofaccounts')
->assertStatus(200) ->assertStatus(200)
->assertJsonCount(3, $key = null)
->assertJsonStructure([ ->assertJsonStructure([
'*' => [ '*' => [
'id', 'id',

View File

@ -35,6 +35,7 @@ public function testUserCreation()
'name' => 'testCreate', 'name' => 'testCreate',
'email' => 'testCreate@example.org', 'email' => 'testCreate@example.org',
'password' => 'test', 'password' => 'test',
'password_confirmation' => 'test',
]); ]);
$response->assertStatus(200) $response->assertStatus(200)
@ -44,6 +45,42 @@ public function testUserCreation()
} }
/**
* test User creation with missing values via API
*
* @test
*/
public function testUserCreationWithMissingValues()
{
$response = $this->json('POST', '/api/register', [
'name' => '',
'email' => '',
'password' => '',
'password_confirmation' => '',
]);
$response->assertStatus(400);
}
/**
* test User creation with invalid values via API
*
* @test
*/
public function testUserCreationWithInvalidData()
{
$response = $this->json('POST', '/api/register', [
'name' => 'testInvalid',
'email' => 'email',
'password' => 'test',
'password_confirmation' => 'tset',
]);
$response->assertStatus(400);
}
/** /**
* test User login via API * test User login via API
* *
@ -63,6 +100,41 @@ public function testUserLogin()
} }
/**
* test User login with missing values via API
*
* @test
*/
public function testUserLoginWithMissingValues()
{
$response = $this->json('POST', '/api/login', [
'email' => '',
'password' => ''
]);
$response->assertStatus(400);
}
/**
* test User login with invalid credentials via API
*
* @test
*/
public function testUserLoginWithInvalidCredential()
{
$response = $this->json('POST', '/api/login', [
'email' => $this->user->email,
'password' => 'badPassword'
]);
$response->assertStatus(401)
->assertJson([
'error' => 'Unauthorised'
]);
}
/** /**
* test User logout via API * test User logout via API
* *