Skip to content

Commit

Permalink
Moving to use bool in code instead of strings
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-lethargic committed May 17, 2022
1 parent 2e56d85 commit f151ea4
Show file tree
Hide file tree
Showing 16 changed files with 129 additions and 46 deletions.
14 changes: 7 additions & 7 deletions Vonage.Test.Unit/ConversionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ public void SmsConversion(bool passCreds)
var expectedContent = "message-id=00A0B0C0&delivered=true&timestamp=2020-01-01+12%3A00%3A00&api_key=testkey&api_secret=testSecret&";
var expectedResponse = "";
Setup(expectedUri, expectedResponse, expectedContent);
var request = new Conversions.ConversionRequest { Delivered = "true", MessageId = "00A0B0C0", TimeStamp = "2020-01-01 12:00:00" };
var creds = Request.Credentials.FromApiKeyAndSecret(ApiKey, ApiSecret);
var client = new VonageClient(creds);
var request = new Conversions.ConversionRequest { Delivered = true, MessageId = "00A0B0C0", TimeStamp = "2020-01-01 12:00:00" };
var credentials = Request.Credentials.FromApiKeyAndSecret(ApiKey, ApiSecret);
var client = new VonageClient(credentials);
bool response;
if (passCreds)
{
response = client.ConversionClient.SmsConversion(request, creds);
response = client.ConversionClient.SmsConversion(request, credentials);
}
else
{
Expand All @@ -42,7 +42,7 @@ public void VoiceConversion(bool passCreds)
var expectedContent = "message-id=00A0B0C0&delivered=true&timestamp=2020-01-01+12%3A00%3A00&api_key=testkey&api_secret=testSecret&";
var expectedResponse = "";
Setup(expectedUri, expectedResponse, expectedContent);
var request = new Conversions.ConversionRequest { Delivered = "true", MessageId = "00A0B0C0", TimeStamp = "2020-01-01 12:00:00" };
var request = new Conversions.ConversionRequest { Delivered = true, MessageId = "00A0B0C0", TimeStamp = "2020-01-01 12:00:00" };
var creds = Request.Credentials.FromApiKeyAndSecret(ApiKey, ApiSecret);
var client = new VonageClient(creds);
bool response;
Expand All @@ -66,7 +66,7 @@ public async void SmsConversionAsync(bool passCreds)
var expectedContent = "message-id=00A0B0C0&delivered=true&timestamp=2020-01-01+12%3A00%3A00&api_key=testkey&api_secret=testSecret&";
var expectedResponse = "";
Setup(expectedUri, expectedResponse, expectedContent);
var request = new Conversions.ConversionRequest { Delivered = "true", MessageId = "00A0B0C0", TimeStamp = "2020-01-01 12:00:00" };
var request = new Conversions.ConversionRequest { Delivered = true, MessageId = "00A0B0C0", TimeStamp = "2020-01-01 12:00:00" };
var creds = Request.Credentials.FromApiKeyAndSecret(ApiKey, ApiSecret);
var client = new VonageClient(creds);
bool response;
Expand All @@ -90,7 +90,7 @@ public async void VoiceConversionAsync(bool passCreds)
var expectedContent = "message-id=00A0B0C0&delivered=true&timestamp=2020-01-01+12%3A00%3A00&api_key=testkey&api_secret=testSecret&";
var expectedResponse = "";
Setup(expectedUri, expectedResponse, expectedContent);
var request = new Conversions.ConversionRequest { Delivered = "true", MessageId = "00A0B0C0", TimeStamp = "2020-01-01 12:00:00" };
var request = new Conversions.ConversionRequest { Delivered = true, MessageId = "00A0B0C0", TimeStamp = "2020-01-01 12:00:00" };
var creds = Request.Credentials.FromApiKeyAndSecret(ApiKey, ApiSecret);
var client = new VonageClient(creds);
bool response;
Expand Down
3 changes: 0 additions & 3 deletions Vonage.Test.Unit/Data/NccoTests/TestConversation-request.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
"musicOnHoldUrl": [
"https://example.com/music.mp3"
],
"startOnEnter": "true",
"endOnExit": "false",
"record": "true",
"canSpeak": [
"6a4d6af0-55a6-4667-be90-8614e4c8e83c"
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"action": "conversation",
"name": "vonage-conference-standard",
"musicOnHoldUrl": [
"https://example.com/music.mp3"
],
"startOnEnter": "true",
"endOnExit": "true",
"record": "true",
"canSpeak": [
"6a4d6af0-55a6-4667-be90-8614e4c8e83c"
],
"canHear": [
"6a4d6af0-55a6-4667-be90-8614e4c8e83c"
]
}
]
30 changes: 15 additions & 15 deletions Vonage.Test.Unit/MessagingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void SendSmsWithAllPropertiesSet(bool passCreds)
Text = "Hello World!",
Title = "welcome",
Ttl = 900000,
Type = Messaging.SmsType.text,
Type = SmsType.text,
Udh = "06050415811581",
Validity = "300000",
Vcal = "none",
Expand Down Expand Up @@ -137,7 +137,7 @@ public void SendSmsTypicalUsage()
var expectedRequestContent = $"from=AcmeInc&to=447700900000&text={HttpUtility.UrlEncode("Hello World!")}&api_key={ApiKey}&api_secret={ApiSecret}&";
Setup(expectedUri, expectedResponse, expectedRequestContent);
var client = new VonageClient(Request.Credentials.FromApiKeyAndSecret(ApiKey, ApiSecret));
var response = client.SmsClient.SendAnSms(new Messaging.SendSmsRequest { From = "AcmeInc", To = "447700900000", Text = "Hello World!" });
var response = client.SmsClient.SendAnSms(new SendSmsRequest { From = "AcmeInc", To = "447700900000", Text = "Hello World!" });
Assert.Equal("1", response.MessageCount);
Assert.Equal("447700900000", response.Messages[0].To);
Assert.Equal("0A0000000123ABCD1", response.Messages[0].MessageId);
Expand Down Expand Up @@ -220,7 +220,7 @@ public void SendSmsUnicode()
var expectedRequestContent = $"from=AcmeInc&to=447700900000&text={HttpUtility.UrlEncode("こんにちは世界")}&api_key={ApiKey}&api_secret={ApiSecret}&";
Setup(expectedUri, expectedResponse, expectedRequestContent);
var client = new VonageClient(Request.Credentials.FromApiKeyAndSecret(ApiKey, ApiSecret));
var response = client.SmsClient.SendAnSms(new Messaging.SendSmsRequest { From = "AcmeInc", To = "447700900000", Text = "こんにちは世界" });
var response = client.SmsClient.SendAnSms(new SendSmsRequest { From = "AcmeInc", To = "447700900000", Text = "こんにちは世界" });
Assert.Equal("1", response.MessageCount);
Assert.Equal("447700900000", response.Messages[0].To);
Assert.Equal("0A0000000123ABCD1", response.Messages[0].MessageId);
Expand Down Expand Up @@ -301,13 +301,13 @@ public void TestDlrStruct()
""sig"": ""1A20E4E2069B609FDA6CECA9DE18D5CAFE99720DDB628BD6BE8B19942A336E1C"",
""client-ref"": ""steve""
}";
var dlr = JsonConvert.DeserializeObject<Messaging.DeliveryReceipt>(jsonFromNDP);
var dlr = JsonConvert.DeserializeObject<DeliveryReceipt>(jsonFromNDP);
Assert.Equal("447700900000", dlr.Msisdn);
Assert.Equal("AcmeInc", dlr.To);
Assert.Equal("12345", dlr.NetworkCode);
Assert.Equal("0A0000001234567B", dlr.MessageId);
Assert.Equal("0.03330000", dlr.Price);
Assert.Equal(Messaging.DlrStatus.delivered, dlr.Status);
Assert.Equal(DlrStatus.delivered, dlr.Status);
Assert.Equal("2001011400", dlr.Scts);
Assert.Equal("0", dlr.ErrorCode);
Assert.Equal("abcd1234", dlr.ApiKey);
Expand Down Expand Up @@ -336,13 +336,13 @@ public void TestDlrStructNoStatus()
""sig"": ""1A20E4E2069B609FDA6CECA9DE18D5CAFE99720DDB628BD6BE8B19942A336E1C"",
""client-ref"": ""steve""
}";
var dlr = JsonConvert.DeserializeObject<Messaging.DeliveryReceipt>(jsonFromNDP);
var dlr = JsonConvert.DeserializeObject<DeliveryReceipt>(jsonFromNDP);
Assert.Equal("447700900000", dlr.Msisdn);
Assert.Equal("AcmeInc", dlr.To);
Assert.Equal("12345", dlr.NetworkCode);
Assert.Equal("0A0000001234567B", dlr.MessageId);
Assert.Equal("0.03330000", dlr.Price);
Assert.Equal(Messaging.DlrStatus.unknown, dlr.Status);
Assert.Equal(DlrStatus.unknown, dlr.Status);
Assert.Equal("2001011400", dlr.Scts);
Assert.Equal("0", dlr.ErrorCode);
Assert.Equal("abcd1234", dlr.ApiKey);
Expand Down Expand Up @@ -374,7 +374,7 @@ public void TestInboundSmsStruct()
""data"": ""abc123"",
""udh"": ""abc123""
}";
var inboundSms = JsonConvert.DeserializeObject<Messaging.InboundSms>(jsonFromNdp);
var inboundSms = JsonConvert.DeserializeObject<InboundSms>(jsonFromNdp);
Assert.Equal("abcd1234", inboundSms.ApiKey);
Assert.Equal("447700900001", inboundSms.Msisdn);
Assert.Equal("447700900000", inboundSms.To);
Expand All @@ -385,7 +385,7 @@ public void TestInboundSmsStruct()
Assert.Equal("2020-01-01T12:00:00.000+00:00", inboundSms.MessageTimestamp);
Assert.Equal("1578787200", inboundSms.Timestamp);
Assert.Equal("aaaaaaaa-bbbb-cccc-dddd-0123456789ab", inboundSms.Nonce);
Assert.Equal("true", inboundSms.Concat);
Assert.True(inboundSms.Concat);
Assert.Equal("1", inboundSms.ConcatRef);
Assert.Equal("3", inboundSms.ConcatTotal);
Assert.Equal("2", inboundSms.ConcatPart);
Expand All @@ -407,14 +407,14 @@ public void TestValidateSignatureMd5()
MessageTimestamp = "2020-01-01T12:00:00.000+00:00",
Timestamp = "1578787200",
Nonce = "aaaaaaaa-bbbb-cccc-dddd-0123456789ab",
Concat = "true",
Concat = true,
ConcatRef = "3",
};
var TestSigningSecret = "Y6dI3wtDP8myVH5tnDoIaTxEvAJhgDVCczBa1mHniEqsdlnnebg";
var json = JsonConvert.SerializeObject(inboundSmsShell, VonageSerialization.SerializerSettings);
var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);

inboundSmsShell.Sig = Cryptography.SmsSignatureGenerator.GenerateSignature(Messaging.InboundSms.ConstructSignatureStringFromDictionary(dict), TestSigningSecret, Cryptography.SmsSignatureGenerator.Method.md5);
inboundSmsShell.Sig = Cryptography.SmsSignatureGenerator.GenerateSignature(InboundSms.ConstructSignatureStringFromDictionary(dict), TestSigningSecret, Cryptography.SmsSignatureGenerator.Method.md5);

Assert.True(inboundSmsShell.ValidateSignature(TestSigningSecret, Cryptography.SmsSignatureGenerator.Method.md5));
}
Expand All @@ -433,7 +433,7 @@ public void TestValidateSignatureMd5Hash()
MessageTimestamp = "2020-01-01T12:00:00.000+00:00",
Timestamp = "1578787200",
Nonce = "aaaaaaaa-bbbb-cccc-dddd-0123456789ab",
Concat = "true",
Concat = true,
ConcatRef = "3",
};
var TestSigningSecret = "17c6ecf583ef7da515bcfc655426970c";
Expand All @@ -457,7 +457,7 @@ public void TestValidateSignatureSha1()
MessageTimestamp = "2020-01-01T12:00:00.000+00:00",
Timestamp = "1578787200",
Nonce = "aaaaaaaa-bbbb-cccc-dddd-0123456789ab",
Concat = "true",
Concat = true,
ConcatRef = "3",
};
var TestSigningSecret = "B462F6EF6C0D161EEA214C5D37FC0E1D31C0BC08";
Expand All @@ -481,7 +481,7 @@ public void TestValidateSignatureSha256()
MessageTimestamp = "2020-01-01T12:00:00.000+00:00",
Timestamp = "1578787200",
Nonce = "aaaaaaaa-bbbb-cccc-dddd-0123456789ab",
Concat = "true",
Concat = true,
ConcatRef = "3",
};
var TestSigningSecret = "D1EA9F0A89C2C62DD89FFE9585E8CD27163DA47458D353EC7084376BADA72817";
Expand All @@ -505,7 +505,7 @@ public void TestValidateSignatureSha512()
MessageTimestamp = "2020-01-01T12:00:00.000+00:00",
Timestamp = "1578787200",
Nonce = "aaaaaaaa-bbbb-cccc-dddd-0123456789ab",
Concat = "true",
Concat = true,
ConcatRef = "3",
};
var TestSigningSecret = "A8E2BB164A894DB7BC7807D7E5A09003B3F25F185D5202F2616EA8D285AD5E8248D50827091B302D07FC967125108339B155938DA45B27C79E45A83CD4914B7C";
Expand Down
31 changes: 25 additions & 6 deletions Vonage.Test.Unit/NccoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void TestRecord(RecordAction.AudioFormat audioFormat)
EndOnSilence = "3",
EndOnKey = "#",
TimeOut = "60",
BeepStart = "true",
BeepStart = true,
EventUrl = new[] {"https://example.com/record", "https://test.com/record"},
EventMethod = "POST"
};
Expand Down Expand Up @@ -54,9 +54,28 @@ public void TestConversation()
{
Name = "vonage-conference-standard",
MusicOnHoldUrl = new[] {"https://example.com/music.mp3"},
StartOnEnter = "true",
EndOnExit = "false",
Record = "true",
StartOnEnter = false,
EndOnExit = false,
Record = false,
CanSpeak = new[] {"6a4d6af0-55a6-4667-be90-8614e4c8e83c"},
CanHear = new[] {"6a4d6af0-55a6-4667-be90-8614e4c8e83c"}
};
var ncco = new Ncco(conversationAction);
var actualJson = JsonConvert.SerializeObject(ncco, Serialization.VonageSerialization.SerializerSettings);
Assert.Equal(expectedJson, actualJson);
}

[Fact]
public void TestConversationAllTrue()
{
var expectedJson = GetRequestJson();
var conversationAction = new ConversationAction
{
Name = "vonage-conference-standard",
MusicOnHoldUrl = new[] {"https://example.com/music.mp3"},
StartOnEnter = true,
EndOnExit = true,
Record = true,
CanSpeak = new[] {"6a4d6af0-55a6-4667-be90-8614e4c8e83c"},
CanHear = new[] {"6a4d6af0-55a6-4667-be90-8614e4c8e83c"}
};
Expand Down Expand Up @@ -105,7 +124,7 @@ public void TestTalk()
var talkAction = new TalkAction
{
Text = "Hello World",
BargeIn = "true",
BargeIn = true,
Loop = "2",
Level = "0",
VoiceName = "kimberly",
Expand Down Expand Up @@ -137,7 +156,7 @@ public void TestStream()
var talkAction = new StreamAction
{
StreamUrl = new[] {"https://acme.com/streams/music.mp3"},
BargeIn = "true",
BargeIn = true,
Loop = "2",
Level = "0",
};
Expand Down
8 changes: 4 additions & 4 deletions Vonage.Test.Unit/ShortCodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ public void ManageOptIn(bool passCredentials)

//ASSERT
Assert.Equal("15559301529", response.Msisdn);
Assert.Equal("true", response.OptIn);
Assert.True(response.OptIn);
Assert.Equal("2014-08-21 17:34:47", response.OptInDate);
Assert.Equal("false", response.OptOut);
Assert.False(response.OptOut);
}

[Theory]
Expand All @@ -157,9 +157,9 @@ public async Task ManageOptInAsync(bool passCredentials)

//ASSERT
Assert.Equal("15559301529", response.Msisdn);
Assert.Equal("true", response.OptIn);
Assert.True(response.OptIn);
Assert.Equal("2014-08-21 17:34:47", response.OptInDate);
Assert.Equal("false", response.OptOut);
Assert.False(response.OptOut);
}

[Theory]
Expand Down
3 changes: 3 additions & 0 deletions Vonage.Test.Unit/Vonage.Test.Unit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@
<None Update="Data\NccoTests\TestVbcEndpoint-request.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Data\NccoTests\TestConversationAllTrue-request.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 3 additions & 1 deletion Vonage/Conversions/ConversionRequest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using Vonage.Serialization;

namespace Vonage.Conversions
{
Expand All @@ -15,7 +16,8 @@ public class ConversionRequest
/// Set to true if your user replied to the message you sent. Otherwise, set to false. Note: for curl, use 0 and 1.
/// </summary>
[JsonProperty("delivered")]
public string Delivered { get; set; }
[JsonConverter(typeof(StringBoolConverter))]
public bool Delivered { get; set; }

/// <summary>
/// When the user completed your call-to-action (e.g. visited your website, installed your app)
Expand Down
3 changes: 2 additions & 1 deletion Vonage/Messaging/InboundSms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public class InboundSms
public string Nonce { get; set; }

[JsonProperty("concat")]
public string Concat { get; set; }
[JsonConverter(typeof(StringBoolConverter))]
public bool Concat { get; set; }

[JsonProperty("concat-ref")]
public string ConcatRef { get; set; }
Expand Down
33 changes: 33 additions & 0 deletions Vonage/Serialization/StringBoolConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using Newtonsoft.Json;

namespace Vonage.Serialization
{
internal class StringBoolConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value == null)
{
writer.WriteNull();
return;
}

if(bool.TryParse(value.ToString(), out var boolValue))
{
writer.WriteValue(boolValue ? "true" : "false");
}
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
JsonSerializer serializer)
{
return serializer.Deserialize<bool>(reader);
}

public override bool CanConvert(Type objectType)
{
return objectType == typeof(bool);
}
}
}
2 changes: 1 addition & 1 deletion Vonage/Serialization/VonageSerialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal static JsonSerializerSettings SerializerSettings
Formatting = Formatting.None,
MissingMemberHandling = MissingMemberHandling.Ignore
};

return settings;
}
}
Expand Down
4 changes: 2 additions & 2 deletions Vonage/ShortCodes/OptInRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ public class OptInRecord
public string Msisdn { get; set; }

[JsonProperty("opt-in")]
public string OptIn { get; set; }
public bool OptIn { get; set; }

[JsonProperty("opt-in-date")]
public string OptInDate { get; set; }

[JsonProperty("opt-out")]
public string OptOut { get; set; }
public bool OptOut { get; set; }

[JsonProperty("opt-out-date")]
public string OptOutDate { get; set; }
Expand Down
Loading

0 comments on commit f151ea4

Please sign in to comment.