added it_update_user_settings, it_update_user_avatar test

This commit is contained in:
Peter Papp
2021-03-03 08:42:07 +01:00
parent 7f21755f5a
commit cd9d1d91bd
8 changed files with 78 additions and 86 deletions

View File

@@ -49,56 +49,13 @@ class AccountController extends Controller
*
* @return InvoiceCollection
*/
public function invoices() {
public function invoices()
{
return new InvoiceCollection(
Auth::user()->invoices()
);
}
/**
* Update user profile
*
* @param Request $request
* @return ResponseFactory|\Illuminate\Http\Response
*/
public function update_profile(Request $request)
{
// Validate request
$validator = Validator::make($request->all(), [
'avatar' => 'file',
'name' => 'string',
'value' => 'string',
]);
// Return error
if ($validator->fails()) abort(400, 'Bad input');
// Get user
$user = Auth::user();
// Check if is demo
if (is_demo($user->id)) {
return Demo::response_204();
}
// Update data
if ($request->hasFile('avatar')) {
// Update avatar
$avatar = store_avatar($request->file('avatar'), 'avatars');
// Update data
$user->update(['avatar' => $avatar]);
} else {
// Update text data
$user->update(make_single_input($request));
}
return response('Saved!', 204);
}
/**
* Update user settings relationship
*
@@ -108,7 +65,9 @@ class AccountController extends Controller
public function update_user_settings(Request $request)
{
// Validate request
// TODO: pridat validator do requestu
$validator = Validator::make($request->all(), [
'avatar' => 'sometimes|file',
'name' => 'string',
'value' => 'string',
]);
@@ -124,7 +83,17 @@ class AccountController extends Controller
return Demo::response_204();
}
// Update text data
// Update avatar
if ($request->hasFile('avatar')) {
$user
->settings()
->update([
'avatar' => store_avatar($request->file('avatar'))
]);
return response('Saved!', 204);
}
$user
->settings()
->update(

View File

@@ -227,16 +227,12 @@ function is_editor($shared)
* Store user avatar to storage
*
* @param $image
* @param $path
* @return string
*/
function store_avatar($image, $path)
function store_avatar($image)
{
// Get directory
$path = check_directory($path);
// Store avatar
$image_path = Str::random(8) . '-' . $image->getClientOriginalName();
$image_path = Str::random(16) . '-' . $image->getClientOriginalName();
// Create intervention image
$img = Image::make($image->getRealPath());
@@ -245,32 +241,28 @@ function store_avatar($image, $path)
$img->fit('150', '150')->stream();
// Store thumbnail to disk
Storage::put($path . '/' . $image_path, $img);
Storage::put("avatars/$image_path", $img);
// Return path to image
return $path . '/' . $image_path;
return "avatars/$image_path";
}
/**
* Store system image
*
* @param $image
* @param $path
* @return string
*/
function store_system_image($image, $path)
function store_system_image($image)
{
// Get directory
$path = check_directory($path);
// Store avatar
$image_path = Str::random(8) . '-' . str_replace(' ', '', $image->getClientOriginalName());
// Store image to disk
Storage::putFileAs($path, $image, $image_path);
Storage::putFileAs('system', $image, $image_path);
// Return path to image
return $path . '/' . $image_path;
return `system/$image_path`;
}
/**

View File

@@ -27,7 +27,6 @@ class UserFactory extends Factory
'role' => $this->faker->randomElement(
['user', 'admin']
),
'name' => $this->faker->name(),
'email' => $this->faker->unique()->safeEmail,
'email_verified_at' => now(),
'password' => Hash::make('secret'),

View File

@@ -16,8 +16,6 @@ class CreateUsersTable extends Migration
Schema::create('users', function (Blueprint $table) {
$table->uuid('id');
$table->enum('role', ['admin', 'user'])->default('user');
$table->string('name');
$table->string('avatar')->nullable();
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');

View File

@@ -16,6 +16,7 @@ class CreateUserSettingsTable extends Migration
Schema::create('user_settings', function (Blueprint $table) {
$table->uuid('user_id');
$table->integer('storage_capacity')->default(5);
$table->string('avatar')->nullable();
$table->text('name')->nullable();
$table->text('address')->nullable();
$table->text('state')->nullable();

View File

@@ -14,7 +14,6 @@ Route::group(['middleware' => ['auth:sanctum']], function () {
// Account
Route::patch('/relationships/settings', [AccountController::class, 'update_user_settings']);
Route::post('/password', [AccountController::class, 'change_password']);
Route::patch('/profile', [AccountController::class, 'update_profile']);
Route::get('/subscription', [SubscriptionController::class, 'show']);
Route::get('/invoices', [AccountController::class, 'invoices']);
Route::get('/storage', [AccountController::class, 'storage']);

View File

@@ -8,7 +8,7 @@ use Laravel\Sanctum\Sanctum;
use Storage;
use Tests\TestCase;
class UserTest extends TestCase
class AuthTest extends TestCase
{
use DatabaseMigrations;
@@ -70,24 +70,4 @@ class UserTest extends TestCase
'password' => 'secret',
])->assertStatus(200);
}
/**
* @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',
]);
}
}

View File

@@ -3,16 +3,25 @@
namespace Tests\Feature;
use App\Models\User;
use App\Services\SetupService;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Http\UploadedFile;
use Laravel\Sanctum\Sanctum;
use Storage;
use Tests\TestCase;
class UserAccountTest extends TestCase
{
use DatabaseMigrations;
public function __construct()
{
parent::__construct();
$this->setup = app()->make(SetupService::class);
}
/**
* @test
*/
@@ -31,4 +40,49 @@ class UserAccountTest extends TestCase
// 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()
{
Storage::fake('local');
$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->avatar);
}
}