Skip to content
Draft
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
25 changes: 25 additions & 0 deletions e2e/CheckoutTest.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { test, expect } from '@playwright/test';

test('Checkout redirects to orders after placing an order', async ({ page }) => {
await page.goto('/');

await expect(page.getByRole('heading', { name: 'Ready for a new adventure?' })).toBeVisible();
await page.getByRole('link', { name: 'Adventurer GPS Watch' }).click();
await page.getByRole('button', { name: 'Add to shopping bag' }).click();
await page.getByRole('link', { name: 'shopping bag' }).click();
await expect(page.getByRole('heading', { name: 'Shopping bag' })).toBeVisible();

await page.getByRole('link', { name: 'Check out' }).click();
await expect(page.getByRole('heading', { name: 'Checkout' })).toBeVisible();

await page.getByLabel('Address').fill('1 Adventure Way');
await page.getByLabel('City').fill('Redmond');
await page.getByLabel('State').fill('WA');
await page.getByLabel('Zip code').fill('98052');
await page.getByLabel('Country').fill('USA');

await page.getByRole('button', { name: 'Place order' }).click();

await expect(page).toHaveURL(/\/user\/orders$/);
await expect(page.getByRole('heading', { name: 'Orders' })).toBeVisible();
});
3 changes: 2 additions & 1 deletion eShop.Web.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"tests\\Basket.UnitTests\\Basket.UnitTests.csproj",
"tests\\Catalog.FunctionalTests\\Catalog.FunctionalTests.csproj",
"tests\\Ordering.FunctionalTests\\Ordering.FunctionalTests.csproj",
"tests\\Ordering.UnitTests\\Ordering.UnitTests.csproj"
"tests\\Ordering.UnitTests\\Ordering.UnitTests.csproj",
"tests\\WebApp.UnitTests\\WebApp.UnitTests.csproj"
]
}
}
1 change: 1 addition & 0 deletions eShop.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<Project Path="tests/Ordering.FunctionalTests/Ordering.FunctionalTests.csproj" />
<Project Path="tests/Ordering.UnitTests/Ordering.UnitTests.csproj" />
<Project Path="tests/ClientApp.UnitTests/ClientApp.UnitTests.csproj" />
<Project Path="tests/WebApp.UnitTests/WebApp.UnitTests.csproj" />
</Folder>
<Folder Name="/Solution Items/">
<File Path=".editorconfig" />
Expand Down
2 changes: 1 addition & 1 deletion playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default defineConfig({
},
{
name: 'e2e tests logged in',
testMatch: ['**/AddItemTest.spec.ts', '**/RemoveItemTest.spec.ts'],
testMatch: ['**/AddItemTest.spec.ts', '**/RemoveItemTest.spec.ts', '**/CheckoutTest.spec.ts'],
dependencies: ['setup'],
use: {
storageState: STORAGE_STATE,
Expand Down
7 changes: 4 additions & 3 deletions src/WebApp/Services/OrderingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ public Task<OrderRecord[]> GetOrders()
return httpClient.GetFromJsonAsync<OrderRecord[]>(remoteServiceBaseUrl)!;
}

public Task CreateOrder(CreateOrderRequest request, Guid requestId)
public async Task CreateOrder(CreateOrderRequest request, Guid requestId)
{
var requestMessage = new HttpRequestMessage(HttpMethod.Post, remoteServiceBaseUrl);
using var requestMessage = new HttpRequestMessage(HttpMethod.Post, remoteServiceBaseUrl);
requestMessage.Headers.Add("x-requestid", requestId.ToString());
requestMessage.Content = JsonContent.Create(request);
return httpClient.SendAsync(requestMessage);
using var response = await httpClient.SendAsync(requestMessage);
response.EnsureSuccessStatusCode();
}
}

Expand Down
9 changes: 9 additions & 0 deletions tests/WebApp.UnitTests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
global using System;
global using System.Collections.Generic;
global using System.Linq;
global using System.Net.Http;
global using System.Threading;
global using System.Threading.Tasks;
global using Microsoft.VisualStudio.TestTools.UnitTesting;

[assembly: Parallelize(Workers = 0, Scope = ExecutionScope.MethodLevel)]
78 changes: 78 additions & 0 deletions tests/WebApp.UnitTests/OrderingServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System.Net;
using eShop.WebApp.Services;

namespace WebApp.UnitTests;

[TestClass]
public class OrderingServiceTests
{
[TestMethod]
public async Task CreateOrderSendsRequestIdHeader()
{
var requestId = Guid.NewGuid();
var handler = new StubHttpMessageHandler(_ => new HttpResponseMessage(HttpStatusCode.OK));
using var httpClient = new HttpClient(handler)
{
BaseAddress = new Uri("http://ordering-api")
};
var service = new OrderingService(httpClient);

await service.CreateOrder(CreateRequest(), requestId);

Assert.AreEqual(requestId.ToString(), handler.LastRequest!.Headers.GetValues("x-requestid").Single());
Assert.AreEqual(HttpMethod.Post, handler.LastRequest.Method);
Assert.AreEqual("/api/Orders/", handler.LastRequest.RequestUri!.PathAndQuery);
}

[TestMethod]
public async Task CreateOrderThrowsWhenOrderingApiReturnsFailure()
{
using var httpClient = new HttpClient(new StubHttpMessageHandler(_ => new HttpResponseMessage(HttpStatusCode.InternalServerError)))
{
BaseAddress = new Uri("http://ordering-api")
};
var service = new OrderingService(httpClient);

await Assert.ThrowsExactlyAsync<HttpRequestException>(() => service.CreateOrder(CreateRequest(), Guid.NewGuid()));
}

private static CreateOrderRequest CreateRequest()
{
return new CreateOrderRequest(
UserId: "buyer-id",
UserName: "Test User",
City: "Redmond",
Street: "1 Adventure Way",
State: "WA",
Country: "USA",
ZipCode: "98052",
CardNumber: "1111222233334444",
CardHolderName: "TESTUSER",
CardExpiration: DateTime.UtcNow.AddYears(1),
CardSecurityNumber: "111",
CardTypeId: 1,
Buyer: "buyer-id",
Items:
[
new BasketItem
{
Id = "item-id",
ProductId = 1,
ProductName = "Adventurer GPS Watch",
UnitPrice = 10,
Quantity = 1
}
]);
}

private sealed class StubHttpMessageHandler(Func<HttpRequestMessage, HttpResponseMessage> responseFactory) : HttpMessageHandler
{
public HttpRequestMessage? LastRequest { get; private set; }

protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
LastRequest = request;
return Task.FromResult(responseFactory(request));
}
}
}
16 changes: 16 additions & 0 deletions tests/WebApp.UnitTests/WebApp.UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="MSTest.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<GenerateErrorForMissingTargetingPacks>false</GenerateErrorForMissingTargetingPacks>
<IsPublishable>false</IsPublishable>
<IsPackable>false</IsPackable>
<OutputType>Exe</OutputType>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\WebApp\WebApp.csproj" />
</ItemGroup>

</Project>
Loading