Skip to content

Commit

Permalink
feat: implement request validation for CreateConversation
Browse files Browse the repository at this point in the history
  • Loading branch information
Tr00d committed Dec 8, 2023
1 parent 61511f3 commit 4250e09
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,22 @@ public void Build_ShouldReturnDefaultValues_GivenNoValuesHaveBeenSet() =>
.Should()
.BeSuccess();

[Fact]
public void Build_ShouldReturnFailure_GivenCallbackEventMaskLengthIsAbove200Characters() =>
CreateConversationRequest.Build()
.WithCallback(new Callback(new Uri("https://example.com"), new string('a', 201),
new CallbackParameters("appId", new Uri("https://example.com")), HttpMethod.Get))
.Create()
.Should()
.BeParsingFailure("Callback EventMask length cannot be higher than 200.");

[Theory]
[InlineData("")]
[InlineData(" ")]
public void Build_ShouldReturnFailure_GivenDisplayNameIsProvidedButEmpty(string invalidDisplayName) =>
CreateConversationRequest.Build()
.WithDisplayName(invalidDisplayName)
.Create()
.Map(request => request.DisplayName)
.Should()
.BeParsingFailure("DisplayName cannot be null or whitespace.");

Expand All @@ -32,7 +40,6 @@ public void Build_ShouldReturnFailure_GivenDisplayNameLengthIsAbove50Characters(
CreateConversationRequest.Build()
.WithDisplayName(new string('a', 51))
.Create()
.Map(request => request.DisplayName)
.Should()
.BeParsingFailure("DisplayName length cannot be higher than 50.");

Expand All @@ -49,7 +56,6 @@ public void Build_ShouldReturnFailure_GivenHttpMethodIsNotGetOrPost(string metho
.WithCallback(new Callback(new Uri("https://example.com"), "mask",
new CallbackParameters("appId", new Uri("https://example.com")), new HttpMethod(method)))
.Create()
.Map(request => request.Callback)
.Should()
.BeParsingFailure("Callback HttpMethod must be GET or POST.");

Expand All @@ -60,7 +66,6 @@ public void Build_ShouldReturnFailure_GivenNameIsProvidedButEmpty(string invalid
CreateConversationRequest.Build()
.WithName(invalidName)
.Create()
.Map(request => request.Name)
.Should()
.BeParsingFailure("Name cannot be null or whitespace.");

Expand All @@ -69,10 +74,25 @@ public void Build_ShouldReturnFailure_GivenNameLengthIsAbove100Characters() =>
CreateConversationRequest.Build()
.WithName(new string('a', 101))
.Create()
.Map(request => request.Name)
.Should()
.BeParsingFailure("Name length cannot be higher than 100.");

[Fact]
public void Build_ShouldReturnFailure_GivenPropertiesCustomSortKeyLengthIsAbove200Characters() =>
CreateConversationRequest.Build()
.WithProperties(new Properties(10, "type", new string('a', 201), new Dictionary<string, string>()))
.Create()
.Should()
.BeParsingFailure("Properties CustomSortKey length cannot be higher than 200.");

[Fact]
public void Build_ShouldReturnFailure_GivenPropertiesTypeLengthIsAbove200Characters() =>
CreateConversationRequest.Build()
.WithProperties(new Properties(10, new string('a', 201), "key", new Dictionary<string, string>()))
.Create()
.Should()
.BeParsingFailure("Properties Type length cannot be higher than 200.");

[Fact]
public void Build_ShouldSetCallback() =>
CreateConversationRequest.Build()
Expand Down Expand Up @@ -114,13 +134,13 @@ public void Build_ShouldSetName() =>
[Fact]
public void Build_ShouldSetProperties() =>
CreateConversationRequest.Build()
.WithProperties(new Properties(55, "Fake", "hello-there",
.WithProperties(new Properties(55, new string('a', 200), "hello-there",
new Dictionary<string, string> {{"temp1", "123"}, {"temp12", "456"}}))
.Create()
.Map(request => request.Properties)
.Should()
.BeSuccess(properties =>
properties.Should().BeSome(new Properties(55, "Fake", "hello-there",
properties.Should().BeSome(new Properties(55, new string('a', 200), "hello-there",
new Dictionary<string, string> {{"temp1", "123"}, {"temp12", "456"}})));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ namespace Vonage.Conversations.CreateConversation;

internal class CreateConversationRequestBuilder : IBuilderForOptional
{
private const int CallbackEventMask = 200;
private const int DisplayNameMaxLength = 50;
private const int NameMaxLength = 100;
private const int PropertiesCustomSortKeyMaxLength = 200;
private const int PropertiesTypeMaxLength = 200;

private readonly IEnumerable<HttpMethod> allowedMethods = new[] {HttpMethod.Get, HttpMethod.Post};
private Maybe<Callback> callback;
Expand All @@ -35,7 +38,11 @@ public Result<CreateConversationRequest> Create() => Result<CreateConversationRe
VerifyName,
VerifyNameLength,
VerifyDisplayName,
VerifyDisplayNameLength, this.VerifyCallbackHttpMethod));
VerifyDisplayNameLength,
this.VerifyCallbackHttpMethod,
VerifyPropertiesTypeLength,
VerifyPropertiesCustomSortKeyLength,
VerifyCallbackEventMaskLength));

public IBuilderForOptional WithCallback(Callback value)
{
Expand Down Expand Up @@ -67,6 +74,12 @@ public IBuilderForOptional WithProperties(Properties value)
return this;
}

private static Result<CreateConversationRequest> VerifyCallbackEventMaskLength(CreateConversationRequest request) =>
request.Callback.Match(
some => InputValidation.VerifyLengthLowerOrEqualThan(request, some.EventMask, CallbackEventMask,
$"{nameof(request.Callback)} {nameof(some.EventMask)}"),
() => request);

private Result<CreateConversationRequest> VerifyCallbackHttpMethod(CreateConversationRequest request) =>
request.Callback.Match(
some => this.allowedMethods.Contains(some.Method)
Expand Down Expand Up @@ -95,6 +108,20 @@ private static Result<CreateConversationRequest> VerifyNameLength(CreateConversa
request.Name.Match(
some => InputValidation.VerifyLengthLowerOrEqualThan(request, some, NameMaxLength, nameof(request.Name)),
() => request);

private static Result<CreateConversationRequest> VerifyPropertiesCustomSortKeyLength(
CreateConversationRequest request) =>
request.Properties.Match(
some => InputValidation.VerifyLengthLowerOrEqualThan(request, some.CustomSortKey,
PropertiesCustomSortKeyMaxLength,
$"{nameof(request.Properties)} {nameof(some.CustomSortKey)}"),
() => request);

private static Result<CreateConversationRequest> VerifyPropertiesTypeLength(CreateConversationRequest request) =>
request.Properties.Match(
some => InputValidation.VerifyLengthLowerOrEqualThan(request, some.Type, PropertiesTypeMaxLength,
$"{nameof(request.Properties)} {nameof(some.Type)}"),
() => request);
}

/// <summary>
Expand Down

0 comments on commit 4250e09

Please sign in to comment.