Use local secret store for tests instead of hand-made solution

pull/1001/head
Tyrrrz 1 year ago
parent bf76f10030
commit fc29735a45

5
.gitignore vendored

@ -20,7 +20,4 @@ bld/
[Oo]bj/
# Coverage
*.opencover.xml
# Secrets
*.secret
*.opencover.xml

@ -1,8 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<UserSecretsId>d1fe5ae2-2a19-404d-a36e-81ba9eada1c1</UserSecretsId>
</PropertyGroup>
<ItemGroup>
@ -15,6 +16,9 @@
<PackageReference Include="FluentAssertions" Version="6.9.0" />
<PackageReference Include="GitHubActionsTestLogger" Version="2.0.1" PrivateAssets="all" />
<PackageReference Include="JsonExtensions" Version="1.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="System.Reactive" Version="5.0.0" />
<PackageReference Include="xunit" Version="2.4.2" />

@ -1,26 +1,17 @@
using System;
using System.IO;
using System.Reflection;
using Microsoft.Extensions.Configuration;
namespace DiscordChatExporter.Cli.Tests.Infra;
internal static class Secrets
{
private static readonly Lazy<string> DiscordTokenLazy = new(() =>
{
var fromEnvironment = Environment.GetEnvironmentVariable("DISCORD_TOKEN");
if (!string.IsNullOrWhiteSpace(fromEnvironment))
return fromEnvironment;
var secretFilePath = Path.Combine(
Path.GetDirectoryName(typeof(Secrets).Assembly.Location) ?? Directory.GetCurrentDirectory(),
"DiscordToken.secret"
);
if (File.Exists(secretFilePath))
return File.ReadAllText(secretFilePath);
private static readonly IConfigurationRoot Configuration = new ConfigurationBuilder()
.AddUserSecrets(Assembly.GetExecutingAssembly())
.AddEnvironmentVariables()
.Build();
public static string DiscordToken =>
Configuration["DISCORD_TOKEN"] ??
throw new InvalidOperationException("Discord token not provided for tests.");
});
public static string DiscordToken => DiscordTokenLazy.Value;
}

@ -5,9 +5,8 @@ In order to run these tests locally, you need to join the test server and config
1. [Join the test server](https://discord.gg/eRV8Vap5bm)
2. Locate your Discord authentication token
3. Specify your token using a file or an environment variable:
- **Using a file**: put your token in a new `DiscordToken.secret` file created in this directory
- **Using an environment variable**: set `DISCORD_TOKEN` variable to your token
3. Add your token to user secrets: `dotnet user-secrets set DISCORD_TOKEN <token>`
4. Run the tests: `dotnet test`
> If you want to add a new test case, please let me know and I will give you the required permissions
> **Note**:
> If you want to add a new test case, please let me know and I will give you the required permissions on the server.
Loading…
Cancel
Save