Skip to content

Commit

Permalink
refactor: simplify test setup with private key
Browse files Browse the repository at this point in the history
  • Loading branch information
Tr00d committed Nov 23, 2023
1 parent e8407e6 commit e080884
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 29 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/multiframework-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,4 @@ jobs:
- name: Build
run: dotnet build --configuration Release
- name: Test
run: dotnet test --configuration Release --no-build
env:
Vonage.Test.RsaPrivateKey: ${{ secrets.VONAGE_RSA_PRIVATE_KEY }}
run: dotnet test --configuration Release --no-build
2 changes: 0 additions & 2 deletions .github/workflows/net-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ jobs:
dotnet build --configuration Release .\Vonage.Server.Test\Vonage.Server.Test.csproj -f net7.0
- name: Test
run: dotnet-coverage collect 'dotnet test --configuration Release --no-build -f net7.0 --filter Category!=Integration' -f xml -o 'coverage.xml'
env:
Vonage.Test.RsaPrivateKey: ${{ secrets.VONAGE_RSA_PRIVATE_KEY }}
- name: End SonarScanner
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ Sign up [for free at vonage.com][signup].
* [Lazy registration (recommended for .NET Core and above)](#lazy-registration-recommended-for-net-core-and-above)
* [Manual initialization (recommended for .NET Framework)](#manual-initialization-recommended-for-net-framework)
* [Configuration reference](#configuration-reference)
* [Test configuration](#test-configuration)
* [Logging](#logging)
* [Monads](#monads)
* [Result](#result)
Expand Down Expand Up @@ -239,13 +238,6 @@ Configuration.Instance.Settings["appSettings:Vonage.Video.Url.Rest"] = "https://
| Vonage.RequestTimeout | Optional. The timeout (in seconds) applied to every request. If not provided, the default timeout will be applied. |
| Vonage.UserAgent | Optional. Your app-specific usage identifier in the format of `name/version`. Example: `"myApp/1.0"` |

### Test configuration

Make sure to set `Vonage.Test.RsaPrivateKey` (with a RSA Private Key) in your environment variables.
Some tests rely on that to verify a token can be created.

For security reasons, not RSA Private Key is hardcoded in the repository.

### Logging

The Library uses Microsoft.Extensions.Logging to preform all of it's logging tasks. To configure logging for you app
Expand Down
1 change: 1 addition & 0 deletions Vonage.Common.Test/TestHelpers/Key.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-----BEGIN PRIVATE KEY----- MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC5tb9yvkGc/CAq dl3YzyixNc8kBQzTsLXEKtXmrZGsuyFgnaI4ea4sLj7Woxg+T7GR7ZTQpkFLiLvv vWXnTBHcF/sU3aW4NWkHZV2XUMFY8wvyjdq/T0x1sz0jHIanyRJQIbcIB5MeNeaS 8byimsKvVv82sW7fNDDgGMdbLIr32H82D5egS7K5ZZRQNkzU6MmsI+BGe9vehQ7e OE0pkRzghmmIe1thxX8BRMVcCRcRdgnJ+IFqeCHcpq4wovhD2O7Wix19JepJDv6J /aS8VP70G/OATOeGaELsZmeRDhXqqtB47ywr8h2dHSwlc3iP9vn3mfINqmgCNpbx mWeTkv+JAgMBAAECggEAAZL4TfUt4jJC5Yk+T2WmHyZpHJGMY8j2KXUBfiSPfQrT BBoh81+Yqdg0gSY9wN11EFsWrVQTbpv4l9YfpH/BEQAFCU69pluRidYCnpM5KFNb ERHBvmhiKgc8Z+1IeOib5qD6h69kwFdNnoY9BpXQO6M9o42KdorfNS8QlYXBocso yp2GcGfK06zWMC3duPTURRz0en4Fp8PXCd9DB+SKxp/8OG7RL9sdFmkw2+XtC0jk DqSxQ+v7K3g0BcrOIa3dhoqNmPyzSGwiDnPFO9dpnzHYa9GscMxS9UV0KFhYj+Xn 4ssqRjj5F9wMNdXlE4qk1pp0/+vOqcAY8Q7tEOfOjwKBgQDaXf4LZdkDxgAGxuA2 3r6WhjR/+ijP/MXnadyro6XDDXb4Ru1/d5hVnnikS/dZNvvvrYS3NULS37DdzM09 OEC0OjsboAJFmA60lw0ij4Bs/U2AwNNI5UVby7s/AIT6Y/DAM1l+WkVggfIt2gU2 +EaYrNczo9rbGhfP7jhRuB9sWwKBgQDZtvjeD3uQFs51VbJM4lMcXOcJ6e75i80R 7WwJJGqyddldPzuK9N4nEgVhT03onXHhLk9s1XgWyaJnjLL4zk21PRyPGDh8xYwq V3vUJ3aTERb6ZC8SxpLFNiN42DIM5a0HbICt3uzhlxITUSSPvDmJgIsqE7izIWAh CW5kglYY6wKBgQCynr/3ws/JbmUHJhaxy3JK3myDYrWPrEyWBtoi7DHjY1g1ro8G /WT2ZDJ68kjaCUf7vgwZcM/AfonGZIhd023Z/ufqqPAyzTb6MbTk2E1M2cZT02cA 8cnSVMxNtLcRuj5seZRy7pRhZOoc54HsfRoCOR+vdhDHuIhR5aLb9ah+kQKBgQCb ixPGYr1etkyOm8klENVcACu4c3+enfjHBB8ZcQEhuvyumAyMPGGy/DcHzMbWmBXS UWnBUcnYTfpPjMAY7huqjpymxyEkU2bOoW4ApqkabS1Deuv+uAwIBaPWJG+tszGp iiVtE0Wd7nalgmVio5Ff4YyLZUeiAwhQ0hIikNO+PQKBgF+ZtWH32tI0+xn5k9Ib IYppbs8CUVkIWvMPBpeUJlLJbud9hEGNi/qdTBewDMKHOeYF4gHKTxCAf6lszKHO In3DRwnnurpODEmXQdH4FW8N2YVxa/LnUKeVj7HEL71HqbeB3cK313dsu5dvXPBS +O6wcZ4Yllb740vNKfuf/wfa -----END PRIVATE KEY-----
12 changes: 12 additions & 0 deletions Vonage.Common.Test/TestHelpers/TokenHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Vonage.Common.Test.TestHelpers;

public static class TokenHelper
{
private const string FilePath = "./TestHelpers/key.txt";

/// <summary>
/// Loads the key from the text file.
/// </summary>
/// <returns>The key.</returns>
public static string GetKey() => File.ReadAllText(FilePath);
}
3 changes: 3 additions & 0 deletions Vonage.Common.Test/Vonage.Common.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
<Content Include="Failures\Data\ShouldDeserializeError413-response.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<None Update="TestHelpers\Key.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="node ../.scripts/init.js"/>
Expand Down
6 changes: 3 additions & 3 deletions Vonage.Server.Test/Authentication/VideoTokenGeneratorTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using FluentAssertions;
using FluentAssertions;
using Vonage.Common.Monads;
using Vonage.Common.Test.Extensions;
using Vonage.Common.Test.TestHelpers;
using Vonage.Request;
using Vonage.Server.Authentication;
using Xunit;
Expand All @@ -11,7 +11,7 @@ namespace Vonage.Server.Test.Authentication
public class VideoTokenGeneratorTest
{
private const string ApplicationId = "ffffffff-ffff-ffff-ffff-ffffffffffff";
private readonly string privateKey = Environment.GetEnvironmentVariable("Vonage.Test.RsaPrivateKey");
private readonly string privateKey = TokenHelper.GetKey();

[Fact]
public void GenerateToken_ShouldReturnFailure_GivenTokenGenerationFails() =>
Expand Down
6 changes: 3 additions & 3 deletions Vonage.Server.Test/TestHelpers/E2EHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Configuration;
using Vonage.Common.Test.TestHelpers;
using Vonage.Request;
using Vonage.Server.Video;
using WireMock.Server;
Expand Down Expand Up @@ -32,8 +33,7 @@ public void Dispose()
public static E2EHelper WithBearerCredentials(string appSettingsKey) =>
new E2EHelper(appSettingsKey, CreateBearerCredentials());

private static Credentials CreateBearerCredentials() => Credentials.FromAppIdAndPrivateKey(
Guid.NewGuid().ToString(),
Environment.GetEnvironmentVariable("Vonage.Test.RsaPrivateKey"));
private static Credentials CreateBearerCredentials() =>
Credentials.FromAppIdAndPrivateKey(Guid.NewGuid().ToString(), TokenHelper.GetKey());
}
}
9 changes: 5 additions & 4 deletions Vonage.Test.Unit/JwtTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using FluentAssertions;
using FluentAssertions;
using Vonage.Common.Failures;
using Vonage.Common.Test.Extensions;
using Vonage.Common.Test.TestHelpers;
using Vonage.Request;
using Xunit;

Expand All @@ -10,7 +10,7 @@ namespace Vonage.Test.Unit
public class JwtTest
{
private const string ApplicationId = "ffffffff-ffff-ffff-ffff-ffffffffffff";
private readonly string privateKey = Environment.GetEnvironmentVariable("Vonage.Test.RsaPrivateKey");
private readonly string privateKey = TokenHelper.GetKey();

[Theory]
[InlineData("")]
Expand Down Expand Up @@ -59,6 +59,7 @@ public void VerifySignature_ShouldReturnFalse_GivenTokenIsEmpty(string emptyToke
Jwt.VerifySignature(emptyToken, this.privateKey).Should().BeFalse();

[Fact]
public void VerifySignature_ShouldReturnTrue_GivenTokenIsValid() => Jwt.VerifySignature(Jwt.CreateToken(ApplicationId, this.privateKey), this.privateKey).Should().BeTrue();
public void VerifySignature_ShouldReturnTrue_GivenTokenIsValid() => Jwt
.VerifySignature(Jwt.CreateToken(ApplicationId, this.privateKey), this.privateKey).Should().BeTrue();
}
}
5 changes: 2 additions & 3 deletions Vonage.Test.Unit/Request/CredentialsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
using System.Collections.Generic;
using System.Text;
using FluentAssertions;
using Microsoft.Extensions.Configuration;
using Vonage.Common.Failures;
using Vonage.Common.Test.Extensions;
using Vonage.Cryptography;
using Vonage.Common.Test.TestHelpers;
using Vonage.Request;
using Xunit;

Expand Down Expand Up @@ -85,6 +84,6 @@ public void GetPreferredAuthenticationType_ReturnsFailure_GivenInformationIsMiss
private static Credentials BuildBasicCredentials() => Credentials.FromApiKeyAndSecret("apiKey", "apiSecret");

private static Credentials BuildBearerCredentials() => Credentials.FromAppIdAndPrivateKey("appId",
Environment.GetEnvironmentVariable("Vonage.Test.RsaPrivateKey"));
TokenHelper.GetKey());
}
}
5 changes: 3 additions & 2 deletions Vonage.Test.Unit/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Moq;
using Moq.Protected;
using Vonage.Common;
using Vonage.Common.Test.TestHelpers;
using Vonage.Request;
using Xunit;

Expand All @@ -31,7 +32,7 @@ public class TestBase
"afed99d2-ae38-487c-bb5a-fe2518febd44";

protected readonly string PrivateKey = Environment.GetEnvironmentVariable("PRIVATE_KEY") ??
Environment.GetEnvironmentVariable("Vonage.Test.RsaPrivateKey");
TokenHelper.GetKey();

protected TestBase() => this.configuration = new Configuration();

Expand All @@ -46,7 +47,7 @@ public class TestBase

protected VonageClient BuildVonageClient(Credentials credentials) =>
new VonageClient(credentials, this.configuration, new TimeProvider());

protected VonageClient BuildVonageClient(Credentials credentials, ITimeProvider timeProvider) =>
new VonageClient(credentials, this.configuration, timeProvider);

Expand Down
3 changes: 2 additions & 1 deletion Vonage.Test.Unit/TestHelpers/E2EHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Vonage.Common;
using Vonage.Common.Test.TestHelpers;
using Vonage.Request;
using WireMock.Server;

Expand Down Expand Up @@ -39,6 +40,6 @@ public static E2EHelper WithBearerCredentials(string appSettingsKey) =>

private static Credentials CreateBearerCredentials() => Credentials.FromAppIdAndPrivateKey(
Guid.NewGuid().ToString(),
Environment.GetEnvironmentVariable("Vonage.Test.RsaPrivateKey"));
TokenHelper.GetKey());
}
}

0 comments on commit e080884

Please sign in to comment.