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,
'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),
];
});

View File

@ -11,10 +11,8 @@ class DatabaseSeeder extends Seeder
*/
public function run()
{
// $this->call(UsersTableSeeder::class);
$this->call([
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()
{
User::create([
'name' => 'testLogin',
'email' => 'test@test.com',
'password' => bcrypt('test'),
'name' => 'admin',
'email' => 'admin@example.org',
'password' => bcrypt('password'),
]);
}
}

View File

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

View File

@ -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]);
}
}

View File

@ -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);
}

View File

@ -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']);
}
}