-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
171 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
database/migrations/2023_08_21_234409_add_is_reported_field_to_recipes_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
{ | ||
|
@@ -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(); | ||
|
||
|
@@ -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.' | ||
]); | ||
} | ||
} |