Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: product image upload #385

Merged
merged 1 commit into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions app/Http/Controllers/Api/V1/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
]);
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Requests/CreateProductRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];
}
}
179 changes: 91 additions & 88 deletions tests/Feature/ProductCreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -120,22 +121,24 @@ 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
$response = $this->postJson("/api/v1/organizations/{$organisation->org_id}/products", $payload);

// 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.']);
}
}
Loading