diff --git a/app/Http/Controllers/Api/V1/ProductController.php b/app/Http/Controllers/Api/V1/ProductController.php index 1094c4b5..846c558a 100755 --- a/app/Http/Controllers/Api/V1/ProductController.php +++ b/app/Http/Controllers/Api/V1/ProductController.php @@ -193,25 +193,27 @@ public function index(Request $request) /** * Store a newly created resource in storage. */ - public function store(CreateProductRequest $request) - { - - $org_id = $request->route('org_id'); - + 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')) { + $imagePath = $request->file('image')->store('product_images', 'public'); + $imageUrl = Storage::url($imagePath); + } + $product = Product::create([ 'name' => $request->input('title'), 'description' => $request->input('description'), 'slug' => Carbon::now(), 'tags' => $request->input('category'), 'price' => $request->input('price'), - // 'imageUrl' => $imageUrl, - 'imageUrl' => $request->input('image'), + 'imageUrl' => $imageUrl, 'user_id' => auth()->id(), 'org_id' => $org_id ]); diff --git a/app/Http/Requests/CreateProductRequest.php b/app/Http/Requests/CreateProductRequest.php index b2e18df5..461a08f3 100755 --- a/app/Http/Requests/CreateProductRequest.php +++ b/app/Http/Requests/CreateProductRequest.php @@ -27,7 +27,7 @@ public function rules(): array 'category' => 'required|uuid|exists:categories,id', 'price' => 'required|numeric', 'stock' => 'required|integer', - // 'image' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048', + 'image' => 'required|image|mimes:jpeg,png,jpg,gif|max:1024', ]; } } diff --git a/tests/Feature/ProductCreateTest.php b/tests/Feature/ProductCreateTest.php index 9c2c48b7..ddcf3ca7 100644 --- a/tests/Feature/ProductCreateTest.php +++ b/tests/Feature/ProductCreateTest.php @@ -10,7 +10,8 @@ use App\Models\Size; use App\Models\User; use Illuminate\Foundation\Testing\RefreshDatabase; -use Illuminate\Foundation\Testing\WithFaker; +use Illuminate\Http\UploadedFile; +use Illuminate\Support\Facades\Storage; use Tests\TestCase; class ProductCreateTest extends TestCase @@ -19,89 +20,89 @@ class ProductCreateTest extends TestCase /** @test */ public function it_can_create_a_product() -{ - // Create a user and authenticate - $user = User::factory()->create(); - $this->actingAs($user); - - // Create a size with 'standard' - $size = Size::create(['size' => 'standard']); - - // Create a category - $category = Category::create(['name' => 'Test Category', 'slug' => 'test-category', 'description' => 'Testing']); - - // Create an organisation and get the first instance - $organisation = Organisation::factory()->create(); - - // Associate the user with the organization as an owner - OrganisationUser::create([ - 'org_id' => $organisation->org_id, - 'user_id' => $user->id, - // 'role' => 'owner' // Ensure this is the correct role for authorization - ]); - - // Define the payload - $payload = [ - 'title' => 'Test Product', - 'description' => 'Test Description', - 'category' => $category->id, // Use the created category ID - 'price' => 100, - 'stock' => 10, - 'image' => 'test_image.jpg', - 'org_id' => $organisation->org_id - ]; - - // Send POST request to create product - $response = $this->postJson("/api/v1/organizations/{$organisation->org_id}/products", $payload); - - // Assert the response status and structure - $response->assertStatus(201) - ->assertJsonStructure([ - 'message', - 'product' => [ - 'name', - 'description', - 'slug', - 'tags', - 'price', - 'imageUrl', - 'user_id', - 'updated_at', - 'created_at', - ] - ]); - - // Assert the product is created in the database - $this->assertDatabaseHas('products', [ - 'name' => 'Test Product', - 'description' => 'Test Description', - 'tags' => $category->id, - 'price' => 100, - 'imageUrl' => 'test_image.jpg', - 'user_id' => $user->id - ]); - - // Assert the category_product relationship is created - $this->assertDatabaseHas('category_product', [ - 'category_id' => $category->id, - 'product_id' => Product::first()->product_id - ]); - - // Assert the product variant is created with the correct size - $this->assertDatabaseHas('product_variants', [ - 'product_id' => Product::first()->product_id, - 'stock' => 10, - 'stock_status' => 'in_stock', - 'price' => 100, - 'size_id' => $size->id - ]); - - // Assert the product variant size is created - $this->assertDatabaseHas('product_variant_sizes', [ - 'product_variant_id' => ProductVariant::first()->id, - 'size_id' => $size->id - ]); -} + { + // Create a user and authenticate + $user = User::factory()->create(); + $this->actingAs($user); + + // Create a size with 'standard' + $size = Size::create(['size' => 'standard']); + + // Create a category + $category = Category::create(['name' => 'Test Category', 'slug' => 'test-category', 'description' => 'Testing']); + + // Create an organisation and get the first instance + $organisation = Organisation::factory()->create(); + + // Associate the user with the organization as an owner + OrganisationUser::create([ + 'org_id' => $organisation->org_id, + 'user_id' => $user->id, + ]); + + // Define the payload with an image file + Storage::fake('public'); + $image = UploadedFile::fake()->image('test_image.jpg'); + + $payload = [ + 'title' => 'Test Product', + 'description' => 'Test Description', + 'category' => $category->id, // Use the created category ID + 'price' => 100, + 'stock' => 10, + 'image' => $image, + ]; + + // Send POST request to create product + $response = $this->postJson("/api/v1/organizations/{$organisation->org_id}/products", $payload); + + // Assert the response status and structure + $response->assertStatus(201) + ->assertJsonStructure([ + 'message', + 'product' => [ + 'name', + 'description', + 'slug', + 'tags', + 'price', + 'imageUrl', + 'user_id', + 'updated_at', + 'created_at', + ] + ]); + + // Assert the product is created in the database + $this->assertDatabaseHas('products', [ + 'name' => 'Test Product', + 'description' => 'Test Description', + 'tags' => $category->id, + 'price' => 100, + 'user_id' => $user->id + ]); + + // Assert the category_product relationship is created + $this->assertDatabaseHas('category_product', [ + 'category_id' => $category->id, + 'product_id' => Product::first()->product_id + ]); + + // Assert the product variant is created with the correct size + $this->assertDatabaseHas('product_variants', [ + 'product_id' => Product::first()->product_id, + 'stock' => 10, + 'stock_status' => 'in_stock', + 'price' => 100, + 'size_id' => $size->id + ]); + + // Assert the product variant size is created + $this->assertDatabaseHas('product_variant_sizes', [ + 'product_variant_id' => ProductVariant::first()->id, + 'size_id' => $size->id + ]); + } /** @test */ public function it_cannot_create_a_product_if_not_an_owner() @@ -120,15 +121,17 @@ public function it_cannot_create_a_product_if_not_an_owner() // Create an organisation and get the first instance $organisation = Organisation::factory()->create(); - // Define the payload + // Define the payload with an image file + Storage::fake('public'); + $image = UploadedFile::fake()->image('test_image.jpg'); + $payload = [ 'title' => 'Unauthorized Product', 'description' => 'Unauthorized Description', 'category' => $category->id, // Use the created category ID 'price' => 100, 'stock' => 10, - 'image' => 'test_image.jpg', - 'org_id' => $organisation->org_id + 'image' => $image, ]; // Send POST request to create product @@ -136,6 +139,6 @@ public function it_cannot_create_a_product_if_not_an_owner() // Assert the response status is 403 Forbidden $response->assertStatus(403) - ->assertJson(['message' => 'You are not authorized to create products for this organization.']); + ->assertJson(['message' => 'You are not authorized to create products for this organization.']); } }