Skip to content

Commit

Permalink
adding error handling to verify, adding Verify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
slorello89 committed Apr 20, 2020
1 parent a76fcc3 commit 9a23461
Show file tree
Hide file tree
Showing 9 changed files with 287 additions and 33 deletions.
230 changes: 230 additions & 0 deletions Nexmo.Api.Test.Unit/VerifyTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Nexmo.Api.Verify;
using Xunit;
namespace Nexmo.Api.Test.Unit
{
public class VerifyTest : TestBase
{
[Theory]
[InlineData(true, true)]
[InlineData(false, false)]
public void RequestVerification(bool passCreds, bool kitchenSink)
{
var expectedResponse = @"{
""request_id"": ""abcdef0123456789abcdef0123456789"",
""status"": ""0""
}";
var expectedUri = $"{ApiUrl}/verify/json";

string expectedRequestContent;
VerifyRequest request = new VerifyRequest { Number= "447700900000", Brand="Acme Inc"};
if (kitchenSink)
{
expectedRequestContent = $"number=447700900000&country=GB&brand={HttpUtility.UrlEncode("Acme Inc")}&sender_id=ACME&code_length=4&lg=en-us&pin_expiry=240&next_event_wait=60&workflow_id=1&api_key={ApiKey}&api_secret={ApiSecret}&";
request.Country = "GB";
request.SenderId = "ACME";
request.CodeLength = 4;
request.Lg = "en-us";
request.PinExpiry = 240;
request.NextEventWait = 60;
request.WorkflowId = VerifyRequest.Workflow.SMS_TTS_TTS;
}
else
{
expectedRequestContent = $"number=447700900000&brand={HttpUtility.UrlEncode("Acme Inc")}&api_key={ApiKey}&api_secret={ApiSecret}&";
}
Setup(expectedUri, expectedResponse, expectedRequestContent);
var creds = Request.Credentials.FromApiKeyAndSecret(ApiKey, ApiSecret);
var client = new NexmoClient(creds);
VerifyResponse response;
if (passCreds)
{
response = client.VerifyClient.VerifyRequest(request, creds);
}
else
{
response = client.VerifyClient.VerifyRequest(request);
}

Assert.Equal("abcdef0123456789abcdef0123456789", response.RequestId);
Assert.Equal("0", response.Status);
}

[Theory]
[InlineData(true, true)]
[InlineData(false, false)]
public void TestCheckVerification(bool passCreds, bool kitchenSink)
{
var expectedResponse = @"{
""request_id"": ""abcdef0123456789abcdef0123456789"",
""event_id"": ""0A00000012345678"",
""status"": ""0"",
""price"": ""0.10000000"",
""currency"": ""EUR"",
""estimated_price_messages_sent"": ""0.03330000""
}";
var expectedUri = $"{ApiUrl}/verify/json";

string expectedRequestContent;
VerifyCheckRequest request = new VerifyCheckRequest { Code = "1234", RequestId = "abcdef0123456789abcdef0123456789" };
if (kitchenSink)
{
expectedRequestContent = $"request_id=abcdef0123456789abcdef0123456789&code=1234&ip_address={HttpUtility.UrlEncode("123.0.0.255")}&api_key={ApiKey}&api_secret={ApiSecret}&";
request.IpAddress = "123.0.0.255";
}
else
{
expectedRequestContent = $"request_id=abcdef0123456789abcdef0123456789&code=1234&api_key={ApiKey}&api_secret={ApiSecret}&";
}
Setup(expectedUri, expectedResponse, expectedRequestContent);
var creds = Request.Credentials.FromApiKeyAndSecret(ApiKey, ApiSecret);
var client = new NexmoClient(creds);
VerifyCheckResponse response;
if (passCreds)
{
response = client.VerifyClient.VerifyCheck(request, creds);
}
else
{
response = client.VerifyClient.VerifyCheck(request);
}
Assert.Equal("0.10000000", response.Price);
Assert.Equal("0.03330000", response.EstimatedPriceMessagesSent);
Assert.Equal("EUR", response.Currency);
Assert.Equal("0A00000012345678", response.EventId);
Assert.Equal("abcdef0123456789abcdef0123456789", response.RequestId);
Assert.Equal("0", response.Status);
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public void TestVerifySearch(bool passCreds)
{
var expectedResponse = @"{
""request_id"": ""abcdef0123456789abcdef0123456789"",
""account_id"": ""abcdef01"",
""status"": ""IN PROGRESS"",
""number"": ""447700900000"",
""price"": ""0.10000000"",
""currency"": ""EUR"",
""sender_id"": ""mySenderId"",
""date_submitted"": ""2020-01-01 12:00:00"",
""date_finalized"": ""2020-01-01 12:00:00"",
""first_event_date"": ""2020-01-01 12:00:00"",
""last_event_date"": ""2020-01-01 12:00:00"",
""checks"": [
{
""date_received"": ""2020-01-01 12:00:00"",
""code"": ""987654"",
""status"": ""abc123"",
""ip_address"": ""123.0.0.255""
}
],
""events"": [
{
""type"": ""abc123"",
""id"": ""abc123""
}
],
""estimated_price_messages_sent"": ""0.03330000""
}";
var expectedUri = $"{ApiUrl}/verify/search/json?request_id=abcdef0123456789abcdef0123456789&api_key={ApiKey}&api_secret={ApiSecret}&";
Setup(expectedUri, expectedResponse);
var request = new VerifySearchRequest { RequestId = "abcdef0123456789abcdef0123456789" };
var creds = Request.Credentials.FromApiKeyAndSecret(ApiKey, ApiSecret);
var client = new NexmoClient(creds);
VerifySearchResponse response;
if (passCreds)
{
response = client.VerifyClient.VerifySearch(request, creds);
}
else
{
response = client.VerifyClient.VerifySearch(request);
}

var req = response;
Assert.Equal("abcdef0123456789abcdef0123456789", req.RequestId);
Assert.Equal("abcdef01", req.AccountId);
Assert.Equal("IN PROGRESS", req.Status);
Assert.Equal("447700900000", req.Number);
Assert.Equal("0.10000000", req.Price);
Assert.Equal("EUR", req.Currency);
Assert.Equal("mySenderId", req.SenderId);
Assert.Equal("2020-01-01 12:00:00", req.DateSubmitted);
Assert.Equal("2020-01-01 12:00:00", req.DateFinalized);
Assert.Equal("2020-01-01 12:00:00", req.FirstEventDate);
Assert.Equal("2020-01-01 12:00:00", req.LastEventDate);
Assert.Equal("2020-01-01 12:00:00", req.Checks[0].DateReceived);
Assert.Equal("987654", req.Checks[0].Code);
Assert.Equal("abc123", req.Checks[0].Status);
Assert.Equal("123.0.0.255", req.Checks[0].IpAddress);
Assert.Equal("abc123", req.Events[0].Type);
Assert.Equal("abc123", req.Events[0].Id);
Assert.Equal("0.03330000", req.EstimatedPriceMessagesSent);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void TestControlVerify(bool passCreds)
{
var expectedResponse = @"{
""status"": ""0"",
""command"": ""cancel""
}";

var expectedUri = $"{ApiUrl}/verify/control/json";
var requestContent = $"request_id=abcdef0123456789abcdef0123456789&cmd=cancel&api_key={ApiKey}&api_secret={ApiSecret}&";
Setup(expectedUri, expectedResponse, requestContent);

var request = new VerifyControlRequest { Cmd = "cancel", RequestId = "abcdef0123456789abcdef0123456789" };
var creds = Request.Credentials.FromApiKeyAndSecret(ApiKey, ApiSecret);
var client = new NexmoClient(creds);
VerifyControlResponse response;
if (passCreds)
{
response = client.VerifyClient.VerifyControl(request, creds);
}
else
{
response = client.VerifyClient.VerifyControl(request, creds);
}
Assert.Equal("0", response.Status);
Assert.Equal("cancel", response.Command);
}

[Fact]
public void TestControlVerifyInvalidCredentials()
{
var expectedResponse = @"{
""status"": ""4"",
""error_text"": ""invalid credentials""
}";

var expectedUri = $"{ApiUrl}/verify/control/json";
var requestContent = $"request_id=abcdef0123456789abcdef0123456789&cmd=cancel&api_key={ApiKey}&api_secret={ApiSecret}&";
Setup(expectedUri, expectedResponse, requestContent);

var request = new VerifyControlRequest { Cmd = "cancel", RequestId = "abcdef0123456789abcdef0123456789" };
var creds = Request.Credentials.FromApiKeyAndSecret(ApiKey, ApiSecret);
var client = new NexmoClient(creds);
try
{
var response = client.VerifyClient.VerifyControl(request, creds);
Assert.True(false, "Automatically failing because exception wasn't thrown");
}
catch(NexmoVerifyResponseException ex)
{
Assert.Equal("4", ex.Response.Status);
Assert.Equal("invalid credentials", ex.Response.ErrorText);
}
}
}
}
15 changes: 15 additions & 0 deletions Nexmo.Api/Verify/NexmoVerifyResponseException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nexmo.Api.Verify
{
public class NexmoVerifyResponseException : NexmoException
{
public NexmoVerifyResponseException(string message) : base(message) { }

public VerifyResponseBase Response {get;set;}
}
}
10 changes: 2 additions & 8 deletions Nexmo.Api/Verify/VerifyCheckResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,21 @@

namespace Nexmo.Api.Verify
{
public class VerifyCheckResponse
public class VerifyCheckResponse : VerifyResponseBase
{
[JsonProperty("request_id")]
public string RequestId { get; set; }

[JsonProperty("event_id")]
public string EventId { get; set; }

[JsonProperty("status")]
public string Status { get; set; }

[JsonProperty("price")]
public string Price { get; set; }

[JsonProperty("currency")]
public string Currency { get; set; }

[JsonProperty("estimated_price_messages_sent")]
public string EstimatedPriceMessagesSent { get; set; }

[JsonProperty("error_text")]
public string ErrorText { get; set; }
public string EstimatedPriceMessagesSent { get; set; }
}
}
23 changes: 17 additions & 6 deletions Nexmo.Api/Verify/VerifyClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,24 @@ public VerifyClient(Credentials creds)
}
public VerifyResponse VerifyRequest(VerifyRequest request, Credentials creds = null)
{
return ApiRequest.DoGetRequestWithQueryParameters<VerifyResponse>(
var response = ApiRequest.DoPostRequestUrlContentFromObject<VerifyResponse>(
ApiRequest.GetBaseUri(ApiRequest.UriType.Api, "/verify/json"),
ApiRequest.AuthType.Query,
request,
creds ?? Credentials
);
ValidateVerifyResponse(response);
return response;
}

public VerifyCheckResponse VerifyCheck(VerifyCheckRequest request, Credentials creds = null)
{
return ApiRequest.DoGetRequestWithQueryParameters<VerifyCheckResponse>(
var response = ApiRequest.DoPostRequestUrlContentFromObject<VerifyCheckResponse>(
ApiRequest.GetBaseUri(ApiRequest.UriType.Api, "/verify/check/json"),
ApiRequest.AuthType.Query,
request,
creds ?? Credentials
);
ValidateVerifyResponse(response);
return response;
}

public VerifySearchResponse VerifySearch(VerifySearchRequest request, Credentials creds = null)
Expand All @@ -42,12 +44,21 @@ public VerifySearchResponse VerifySearch(VerifySearchRequest request, Credential

public VerifyControlResponse VerifyControl(VerifyControlRequest request, Credentials creds = null)
{
return ApiRequest.DoGetRequestWithQueryParameters<VerifyControlResponse>(
var response = ApiRequest.DoPostRequestUrlContentFromObject<VerifyControlResponse>(
ApiRequest.GetBaseUri(ApiRequest.UriType.Api, "/verify/control/json"),
ApiRequest.AuthType.Query,
request,
creds ?? Credentials
);
ValidateVerifyResponse(response);
return response;
}

public void ValidateVerifyResponse(VerifyResponseBase response)
{
if (response.Status != "0")
{
throw new NexmoVerifyResponseException($"Verify Request Failed with status: {response.Status} and Error Text: {response.ErrorText}") { Response = response };
}
}
}
}
8 changes: 1 addition & 7 deletions Nexmo.Api/Verify/VerifyControlResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@

namespace Nexmo.Api.Verify
{
public class VerifyControlResponse
public class VerifyControlResponse : VerifyResponseBase
{
[JsonProperty("status")]
public string Status { get; set; }

[JsonProperty("error_text")]
public string ErrorText { get; set; }

[JsonProperty("command")]
public string Command { get; set; }
}
Expand Down
8 changes: 1 addition & 7 deletions Nexmo.Api/Verify/VerifyResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@

namespace Nexmo.Api.Verify
{
public class VerifyResponse
public class VerifyResponse : VerifyResponseBase
{
[JsonProperty("request_id")]
public string RequestId { get; set; }

[JsonProperty("status")]
public string Status { get; set; }

[JsonProperty("error_text")]
public string ErrorText { get; set; }
}
}
18 changes: 18 additions & 0 deletions Nexmo.Api/Verify/VerifyResponseBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nexmo.Api.Verify
{
public abstract class VerifyResponseBase
{

[JsonProperty("status")]
public string Status { get; set; }
[JsonProperty("error_text")]
public string ErrorText { get; set; }
}
}
5 changes: 1 addition & 4 deletions Nexmo.Api/Verify/VerifySearchRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,5 @@ public class VerifySearchRequest
{
[JsonProperty("request_id")]
public string RequestId { get; set; }

[JsonProperty("request_ids")]
public string[] RequestIds { get; set; }
}
}
}
Loading

0 comments on commit 9a23461

Please # to comment.