// Copyright (c) HelloShop Corporation. All rights reserved. // See the license file in the project root for more information. using Microsoft.AspNetCore.Authentication.BearerToken; using System.Dynamic; using System.Net; using System.Net.Http.Headers; using System.Net.Http.Json; using System.Text.Json.Nodes; namespace HelloShop.FunctionalTests { public class FirstWebApiIntegrationTest { [Fact] public async Task WebAppRootReturnsOkStatusCode() { // Arrange IDistributedApplicationTestingBuilder appHost = await DistributedApplicationTestingBuilder.CreateAsync(); await using DistributedApplication app = await appHost.BuildAsync(); await app.StartAsync(); // Act HttpClient httpClient = app.CreateHttpClient("webapp"); HttpResponseMessage response = await httpClient.GetAsync("/"); // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); } [Fact] public async Task IdetityServiceAccountLoginReturnsSuccessStatusCode() { // Arrange IDistributedApplicationTestingBuilder appHost = await DistributedApplicationTestingBuilder.CreateAsync(); await using DistributedApplication app = await appHost.BuildAsync(); await app.StartAsync(); // Act HttpClient httpClient = app.CreateHttpClient("identityservice"); HttpResponseMessage response = await httpClient.PostAsJsonAsync("api/Account/Login", new { UserName = "guest", Password = "guest" }); // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); } [Fact] public async Task IdetityServiceAccountLoginReturnsAccessToken() { // Arrange IDistributedApplicationTestingBuilder appHost = await DistributedApplicationTestingBuilder.CreateAsync(); await using DistributedApplication app = await appHost.BuildAsync(); await app.StartAsync(); // Act HttpClient httpClient = app.CreateHttpClient("identityservice"); HttpResponseMessage response = await httpClient.PostAsJsonAsync("api/Account/Login", new { UserName = "guest", Password = "guest" }); dynamic? result = await response.Content.ReadFromJsonAsync(); // Assert Assert.NotNull(result?.accessToken); } [Fact] public async Task IdetityServiceAccountLoginReturnsTokenExpiresInSeconds() { // Arrange IDistributedApplicationTestingBuilder appHost = await DistributedApplicationTestingBuilder.CreateAsync(); await using DistributedApplication app = await appHost.BuildAsync(); await app.StartAsync(); // Act HttpClient httpClient = app.CreateHttpClient("identityservice"); HttpResponseMessage response = await httpClient.PostAsJsonAsync("api/Account/Login", new { UserName = "guest", Password = "guest" }); JsonNode? result = await response.Content.ReadFromJsonAsync(); int expiresInSeconds = result?["expiresIn"]?.GetValue() ?? default; // Assert Assert.Equal(3600, expiresInSeconds); } [Fact] public async Task ProductServiceGetProductReturnsProductDetails() { // Arrange IDistributedApplicationTestingBuilder appHost = await DistributedApplicationTestingBuilder.CreateAsync(); await using DistributedApplication app = await appHost.BuildAsync(); await app.StartAsync(); // Act HttpClient identityServiceHttpClient = app.CreateHttpClient("identityservice"); HttpResponseMessage loginResponse = await identityServiceHttpClient.PostAsJsonAsync("api/Account/Login", new { UserName = "admin", Password = "admin" }); AccessTokenResponse? accessTokenResponse = await loginResponse.Content.ReadFromJsonAsync(); HttpClient productServiceHttpClient = app.CreateHttpClient("productservice"); productServiceHttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessTokenResponse?.AccessToken); HttpResponseMessage productDetailsResponse = await productServiceHttpClient.GetAsync("api/Products/1"); JsonNode? result = await productDetailsResponse.Content.ReadFromJsonAsync(); string? productName = result?["Name"]?.GetValue(); // Assert Assert.NotNull(productName); Assert.Equal("Product 1", productName); } } }