Skip to content

Commit

Permalink
Tests for User uploads, Resolve #31
Browse files Browse the repository at this point in the history
  • Loading branch information
Jovert Lota Palonpon committed Apr 5, 2019
1 parent 1dc4465 commit 2877a1c
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

FILESYSTEM_DRIVER=local
FILESYSTEM_DRIVER=public

TELESCOPE_ENABLED=true

Expand Down
2 changes: 2 additions & 0 deletions .env.testing
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ SESSION_DRIVER=array
DB_CONNECTION=sqlite
DB_DATABASE=:memory:

FILESYSTEM_DRIVER=public

TELESCOPE_ENABLED=false

JWT_SECRET=tuHlDtSr7DO4JxxmjfuddNHOdgZLhgVC
Expand Down
12 changes: 8 additions & 4 deletions app/Http/Controllers/Api/V1/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ public function restore(Request $request, $id)
*/
public function storeAvatar(Request $request, User $user) : JsonResponse
{
if (! $user->upload($request->files->get('avatar'))) {
return response()->json('Unable to process the upload', 422);
if ($user->upload($request->files->get('avatar'))) {
return response()->json($user);
}

return response()->json('Uploaded successfully!');
return response()->json('Unable to process the upload', 422);
}

/**
Expand All @@ -177,7 +177,11 @@ public function storeAvatar(Request $request, User $user) : JsonResponse
*/
public function destroyAvatar(Request $request, User $user) : JsonResponse
{
return response()->json($user->destroyUpload());
if ($user->destroyUpload()) {
return response()->json($user);
}

return response()->json('Uploaded file not removed', 422);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,14 @@ public function getDirectory() : string
{
return 'users/'.$this->getKey();
}

/**
* Get the upload attributes
*
* @return array
*/
public function getUploadAttributes() : array
{
return $this->uploadAttributes;
}
}
76 changes: 76 additions & 0 deletions tests/Feature/Api/V1/UsersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
namespace Tests\Feature\Api\V1;

use App\User;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Illuminate\Foundation\Testing\TestResponse;

class UsersTest extends BaseTest
{
/** @test */
public function a_user_can_list_users()
{
// The payload that should be sent alongside the request.
$payload = array_merge($this->getDefaultPayload(), []);

$this->get(route('api.v1.users.index'), $payload)->assertStatus(200);
Expand All @@ -32,6 +36,7 @@ public function a_user_can_create_a_user()
'username' => $this->faker->userName,
];

// The payload that should be sent alongside the request.
$payload = array_merge($this->getDefaultPayload(), []);

// Assuming that the user is created through the test data,
Expand All @@ -51,6 +56,7 @@ public function a_user_can_create_a_user()
/** @test */
public function a_user_can_view_a_user()
{
// The payload that should be sent alongside the request.
$payload = array_merge($this->getDefaultPayload(), []);

// The user to be shown.
Expand All @@ -67,6 +73,7 @@ public function a_user_can_view_a_user()
/** @test */
public function a_user_can_update_a_user()
{
// The payload that should be sent alongside the request.
$payload = array_merge($this->getDefaultPayload(), []);

// The user to be updated.
Expand All @@ -85,6 +92,7 @@ public function a_user_can_update_a_user()
/** @test */
public function a_user_can_delete_a_user()
{
// The payload that should be sent alongside the request.
$payload = array_merge($this->getDefaultPayload(), []);

// The user to be deleted.
Expand Down Expand Up @@ -127,4 +135,72 @@ public function a_user_can_restore_a_user()
'total' => $incremented
]);
}

/** @test */
public function a_user_can_store_an_avatar()
{
// The user to upload the file for.
$user = User::first();

// Store a fake avatar.
$response = $this->storeAvatar($user);

$data = $response->decodeResponseJson();

// The original & thumbnail file should exist in the disk.
Storage::disk(config('filesystems.default'))
->assertExists("{$data['directory']}/{$data['filename']}")
->assertExists("{$data['directory']}/thumbnails/{$data['filename']}");
}

/** @test */
public function a_user_can_destroy_an_avatar()
{
// The user to upload the file for.
$user = User::first();

// Fake an upload so that we could destroy it.
$response = $this->storeAvatar($user);

// The payload that should be sent alongside the request.
$payload = array_merge($this->getDefaultPayload());

// Assuming that the user's avatar is removed,
// It must return a 200 response status and then,
// It must return a response with a user containing
// null upload attributes to indicate that it was completely destroyed.
$response = $this->delete(route('api.v1.users.avatar.destroy', $user), $payload)
->assertStatus(200)
->assertJsonFragment(
array_fill_keys($user->getUploadAttributes(), null)
);

// The original & thumbnail file should not exist in the disk.
Storage::disk(config('filesystems.default'))
->assertMissing("{$user->directory}/{$user->filename}")
->assertMissing("{$user->directory}/thumbnails/{$user->filename}");
}

/**
* Store a fake avatar.
*
* @param App\User $user
*
* @return Illuminate\Foundation\Testing\TestResponse
*/
protected function storeAvatar(User $user) : TestResponse
{
// The payload that should be sent alongside the request.
$payload = array_merge($this->getDefaultPayload(), [
'avatar' => UploadedFile::fake()->image('avatar.jpg')
]);

return $this->post(
route('api.v1.users.avatar.store', $user), $payload
)
->assertStatus(200)
->assertJsonMissing(
array_fill_keys($user->getUploadAttributes(), null)
);
}
}

0 comments on commit 2877a1c

Please sign in to comment.