Skip to content

Commit

Permalink
chore: improving test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
fokosun committed Aug 23, 2023
1 parent 0700c55 commit f5a1894
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 10 deletions.
29 changes: 22 additions & 7 deletions app/Http/Controllers/RecipeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@

use App\Exceptions\CookbookModelNotFoundException;
use App\Http\Requests\RecipeStoreRequest;
use App\Models\Recipe;
use App\Services\RecipeService;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Log;
use PHPOpenSourceSaver\JWTAuth\Facades\JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\JWT;

Expand Down Expand Up @@ -157,14 +159,27 @@ public function destroy(Request $request, $recipeId, JWT $jwtAuth)
], Response::HTTP_UNAUTHORIZED);
}

public function report(Request $request, JWT $jwtAuth)
public function report(Request $request)
{
if ($jwtAuth->parseToken()->check()) {
return response()->json(['message' => 'feedback submitted.']);
}
if (JWTAuth::parseToken()->user()) {
$recipe = Recipe::find($request->get('recipe_id'));

return response()->json([
'error' => 'You are not authorized to perform this action.'
], Response::HTTP_UNAUTHORIZED);
if ($recipe instanceof Recipe) {
$recipe->update(['is_reported' => 1]);
return response()->json(['message' => 'feedback submitted.']);
}

Log::debug(
'Error reporting recipe',
[
'message' => 'Invalid recipe id',
'recipe_id' => $request->get('recipe_id')
]
);

return $this->errorResponse([
'message' => 'There was an error processing this request. Please try again later.'
]);
}
}
}
1 change: 1 addition & 0 deletions app/Models/Recipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Recipe extends Model
'course',
'cuisine',
'nationality',
'is_reported'
];

protected $hidden = ['user_id'];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('recipes', function (Blueprint $table) {
$table->boolean('is_reported')->default(0);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('recipes', function (Blueprint $table) {
$table->dropColumn('is_reported');
});
}
};
123 changes: 120 additions & 3 deletions tests/Feature/RecipeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
use App\Models\Cookbook;
use App\Models\Recipe;
use App\Models\User;
use Illuminate\Hashing\BcryptHasher;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;

class RecipeTest extends \TestCase
{
/**
* @test
* todo: service->validatePayload
*/
public function it_can_retrieve_all_recipes_and_respond_with_a_200_status_code()
{
Expand Down Expand Up @@ -163,11 +164,14 @@ public function it_cannot_clap_for_a_recipe_that_does_not_exist()
]);
}

/**
* @test
*/
public function it_can_show_my_recipes()
{
$user = User::factory()->make([
'email' => '[email protected]',
'password' => 'pass123'
'password' => (new BcryptHasher)->make('pass123'),
]);
$user->save();

Expand All @@ -191,13 +195,126 @@ public function it_can_show_my_recipes()
$recipe->save();
});

$this->json(
$response = $this->json(
'GET',
'/api/v1/my/recipes',
[],
[
'Authorization' => 'Bearer ' . $token
]
)->assertStatus(200);

$decoded = json_decode($response->getContent(), true);

$this->assertCount(3, $decoded['data']);
}

/**
* @test
*/
public function it_can_report_a_recipe()
{
$user = User::factory()->make([
'email' => '[email protected]',
'password' => (new BcryptHasher)->make('pass123'),
]);
$user->save();

$token = Auth::attempt([
'email' => '[email protected]',
'password' => 'pass123'
]);

$cookbook = Cookbook::factory()->make([
'user_id' => $user->getKey()
]);

$cookbook->save();

$recipe = Recipe::factory()->make([
'cookbook_id' => $cookbook->refresh()->getKey(),
'user_id' => $user->getKey()
]);

$recipe->save();

$this->assertFalse((bool) $recipe->refresh()->is_reported);

$this->json(
'POST',
'/api/v1/report-recipe',
[
'recipe_id' => $recipe->refresh()->getKey()
],
[
'Authorization' => 'Bearer ' . $token
]
)->assertStatus(200)
->assertExactJson([
'message' => 'feedback submitted.'
]);

$this->assertTrue((bool) $recipe->refresh()->is_reported);
}

/**
* @test
*/
public function it_can_handle_error_reporting_recipe()
{
$user = User::factory()->make([
'email' => '[email protected]',
'password' => (new BcryptHasher)->make('pass123'),
]);
$user->save();

$token = Auth::attempt([
'email' => '[email protected]',
'password' => 'pass123'
]);

Log::shouldReceive('debug')
->once()
->with(
'Error reporting recipe',
[
'message' => 'Invalid recipe id',
'recipe_id' => 1
]
);

$this->withoutExceptionHandling()->json(
'POST',
'/api/v1/report-recipe',
[
'recipe_id' => 1
],
[
'Authorization' => 'Bearer ' . $token
]
)->assertStatus(400)
->assertExactJson([
'message' => 'There was an error processing this request. Please try again later.'
]);
}

/**
* @test
*/
public function it_allows_only_authorized_user_to_report_a_recipe()
{
$this->json(
'POST',
'/api/v1/report-recipe',
[
'recipe_id' => 1
],
[
'Authorization' => 'Bearer invalid-token'
]
)->assertStatus(401)
->assertExactJson([
'error' => 'Your session has expired. Please login and try again.'
]);
}
}

0 comments on commit f5a1894

Please sign in to comment.