Skip to content

Commit

Permalink
Merge branch 'feature/globalsuppression' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Jericho committed Oct 20, 2016
2 parents 7c4403a + 15359ef commit bbc4d1b
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Source/StrongGrid.452/StrongGrid.452.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@
<Compile Include="..\StrongGrid.Shared\Resources\ApiKeys.cs">
<Link>Resources\ApiKeys.cs</Link>
</Compile>
<Compile Include="..\StrongGrid.Shared\Resources\GlobalSuppressions.cs">
<Link>Resources\GlobalSuppressions.cs</Link>
</Compile>
<Compile Include="..\StrongGrid.Shared\Resources\Mail.cs">
<Link>Resources\Mail.cs</Link>
</Compile>
Expand Down
3 changes: 3 additions & 0 deletions Source/StrongGrid.46/StrongGrid.46.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@
<Compile Include="..\StrongGrid.Shared\Resources\ApiKeys.cs">
<Link>Resources\ApiKeys.cs</Link>
</Compile>
<Compile Include="..\StrongGrid.Shared\Resources\GlobalSuppressions.cs">
<Link>Resources\GlobalSuppressions.cs</Link>
</Compile>
<Compile Include="..\StrongGrid.Shared\Resources\Mail.cs">
<Link>Resources\Mail.cs</Link>
</Compile>
Expand Down
3 changes: 3 additions & 0 deletions Source/StrongGrid.461/StrongGrid.461.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@
<Compile Include="..\StrongGrid.Shared\Resources\ApiKeys.cs">
<Link>Resources\ApiKeys.cs</Link>
</Compile>
<Compile Include="..\StrongGrid.Shared\Resources\GlobalSuppressions.cs">
<Link>Resources\GlobalSuppressions.cs</Link>
</Compile>
<Compile Include="..\StrongGrid.Shared\Resources\Mail.cs">
<Link>Resources\Mail.cs</Link>
</Compile>
Expand Down
3 changes: 3 additions & 0 deletions Source/StrongGrid.462/StrongGrid.462.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@
<Compile Include="..\StrongGrid.Shared\Resources\ApiKeys.cs">
<Link>Resources\ApiKeys.cs</Link>
</Compile>
<Compile Include="..\StrongGrid.Shared\Resources\GlobalSuppressions.cs">
<Link>Resources\GlobalSuppressions.cs</Link>
</Compile>
<Compile Include="..\StrongGrid.Shared\Resources\Mail.cs">
<Link>Resources\Mail.cs</Link>
</Compile>
Expand Down
25 changes: 25 additions & 0 deletions Source/StrongGrid.IntegrationTests/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ static void Main()
Mail(client);
ApiKeys(client);
UnsubscribeGroups(client);
GlobalSuppressions(client);
User(client);
Statistics(client);
}
Expand Down Expand Up @@ -182,6 +183,30 @@ private static void UnsubscribeGroups(IClient client)
Console.ReadKey();
}

private static void GlobalSuppressions(IClient client)
{
Console.WriteLine("\n***** GLOBAL SUPPRESSION *****");

// ADD EMAILS TO THE GLOBAL SUPPRESSION LIST
var emails = new[] { "example@example.com", "example2@example.com" };
client.GlobalSuppressions.AddAsync(emails).Wait();
Console.WriteLine("The following emails have been added to the global suppression list: {0}", string.Join(", ", emails));
Console.WriteLine("Is {0} unsubscribed (should be true): {1}", emails[0], client.GlobalSuppressions.IsUnsubscribedAsync(emails[0]).Result);
Console.WriteLine("Is {0} unsubscribed (should be true): {1}", emails[1], client.GlobalSuppressions.IsUnsubscribedAsync(emails[1]).Result);

// DELETE EMAILS FROM THE GLOBAL SUPPRESSION GROUP
client.GlobalSuppressions.RemoveAsync(emails[0]).Wait();
Console.WriteLine("{0} has been removed from the global suppression list", emails[0]);
client.GlobalSuppressions.RemoveAsync(emails[1]).Wait();
Console.WriteLine("{0} has been removed from the global suppression list", emails[1]);

Console.WriteLine("Is {0} unsubscribed (should be false): {1}", emails[0], client.GlobalSuppressions.IsUnsubscribedAsync(emails[0]).Result);
Console.WriteLine("Is {0} unsubscribed (should be false): {1}", emails[1], client.GlobalSuppressions.IsUnsubscribedAsync(emails[1]).Result);

Console.WriteLine("\n\nPress any key to continue");
Console.ReadKey();
}

private static void Statistics(IClient client)
{
Console.WriteLine("\n***** STATISTICS *****");
Expand Down
2 changes: 2 additions & 0 deletions Source/StrongGrid.Shared/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ private enum Methods
public ApiKeys ApiKeys { get; private set; }
public UnsubscribeGroups UnsubscribeGroups { get; private set; }
public Suppressions Suppressions { get; private set; }
public GlobalSuppressions GlobalSuppressions { get; private set; }
public Statistics Statistics { get; private set; }
public User User { get; private set; }
public string Version { get; private set; }
Expand All @@ -56,6 +57,7 @@ public Client(string apiKey, string baseUri = "https://api.sendgrid.com", string
ApiKeys = new ApiKeys(this);
UnsubscribeGroups = new UnsubscribeGroups(this);
Suppressions = new Suppressions(this);
GlobalSuppressions = new GlobalSuppressions(this);
Statistics = new Statistics(this);
User = new User(this);
Version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
Expand Down
1 change: 1 addition & 0 deletions Source/StrongGrid.Shared/IClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public interface IClient
ApiKeys ApiKeys { get; }
UnsubscribeGroups UnsubscribeGroups { get; }
Suppressions Suppressions { get; }
GlobalSuppressions GlobalSuppressions { get; }
Statistics Statistics { get; }
User User { get; }

Expand Down
75 changes: 75 additions & 0 deletions Source/StrongGrid.Shared/Resources/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Newtonsoft.Json.Linq;
using StrongGrid.Utilities;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace StrongGrid.Resources
{
public class GlobalSuppressions
{
private readonly string _endpoint;
private readonly IClient _client;

/// <summary>
/// Constructs the SendGrid Global Suppressions object.
/// See https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/global_suppressions.html
/// </summary>
/// <param name="client">SendGrid Web API v3 client</param>
/// <param name="endpoint">Resource endpoint, do not prepend slash</param>
public GlobalSuppressions(IClient client, string endpoint = "/asm/suppressions/global")
{
_endpoint = endpoint;
_client = client;
}

/// <summary>
/// Check if a recipient address is in the global suppressions group.
/// </summary>
/// <param name="email">email address to check</param>
/// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/global_suppressions.html</returns>
public async Task<bool> IsUnsubscribedAsync(string email, CancellationToken cancellationToken = default(CancellationToken))
{
var response = await _client.GetAsync(string.Format("{0}/{1}", _endpoint, email), cancellationToken).ConfigureAwait(false);
response.EnsureSuccess();

var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

// If the email address is on the global suppression list, the response will look like this:
// {
// "recipient_email": "{email}"
// }
// If the email address is not on the global suppression list, the response will be empty
//
// Therefore, we check for the presence of the 'recipient_email' to indicate if the email
// address is on the global suppression list or not.
var dynamicObject = JObject.Parse(responseContent);
var propertyDictionary = (IDictionary<string, JToken>)dynamicObject;
return propertyDictionary.ContainsKey("recipient_email");
}

/// <summary>
/// Add recipient addresses to the global suppression group.
/// </summary>
/// <param name="emails">Array of email addresses to add to the suppression group</param>
/// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/global_suppressions.html</returns>
public async Task AddAsync(IEnumerable<string> emails, CancellationToken cancellationToken = default(CancellationToken))
{
var data = new JObject(new JProperty("recipient_emails", JArray.FromObject(emails.ToArray())));
var response = await _client.PostAsync(_endpoint, data, cancellationToken).ConfigureAwait(false);
response.EnsureSuccess();
}

/// <summary>
/// Delete a recipient email from the global suppressions group.
/// </summary>
/// <param name="email">email address to be removed from the global suppressions group</param>
/// <returns>https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/global_suppressions.html</returns>
public async Task RemoveAsync(string email, CancellationToken cancellationToken = default(CancellationToken))
{
var response = await _client.DeleteAsync(string.Format("{0}/{1}", _endpoint, email), cancellationToken).ConfigureAwait(false);
response.EnsureSuccess();
}
}
}
100 changes: 100 additions & 0 deletions Source/StrongGrid.UnitTests/Resources/GlobalSuppressionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Newtonsoft.Json.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;

namespace StrongGrid.Resources.UnitTests
{
[TestClass]
public class GlobalSuppressionTests
{
private const string ENDPOINT = "/asm/suppressions/global";

[TestMethod]
public void Add()
{
// Arrange
var emails = new[] { "test1@example.com", "test2@example.com" };
var apiResponse = @"{
'recipient_emails': [
'test1@example.com',
'test2@example.com'
]
}";

var mockClient = new Mock<IClient>(MockBehavior.Strict);
mockClient.Setup(c => c.PostAsync(ENDPOINT, It.Is<JObject>(o => o["recipient_emails"].ToObject<JArray>().Count == emails.Length), It.IsAny<CancellationToken>()))
.ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(apiResponse) });

var globalSuppressions = new GlobalSuppressions(mockClient.Object);

// Act
globalSuppressions.AddAsync(emails, CancellationToken.None).Wait();

// Assert
}

[TestMethod]
public void Delete()
{
// Arrange
var email = "test1@example.com";

var mockClient = new Mock<IClient>(MockBehavior.Strict);
mockClient.Setup(c => c.DeleteAsync(ENDPOINT + "/" + email, It.IsAny<CancellationToken>()))
.ReturnsAsync(new HttpResponseMessage(HttpStatusCode.NoContent));

var globalSuppressions = new GlobalSuppressions(mockClient.Object);

// Act
globalSuppressions.RemoveAsync(email, CancellationToken.None).Wait();

// Assert
}

[TestMethod]
public void IsUnsubscribed_true()
{
// Arrange
var email = "test1@example.com";

var apiResponse = @"{
'recipient_email': 'test1@example.com'
}";
var mockClient = new Mock<IClient>(MockBehavior.Strict);
mockClient.Setup(c => c.GetAsync(ENDPOINT + "/" + email, It.IsAny<CancellationToken>()))
.ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(apiResponse) });

var globalSuppressions = new GlobalSuppressions(mockClient.Object);

// Act
var result = globalSuppressions.IsUnsubscribedAsync(email, CancellationToken.None).Result;

// Assert
Assert.IsTrue(result);
}

[TestMethod]
public void IsUnsubscribed_false()
{
// Arrange
var email = "test1@example.com";

var apiResponse = @"{
}";
var mockClient = new Mock<IClient>(MockBehavior.Strict);
mockClient.Setup(c => c.GetAsync(ENDPOINT + "/" + email, It.IsAny<CancellationToken>()))
.ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(apiResponse) });

var globalSuppressions = new GlobalSuppressions(mockClient.Object);

// Act
var result = globalSuppressions.IsUnsubscribedAsync(email, CancellationToken.None).Result;

// Assert
Assert.IsFalse(result);
}
}
}
2 changes: 1 addition & 1 deletion Source/StrongGrid.UnitTests/Resources/SuppresionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void AddAddressToUnsubscribeGroup_single_email()
]
}";
var mockClient = new Mock<IClient>(MockBehavior.Strict);
mockClient.Setup(c => c.PostAsync(ENDPOINT + "/" + groupId + "/suppressions", It.Is<JObject>(o => o["recipient_emails"].ToObject< JArray>().Count == 1), It.IsAny<CancellationToken>()))
mockClient.Setup(c => c.PostAsync(ENDPOINT + "/" + groupId + "/suppressions", It.Is<JObject>(o => o["recipient_emails"].ToObject<JArray>().Count == 1), It.IsAny<CancellationToken>()))
.ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(apiResponse) });

var suppressions = new Suppressions(mockClient.Object);
Expand Down
1 change: 1 addition & 0 deletions Source/StrongGrid.UnitTests/StrongGrid.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
<ItemGroup>
<Compile Include="ClientTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Resources\GlobalSuppressionTests.cs" />
<Compile Include="Resources\StatisticsTests.cs" />
<Compile Include="Resources\SuppresionsTests.cs" />
<Compile Include="Resources\MailTests.cs" />
Expand Down

0 comments on commit bbc4d1b

Please sign in to comment.