diff --git a/database/factories/TwoFAccountFactory.php b/database/factories/TwoFAccountFactory.php new file mode 100644 index 00000000..36b58c72 --- /dev/null +++ b/database/factories/TwoFAccountFactory.php @@ -0,0 +1,25 @@ +define(TwoFAccount::class, function (Faker $faker) { + return [ + 'name' => $faker->unique()->domainName, + 'uri' => 'otpauth://totp/' . $faker->email . '?secret=' . $faker->regexify('[A-Z0-9]{16}') . '&issuer=test', + ]; +}); diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index 5e516cee..cb9a44fd 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -21,7 +21,7 @@ 'name' => $faker->name, 'email' => $faker->unique()->safeEmail, 'email_verified_at' => now(), - 'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password + 'password' => bcrypt('password'), 'remember_token' => Str::random(10), ]; }); diff --git a/database/seeds/DatabaseSeeder.php b/database/seeds/DatabaseSeeder.php index 5b103314..c7750117 100644 --- a/database/seeds/DatabaseSeeder.php +++ b/database/seeds/DatabaseSeeder.php @@ -11,10 +11,8 @@ class DatabaseSeeder extends Seeder */ public function run() { - // $this->call(UsersTableSeeder::class); $this->call([ UsersTableSeeder::class, - TwoFAccountsTableSeeder::class, ]); } } diff --git a/database/seeds/TwoFAccountsTableSeeder.php b/database/seeds/TwoFAccountsTableSeeder.php deleted file mode 100644 index e474f9b2..00000000 --- a/database/seeds/TwoFAccountsTableSeeder.php +++ /dev/null @@ -1,42 +0,0 @@ - $faker->unique()->domainName, - 'uri' => 'otpauth://totp/test@test.com?secret=A4GRFHVVRBGY7UIW&issuer=test', - ]); - - $deletedResource = TwoFAccount::create([ - 'name' => $faker->unique()->domainName, - 'uri' => 'otpauth://totp/test@test.com?secret=A4GRFHVVRBGY7UIW&issuer=test', - ]); - $deletedResource->delete(); - - TwoFAccount::create([ - 'name' => $faker->unique()->domainName, - 'uri' => 'otpauth://totp/test@test.com?secret=A4GRFHVVRBGY7UIW&issuer=test', - ]); - TwoFAccount::create([ - 'name' => $faker->unique()->domainName, - 'uri' => 'otpauth://totp/test@test.com?secret=A4GRFHVVRBGY7UIW&issuer=test', - ]); - TwoFAccount::create([ - 'name' => $faker->unique()->domainName, - 'uri' => 'otpauth://totp/test@test.com?secret=A4GRFHVVRBGY7UIW&issuer=test', - ]); - } -} diff --git a/database/seeds/UsersTableSeeder.php b/database/seeds/UsersTableSeeder.php index 5116d4b3..745eaffa 100644 --- a/database/seeds/UsersTableSeeder.php +++ b/database/seeds/UsersTableSeeder.php @@ -13,9 +13,9 @@ class UsersTableSeeder extends Seeder public function run() { User::create([ - 'name' => 'testLogin', - 'email' => 'test@test.com', - 'password' => bcrypt('test'), + 'name' => 'admin', + 'email' => 'admin@example.org', + 'password' => bcrypt('password'), ]); } } diff --git a/phpunit.xml b/phpunit.xml index c594cc14..17fdb383 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -7,7 +7,8 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" - stopOnFailure="false"> + stopOnFailure="false" + beStrictAboutTestsThatDoNotTestAnything="false"> ./tests/Unit diff --git a/tests/TestCase.php b/tests/TestCase.php index ee9cd130..f5b43798 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -15,10 +15,10 @@ abstract class TestCase extends BaseTestCase */ use DatabaseTransactions; - public function setUp(): void + protected function setUp(): void { parent::setUp(); - Artisan::call('migrate', ['--seed' => true]); + Artisan::call('migrate'); Artisan::call('passport:install',['--verbose' => 2]); } } diff --git a/tests/Unit/TwoFAccountTest.php b/tests/Unit/TwoFAccountTest.php index d86d6a64..292a73a7 100644 --- a/tests/Unit/TwoFAccountTest.php +++ b/tests/Unit/TwoFAccountTest.php @@ -2,23 +2,35 @@ namespace Tests\Unit; +use App\User; use Tests\TestCase; use App\TwoFAccount; -use Illuminate\Foundation\Testing\WithFaker; -use Illuminate\Auth\Authenticatable; class TwoFAccountTest extends TestCase { + /** @var \App\User */ + protected $user; + + + /** + * @test + */ + public function setUp(): void + { + parent::setUp(); + + $this->user = factory(User::class)->create(); + } + + /** * test TwoFAccount creation via API * - * @return void + * @test */ public function testTwoFAccountCreation() { - $user = \App\User::find(1); - - $response = $this->actingAs($user, 'api') + $response = $this->actingAs($this->user, 'api') ->json('POST', '/api/twofaccounts', [ 'name' => 'testCreation', 'uri' => 'test', @@ -34,18 +46,16 @@ public function testTwoFAccountCreation() /** * test TOTP generation via API * - * @return void + * @test */ public function testTOTPgeneration() { - $user = \App\User::find(1); - - $twofaccount = TwoFAccount::create([ + $twofaccount = factory(TwoFAccount::class)->create([ 'name' => 'testTOTP', 'uri' => 'otpauth://totp/test@test.com?secret=A4GRFHVVRBGY7UIW&issuer=test' ]); - $response = $this->actingAs($user, 'api') + $response = $this->actingAs($this->user, 'api') ->json('GET', '/api/twofaccounts/' . $twofaccount->id . '/totp') ->assertStatus(200) ->assertJsonStructure([ @@ -57,14 +67,14 @@ public function testTOTPgeneration() /** * test TwoFAccount update via API * - * @return void + * @test */ public function testTwoFAccountUpdate() { - $user = \App\User::find(1); + $twofaccount = factory(TwoFAccount::class)->create(); - $response = $this->actingAs($user, 'api') - ->json('PUT', '/api/twofaccounts/1', [ + $response = $this->actingAs($this->user, 'api') + ->json('PUT', '/api/twofaccounts/' . $twofaccount->id, [ 'name' => 'testUpdate', 'uri' => 'testUpdate', ]) @@ -81,13 +91,13 @@ public function testTwoFAccountUpdate() /** * test TwoFAccount index fetching via API * - * @return void + * @test */ public function testTwoFAccountIndexListing() { - $user = \App\User::find(1); + $twofaccount = factory(TwoFAccount::class, 3)->create(); - $response = $this->actingAs($user, 'api') + $response = $this->actingAs($this->user, 'api') ->json('GET', '/api/twofaccounts') ->assertStatus(200) ->assertJsonStructure([ @@ -107,18 +117,14 @@ public function testTwoFAccountIndexListing() /** * test TwoFAccount deletion via API - * @return [type] [description] + * + * @test */ public function testTwoFAccountDeletion() { - $user = \App\User::find(1); + $twofaccount = factory(TwoFAccount::class)->create(); - $twofaccount = TwoFAccount::create([ - 'name' => 'testDelete', - 'uri' => 'test' - ]); - - $response = $this->actingAs($user, 'api') + $response = $this->actingAs($this->user, 'api') ->json('DELETE', '/api/twofaccounts/' . $twofaccount->id) ->assertStatus(204); } @@ -126,20 +132,15 @@ public function testTwoFAccountDeletion() /** * test TwoFAccount permanent deletion via API - * @return [type] [description] + * + * @test */ public function testTwoFAccountPermanentDeletion() { - $user = \App\User::find(1); - - $twofaccount = TwoFAccount::create([ - 'name' => 'testHardDelete', - 'uri' => 'test' - ]); - + $twofaccount = factory(TwoFAccount::class)->create(); $twofaccount->delete(); - $response = $this->actingAs($user, 'api') + $response = $this->actingAs($this->user, 'api') ->json('DELETE', '/api/twofaccounts/force/' . $twofaccount->id) ->assertStatus(204); } diff --git a/tests/Unit/UserTest.php b/tests/Unit/UserTest.php index 6032f106..99fdf411 100644 --- a/tests/Unit/UserTest.php +++ b/tests/Unit/UserTest.php @@ -2,24 +2,38 @@ namespace Tests\Unit; -use Tests\TestCase; use App\User; -use Illuminate\Foundation\Testing\WithFaker; +use Tests\TestCase; use Illuminate\Auth\Authenticatable; use Illuminate\Support\Facades\Auth; class UserTest extends TestCase { + /** @var \App\User */ + protected $user; + + + /** + * @test + */ + public function setUp(): void + { + parent::setUp(); + + $this->user = factory(User::class)->create(); + } + + /** * test User creation via API * - * @return void + * @test */ public function testUserCreation() { $response = $this->json('POST', '/api/register', [ 'name' => 'testCreate', - 'email' => str_random(10) . '@test.com', + 'email' => 'testCreate@example.org', 'password' => 'test', ]); @@ -33,13 +47,13 @@ public function testUserCreation() /** * test User login via API * - * @return void + * @test */ public function testUserLogin() { $response = $this->json('POST', '/api/login', [ - 'email' => 'test@test.com', - 'password' => 'test' + 'email' => $this->user->email, + 'password' => 'password' ]); $response->assertStatus(200) @@ -52,15 +66,16 @@ public function testUserLogin() /** * test User logout via API * - * @return void + * @test */ public function testUserLogout() { - $user = ['email' => 'test@test.com', - 'password' => 'test' + $credentials = [ + 'email' => $this->user->email, + 'password' => 'password' ]; - Auth::attempt($user); + Auth::attempt($credentials); $token = Auth::user()->createToken('testToken')->accessToken; $headers = ['Authorization' => "Bearer $token"]; @@ -75,20 +90,14 @@ public function testUserLogout() /** * test User logout via API * - * @return void + * @test */ public function testGetUserDetails() { - $user = \App\User::find(1); - - $response = $this->actingAs($user, 'api') + $response = $this->actingAs($this->user, 'api') ->json('GET', '/api/user') ->assertStatus(200) - ->assertJsonFragment([ - 'id' => 1, - 'name' => 'testLogin', - 'email' => 'test@test.com', - ]); + ->assertJsonStructure(['id', 'name', 'email']); } }