Enhance unit tests coverage

This commit is contained in:
Bubka 2020-03-03 17:06:40 +01:00
parent c1d556060f
commit 284a9f75b2
4 changed files with 289 additions and 39 deletions

174
tests/Feature/AuthTest.php Normal file
View File

@ -0,0 +1,174 @@
<?php
namespace Tests\Feature;
use App\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Password;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Support\Facades\Notification;
use Tests\TestCase;
class PasswordResetTest extends TestCase
{
/** @var \App\User */
protected $user;
/**
* Testing submitting the email password request without
* email address.
*/
public function testSubmitEmailPasswordRequestWithoutEmail()
{
$response = $this->json('POST', '/api/password/email', [
'email' => ''
]);
$response->assertStatus(422)
->assertJsonValidationErrors(['email']);
}
/**
* Testing submitting the email password request with an invalid
* email address.
*/
public function testSubmitEmailPasswordRequestWithInvalidEmail()
{
$response = $this->json('POST', '/api/password/email', [
'email' => 'nametest.com'
]);
$response->assertStatus(422)
->assertJsonValidationErrors(['email']);
}
/**
* Testing submitting the email password request with an unknown
* email address.
*/
public function testSubmitEmailPasswordRequestWithUnknownEmail()
{
$response = $this->json('POST', '/api/password/email', [
'email' => 'name@test.com'
]);
$response->assertStatus(422)
->assertJsonValidationErrors(['email']);
}
/**
* Testing submitting the email password request with a valid email address.
*/
public function testSubmitEmailPasswordRequest()
{
Notification::fake();
$this->user = factory(User::class)->create([
'name' => 'user',
'email' => 'user@example.org',
'password' => bcrypt('password'),
'email_verified_at' => now(),
'remember_token' => \Illuminate\Support\Str::random(10),
]);
//$this->expectsNotification($this->user, ResetPassword::class);
$response = $this->json('POST', '/api/password/email', [
'email' => $this->user->email
]);
$response->assertStatus(200);
$token = \Illuminate\Support\Facades\DB::table('password_resets')->first();
$this->assertNotNull($token);
// Notification::assertSentTo($this->user, ResetPassword::class, function ($notification, $channels) use ($token) {
// return Hash::check($notification->token, $token->token) === true;
// });
}
/**
* Testing submitting the reset password without
* email address.
*/
public function testSubmitResetPasswordWithoutInput()
{
$response = $this->json('POST', '/api/password/reset', [
'email' => '',
'password' => '',
'password_confirmation' => '',
'token' => ''
]);
$response->assertStatus(422)
->assertJsonValidationErrors(['email', 'password', 'token']);
}
/**
* Testing submitting the reset password with
* invalid input.
*/
public function testSubmitResetPasswordWithInvalidInput()
{
$response = $this->json('POST', '/api/password/reset', [
'email' => 'qsdqsdqsd',
'password' => 'foofoofoo',
'password_confirmation' => 'barbarbar',
'token' => 'token'
]);
$response->assertStatus(422)
->assertJsonValidationErrors(['email', 'password']);
}
/**
* Testing submitting the reset password with
* invalid input.
*/
public function testSubmitResetPasswordWithTooShortPasswords()
{
$response = $this->json('POST', '/api/password/reset', [
'email' => 'foo@bar.com',
'password' => 'foo',
'password_confirmation' => 'foo',
'token' => 'token'
]);
$response->assertStatus(422)
->assertJsonValidationErrors(['password']);
}
/**
* Testing submitting the rest password.
*/
public function testSubmitResetPassword()
{
Notification::fake();
$this->user = factory(User::class)->create([
'name' => 'user',
'email' => 'user@example.org',
'password' => bcrypt('password'),
'email_verified_at' => now(),
'remember_token' => \Illuminate\Support\Str::random(10)
]);
$token = Password::broker()->createToken($this->user);
$response = $this->json('POST', '/api/password/reset', [
'email' => $this->user->email,
'password' => 'newpassword',
'password_confirmation' => 'newpassword',
'token' => $token
]);
$this->user->refresh();
$response->assertStatus(200);
$this->assertTrue(Hash::check('newpassword', $this->user->password));
}
}

File diff suppressed because one or more lines are too long

View File

@ -127,11 +127,11 @@ class TwoFAccountTest extends TestCase
/** /**
* test TOTP generation via API * test TOTP generation for a given existing account via API
* *
* @test * @test
*/ */
public function testTOTPgeneration() public function testTOTPgenerationWithProvidedAccountId()
{ {
$twofaccount = factory(TwoFAccount::class)->create([ $twofaccount = factory(TwoFAccount::class)->create([
'service' => 'testTOTP', 'service' => 'testTOTP',
@ -149,11 +149,29 @@ class TwoFAccountTest extends TestCase
/** /**
* test TwoFAccount update via API * test TOTP generation as preview via API
* *
* @test * @test
*/ */
public function testTwoFAccountUpdate() public function testTOTPgenerationPreview()
{
$uri = 'otpauth://totp/test@test.com?secret=A4GRFHVVRBGY7UIW&issuer=test';
$response = $this->actingAs($this->user, 'api')
->json('POST', '/api/twofaccounts/otp', ['data' => $uri])
->assertStatus(200)
->assertJsonStructure([
'otp',
]);
}
/**
* test TwoFAccount TOTP update via API
*
* @test
*/
public function testTwoFAccountTOTPUpdate()
{ {
$twofaccount = factory(TwoFAccount::class)->create(); $twofaccount = factory(TwoFAccount::class)->create();
@ -174,6 +192,37 @@ class TwoFAccountTest extends TestCase
} }
/**
* test TwoFAccount HOTP update via API
*
* @test
*/
public function testTwoFAccountHOTPUpdate()
{
$twofaccount = factory(TwoFAccount::class)->create([
'service' => 'test.com',
'account' => 'test',
'uri' => 'otpauth://hotp/service?counter=1&secret=A4GRFHVVRBGY7UIW'
]);
$response = $this->actingAs($this->user, 'api')
->json('PUT', '/api/twofaccounts/' . $twofaccount->id, [
'service' => 'testUpdate.com',
'account' => 'testUpdate',
'icon' => 'testUpdate.png',
'counter' => '5'
])
->assertStatus(200)
->assertJson([
'id' => 1,
'service' => 'testUpdate.com',
'account' => 'testUpdate',
'uri' => 'otpauth://hotp/service?counter=5&secret=A4GRFHVVRBGY7UIW',
'icon' => 'testUpdate.png',
]);
}
/** /**
* test TwoFAccount update via API * test TwoFAccount update via API
* *
@ -187,7 +236,8 @@ class TwoFAccountTest extends TestCase
$response = $this->actingAs($this->user, 'api') $response = $this->actingAs($this->user, 'api')
->json('PUT', '/api/twofaccounts/' . $id, [ ->json('PUT', '/api/twofaccounts/' . $id, [
'service' => 'testUpdate' 'service' => 'testUpdate',
'icon' => 'name.png'
]) ])
->assertStatus(404); ->assertStatus(404);
} }
@ -235,4 +285,24 @@ class TwoFAccountTest extends TestCase
->assertStatus(204); ->assertStatus(204);
} }
/**
* test TwoFAccounts batch deletion via API
*
* @test
*/
public function testTwoFAccountBatchDestroy()
{
$twofaccount = factory(TwoFAccount::class)->create();
$twofaccount = factory(TwoFAccount::class)->create();
$twofaccount = factory(TwoFAccount::class)->create();
$ids = \Illuminate\Support\Facades\DB::table('twofaccounts')->value('id');
$response = $this->actingAs($this->user, 'api')
->json('DELETE', '/api/twofaccounts/batch', [
'data' => $ids])
->assertStatus(204);
}
} }

View File

@ -6,6 +6,7 @@ use App\User;
use Tests\TestCase; use Tests\TestCase;
use Illuminate\Auth\Authenticatable; use Illuminate\Auth\Authenticatable;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
class UserTest extends TestCase class UserTest extends TestCase
{ {
@ -277,6 +278,10 @@ class UserTest extends TestCase
$response->assertStatus(200) $response->assertStatus(200)
->assertJsonStructure(['message']); ->assertJsonStructure(['message']);
$user->refresh();
$this->assertTrue(Hash::check('passwordUpdated', $user->password));
} }