Skip to content

Commit

Permalink
fix: added unit test for the product delete
Browse files Browse the repository at this point in the history
  • Loading branch information
Dev-Tonia committed Aug 7, 2024
1 parent b8f14f3 commit 2becaee
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 117 deletions.
6 changes: 3 additions & 3 deletions app/Http/Controllers/Api/V1/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,15 @@ public function index(Request $request)
* Store a newly created resource in storage.
*/
public function store(CreateProductRequest $request, $org_id)
{
{
$isOwner = OrganisationUser::where('org_id', $org_id)->where('user_id', auth()->id())->exists();

if (!$isOwner) {
return response()->json(['message' => 'You are not authorized to create products for this organization.'], 403);
}

$imageUrl = null;
if($request->hasFile('image')) {
if ($request->hasFile('image')) {
$imagePath = $request->file('image')->store('product_images', 'public');
$imageUrl = Storage::url($imagePath);
}
Expand Down Expand Up @@ -323,7 +323,7 @@ public function destroy($org_id, $product_id)
{

$isOwner = OrganisationUser::where('org_id', $org_id)->where('user_id', auth()->id())->exists();
// dd($isOwner); // Check if the user's organization matches the org_id in the request
// Check if the user's organization matches the org_id in the request
if (!$isOwner) {
return response()->json(
[
Expand Down
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');
//
});
}
};
Binary file added public/uploads/1722986116.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/uploads/1722986951.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/uploads/1722987043.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/uploads/1722989699.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/uploads/1722989897.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/uploads/1722990809.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/uploads/1723050163.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
114 changes: 0 additions & 114 deletions tests/Feature/ProductDeletionTest.php

This file was deleted.

104 changes: 104 additions & 0 deletions tests/Unit/ProductDeleteTest.php
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
]);
}
}

0 comments on commit 2becaee

Please sign in to comment.