-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement SimSwap date retrieval
- Loading branch information
Showing
16 changed files
with
280 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
Vonage.Test/SimSwap/GetSwapDate/Data/ShouldDeserializeAccessToken-response.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"access_token": "ABCDEFG", | ||
"token_type": "Bearer", | ||
"expires_in": 3600 | ||
} |
5 changes: 5 additions & 0 deletions
5
Vonage.Test/SimSwap/GetSwapDate/Data/ShouldDeserializeAuthorize-response.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"auth_req_id": "123456789", | ||
"expires_in": "120", | ||
"interval": "2" | ||
} |
3 changes: 3 additions & 0 deletions
3
Vonage.Test/SimSwap/GetSwapDate/Data/ShouldDeserializeGetSwapDate-response.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"latestSimChange": "2019-08-24T14:15:22Z" | ||
} |
3 changes: 3 additions & 0 deletions
3
Vonage.Test/SimSwap/GetSwapDate/Data/ShouldSerialize-request.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"phoneNumber": "346661113334" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using Vonage.SimSwap.GetSwapDate; | ||
using Vonage.Test.Common.Extensions; | ||
using WireMock.ResponseBuilders; | ||
using Xunit; | ||
|
||
namespace Vonage.Test.SimSwap.GetSwapDate; | ||
|
||
[Trait("Category", "E2E")] | ||
public class E2ETest : E2EBase | ||
{ | ||
public E2ETest() : base(typeof(E2ETest).Namespace) | ||
{ | ||
} | ||
|
||
[Fact] | ||
public async Task GetSwapDateAsync() | ||
{ | ||
this.SetupAuthorization(); | ||
this.SetupToken(); | ||
this.SetupSimSwap(nameof(SerializationTest.ShouldSerialize)); | ||
await this.Helper.VonageClient.SimSwapClient | ||
.GetSwapDateAsync(GetSwapDateRequest.Parse("346661113334")) | ||
.Should() | ||
.BeSuccessAsync(DateTimeOffset.Parse("2019-08-24T14:15:22Z")); | ||
} | ||
|
||
private void SetupSimSwap(string expectedOutput) => | ||
this.Helper.Server.Given(WireMock.RequestBuilders.Request.Create() | ||
.WithPath("/camara/sim-swap/v040/retrieve-date") | ||
.WithHeader("Authorization", "Bearer ABCDEFG") | ||
.WithBody(this.Serialization.GetRequestJson(expectedOutput)) | ||
.UsingPost()) | ||
.RespondWith(Response.Create().WithStatusCode(HttpStatusCode.OK) | ||
.WithBody(this.Serialization.GetResponseJson(nameof(SerializationTest.ShouldDeserializeGetSwapDate)))); | ||
|
||
private void SetupToken() => | ||
this.Helper.Server.Given(WireMock.RequestBuilders.Request.Create() | ||
.WithPath("/oauth2/token") | ||
.WithHeader("Authorization", this.Helper.ExpectedAuthorizationHeaderValue) | ||
.WithBody("auth_req_id=123456789&grant_type=urn:openid:params:grant-type:ciba") | ||
.UsingPost()) | ||
.RespondWith(Response.Create().WithStatusCode(HttpStatusCode.OK) | ||
.WithBody(this.Serialization.GetResponseJson(nameof(SerializationTest.ShouldDeserializeAccessToken)))); | ||
|
||
private void SetupAuthorization() => | ||
this.Helper.Server.Given(WireMock.RequestBuilders.Request.Create() | ||
.WithPath("/oauth2/bc-authorize") | ||
.WithHeader("Authorization", this.Helper.ExpectedAuthorizationHeaderValue) | ||
.WithBody( | ||
"login_hint=tel:%2B346661113334&scope=openid+dpv%3AFraudPreventionAndDetection%23retrieve-sim-swap-date") | ||
.UsingPost()) | ||
.RespondWith(Response.Create().WithStatusCode(HttpStatusCode.OK) | ||
.WithBody(this.Serialization.GetResponseJson(nameof(SerializationTest.ShouldDeserializeAuthorize)))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using Vonage.Common.Failures; | ||
using Vonage.SimSwap.GetSwapDate; | ||
using Vonage.Test.Common.Extensions; | ||
using Xunit; | ||
|
||
namespace Vonage.Test.SimSwap.GetSwapDate; | ||
|
||
[Trait("Category", "Request")] | ||
public class RequestTest | ||
{ | ||
[Theory] | ||
[InlineData("")] | ||
[InlineData(" ")] | ||
[InlineData(null)] | ||
public void Parse_ShouldReturnFailure_GivenNumberIsNullOrWhitespace(string value) => | ||
GetSwapDateRequest.Parse(value).Should() | ||
.BeFailure(ResultFailure.FromErrorMessage("Number cannot be null or whitespace.")); | ||
|
||
[Fact] | ||
public void Parse_ShouldReturnFailure_GivenNumberContainsNonDigits() => | ||
GetSwapDateRequest.Parse("1234567abc123").Should() | ||
.BeFailure(ResultFailure.FromErrorMessage("Number can only contain digits.")); | ||
|
||
[Fact] | ||
public void Parse_ShouldReturnFailure_GivenNumberLengthIsHigherThan7() => | ||
GetSwapDateRequest.Parse("123456").Should() | ||
.BeFailure(ResultFailure.FromErrorMessage("Number length cannot be lower than 7.")); | ||
|
||
[Fact] | ||
public void Parse_ShouldReturnFailure_GivenNumberLengthIsLowerThan15() => | ||
GetSwapDateRequest.Parse("1234567890123456").Should() | ||
.BeFailure(ResultFailure.FromErrorMessage("Number length cannot be higher than 15.")); | ||
|
||
[Theory] | ||
[InlineData("1234567", "1234567")] | ||
[InlineData("123456789012345", "123456789012345")] | ||
[InlineData("+1234567890", "1234567890")] | ||
[InlineData("+123456789012345", "123456789012345")] | ||
[InlineData("+++1234567890", "1234567890")] | ||
public void Parse_ShouldReturnSuccess(string value, string expected) => | ||
GetSwapDateRequest.Parse(value) | ||
.Map(request => request.PhoneNumber.Number) | ||
.Should() | ||
.BeSuccess(expected); | ||
|
||
[Fact] | ||
public void GetEndpointPath_ShouldReturnApiEndpoint() => | ||
GetSwapDateRequest.Parse("123456789") | ||
.Map(request => request.GetEndpointPath()) | ||
.Should() | ||
.BeSuccess("camara/sim-swap/v040/retrieve-date"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using Vonage.Serialization; | ||
using Vonage.SimSwap.Authenticate; | ||
using Vonage.SimSwap.GetSwapDate; | ||
using Vonage.Test.Common; | ||
using Vonage.Test.Common.Extensions; | ||
using Xunit; | ||
|
||
namespace Vonage.Test.SimSwap.GetSwapDate; | ||
|
||
[Trait("Category", "Serialization")] | ||
public class SerializationTest | ||
{ | ||
private readonly SerializationTestHelper helper = new SerializationTestHelper( | ||
typeof(SerializationTest).Namespace, | ||
JsonSerializerBuilder.BuildWithSnakeCase()); | ||
|
||
[Fact] | ||
public void ShouldDeserializeAuthorize() => this.helper.Serializer | ||
.DeserializeObject<AuthorizeResponse>(this.helper.GetResponseJson()) | ||
.Should() | ||
.BeSuccess(GetExpectedAuthorizeResponse()); | ||
|
||
[Fact] | ||
public void ShouldDeserializeAccessToken() => this.helper.Serializer | ||
.DeserializeObject<GetTokenResponse>(this.helper.GetResponseJson()) | ||
.Should() | ||
.BeSuccess(GetExpectedTokenResponse()); | ||
|
||
[Fact] | ||
public void ShouldDeserializeGetSwapDate() => this.helper.Serializer | ||
.DeserializeObject<GetSwapDateResponse>(this.helper.GetResponseJson()) | ||
.Should() | ||
.BeSuccess(GetExpectedResponse()); | ||
|
||
[Fact] | ||
public void ShouldSerialize() => | ||
GetSwapDateRequest.Parse("346661113334") | ||
.GetStringContent() | ||
.Should() | ||
.BeSuccess(this.helper.GetRequestJson()); | ||
|
||
private static AuthorizeResponse GetExpectedAuthorizeResponse() => new AuthorizeResponse("123456789", "120", "2"); | ||
|
||
private static GetTokenResponse GetExpectedTokenResponse() => new GetTokenResponse("ABCDEFG", "Bearer", 3600); | ||
|
||
private static GetSwapDateResponse GetExpectedResponse() => | ||
new GetSwapDateResponse(DateTimeOffset.Parse("2019-08-24T14:15:22Z")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Text.Json.Serialization; | ||
using Vonage.Common; | ||
using Vonage.Common.Client; | ||
using Vonage.Common.Monads; | ||
using Vonage.Common.Serialization; | ||
using Vonage.Serialization; | ||
using Vonage.SimSwap.Authenticate; | ||
|
||
namespace Vonage.SimSwap.GetSwapDate; | ||
|
||
/// <summary> | ||
/// Represents a request to retrieve a SIM swap date. | ||
/// </summary> | ||
public readonly struct GetSwapDateRequest : IVonageRequest | ||
{ | ||
/// <inheritdoc /> | ||
public HttpRequestMessage BuildRequestMessage() => VonageRequestBuilder | ||
.Initialize(HttpMethod.Post, this.GetEndpointPath()) | ||
.WithContent(this.GetRequestContent()) | ||
.Build(); | ||
|
||
private StringContent GetRequestContent() => | ||
new StringContent(JsonSerializerBuilder.BuildWithSnakeCase().SerializeObject(this), Encoding.UTF8, | ||
"application/json"); | ||
|
||
/// <inheritdoc /> | ||
public string GetEndpointPath() => "camara/sim-swap/v040/retrieve-date"; | ||
|
||
/// <summary> | ||
/// Subscriber number in E.164 format (starting with country code). Optionally prefixed with '+'. | ||
/// </summary> | ||
[JsonConverter(typeof(PhoneNumberJsonConverter))] | ||
[JsonPropertyName("phoneNumber")] | ||
public PhoneNumber PhoneNumber { get; internal init; } | ||
|
||
private static string Scope => "dpv:FraudPreventionAndDetection#retrieve-sim-swap-date"; | ||
|
||
internal Result<AuthenticateRequest> BuildAuthenticationRequest() => | ||
AuthenticateRequest.Parse(this.PhoneNumber.NumberWithInternationalIndicator, Scope); | ||
|
||
/// <summary> | ||
/// Parses the input into an GetSwapDateRequest. | ||
/// </summary> | ||
/// <param name="number">The phone number.</param> | ||
/// <returns>Success if the input matches all requirements. Failure otherwise.</returns> | ||
public static Result<GetSwapDateRequest> Parse(string number) => | ||
PhoneNumber.Parse(number).Map(phoneNumber => new GetSwapDateRequest | ||
{ | ||
PhoneNumber = phoneNumber, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using System; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Vonage.SimSwap.GetSwapDate; | ||
|
||
internal record GetSwapDateResponse( | ||
[property: JsonPropertyName("latestSimChange")] | ||
DateTimeOffset LatestSimChange); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters