Skip to content

Commit

Permalink
(GH-367) Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jericho committed Jan 25, 2021
1 parent 510e729 commit dd153d8
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 22 deletions.
15 changes: 14 additions & 1 deletion Source/StrongGrid.UnitTests/Resources/ApiKeysTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 10 additions & 1 deletion Source/StrongGrid.UnitTests/Resources/BlocksTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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", "<https://api.sendgrid.com/v3/suppression/unsubscribes?limit=50&offset=0>; rel=\"next\"; title=\"1\", <https://api.sendgrid.com/v3/suppression/unsubscribes?limit=50&offset=0>; rel=\"prev\"; title=\"1\", <https://api.sendgrid.com/v3/suppression/unsubscribes?limit=50&offset=0>; rel=\"last\"; title=\"1\", <https://api.sendgrid.com/v3/suppression/unsubscribes?limit=50&offset=0>; 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;
});
Expand All @@ -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();
Expand Down
11 changes: 10 additions & 1 deletion Source/StrongGrid.UnitTests/Resources/InvalidEmailsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 21 additions & 3 deletions Source/StrongGrid.UnitTests/Resources/IpAddressesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
44 changes: 40 additions & 4 deletions Source/StrongGrid.UnitTests/Resources/SenderAuthenticationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 10 additions & 1 deletion Source/StrongGrid.UnitTests/Resources/SpamReportsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
28 changes: 25 additions & 3 deletions Source/StrongGrid.UnitTests/Resources/TeammatesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
22 changes: 17 additions & 5 deletions Source/StrongGrid.UnitTests/Resources/WebhookSettingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit dd153d8

Please sign in to comment.