Skip to content

Commit 194c386

Browse files
added product api tests
1 parent c7792d8 commit 194c386

1 file changed

Lines changed: 91 additions & 0 deletions

File tree

ECommerceApp.IntegrationTests/ProductsApiTests.cs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,95 @@ public async Task Get_Products_ReturnsOk()
4747
// Assert
4848
Assert.Equal(HttpStatusCode.OK, actualStatusCode);
4949
}
50+
51+
[Fact]
52+
public async Task Create_Product_ReturnsCreated()
53+
{
54+
// Arrange
55+
var user = new { user_name = "testuser" };
56+
var tokenResponse = await _client.PostAsJsonAsync("/admin/generateToken", user);
57+
tokenResponse.EnsureSuccessStatusCode();
58+
var token = await tokenResponse.Content.ReadAsStringAsync();
59+
token = token.Trim('"');
60+
61+
var product = new
62+
{
63+
name = "Test Product",
64+
description = "A product for testing.",
65+
price = 9.99M,
66+
stock = 100
67+
};
68+
var request = new HttpRequestMessage(HttpMethod.Post, "/products");
69+
request.Headers.Add("Authorization", $"Bearer {token}");
70+
request.Content = JsonContent.Create(product);
71+
72+
// Act
73+
var response = await _client.SendAsync(request);
74+
75+
// Log the actual status code for debugging
76+
var actualStatusCode = response.StatusCode;
77+
if (actualStatusCode != HttpStatusCode.Created)
78+
{
79+
string content = await response.Content.ReadAsStringAsync();
80+
_testOutputHelper.WriteLine($"Expected 201 Created but got {(int)actualStatusCode} {actualStatusCode}. Response content: {content}");
81+
}
82+
83+
// Assert
84+
Assert.Equal(HttpStatusCode.Created, actualStatusCode);
85+
}
86+
87+
[Fact]
88+
public async Task Update_Product_ReturnsOk()
89+
{
90+
// Arrange
91+
var user = new { user_name = "testuser" };
92+
var tokenResponse = await _client.PostAsJsonAsync("/admin/generateToken", user);
93+
tokenResponse.EnsureSuccessStatusCode();
94+
var token = await tokenResponse.Content.ReadAsStringAsync();
95+
token = token.Trim('"');
96+
97+
// First, create a product
98+
var product = new
99+
{
100+
name = "Product To Update",
101+
description = "Original description.",
102+
price = 5.00M,
103+
stock = 10
104+
};
105+
var createRequest = new HttpRequestMessage(HttpMethod.Post, "/products");
106+
createRequest.Headers.Add("Authorization", $"Bearer {token}");
107+
createRequest.Content = JsonContent.Create(product);
108+
var createResponse = await _client.SendAsync(createRequest);
109+
createResponse.EnsureSuccessStatusCode();
110+
var createdContent = await createResponse.Content.ReadAsStringAsync();
111+
// Extract the product ID from the response (assuming it is returned as JSON with an id property as string)
112+
var id = System.Text.Json.JsonDocument.Parse(createdContent).RootElement.GetProperty("id").GetString();
113+
114+
// Prepare update payload
115+
var updatedProduct = new
116+
{
117+
id = id,
118+
name = "Product To Update",
119+
description = "Updated description!",
120+
price = 7.50M,
121+
stock = 20
122+
};
123+
var updateRequest = new HttpRequestMessage(HttpMethod.Put, $"/products/{id}");
124+
updateRequest.Headers.Add("Authorization", $"Bearer {token}");
125+
updateRequest.Content = JsonContent.Create(updatedProduct);
126+
127+
// Act
128+
var updateResponse = await _client.SendAsync(updateRequest);
129+
130+
// Log the actual status code for debugging
131+
var actualStatusCode = updateResponse.StatusCode;
132+
if (actualStatusCode != HttpStatusCode.OK)
133+
{
134+
string content = await updateResponse.Content.ReadAsStringAsync();
135+
_testOutputHelper.WriteLine($"Expected 200 OK but got {(int)actualStatusCode} {actualStatusCode}. Response content: {content}");
136+
}
137+
138+
// Assert
139+
Assert.Equal(HttpStatusCode.OK, actualStatusCode);
140+
}
50141
}

0 commit comments

Comments
 (0)