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

Add integration test project #268

Merged
merged 2 commits into from
Aug 29, 2023
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
9 changes: 7 additions & 2 deletions CopilotChat.sln
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.6.33706.43
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CopilotChatWebApi", "webapi\CopilotChatWebApi.csproj", "{5252E68F-B653-44CE-9A32-360A75C54E0E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChatCopilotIntegrationTests", "integration-tests\ChatCopilotIntegrationTests.csproj", "{0CD2CD95-536B-455F-B74A-772A455FA607}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,11 +16,15 @@ Global
{5252E68F-B653-44CE-9A32-360A75C54E0E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5252E68F-B653-44CE-9A32-360A75C54E0E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5252E68F-B653-44CE-9A32-360A75C54E0E}.Release|Any CPU.Build.0 = Release|Any CPU
{0CD2CD95-536B-455F-B74A-772A455FA607}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0CD2CD95-536B-455F-B74A-772A455FA607}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0CD2CD95-536B-455F-B74A-772A455FA607}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0CD2CD95-536B-455F-B74A-772A455FA607}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {971570D3-60EA-4EE4-980C-0BDA3E66E741}
EndGlobalSection
EndGlobal
EndGlobal
50 changes: 50 additions & 0 deletions integration-tests/ChatCopilotIntegrationTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) Microsoft. All rights reserved.

using System;
using System.Net.Http;
using Microsoft.Extensions.Configuration;
using Xunit;

namespace ChatCopilotIntegrationTests;

/// <summary>
/// Base class for Chat Copilot integration tests
/// </summary>
public abstract class ChatCopilotIntegrationTest : IDisposable
{
protected const string BaseAddressSettingName = "BaseAddress";

protected readonly HttpClient _httpClient;

protected ChatCopilotIntegrationTest()
{
// Load configuration
IConfigurationRoot configuration = new ConfigurationBuilder()
.AddJsonFile(path: "testsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile(path: "testsettings.development.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.AddUserSecrets<HealthzTests>()
.Build();

string? baseAddress = configuration[BaseAddressSettingName];
Assert.False(string.IsNullOrEmpty(baseAddress));
Assert.True(baseAddress.EndsWith('/'));

this._httpClient = new HttpClient();
this._httpClient.BaseAddress = new Uri(baseAddress);
}

public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
this._httpClient.Dispose();
}
}
}
39 changes: 39 additions & 0 deletions integration-tests/ChatCopilotIntegrationTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<UserSecretsId>81136671-a63f-4f20-bd0e-a65b2561999a</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\webapi\CopilotChatWebApi.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="testsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
20 changes: 20 additions & 0 deletions integration-tests/HealthzTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Net.Http;
using Xunit;

namespace ChatCopilotIntegrationTests;

/// <summary>
/// Class for testing the healthcheck endpoint
/// </summary>
public class HealthzTests : ChatCopilotIntegrationTest
{
[Fact]
public async void HealthzSuccessfullyReturns()
{
HttpResponseMessage response = await this._httpClient.GetAsync("healthz");

response.EnsureSuccessStatusCode();
}
}
55 changes: 55 additions & 0 deletions integration-tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Chat Copilot Integration Tests

## Requirements

1. A running instance of the Chat Copilot's [backend](../webapi/README.md).

## Setup

### Option 1: Use Secret Manager

Integration tests require the URL of the backend instance.

We suggest using the .NET [Secret Manager](https://learn.microsoft.com/en-us/aspnet/core/security/app-secrets)
to avoid the risk of leaking secrets into the repository, branches and pull requests.

Values set using the Secret Manager will override the settings set in the `testsettings.development.json` file and in environment variables.

To set your secrets with Secret Manager:

```ps
cd integration-tests

dotnet user-secrets init
dotnet user-secrets set "BaseAddress" "https://your-backend-address/"
```

### Option 2: Use a Configuration File

1. Create a `testsettings.development.json` file next to `testsettings.json`. This file will be ignored by git,
the content will not end up in pull requests, so it's safe for personal settings. Keep the file safe.
2. Edit `testsettings.development.json` and
1. Set your base address - **make sure it ends with a trailing '/' **

For example:

```json
{
"BaseAddress": "https://localhost:40443/"
}
```

### Option 3: Use Environment Variables
You may also set the test settings in your environment variables. The environment variables will override the settings in the `testsettings.development.json` file.

- bash:

```bash
export BaseAddress="https://localhost:40443/"
```

- PowerShell:

```ps
$env:BaseAddress = "https://localhost:40443/"
```
3 changes: 3 additions & 0 deletions integration-tests/testsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"BaseAddress": "https://localhost:40443/"
}