Add & validate MySQL tests

This commit is contained in:
Bubka 2022-04-01 13:35:59 +02:00
parent fd1ea631f2
commit b0b648cf80
10 changed files with 106 additions and 37 deletions

View File

@ -1,10 +1,10 @@
APP_NAME=2FAuth
APP_NAME=TestApp
APP_ENV=testing
APP_KEY=base64:pdjaSqs7dDu8SdYCQzsayBAqeMC+85fmo3bpeBCCT94=
APP_DEBUG=true
APP_URL=http://localhost
WEBAUTHN_NAME=2FAuth
WEBAUTHN_NAME=TestApp
WEBAUTHN_ID=localhost
WEBAUTHN_USER_VERIFICATION=discouraged
@ -18,6 +18,15 @@ LOG_CHANNEL=stack
DB_CONNECTION=testing
DB_DATABASE=:memory:
# DB_CONNECTION=mysql
# DB_HOST=localhost
# DB_PORT=3306
# DB_DATABASE=2fauth_test
# DB_USERNAME=test_user
# DB_PASSWORD=password
BCRYPT_ROUNDS=4
BROADCAST_DRIVER=log
CACHE_DRIVER=array
QUEUE_CONNECTION=sync
@ -28,7 +37,7 @@ REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_DRIVER=array
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null

View File

@ -74,16 +74,16 @@ public function handle()
$this->line('Icons regenerated');
// Reset the db
DB::table('users')->truncate();
DB::table('password_resets')->truncate();
DB::table('oauth_access_tokens')->truncate();
DB::table('oauth_personal_access_clients')->truncate();
DB::table('oauth_refresh_tokens')->truncate();
DB::table('web_authn_credentials')->truncate();
DB::table('web_authn_recoveries')->truncate();
DB::table('twofaccounts')->truncate();
DB::table('options')->truncate();
DB::table('groups')->truncate();
DB::table('users')->delete();
DB::table('password_resets')->delete();
DB::table('oauth_access_tokens')->delete();
DB::table('oauth_personal_access_clients')->delete();
DB::table('oauth_refresh_tokens')->delete();
DB::table('web_authn_credentials')->delete();
DB::table('web_authn_recoveries')->delete();
DB::table('twofaccounts')->delete();
DB::table('options')->delete();
DB::table('groups')->delete();
// Seed the db
$this->callSilent('db:seed', [

View File

@ -66,8 +66,13 @@
"@php artisan key:generate --ansi"
],
"test" : [
"php artisan config:clear",
"vendor/bin/phpunit"
],
"test-mysql" : [
"php artisan config:clear",
"vendor/bin/phpunit -c phpunit-mysql.xml"
],
"test-coverage-html" : [
"@putenv XDEBUG_MODE=coverage",
"vendor/bin/phpunit --coverage-html tests/Coverage/"

41
phpunit-mysql.xml Normal file
View File

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
stopOnError="false"
beStrictAboutTestsThatDoNotTestAnything="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./app</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
<testsuite name="Api.v1">
<directory suffix="Test.php">./tests/Api/v1</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
<!-- following values override .env.testing vars -->
<env name="DB_CONNECTION" value="mysql"/>
<env name="DB_HOST" value="localhost"/>
<env name="DB_PORT" value="3306"/>
<env name="DB_DATABASE" value="2fauth_test"/>
<env name="DB_USERNAME" value="test_user"/>
<env name="DB_PASSWORD" value="password"/>
</php>
</phpunit>

View File

@ -9,6 +9,7 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
stopOnError="false"
beStrictAboutTestsThatDoNotTestAnything="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
@ -28,13 +29,7 @@
</testsuite>
</testsuites>
<php>
<server name="APP_ENV" value="testing"/>
<server name="DB_CONNECTION" value="sqlite"/>
<server name="DB_DATABASE" value=":memory:"/>
<server name="BCRYPT_ROUNDS" value="4"/>
<server name="CACHE_DRIVER" value="array"/>
<server name="MAIL_DRIVER" value="array"/>
<server name="QUEUE_CONNECTION" value="sync"/>
<server name="SESSION_DRIVER" value="array"/>
<env name="APP_ENV" value="testing"/>
<!-- Use .env.testing to customize other testing vars -->
</php>
</phpunit>

View File

@ -67,8 +67,7 @@ public function test_store_returns_created_group_resource()
'name' => 'My second group',
])
->assertCreated()
->assertExactJson([
'id' => 1,
->assertJsonFragment([
'name' => 'My second group',
'twofaccounts_count' => 0,
]);
@ -100,8 +99,7 @@ public function test_show_returns_group_resource()
$response = $this->actingAs($this->user, 'api-guard')
->json('GET', '/api/v1/groups/' . $group->id)
->assertOk()
->assertExactJson([
'id' => 1,
->assertJsonFragment([
'name' => 'My group',
'twofaccounts_count' => 0,
]);
@ -134,8 +132,7 @@ public function test_update_returns_updated_group_resource()
'name' => 'name updated',
])
->assertOk()
->assertExactJson([
'id' => 1,
->assertJsonFragment([
'name' => 'name updated',
'twofaccounts_count' => 0,
]);
@ -183,7 +180,7 @@ public function test_assign_accounts_returns_updated_group_resource()
$response = $this->actingAs($this->user, 'api-guard')
->json('POST', '/api/v1/groups/' . $group->id . '/assign', [
'ids' => [1,2],
'ids' => [$accounts[0]->id, $accounts[1]->id],
])
->assertOk()
->assertExactJson([
@ -203,7 +200,7 @@ public function test_assign_accounts_to_missing_group_returns_not_found()
$response = $this->actingAs($this->user, 'api-guard')
->json('POST', '/api/v1/groups/1000/assign', [
'ids' => [1,2],
'ids' => [$accounts[0]->id, $accounts[1]->id],
])
->assertNotFound()
->assertJsonStructure([
@ -238,7 +235,7 @@ public function test_get_assigned_accounts_returns_twofaccounts_collection()
$assign = $this->actingAs($this->user, 'api-guard')
->json('POST', '/api/v1/groups/' . $group->id . '/assign', [
'ids' => [1,2],
'ids' => [$accounts[0]->id, $accounts[1]->id],
]);
$response = $this->actingAs($this->user, 'api-guard')
@ -271,7 +268,7 @@ public function test_get_assigned_accounts_returns_twofaccounts_collection_with_
$assign = $this->actingAs($this->user, 'api-guard')
->json('POST', '/api/v1/groups/' . $group->id . '/assign', [
'ids' => [1,2],
'ids' => [$accounts[0]->id, $accounts[1]->id],
]);
$response = $this->actingAs($this->user, 'api-guard')

View File

@ -461,14 +461,14 @@ public function test_store_assigns_created_account_when_default_group_is_the_act
// Set the default group to be the active one
$settingService->set('defaultGroup', -1);
// Set the active group
$settingService->set('activeGroup', 1);
$settingService->set('activeGroup', $this->group->id);
$response = $this->actingAs($this->user, 'api-guard')
->json('POST', '/api/v1/twofaccounts', [
'uri' => self::TOTP_SHORT_URI,
])
->assertJsonFragment([
'group_id' => 1
'group_id' => $this->group->id
]);
}

View File

@ -32,8 +32,6 @@ public function test_reset_demo_succeeded()
->expectsOutput('Demo app refreshed')
->assertExitCode(0);
$user = User::find(1);
$this->assertDatabaseCount('twofaccounts', 9);
$this->assertDatabaseHas('twofaccounts', [

View File

@ -2,7 +2,9 @@
namespace Tests\Feature\Http\Auth;
use \App\Models\User;
use Tests\FeatureTestCase;
use Illuminate\Support\Facades\DB;
class RegisterControllerTest extends FeatureTestCase
{
@ -25,6 +27,8 @@ public function setUp(): void
*/
public function test_register_returns_success()
{
DB::table('users')->delete();
$response = $this->json('POST', '/user', [
'name' => self::USERNAME,
'email' => self::EMAIL,
@ -42,6 +46,24 @@ public function test_register_returns_success()
}
/**
* @test
*/
public function test_register_returns_already_an_existing_user()
{
DB::table('users')->delete();
$user = User::factory()->create();
$response = $this->json('POST', '/user', [
'name' => self::USERNAME,
'email' => self::EMAIL,
'password' => self::PASSWORD,
'password_confirmation' => self::PASSWORD,
])
->assertJsonValidationErrorFor('name');
}
/**
* @test
*/

View File

@ -24,6 +24,8 @@ class WebAuthnLoginControllerTest extends FeatureTestCase
public function setUp(): void
{
parent::setUp();
DB::table('users')->delete();
}
@ -41,7 +43,7 @@ public function test_user_login_returns_success()
DB::table('web_authn_credentials')->insert([
'id' => 'dGVzdF9jcmVkZW50aWFsX2lk',
'user_id' => 1,
'user_id' => $this->user->id,
'type' => 'public_key',
'transports' => json_encode([]),
'attestation_type' => 'none',
@ -98,7 +100,7 @@ public function test_user_login_without_userhandle_returns_success()
DB::table('web_authn_credentials')->insert([
'id' => 'dGVzdF9jcmVkZW50aWFsX2lk',
'user_id' => 1,
'user_id' => $this->user->id,
'type' => 'public_key',
'transports' => json_encode([]),
'attestation_type' => 'none',