test folder refactoring

This commit is contained in:
Peter Papp
2021-07-18 14:53:00 +02:00
parent 5046071f3a
commit 18150cd920
20 changed files with 25 additions and 25 deletions

View File

@@ -0,0 +1,118 @@
<?php
namespace Tests\Feature\Accounts;
use App\Models\File;
use App\Models\Folder;
use App\Models\User;
use Tests\TestCase;
class PersonalAccessTokenTest extends TestCase
{
/**
* @test
*/
public function it_create_user_token()
{
$user = User::factory(User::class)
->create();
$this
->actingAs($user)
->postJson('/api/user/token/create', [
'name' => 'token',
])
->assertStatus(201);
$this->assertDatabaseHas('personal_access_tokens', [
'tokenable_id' => $user->id,
'name' => 'token',
]);
}
/**
* @test
*/
public function it_revoke_user_token()
{
$user = User::factory(User::class)
->create();
$user->createToken('token');
$token_id = $user->tokens()->first()->id;
$this
->actingAs($user)
->deleteJson("/api/user/token/revoke/$token_id")
->assertStatus(204);
$this->assertDatabaseMissing('personal_access_tokens', [
'id' => $token_id,
]);
}
/**
* @test
*/
public function it_get_user_tokens()
{
$user = User::factory(User::class)
->create();
$user->createToken('token');
$token = $user->tokens()->first();
$this
->actingAs($user)
->getJson('/api/user/tokens')
->assertStatus(200)
->assertJsonFragment([
'id' => $token->id,
'tokenable_type' => $token->tokenable_type,
'tokenable_id' => $user->id,
'name' => $token->name,
'abilities' => $token->abilities,
]);
}
/**
* @test
*/
public function it_use_user_token_in_public_api_request()
{
$user = User::factory(User::class)
->create();
$folder = Folder::factory(Folder::class)
->create([
'user_id' => $user->id,
]);
$file = File::factory(File::class)
->create([
'user_id' => $user->id,
'folder_id' => $folder->id,
]);
$token = $user->createToken('token')->plainTextToken;
$this->assertDatabaseHas('personal_access_tokens', [
'tokenable_id' => $user->id,
]);
$this->assertDatabaseHas('folders', [
'id' => $folder->id,
'user_id' => $user->id,
]);
$this
->withToken($token)
->getJson("/api/browse/folders/$folder->id")
->assertOk()
->assertJsonFragment([
'id' => $file->id,
]);
}
}

View File

@@ -0,0 +1,154 @@
<?php
namespace Tests\Feature\Accounts;
use App\Models\Setting;
use App\Models\User;
use App\Notifications\ResetPassword;
use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Support\Facades\Password;
use Laravel\Sanctum\Sanctum;
use Notification;
use Storage;
use Tests\TestCase;
class SignFlowTest extends TestCase
{
/**
* @test
*/
public function it_register_user()
{
collect([
[
'name' => 'storage_default',
'value' => 12,
],
[
'name' => 'registration',
'value' => 1,
],
[
'name' => 'user_verification',
'value' => 1,
]
])->each(function ($setting) {
Setting::create([
'name' => $setting['name'],
'value' => $setting['value'],
]);
});
$this->postJson('api/register', [
'email' => 'john@doe.com',
'password' => 'SecretPassword',
'password_confirmation' => 'SecretPassword',
'name' => 'John Doe',
])->assertStatus(201);
$this->assertDatabaseHas('users', [
'email' => 'john@doe.com',
'email_verified_at' => null,
]);
$this->assertDatabaseHas('user_settings', [
'name' => 'John Doe',
'storage_capacity' => 12,
]);
Storage::disk('local')
->assertExists('files/' . User::first()->id);
Notification::assertTimesSent(1, VerifyEmail::class);
}
/**
* @test
*/
public function it_check_if_user_exist_and_return_name_with_avatar()
{
$user = User::factory(User::class)
->create(['email' => 'john@doe.com']);
$this->postJson('/api/user/check', [
'email' => $user->email,
])->assertStatus(200);
}
/**
* @test
*/
public function it_check_non_existed_user_and_return_not_found()
{
$this->postJson('/api/user/check', [
'email' => 'jane@doe.com',
])->assertStatus(404);
}
/**
* @test
*/
public function it_login_user()
{
$user = User::factory(User::class)
->create(['email' => 'john@doe.com']);
$this->postJson('/login', [
'email' => $user->email,
'password' => 'secret',
])->assertStatus(200);
}
/**
* @test
*/
public function it_logout_user()
{
$user = User::factory(User::class)
->create();
Sanctum::actingAs($user);
$this->postJson('/logout')
->assertStatus(204);
}
/**
* @test
*/
public function it_send_reset_link_to_email()
{
$user = User::factory(User::class)
->create(['email' => 'john@doe.com']);
$this->postJson('/api/password/email', [
'email' => $user->email,
])->assertStatus(200);
Notification::assertTimesSent(1, ResetPassword::class);
}
/**
* @test
*/
public function it_reset_user_password()
{
$user = User::factory(User::class)
->create(['email' => 'john@doe.com']);
// Get password token
$token = Password::getRepository()
->create($user);
$this->postJson('/api/password/reset', [
'token' => $token,
'email' => $user->email,
'password' => 'VeryStrongPassword',
'password_confirmation' => 'VeryStrongPassword',
])->assertStatus(200);
$this->assertDatabaseMissing('password_resets', [
'email' => $user->email,
]);
}
}

View File

@@ -0,0 +1,212 @@
<?php
namespace Tests\Feature\Accounts;
use Storage;
use Notification;
use Tests\TestCase;
use App\Models\User;
use Laravel\Sanctum\Sanctum;
use App\Services\SetupService;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\URL;
use Illuminate\Auth\Notifications\VerifyEmail;
class UserAccountTest extends TestCase
{
public function __construct()
{
parent::__construct();
$this->setup = resolve(SetupService::class);
}
/**
* @test
*/
public function it_generate_and_store_user()
{
$user = User::factory(User::class)
->create(['role' => 'user']);
$this->assertDatabaseHas('users', [
'id' => $user->id,
'role' => 'user',
]);
$this->assertDatabaseHas('user_settings', [
'user_id' => $user->id,
]);
Storage::disk('local')
->assertExists('files/' . User::first()->id);
}
/**
* @test
*/
public function it_change_user_password_in_profile_settings()
{
$user = User::factory(User::class)
->create();
Sanctum::actingAs($user);
$this->postJson('/api/user/password', [
'current_password' => 'secret',
'password' => 'VerySecretPassword',
'password_confirmation' => 'VerySecretPassword',
])->assertStatus(204);
// TODO: login s novym heslom
}
/**
* @test
*/
public function it_update_user_settings()
{
$user = User::factory(User::class)
->create();
Sanctum::actingAs($user);
$this->patchJson('/api/user/relationships/settings', [
'name' => 'address',
'value' => 'Jantar',
])->assertStatus(204);
$this->assertDatabaseHas('user_settings', [
'address' => 'Jantar',
]);
}
/**
* @test
*/
public function it_update_user_avatar()
{
$this->setup->create_directories();
$user = User::factory(User::class)
->create();
Sanctum::actingAs($user);
$avatar = UploadedFile::fake()
->image('fake-image.jpg');
$this->patchJson('/api/user/relationships/settings', [
'avatar' => $avatar,
])->assertStatus(204);
Storage::disk('local')
->assertExists($user->settings->getRawOriginal('avatar'));
}
/**
* @test
*/
public function it_get_user_data()
{
$user = User::factory(User::class)
->create();
$this
->actingAs($user)
->getJson('/api/user')
->assertStatus(200)
->assertExactJson([
'data' => [
'id' => (string) $user->id,
'type' => 'user',
'attributes' => [
'storage_capacity' => '5',
'subscription' => false,
'incomplete_payment' => null,
'stripe_customer' => false,
'email' => $user->email,
'role' => $user->role,
'two_factor_authentication' => false,
'folders' => [],
'storage' => [
'used' => 0,
'used_formatted' => '0.00%',
'capacity' => '5',
'capacity_formatted' => '5GB',
],
'created_at_formatted' => format_date($user->created_at, '%d. %B. %Y'),
'created_at' => $user->created_at->toJson(),
'updated_at' => $user->updated_at->toJson(),
],
'relationships' => [
'settings' => [
'data' => [
'id' => (string) $user->id,
'type' => 'settings',
'attributes' => [
'avatar' => $user->settings->avatar,
'name' => $user->settings->name,
'address' => $user->settings->address,
'state' => $user->settings->state,
'city' => $user->settings->city,
'postal_code' => $user->settings->postal_code,
'country' => $user->settings->country,
'phone_number' => $user->settings->phone_number,
'timezone' => $user->settings->timezone,
],
],
],
'favourites' => [
'data' => [
'id' => (string) $user->id,
'type' => 'favourite_folders',
'attributes' => [
'folders' => [],
],
],
],
],
],
]);
}
/**
* @test
*/
public function it_verify_user_email()
{
$user = User::factory(User::class)
->create([
'email_verified_at' => null,
]);
$verificationUrl = URL::temporarySignedRoute(
'verification.verify',
now()->addMinutes(60),
['id' => $user->id, 'hash' => sha1($user->email)]
);
$this
->getJson($verificationUrl)
->assertRedirect('successfully-verified');
$this->assertNotNull(User::find($user->id)->get('email_verified_at'));
}
/**
* @test
*/
public function it_resend_user_verify_email()
{
$user = User::factory(User::class)
->create([
'email_verified_at' => null,
]);
$this->postJson('/api/user/email/resend/verify', [
'email' => $user->email,
])
->assertStatus(204);
Notification::assertTimesSent(1, VerifyEmail::class);
}
}