From dd153d86da4265dd2ca385143897e93d5ac9cc02 Mon Sep 17 00:00:00 2001 From: Jericho Date: Mon, 25 Jan 2021 13:10:20 -0500 Subject: [PATCH] (GH-367) Fix unit tests --- .../Resources/ApiKeysTests.cs | 15 ++++++- .../Resources/BlocksTests.cs | 11 ++++- .../Resources/GlobalSuppressionTests.cs | 9 ++-- .../Resources/InvalidEmailsTests.cs | 11 ++++- .../Resources/IpAddressesTests.cs | 24 ++++++++-- .../Resources/SenderAuthenticationTests.cs | 44 +++++++++++++++++-- .../Resources/SpamReportsTests.cs | 11 ++++- .../Resources/TeammatesTests.cs | 28 ++++++++++-- .../Resources/WebhookSettingsTests.cs | 22 +++++++--- 9 files changed, 153 insertions(+), 22 deletions(-) diff --git a/Source/StrongGrid.UnitTests/Resources/ApiKeysTests.cs b/Source/StrongGrid.UnitTests/Resources/ApiKeysTests.cs index 30c95705..99ba4183 100644 --- a/Source/StrongGrid.UnitTests/Resources/ApiKeysTests.cs +++ b/Source/StrongGrid.UnitTests/Resources/ApiKeysTests.cs @@ -105,8 +105,21 @@ public async Task GetAsync() public async Task GetAllAsync() { // Arrange + var limit = 50; + var endpoint = Utils.GetSendGridApiUri(ENDPOINT); + + // This is what the endpoint URL should be but we don't support limit and offset yet. + // See: https://github.com/Jericho/StrongGrid/issues/368 + // var endpoint = Utils.GetSendGridApiUri(ENDPOINT) + $"?limit={limit}&offset=0"; + var mockHttp = new MockHttpMessageHandler(); - mockHttp.Expect(HttpMethod.Get, Utils.GetSendGridApiUri(ENDPOINT)).Respond("application/json", MULTIPLE_API_KEY_JSON); + mockHttp.Expect(HttpMethod.Get, endpoint).Respond((HttpRequestMessage request) => + { + var response = new HttpResponseMessage(HttpStatusCode.OK); + response.Headers.Add("Link", $"<{endpoint}>; rel=\"next\"; title=\"1\", <{endpoint}>; rel=\"prev\"; title=\"1\", <{endpoint}>; rel=\"last\"; title=\"1\", <{endpoint}>; rel=\"first\"; title=\"1\""); + response.Content = new StringContent(MULTIPLE_API_KEY_JSON); + return response; + }); var client = Utils.GetFluentClient(mockHttp); var apiKeys = new ApiKeys(client); diff --git a/Source/StrongGrid.UnitTests/Resources/BlocksTests.cs b/Source/StrongGrid.UnitTests/Resources/BlocksTests.cs index d482ca73..5fb77910 100644 --- a/Source/StrongGrid.UnitTests/Resources/BlocksTests.cs +++ b/Source/StrongGrid.UnitTests/Resources/BlocksTests.cs @@ -54,8 +54,17 @@ public void Parse_json() public async Task GetAllAsync() { // Arrange + var limit = 25; + var endpoint = Utils.GetSendGridApiUri(ENDPOINT) + $"?limit={limit}&offset=0"; + var mockHttp = new MockHttpMessageHandler(); - mockHttp.Expect(HttpMethod.Get, Utils.GetSendGridApiUri(ENDPOINT) + $"?limit=25&offset=0").Respond("application/json", MULTIPLE_BLOCKS_JSON); + mockHttp.Expect(HttpMethod.Get, endpoint).Respond((HttpRequestMessage request) => + { + var response = new HttpResponseMessage(HttpStatusCode.OK); + response.Headers.Add("Link", $"<{endpoint}>; rel=\"next\"; title=\"1\", <{endpoint}>; rel=\"prev\"; title=\"1\", <{endpoint}>; rel=\"last\"; title=\"1\", <{endpoint}>; rel=\"first\"; title=\"1\""); + response.Content = new StringContent(MULTIPLE_BLOCKS_JSON); + return response; + }); var client = Utils.GetFluentClient(mockHttp); var blocks = new Blocks(client); diff --git a/Source/StrongGrid.UnitTests/Resources/GlobalSuppressionTests.cs b/Source/StrongGrid.UnitTests/Resources/GlobalSuppressionTests.cs index a2f25fb3..d02e938c 100644 --- a/Source/StrongGrid.UnitTests/Resources/GlobalSuppressionTests.cs +++ b/Source/StrongGrid.UnitTests/Resources/GlobalSuppressionTests.cs @@ -35,11 +35,14 @@ public class GlobalSuppressionTests public async Task GetAll() { // Arrange + var limit = 50; + var endpoint = Utils.GetSendGridApiUri("suppression/unsubscribes") + $"?limit={limit}&offset=0"; + var mockHttp = new MockHttpMessageHandler(); - mockHttp.Expect(HttpMethod.Get, Utils.GetSendGridApiUri("suppression/unsubscribes")).Respond((HttpRequestMessage request) => + mockHttp.Expect(HttpMethod.Get, endpoint).Respond((HttpRequestMessage request) => { var response = new HttpResponseMessage(HttpStatusCode.OK); - response.Headers.Add("link", "; rel=\"next\"; title=\"1\", ; rel=\"prev\"; title=\"1\", ; rel=\"last\"; title=\"1\", ; rel=\"first\"; title=\"1\""); + response.Headers.Add("Link", $"<{endpoint}>; rel=\"next\"; title=\"1\", <{endpoint}>; rel=\"prev\"; title=\"1\", <{endpoint}>; rel=\"last\"; title=\"1\", <{endpoint}>; rel=\"first\"; title=\"1\""); response.Content = new StringContent(GLOBALLY_UNSUBSCRIBED); return response; }); @@ -48,7 +51,7 @@ public async Task GetAll() var globalSuppressions = new GlobalSuppressions(client); // Act - var result = await globalSuppressions.GetAllAsync(null, null, null, 50, 0, null, CancellationToken.None).ConfigureAwait(false); + var result = await globalSuppressions.GetAllAsync(null, null, null, limit, 0, null, CancellationToken.None).ConfigureAwait(false); // Assert mockHttp.VerifyNoOutstandingExpectation(); diff --git a/Source/StrongGrid.UnitTests/Resources/InvalidEmailsTests.cs b/Source/StrongGrid.UnitTests/Resources/InvalidEmailsTests.cs index c2de5675..66b7b517 100644 --- a/Source/StrongGrid.UnitTests/Resources/InvalidEmailsTests.cs +++ b/Source/StrongGrid.UnitTests/Resources/InvalidEmailsTests.cs @@ -57,8 +57,17 @@ public void Parse_json() public async Task GetAllAsync() { // Arrange + var limit = 25; + var endpoint = Utils.GetSendGridApiUri(ENDPOINT) + $"?limit={limit}&offset=0"; + var mockHttp = new MockHttpMessageHandler(); - mockHttp.Expect(HttpMethod.Get, Utils.GetSendGridApiUri(ENDPOINT) + $"?limit=25&offset=0").Respond("application/json", MULTIPLE_INVALID_EMAILS_JSON); + mockHttp.Expect(HttpMethod.Get, endpoint).Respond((HttpRequestMessage request) => + { + var response = new HttpResponseMessage(HttpStatusCode.OK); + response.Headers.Add("Link", $"<{endpoint}>; rel=\"next\"; title=\"1\", <{endpoint}>; rel=\"prev\"; title=\"1\", <{endpoint}>; rel=\"last\"; title=\"1\", <{endpoint}>; rel=\"first\"; title=\"1\""); + response.Content = new StringContent(MULTIPLE_INVALID_EMAILS_JSON); + return response; + }); var client = Utils.GetFluentClient(mockHttp); var invalidEmails = new InvalidEmails(client); diff --git a/Source/StrongGrid.UnitTests/Resources/IpAddressesTests.cs b/Source/StrongGrid.UnitTests/Resources/IpAddressesTests.cs index 91ce7ba5..297acf93 100644 --- a/Source/StrongGrid.UnitTests/Resources/IpAddressesTests.cs +++ b/Source/StrongGrid.UnitTests/Resources/IpAddressesTests.cs @@ -178,14 +178,23 @@ public async Task GetRemainingCountAsync() public async Task GetAllAsync() { // Arrange + var limit = 10; + var endpoint = Utils.GetSendGridApiUri(ENDPOINT) + $"?limit={limit}&offset=0"; + var mockHttp = new MockHttpMessageHandler(); - mockHttp.Expect(HttpMethod.Get, Utils.GetSendGridApiUri(ENDPOINT)).Respond("application/json", MULTIPLE_IPADDRESSES_JSON); + mockHttp.Expect(HttpMethod.Get, endpoint).Respond((HttpRequestMessage request) => + { + var response = new HttpResponseMessage(HttpStatusCode.OK); + response.Headers.Add("Link", $"<{endpoint}>; rel=\"next\"; title=\"1\", <{endpoint}>; rel=\"prev\"; title=\"1\", <{endpoint}>; rel=\"last\"; title=\"1\", <{endpoint}>; rel=\"first\"; title=\"1\""); + response.Content = new StringContent(MULTIPLE_IPADDRESSES_JSON); + return response; + }); var client = Utils.GetFluentClient(mockHttp); var ipAddresses = new IpAddresses(client); // Act - var result = await ipAddresses.GetAllAsync(false, null, 10, 0, CancellationToken.None).ConfigureAwait(false); + var result = await ipAddresses.GetAllAsync(false, null, limit, 0, CancellationToken.None).ConfigureAwait(false); // Assert mockHttp.VerifyNoOutstandingExpectation(); @@ -218,8 +227,17 @@ public async Task GetAssignedAsync() public async Task GetUnassignedAsync() { // Arrange + var limit = 10; + var endpoint = Utils.GetSendGridApiUri(ENDPOINT) + $"?limit={limit}&offset=0"; + var mockHttp = new MockHttpMessageHandler(); - mockHttp.Expect(HttpMethod.Get, Utils.GetSendGridApiUri(ENDPOINT)).Respond("application/json", MULTIPLE_IPADDRESSES_JSON); + mockHttp.Expect(HttpMethod.Get, endpoint).Respond((HttpRequestMessage request) => + { + var response = new HttpResponseMessage(HttpStatusCode.OK); + response.Headers.Add("Link", $"<{endpoint}>; rel=\"next\"; title=\"1\", <{endpoint}>; rel=\"prev\"; title=\"1\", <{endpoint}>; rel=\"last\"; title=\"1\", <{endpoint}>; rel=\"first\"; title=\"1\""); + response.Content = new StringContent(MULTIPLE_IPADDRESSES_JSON); + return response; + }); var client = Utils.GetFluentClient(mockHttp); var ipAddresses = new IpAddresses(client); diff --git a/Source/StrongGrid.UnitTests/Resources/SenderAuthenticationTests.cs b/Source/StrongGrid.UnitTests/Resources/SenderAuthenticationTests.cs index 5508ce61..67431ea4 100644 --- a/Source/StrongGrid.UnitTests/Resources/SenderAuthenticationTests.cs +++ b/Source/StrongGrid.UnitTests/Resources/SenderAuthenticationTests.cs @@ -342,8 +342,17 @@ public void Parse_AuthenticatedDomain_json() public async Task GetAllDomainsAsync_include_subusers() { // Arrange + var limit = 50; + var endpoint = Utils.GetSendGridApiUri(ENDPOINT, "domains") + $"?exclude_subusers=false&limit={limit}&offset=0"; + var mockHttp = new MockHttpMessageHandler(); - mockHttp.Expect(HttpMethod.Get, Utils.GetSendGridApiUri(ENDPOINT, "domains?exclude_subusers=false&limit=50&offset=0")).Respond("application/json", MULTIPLE_DOMAINS_JSON); + mockHttp.Expect(HttpMethod.Get, endpoint).Respond((HttpRequestMessage request) => + { + var response = new HttpResponseMessage(HttpStatusCode.OK); + response.Headers.Add("Link", $"<{endpoint}>; rel=\"next\"; title=\"1\", <{endpoint}>; rel=\"prev\"; title=\"1\", <{endpoint}>; rel=\"last\"; title=\"1\", <{endpoint}>; rel=\"first\"; title=\"1\""); + response.Content = new StringContent(MULTIPLE_DOMAINS_JSON); + return response; + }); var client = Utils.GetFluentClient(mockHttp); var senderAuthentication = new SenderAuthentication(client); @@ -362,8 +371,17 @@ public async Task GetAllDomainsAsync_include_subusers() public async Task GetAllDomainsAsync_exclude_subusers() { // Arrange + var limit = 50; + var endpoint = Utils.GetSendGridApiUri(ENDPOINT, "domains") + $"?exclude_subusers=true&limit={limit}&offset=0"; + var mockHttp = new MockHttpMessageHandler(); - mockHttp.Expect(HttpMethod.Get, Utils.GetSendGridApiUri(ENDPOINT, "domains?exclude_subusers=true&limit=50&offset=0")).Respond("application/json", MULTIPLE_DOMAINS_JSON); + mockHttp.Expect(HttpMethod.Get, endpoint).Respond((HttpRequestMessage request) => + { + var response = new HttpResponseMessage(HttpStatusCode.OK); + response.Headers.Add("Link", $"<{endpoint}>; rel=\"next\"; title=\"1\", <{endpoint}>; rel=\"prev\"; title=\"1\", <{endpoint}>; rel=\"last\"; title=\"1\", <{endpoint}>; rel=\"first\"; title=\"1\""); + response.Content = new StringContent(MULTIPLE_DOMAINS_JSON); + return response; + }); var client = Utils.GetFluentClient(mockHttp); var senderAuthentication = new SenderAuthentication(client); @@ -635,8 +653,17 @@ public async Task AssociateDomainAsync() public async Task GetAllReverseDnsAsync() { // Arrange + var limit = 50; + var endpoint = Utils.GetSendGridApiUri(ENDPOINT, "ips") + $"?limit={limit}&offset=0"; + var mockHttp = new MockHttpMessageHandler(); - mockHttp.Expect(HttpMethod.Get, Utils.GetSendGridApiUri(ENDPOINT, "ips?limit=50&offset=0")).Respond("application/json", MULTIPLE_IPS_JSON); + mockHttp.Expect(HttpMethod.Get, endpoint).Respond((HttpRequestMessage request) => + { + var response = new HttpResponseMessage(HttpStatusCode.OK); + response.Headers.Add("Link", $"<{endpoint}>; rel=\"next\"; title=\"1\", <{endpoint}>; rel=\"prev\"; title=\"1\", <{endpoint}>; rel=\"last\"; title=\"1\", <{endpoint}>; rel=\"first\"; title=\"1\""); + response.Content = new StringContent(MULTIPLE_IPS_JSON); + return response; + }); var client = Utils.GetFluentClient(mockHttp); var senderAuthentication = new SenderAuthentication(client); @@ -967,8 +994,17 @@ public async Task AssociateLinkAsync() public async Task GetAllLinksAsync() { // Arrange + var limit = 50; + var endpoint = Utils.GetSendGridApiUri(ENDPOINT, "links") + $"?limit={limit}&offset=0"; + var mockHttp = new MockHttpMessageHandler(); - mockHttp.Expect(HttpMethod.Get, Utils.GetSendGridApiUri(ENDPOINT, "links?limit=50&offset=0")).Respond("application/json", MULTIPLE_LINKS_JSON); + mockHttp.Expect(HttpMethod.Get, endpoint).Respond((HttpRequestMessage request) => + { + var response = new HttpResponseMessage(HttpStatusCode.OK); + response.Headers.Add("Link", $"<{endpoint}>; rel=\"next\"; title=\"1\", <{endpoint}>; rel=\"prev\"; title=\"1\", <{endpoint}>; rel=\"last\"; title=\"1\", <{endpoint}>; rel=\"first\"; title=\"1\""); + response.Content = new StringContent(MULTIPLE_LINKS_JSON); + return response; + }); var client = Utils.GetFluentClient(mockHttp); var senderAuthentication = new SenderAuthentication(client); diff --git a/Source/StrongGrid.UnitTests/Resources/SpamReportsTests.cs b/Source/StrongGrid.UnitTests/Resources/SpamReportsTests.cs index 50c45a2a..cdca9085 100644 --- a/Source/StrongGrid.UnitTests/Resources/SpamReportsTests.cs +++ b/Source/StrongGrid.UnitTests/Resources/SpamReportsTests.cs @@ -60,8 +60,17 @@ public void Parse_json() public async Task GetAllAsync() { // Arrange + var limit = 25; + var endpoint = Utils.GetSendGridApiUri(ENDPOINT) + $"?limit={limit}&offset=0"; + var mockHttp = new MockHttpMessageHandler(); - mockHttp.Expect(HttpMethod.Get, Utils.GetSendGridApiUri(ENDPOINT) + $"?limit=25&offset=0").Respond("application/json", MULTIPLE_SPAM_REPORTS_JSON); + mockHttp.Expect(HttpMethod.Get, endpoint).Respond((HttpRequestMessage request) => + { + var response = new HttpResponseMessage(HttpStatusCode.OK); + response.Headers.Add("Link", $"<{endpoint}>; rel=\"next\"; title=\"1\", <{endpoint}>; rel=\"prev\"; title=\"1\", <{endpoint}>; rel=\"last\"; title=\"1\", <{endpoint}>; rel=\"first\"; title=\"1\""); + response.Content = new StringContent(MULTIPLE_SPAM_REPORTS_JSON); + return response; + }); var client = Utils.GetFluentClient(mockHttp); var spamReports = new SpamReports(client); diff --git a/Source/StrongGrid.UnitTests/Resources/TeammatesTests.cs b/Source/StrongGrid.UnitTests/Resources/TeammatesTests.cs index 3551ad1d..2ae1412e 100644 --- a/Source/StrongGrid.UnitTests/Resources/TeammatesTests.cs +++ b/Source/StrongGrid.UnitTests/Resources/TeammatesTests.cs @@ -234,8 +234,21 @@ public async Task ResendInvitationAsync() public async Task GetAllPendingInvitationsAsync() { // Arrange + var limit = 50; + var endpoint = Utils.GetSendGridApiUri(ENDPOINT, "pending"); + + // This is what the endpoint URL should be but we don't support limit and offset yet. + // See: https://github.com/Jericho/StrongGrid/issues/368 + // var endpoint = Utils.GetSendGridApiUri(ENDPOINT, "pending") + $"?limit={limit}&offset=0"; + var mockHttp = new MockHttpMessageHandler(); - mockHttp.Expect(HttpMethod.Get, Utils.GetSendGridApiUri(ENDPOINT, "pending")).Respond("application/json", MULTIPLE_INVITATIONS_JSON); + mockHttp.Expect(HttpMethod.Get, endpoint).Respond((HttpRequestMessage request) => + { + var response = new HttpResponseMessage(HttpStatusCode.OK); + response.Headers.Add("Link", $"<{endpoint}>; rel=\"next\"; title=\"1\", <{endpoint}>; rel=\"prev\"; title=\"1\", <{endpoint}>; rel=\"last\"; title=\"1\", <{endpoint}>; rel=\"first\"; title=\"1\""); + response.Content = new StringContent(MULTIPLE_INVITATIONS_JSON); + return response; + }); var client = Utils.GetFluentClient(mockHttp); var teammates = new Teammates(client); @@ -317,14 +330,23 @@ public async Task InviteTeammateAsAdminAsync() public async Task GetAllTeammatesAsync() { // Arrange + var limit = 50; + var endpoint = Utils.GetSendGridApiUri(ENDPOINT) + $"?limit={limit}&offset=0"; + var mockHttp = new MockHttpMessageHandler(); - mockHttp.Expect(HttpMethod.Get, Utils.GetSendGridApiUri(ENDPOINT)).Respond("application/json", MULTIPLE_TEAMMATES_JSON); + mockHttp.Expect(HttpMethod.Get, endpoint).Respond((HttpRequestMessage request) => + { + var response = new HttpResponseMessage(HttpStatusCode.OK); + response.Headers.Add("Link", $"<{endpoint}>; rel=\"next\"; title=\"1\", <{endpoint}>; rel=\"prev\"; title=\"1\", <{endpoint}>; rel=\"last\"; title=\"1\", <{endpoint}>; rel=\"first\"; title=\"1\""); + response.Content = new StringContent(MULTIPLE_TEAMMATES_JSON); + return response; + }); var client = Utils.GetFluentClient(mockHttp); var teammates = new Teammates(client); // Act - var result = await teammates.GetAllTeammatesAsync(10, 0, CancellationToken.None).ConfigureAwait(false); + var result = await teammates.GetAllTeammatesAsync(limit, 0, CancellationToken.None).ConfigureAwait(false); // Assert mockHttp.VerifyNoOutstandingExpectation(); diff --git a/Source/StrongGrid.UnitTests/Resources/WebhookSettingsTests.cs b/Source/StrongGrid.UnitTests/Resources/WebhookSettingsTests.cs index df70b8bc..7e41ef46 100644 --- a/Source/StrongGrid.UnitTests/Resources/WebhookSettingsTests.cs +++ b/Source/StrongGrid.UnitTests/Resources/WebhookSettingsTests.cs @@ -225,13 +225,25 @@ public async Task GetInboundParseWebhookSettings() public async Task GetAllInboundParseWebhookSettings() { // Arrange - var apiResponse = "{'result':[" + - SINGLE_INBOUNDPARSE_WEBHOOK_SETTING_JSON + "," + - SINGLE_INBOUNDPARSE_WEBHOOK_SETTING_JSON + - "]}"; + var limit = 25; + var endpoint = Utils.GetSendGridApiUri(INBOUNDPARSE_ENDPOINT, "settings"); + + // This is what the endpoint URL should be but we don't support limit and offset yet. + // See: https://github.com/Jericho/StrongGrid/issues/368 + // var endpoint = Utils.GetSendGridApiUri(INBOUNDPARSE_ENDPOINT, "settings") + $"?limit={limit}&offset=0"; var mockHttp = new MockHttpMessageHandler(); - mockHttp.Expect(HttpMethod.Get, Utils.GetSendGridApiUri(INBOUNDPARSE_ENDPOINT, "settings")).Respond("application/json", apiResponse); + mockHttp.Expect(HttpMethod.Get, endpoint).Respond((HttpRequestMessage request) => + { + var response = new HttpResponseMessage(HttpStatusCode.OK); + response.Headers.Add("Link", $"<{endpoint}>; rel=\"next\"; title=\"1\", <{endpoint}>; rel=\"prev\"; title=\"1\", <{endpoint}>; rel=\"last\"; title=\"1\", <{endpoint}>; rel=\"first\"; title=\"1\""); + response.Content = new StringContent( + "{'result':[" + + SINGLE_INBOUNDPARSE_WEBHOOK_SETTING_JSON + "," + + SINGLE_INBOUNDPARSE_WEBHOOK_SETTING_JSON + + "]}"); + return response; + }); var client = Utils.GetFluentClient(mockHttp); var webhooks = new WebhookSettings(client);