Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/reply option requires https #615

Merged
merged 3 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ public CorrespondenceBuilder WithAttachments()
};
return this;
}

public CorrespondenceBuilder WithReplyOptions(List<CorrespondenceReplyOptionExt> replyOptions)
{
_correspondence.Correspondence.ReplyOptions = replyOptions;
return this;
}

public CorrespondenceBuilder WithExternalReference()
{
_correspondence.Correspondence.ExternalReferences = new List<ExternalReferenceExt>(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,5 +499,64 @@ public async Task InitializeCorrespondence_RecipientIsReservedButIgnoreReservati
// Assert
Assert.Equal(HttpStatusCode.OK, initializeCorrespondenceResponse.StatusCode);
}

[Fact]
public async Task IntializeCorrespondence_WithValidReplyOptions_ReturnsOK()
{
// Arrange
var payload = new CorrespondenceBuilder()
.CreateCorrespondence()
.WithReplyOptions(new List<CorrespondenceReplyOptionExt>
{
new CorrespondenceReplyOptionExt
{
LinkURL = "https://www.altinn.no",
LinkText = "Altinn"
}
})
.Build();

// Act
var initializeCorrespondenceResponse = await _senderClient.PostAsJsonAsync("correspondence/api/v1/correspondence", payload);

// Assert
Assert.Equal(HttpStatusCode.OK, initializeCorrespondenceResponse.StatusCode);
}
[Fact]
public async Task InitializeCorrespondence_WithInvalidReplyOptions_ReturnsBadRequest()
{
// Arrange
var payload = new CorrespondenceBuilder()
.CreateCorrespondence()
.WithReplyOptions(new List<CorrespondenceReplyOptionExt>
{
new CorrespondenceReplyOptionExt
{
LinkURL = "http://www.altinn.no",
LinkText = "Altinn"
}
})
.Build();

// Act
var initializeCorrespondenceResponse = await _senderClient.PostAsJsonAsync("correspondence/api/v1/correspondence", payload);
// Assert
Assert.Equal(HttpStatusCode.BadRequest, initializeCorrespondenceResponse.StatusCode);

payload.Correspondence.ReplyOptions.First().LinkURL = "www.altinn.no";
initializeCorrespondenceResponse = await _senderClient.PostAsJsonAsync("correspondence/api/v1/correspondence", payload);
// Assert
Assert.Equal(HttpStatusCode.BadRequest, initializeCorrespondenceResponse.StatusCode);

payload.Correspondence.ReplyOptions.First().LinkURL = "https://www.al tinn.no";
initializeCorrespondenceResponse = await _senderClient.PostAsJsonAsync("correspondence/api/v1/correspondence", payload);
// Assert
Assert.Equal(HttpStatusCode.BadRequest, initializeCorrespondenceResponse.StatusCode);

payload.Correspondence.ReplyOptions.First().LinkURL = "C:\\Users\\User\\Desktop";
initializeCorrespondenceResponse = await _senderClient.PostAsJsonAsync("correspondence/api/v1/correspondence", payload);
// Assert
Assert.Equal(HttpStatusCode.BadRequest, initializeCorrespondenceResponse.StatusCode);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json.Serialization;
using System.ComponentModel.DataAnnotations;

namespace Altinn.Correspondence.API.Models
{
Expand All @@ -11,6 +12,7 @@ public class CorrespondenceReplyOptionExt
/// <summary>
/// Gets or sets the URL to be used as a reply/response to a correspondence.
/// </summary>
[IsLink]
[JsonPropertyName("linkURL")]
public required string LinkURL { get; set; }

Expand All @@ -19,5 +21,42 @@ public class CorrespondenceReplyOptionExt
/// </summary>
[JsonPropertyName("linkText")]
public string? LinkText { get; set; }

[AttributeUsage(AttributeTargets.Property)]
internal class IsLinkAttribute : ValidationAttribute
{
private const int maxLength = 255;
private const string httpsPrefix = "https://";
private const string httpPrefix = "http://";
public IsLinkAttribute()
{

}

protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
{
if (value is not string strValue)
{
return new ValidationResult("LinkURL is not of type string");
}
if (strValue.Length > maxLength)
{
return new ValidationResult($"LinkURL must not exceed {maxLength} characters");
}
if (!Uri.IsWellFormedUriString((string)value, UriKind.Absolute))
{
return new ValidationResult("LinkURL is not a valid URL");
}
if (strValue.StartsWith(httpPrefix, StringComparison.OrdinalIgnoreCase))
{
return new ValidationResult("LinkURL must use HTTPS");
}
if (!strValue.StartsWith(httpsPrefix, StringComparison.OrdinalIgnoreCase))
{
return new ValidationResult($"LinkURL must start with{httpsPrefix}");
}
return ValidationResult.Success;
}
}
}
}
Loading