Skip to content

Commit

Permalink
Add unit tests for new GetPaymentLinkPaymentListAsync method
Browse files Browse the repository at this point in the history
  • Loading branch information
Viincenttt committed Jul 9, 2024
1 parent 0c0f2e3 commit c769bc9
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 33 deletions.
1 change: 0 additions & 1 deletion tests/Mollie.Tests.Unit/Client/OrderClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Net.Http;
using System.Threading.Tasks;
using FluentAssertions;
using FluentAssertions.Extensions;
using Mollie.Api.Models.Order.Request;
using Mollie.Api.Models.Order.Request.ManageOrderLines;
using Mollie.Api.Models.Order.Response;
Expand Down
169 changes: 137 additions & 32 deletions tests/Mollie.Tests.Unit/Client/PaymentLinkClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using FluentAssertions;
using Mollie.Api.Client;
using Mollie.Api.Models;
using Mollie.Api.Models.List.Response;
using Mollie.Api.Models.Payment.Response;
using Mollie.Api.Models.PaymentLink.Request;
using Mollie.Api.Models.PaymentLink.Response;
using RichardSzalay.MockHttp;
Expand All @@ -17,38 +20,6 @@ public class PaymentLinkClientTests : BaseClientTests {
private const string DefaultDescription = "A car";
private const string DefaultWebhookUrl = "https://www.mollie.com";

private readonly string _defaultPaymentLinkJsonResponse = @$"{{
""resource"": ""payment-link"",
""id"": ""{DefaultPaymentLinkId}"",
""mode"": ""test"",
""profileId"": ""pfl_QkEhN94Ba"",
""createdAt"": ""2021-03-20T09:13:37+00:00"",
""paidAt"": null,
""updatedAt"": null,
""expiresAt"": ""2021-06-06T11:00:00+00:00"",
""amount"": {{
""value"": ""{DefaultPaymentAmount.ToString(CultureInfo.InvariantCulture)}"",
""currency"": ""EUR""
}},
""description"": ""{DefaultDescription}"",
""redirectUrl"": ""{DefaultRedirectUrl}"",
""webhookUrl"": ""{DefaultWebhookUrl}"",
""_links"": {{
""self"": {{
""href"": ""https://api.mollie.com/v2/payment-links/pl_4Y0eZitmBnQ6IDoMqZQKh"",
""type"": ""application/json""
}},
""paymentLink"": {{
""href"": ""https://useplink.com/payment/4Y0eZitmBnQ6IDoMqZQKh/"",
""type"": ""text/html""
}},
""documentation"": {{
""href"": ""https://docs.mollie.com/reference/v2/payment-links-api/create-payment-link"",
""type"": ""text/html""
}}
}}
}}";

[Fact]
public async Task CreatePaymentLinkAsync_PaymentLinkWithRequiredParameters_ResponseIsDeserializedInExpectedFormat() {
// Given: we create a payment link request with only the required parameters
Expand Down Expand Up @@ -107,12 +78,146 @@ public async Task GetPaymentLinkAsync_NoPaymentLinkIdIsGiven_ArgumentExceptionIs
exception.Message.Should().Be("Required URL argument 'paymentLinkId' is null or empty");
}

[Theory]
[InlineData(null, null, false, null, "")]
[InlineData("from", null, false, null, "?from=from")]
[InlineData("from", 50, false, null, "?from=from&limit=50")]
[InlineData(null, null, true, null, "?testmode=true")]
[InlineData(null, null, true, SortDirection.Desc, "?testmode=true&sort=desc")]
[InlineData(null, null, true, SortDirection.Asc, "?testmode=true&sort=asc")]
public async Task GetPaymentLinkPaymentListAsync_QueryParameterOptions_CorrectParametersAreAdded(
string from,
int? limit,
bool testmode,
SortDirection? sortDirection,
string expectedQueryString) {
// Given: We make a request to retrieve the list of orders
var mockHttp = CreateMockHttpMessageHandler(
HttpMethod.Get,
$"{BaseMollieClient.ApiEndPoint}payment-links/{DefaultPaymentLinkId}/payments{expectedQueryString}",
_defaultPaymentLinkPaymentsJsonResponse);
HttpClient httpClient = mockHttp.ToHttpClient();
var paymentLinkClient = new PaymentLinkClient("access_abcde", httpClient);

// When: We send the request
await paymentLinkClient.GetPaymentLinkPaymentListAsync(DefaultPaymentLinkId, from, limit, testmode, sortDirection);

// Then
mockHttp.VerifyNoOutstandingRequest();
}

[Fact]
public async Task GetPaymentLinkPaymentListAsync_ResponseIsDeserializedInExpectedFormat() {
// Given: We make a request to retrieve the list of orders
var mockHttp = CreateMockHttpMessageHandler(
HttpMethod.Get,
$"{BaseMollieClient.ApiEndPoint}payment-links/{DefaultPaymentLinkId}/payments",
_defaultPaymentLinkPaymentsJsonResponse);
HttpClient httpClient = mockHttp.ToHttpClient();
var paymentLinkClient = new PaymentLinkClient("abcde", httpClient);

// When: We send the request
ListResponse<PaymentResponse> result = await paymentLinkClient.GetPaymentLinkPaymentListAsync(DefaultPaymentLinkId);

// Then
mockHttp.VerifyNoOutstandingRequest();
result.Should().NotBeNull();
result.Count.Should().Be(1);
PaymentResponse payment = result.Items.Single();
payment.Id.Should().Be("tr_7UhSN1zuXS");
payment.Amount.Value.Should().Be(DefaultPaymentAmount.ToString(CultureInfo.InvariantCulture));
payment.Description.Should().Be(DefaultDescription);
payment.RedirectUrl.Should().Be(DefaultRedirectUrl);
payment.WebhookUrl.Should().Be(DefaultWebhookUrl);
}

private void VerifyPaymentLinkResponse(PaymentLinkResponse response) {
response.Amount.Value.Should().Be(DefaultPaymentAmount.ToString(CultureInfo.InvariantCulture));
response.Description.Should().Be(DefaultDescription);
response.Id.Should().Be(DefaultPaymentLinkId);
response.RedirectUrl.Should().Be(DefaultRedirectUrl);
response.WebhookUrl.Should().Be(DefaultWebhookUrl);
}

private readonly string _defaultPaymentLinkPaymentsJsonResponse = $@"{{
""count"": 1,
""_embedded"": {{
""payments"": [
{{
""resource"": ""payment"",
""id"": ""tr_7UhSN1zuXS"",
""mode"": ""live"",
""status"": ""open"",
""isCancelable"": false,
""amount"": {{
""value"": ""{DefaultPaymentAmount.ToString(CultureInfo.InvariantCulture)}"",
""currency"": ""EUR""
}},
""description"": ""{DefaultDescription}"",
""method"": ""ideal"",
""metadata"": null,
""details"": null,
""profileId"": ""pfl_QkEhN94Ba"",
""redirectUrl"": ""{DefaultRedirectUrl}"",
""webhookUrl"": ""{DefaultWebhookUrl}"",
""createdAt"": ""2024-02-12T11:58:35.0Z"",
""expiresAt"": ""2024-02-12T12:13:35.0Z"",
""_links"": {{
""self"": {{
""href"": ""..."",
""type"": ""application/hal+json""
}},
""checkout"": {{
""href"": ""https://www.mollie.com/checkout/issuer/select/ideal/7UhSN1zuXS"",
""type"": ""text/html""
}},
""dashboard"": {{
""href"": ""https://www.mollie.com/dashboard/org_12345678/payments/tr_7UhSN1zuXS"",
""type"": ""text/html""
}}
}}
}}
]
}},
""_links"": {{
""previous"": null,
""next"": {{
""href"": ""https://api.mollie.com/v2/payment-links/pl_4Y0eZitmBnQ6IDoMqZQKh/payments?from=tr_SDkzMggpvx&limit=5"",
""type"": ""application/hal+json""
}}
}}
}}";

private readonly string _defaultPaymentLinkJsonResponse = @$"{{
""resource"": ""payment-link"",
""id"": ""{DefaultPaymentLinkId}"",
""mode"": ""test"",
""profileId"": ""pfl_QkEhN94Ba"",
""createdAt"": ""2021-03-20T09:13:37+00:00"",
""paidAt"": null,
""updatedAt"": null,
""expiresAt"": ""2021-06-06T11:00:00+00:00"",
""amount"": {{
""value"": ""{DefaultPaymentAmount.ToString(CultureInfo.InvariantCulture)}"",
""currency"": ""EUR""
}},
""description"": ""{DefaultDescription}"",
""redirectUrl"": ""{DefaultRedirectUrl}"",
""webhookUrl"": ""{DefaultWebhookUrl}"",
""_links"": {{
""self"": {{
""href"": ""https://api.mollie.com/v2/payment-links/pl_4Y0eZitmBnQ6IDoMqZQKh"",
""type"": ""application/json""
}},
""paymentLink"": {{
""href"": ""https://useplink.com/payment/4Y0eZitmBnQ6IDoMqZQKh/"",
""type"": ""text/html""
}},
""documentation"": {{
""href"": ""https://docs.mollie.com/reference/v2/payment-links-api/create-payment-link"",
""type"": ""text/html""
}}
}}
}}";
}
}

0 comments on commit c769bc9

Please sign in to comment.