-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: added unit test for the product delete
- Loading branch information
Showing
11 changed files
with
137 additions
and
117 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
30 changes: 30 additions & 0 deletions
30
database/migrations/2024_08_07_145415_add_category_column_products_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,30 @@ | ||
<?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('products', function (Blueprint $table) { | ||
$table->string('category')->nullable(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
|
||
Schema::table('products', function (Blueprint $table) { | ||
$table->dropColumn('category'); | ||
// | ||
}); | ||
} | ||
}; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file was deleted.
Oops, something went wrong.
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,104 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use App\Models\CategoryProduct; | ||
use Tests\TestCase; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use App\Models\User; | ||
use App\Models\Product; | ||
use App\Models\Organisation; | ||
use App\Models\OrganisationUser; | ||
use Carbon\Carbon; | ||
use Tymon\JWTAuth\Facades\JWTAuth; | ||
|
||
class ProductDeleteTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
protected $user; | ||
protected $otherUser; | ||
protected $org; | ||
protected $otherOrg; | ||
protected $product; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
// Create users | ||
$this->user = User::factory()->create(); | ||
$this->otherUser = User::factory()->create(); | ||
|
||
// Create organizations | ||
$this->org = Organisation::create([ | ||
'name' => 'Bamo', | ||
'description' => 'Bamo\'s Organisation', | ||
'email' => '[email protected]', | ||
'industry' => 'Tech', | ||
'type' => 'fulltime', | ||
'country' => 'Nigeria', | ||
'address' => 'Tanke, Ilorin', | ||
'state' => 'Kwara', | ||
'user_id' => $this->user->id, | ||
|
||
]); | ||
|
||
$this->otherOrg = Organisation::create([ | ||
'name' => 'Other Org', | ||
'description' => 'Another organization', | ||
'email' => '[email protected]', | ||
'industry' => 'Finance', | ||
'type' => 'parttime', | ||
'country' => 'Nigeria', | ||
'address' => 'Other address', | ||
'state' => 'Lagos', | ||
'user_id' => $this->otherUser->id, | ||
|
||
]); | ||
// Assign users as owners of the organizations | ||
$this->org->users()->attach($this->user->id); | ||
$this->otherOrg->users()->attach($this->otherUser->id); | ||
|
||
$this->product = Product::create([ | ||
'name' => 'Test', | ||
'description' => 'Testing', | ||
'slug' => Carbon::now(), | ||
'category' => 'food', | ||
'tags' => 'Food', | ||
'price' => 100, | ||
'imageUrl' => 'https://via.placeholder.com/640x480.png/0099ff?text=fuga', | ||
'org_id' => $this->org->org_id, | ||
'user_id' => $this->user->id, | ||
'status' => 'active', | ||
'quantity' => 80 | ||
]); | ||
} | ||
|
||
public function testUserCanDeleteProduct() | ||
{ | ||
|
||
$token = JWTAuth::fromUser($this->user); | ||
$response = $this->withHeaders(['Authorization' => "Bearer $token"]) | ||
->deleteJson("api/v1/organizations/{$this->org->org_id}/products/{$this->product->product_id}"); | ||
|
||
|
||
$response->assertStatus(204); | ||
$this->assertDatabaseMissing('products', ['product_id' => $this->product->product_id]); | ||
} | ||
|
||
public function testUserCannotDeleteProductIfNotOwner() | ||
{ | ||
|
||
$token = JWTAuth::fromUser($this->otherUser); | ||
$response = $this->withHeaders(['Authorization' => "Bearer $token"]) | ||
->deleteJson("api/v1/organizations/{$this->org->org_id}/products/{$this->product->product_id}"); | ||
|
||
$response->assertStatus(403); | ||
$response->assertJson([ | ||
'status' => 'Forbidden', | ||
'message' => 'You do not have permission to delete a product from this organization.', | ||
'status_code' => 403 | ||
]); | ||
} | ||
} |