Skip to content

Commit

Permalink
refactor: use case enhancement (#430)
Browse files Browse the repository at this point in the history
* Remove unnecessary test over auth

* Add tests for Credentials methods
  • Loading branch information
Tr00d authored Jun 13, 2023
1 parent 7ba8fd1 commit 851ceac
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
2 changes: 0 additions & 2 deletions Vonage.Common.Test/TestHelpers/FakeHttpMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ private void CompareRequests(ReceivedRequest received, ExpectedRequest expected)
received.RequestUri.Should()
.Be(new Uri(this.baseUri, expected.RequestUri));
received.Method.Should().Be(expected.Method);
received.Headers.Authorization.Scheme.Should().Be("Bearer");
received.Headers.Authorization.Parameter.Should().NotBeNullOrWhiteSpace();
received.Content.Should().Be(expected.Content);
}

Expand Down
3 changes: 1 addition & 2 deletions Vonage.Common.Test/UseCaseHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,7 @@ public async Task VerifyReturnsUnitGivenApiResponseIsSuccess(ExpectedRequest exp
public static UseCaseHelper WithSerializer(JsonSerializer serializer) => new(serializer);

private VonageHttpClientConfiguration CreateConfiguration(FakeHttpRequestHandler handler) =>
new(handler.ToHttpClient(), new AuthenticationHeaderValue("Bearer", this.Fixture.Create<string>()),
this.Fixture.Create<string>());
new(handler.ToHttpClient(), new AuthenticationHeaderValue("Anonymous"), this.Fixture.Create<string>());
}

public struct ExpectedRequest
Expand Down
46 changes: 43 additions & 3 deletions Vonage.Test.Unit/Request/CredentialsTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using FluentAssertions;
using Vonage.Common.Failures;
using Vonage.Common.Test.Extensions;
using Vonage.Request;
Expand All @@ -8,6 +11,38 @@ namespace Vonage.Test.Unit.Request
{
public class CredentialsTest
{
[Fact]
public void GetAuthenticationHeader_ReturnsBasicScheme_GivenContainsApiKeyAndApiSecret() =>
BuildBasicCredentials()
.GetAuthenticationHeader()
.Map(header => header.Scheme)
.Should()
.BeSuccess("Basic");

[Fact]
public void GetAuthenticationHeader_ReturnsBearerScheme_GivenContainsApplicationIdAndPrivateKey() =>
BuildBearerCredentials()
.GetAuthenticationHeader()
.Map(header => header.Scheme)
.Should()
.BeSuccess("Bearer");

[Fact]
public void GetAuthenticationHeader_ReturnsEncodedCredentials_GivenContainsApiKeyAndApiSecret() =>
BuildBasicCredentials()
.GetAuthenticationHeader()
.Map(header => header.Parameter)
.Should()
.BeSuccess(Convert.ToBase64String(Encoding.UTF8.GetBytes("apiKey:apiSecret")));

[Fact]
public void GetAuthenticationHeader_ReturnsToken_GivenContainsApplicationIdAndPrivateKey() =>
BuildBearerCredentials()
.GetAuthenticationHeader()
.Map(header => header.Parameter)
.Should()
.BeSuccess(success => success.Should().NotBeNullOrWhiteSpace());

public static IEnumerable<object[]> GetInvalidCredentials()
{
yield return new object[] {Credentials.FromApiKeyAndSecret("", "apiSecret")};
Expand All @@ -26,23 +61,28 @@ public static IEnumerable<object[]> GetInvalidCredentials()

[Fact]
public void GetPreferredAuthenticationType_ReturnsBasic_GivenContainsApiKeyAndApiSecret() =>
Credentials.FromApiKeyAndSecret("apiKey", "apiSecret")
BuildBasicCredentials()
.GetPreferredAuthenticationType()
.Should()
.BeSuccess(AuthType.Basic);

[Fact]
public void GetPreferredAuthenticationType_ReturnsBearer_GivenContainsApplicationIdAndPrivateKey() =>
Credentials.FromAppIdAndPrivateKey("appId", "privateKey")
BuildBearerCredentials()
.GetPreferredAuthenticationType()
.Should()
.BeSuccess(AuthType.Bearer);

[Theory]
[MemberData(nameof(GetInvalidCredentials))]
public void GetPreferredAuthenticationType_ReturnsNone_GivenInformationIsMissing(Credentials credentials) =>
public void GetPreferredAuthenticationType_ReturnsFailure_GivenInformationIsMissing(Credentials credentials) =>
credentials.GetPreferredAuthenticationType()
.Should()
.BeFailure(new AuthenticationFailure());

private static Credentials BuildBasicCredentials() => Credentials.FromApiKeyAndSecret("apiKey", "apiSecret");

private static Credentials BuildBearerCredentials() => Credentials.FromAppIdAndPrivateKey("appId",
Environment.GetEnvironmentVariable("Vonage.Test.RsaPrivateKey"));
}
}

0 comments on commit 851ceac

Please # to comment.