Seeder replaced by factories

This commit is contained in:
Bubka 2019-06-06 13:40:06 +02:00
parent 4266674e5c
commit 82cc40fbd6
9 changed files with 98 additions and 106 deletions

View File

@ -0,0 +1,25 @@
<?php
/* @var $factory \Illuminate\Database\Eloquent\Factory */
use App\TwoFAccount;
use Faker\Generator as Faker;
/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| This directory should contain each of the model factory definitions for
| your application. Factories provide a convenient way to generate new
| model instances for testing / seeding your application's database.
|
*/
$factory->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',
];
});

View File

@ -21,7 +21,7 @@
'name' => $faker->name, 'name' => $faker->name,
'email' => $faker->unique()->safeEmail, 'email' => $faker->unique()->safeEmail,
'email_verified_at' => now(), 'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password 'password' => bcrypt('password'),
'remember_token' => Str::random(10), 'remember_token' => Str::random(10),
]; ];
}); });

View File

@ -11,10 +11,8 @@ class DatabaseSeeder extends Seeder
*/ */
public function run() public function run()
{ {
// $this->call(UsersTableSeeder::class);
$this->call([ $this->call([
UsersTableSeeder::class, UsersTableSeeder::class,
TwoFAccountsTableSeeder::class,
]); ]);
} }
} }

View File

@ -1,42 +0,0 @@
<?php
use App\TwoFAccount;
use Illuminate\Database\Seeder;
use Illuminate\Foundation\Testing\WithFaker;
class TwoFAccountsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$faker = \Faker\Factory::create();
TwoFAccount::create([
'name' => $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',
]);
}
}

View File

@ -13,9 +13,9 @@ class UsersTableSeeder extends Seeder
public function run() public function run()
{ {
User::create([ User::create([
'name' => 'testLogin', 'name' => 'admin',
'email' => 'test@test.com', 'email' => 'admin@example.org',
'password' => bcrypt('test'), 'password' => bcrypt('password'),
]); ]);
} }
} }

View File

@ -7,7 +7,8 @@
convertNoticesToExceptions="true" convertNoticesToExceptions="true"
convertWarningsToExceptions="true" convertWarningsToExceptions="true"
processIsolation="false" processIsolation="false"
stopOnFailure="false"> stopOnFailure="false"
beStrictAboutTestsThatDoNotTestAnything="false">
<testsuites> <testsuites>
<testsuite name="Unit"> <testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory> <directory suffix="Test.php">./tests/Unit</directory>

View File

@ -15,10 +15,10 @@ abstract class TestCase extends BaseTestCase
*/ */
use DatabaseTransactions; use DatabaseTransactions;
public function setUp(): void protected function setUp(): void
{ {
parent::setUp(); parent::setUp();
Artisan::call('migrate', ['--seed' => true]); Artisan::call('migrate');
Artisan::call('passport:install',['--verbose' => 2]); Artisan::call('passport:install',['--verbose' => 2]);
} }
} }

View File

@ -2,23 +2,35 @@
namespace Tests\Unit; namespace Tests\Unit;
use App\User;
use Tests\TestCase; use Tests\TestCase;
use App\TwoFAccount; use App\TwoFAccount;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Auth\Authenticatable;
class TwoFAccountTest extends TestCase 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 * test TwoFAccount creation via API
* *
* @return void * @test
*/ */
public function testTwoFAccountCreation() public function testTwoFAccountCreation()
{ {
$user = \App\User::find(1); $response = $this->actingAs($this->user, 'api')
$response = $this->actingAs($user, 'api')
->json('POST', '/api/twofaccounts', [ ->json('POST', '/api/twofaccounts', [
'name' => 'testCreation', 'name' => 'testCreation',
'uri' => 'test', 'uri' => 'test',
@ -34,18 +46,16 @@ public function testTwoFAccountCreation()
/** /**
* test TOTP generation via API * test TOTP generation via API
* *
* @return void * @test
*/ */
public function testTOTPgeneration() public function testTOTPgeneration()
{ {
$user = \App\User::find(1); $twofaccount = factory(TwoFAccount::class)->create([
$twofaccount = TwoFAccount::create([
'name' => 'testTOTP', 'name' => 'testTOTP',
'uri' => 'otpauth://totp/test@test.com?secret=A4GRFHVVRBGY7UIW&issuer=test' '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') ->json('GET', '/api/twofaccounts/' . $twofaccount->id . '/totp')
->assertStatus(200) ->assertStatus(200)
->assertJsonStructure([ ->assertJsonStructure([
@ -57,14 +67,14 @@ public function testTOTPgeneration()
/** /**
* test TwoFAccount update via API * test TwoFAccount update via API
* *
* @return void * @test
*/ */
public function testTwoFAccountUpdate() public function testTwoFAccountUpdate()
{ {
$user = \App\User::find(1); $twofaccount = factory(TwoFAccount::class)->create();
$response = $this->actingAs($user, 'api') $response = $this->actingAs($this->user, 'api')
->json('PUT', '/api/twofaccounts/1', [ ->json('PUT', '/api/twofaccounts/' . $twofaccount->id, [
'name' => 'testUpdate', 'name' => 'testUpdate',
'uri' => 'testUpdate', 'uri' => 'testUpdate',
]) ])
@ -81,13 +91,13 @@ public function testTwoFAccountUpdate()
/** /**
* test TwoFAccount index fetching via API * test TwoFAccount index fetching via API
* *
* @return void * @test
*/ */
public function testTwoFAccountIndexListing() 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') ->json('GET', '/api/twofaccounts')
->assertStatus(200) ->assertStatus(200)
->assertJsonStructure([ ->assertJsonStructure([
@ -107,18 +117,14 @@ public function testTwoFAccountIndexListing()
/** /**
* test TwoFAccount deletion via API * test TwoFAccount deletion via API
* @return [type] [description] *
* @test
*/ */
public function testTwoFAccountDeletion() public function testTwoFAccountDeletion()
{ {
$user = \App\User::find(1); $twofaccount = factory(TwoFAccount::class)->create();
$twofaccount = TwoFAccount::create([ $response = $this->actingAs($this->user, 'api')
'name' => 'testDelete',
'uri' => 'test'
]);
$response = $this->actingAs($user, 'api')
->json('DELETE', '/api/twofaccounts/' . $twofaccount->id) ->json('DELETE', '/api/twofaccounts/' . $twofaccount->id)
->assertStatus(204); ->assertStatus(204);
} }
@ -126,20 +132,15 @@ public function testTwoFAccountDeletion()
/** /**
* test TwoFAccount permanent deletion via API * test TwoFAccount permanent deletion via API
* @return [type] [description] *
* @test
*/ */
public function testTwoFAccountPermanentDeletion() public function testTwoFAccountPermanentDeletion()
{ {
$user = \App\User::find(1); $twofaccount = factory(TwoFAccount::class)->create();
$twofaccount = TwoFAccount::create([
'name' => 'testHardDelete',
'uri' => 'test'
]);
$twofaccount->delete(); $twofaccount->delete();
$response = $this->actingAs($user, 'api') $response = $this->actingAs($this->user, 'api')
->json('DELETE', '/api/twofaccounts/force/' . $twofaccount->id) ->json('DELETE', '/api/twofaccounts/force/' . $twofaccount->id)
->assertStatus(204); ->assertStatus(204);
} }

View File

@ -2,24 +2,38 @@
namespace Tests\Unit; namespace Tests\Unit;
use Tests\TestCase;
use App\User; use App\User;
use Illuminate\Foundation\Testing\WithFaker; use Tests\TestCase;
use Illuminate\Auth\Authenticatable; use Illuminate\Auth\Authenticatable;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
class UserTest extends TestCase 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 * test User creation via API
* *
* @return void * @test
*/ */
public function testUserCreation() public function testUserCreation()
{ {
$response = $this->json('POST', '/api/register', [ $response = $this->json('POST', '/api/register', [
'name' => 'testCreate', 'name' => 'testCreate',
'email' => str_random(10) . '@test.com', 'email' => 'testCreate@example.org',
'password' => 'test', 'password' => 'test',
]); ]);
@ -33,13 +47,13 @@ public function testUserCreation()
/** /**
* test User login via API * test User login via API
* *
* @return void * @test
*/ */
public function testUserLogin() public function testUserLogin()
{ {
$response = $this->json('POST', '/api/login', [ $response = $this->json('POST', '/api/login', [
'email' => 'test@test.com', 'email' => $this->user->email,
'password' => 'test' 'password' => 'password'
]); ]);
$response->assertStatus(200) $response->assertStatus(200)
@ -52,15 +66,16 @@ public function testUserLogin()
/** /**
* test User logout via API * test User logout via API
* *
* @return void * @test
*/ */
public function testUserLogout() public function testUserLogout()
{ {
$user = ['email' => 'test@test.com', $credentials = [
'password' => 'test' 'email' => $this->user->email,
'password' => 'password'
]; ];
Auth::attempt($user); Auth::attempt($credentials);
$token = Auth::user()->createToken('testToken')->accessToken; $token = Auth::user()->createToken('testToken')->accessToken;
$headers = ['Authorization' => "Bearer $token"]; $headers = ['Authorization' => "Bearer $token"];
@ -75,20 +90,14 @@ public function testUserLogout()
/** /**
* test User logout via API * test User logout via API
* *
* @return void * @test
*/ */
public function testGetUserDetails() public function testGetUserDetails()
{ {
$user = \App\User::find(1); $response = $this->actingAs($this->user, 'api')
$response = $this->actingAs($user, 'api')
->json('GET', '/api/user') ->json('GET', '/api/user')
->assertStatus(200) ->assertStatus(200)
->assertJsonFragment([ ->assertJsonStructure(['id', 'name', 'email']);
'id' => 1,
'name' => 'testLogin',
'email' => 'test@test.com',
]);
} }
} }