-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from merodriguezblanco/slack-support
Slack support
- Loading branch information
Showing
20 changed files
with
321 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
ExceptionNotification.Core.Tests/Slack/SlackClientTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using ExceptionNotification.Core.Exceptions; | ||
using ExceptionNotification.Core.Slack; | ||
using Xunit; | ||
|
||
namespace ExceptionNotification.Core.Tests.Slack | ||
{ | ||
[Trait("Category", "Unit")] | ||
public class SlackClientTests | ||
{ | ||
[Fact] | ||
public void SlackClientThrowsExceptionWhenMalformedWebhookUri() | ||
{ | ||
var uri = ""; | ||
var exception = Assert.Throws<MalformedUriException>(() => new SlackClient(uri)); | ||
Assert.Equal("SlackClient failure: Slack's webhook URI is invalid.", exception.Message); | ||
|
||
uri = null; | ||
exception = Assert.Throws<MalformedUriException>(() => new SlackClient(uri)); | ||
Assert.Equal("SlackClient failure: Slack's webhook URI is invalid.", exception.Message); | ||
|
||
uri = "invalid/uri"; | ||
exception = Assert.Throws<MalformedUriException>(() => new SlackClient(uri)); | ||
Assert.Equal("SlackClient failure: Slack's webhook URI is invalid.", exception.Message); | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
ExceptionNotification.Core.Tests/Slack/SlackMessageBuilderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
using ExceptionNotification.Core.Exceptions; | ||
using ExceptionNotification.Core.Slack; | ||
using Xunit; | ||
|
||
namespace ExceptionNotification.Core.Tests.Slack | ||
{ | ||
[Trait("Category", "Unit")] | ||
public class SlackMessageBuilderTests | ||
{ | ||
private readonly Exception _exception; | ||
|
||
private readonly NotifierOptions _notifierOptions; | ||
|
||
private readonly SlackConfiguration _slackConfiguration; | ||
|
||
public SlackMessageBuilderTests() | ||
{ | ||
_exception = new Exception("This is an exception!"); | ||
_notifierOptions = new NotifierOptions | ||
{ | ||
ProjectName = "Fried Chicken", | ||
Environment = "Development" | ||
}; | ||
_slackConfiguration = new SlackConfiguration | ||
{ | ||
Channel = "the chicken channel", | ||
Username = "achicken", | ||
WebhookUri = "http://chicken-channel.slack.com/services/hooks/incomig-webhook?token=123" | ||
}; | ||
} | ||
|
||
[Fact] | ||
public void SlackMessageBuilderThrowsExceptionWhenInvalidWebhookUri() | ||
{ | ||
_slackConfiguration.WebhookUri = "invalid"; | ||
|
||
var exception = Assert.Throws<MalformedUriException>(() => new SlackMessageBuilder(_slackConfiguration, _exception, _notifierOptions, null)); | ||
Assert.Equal("SlackMessageBuilder failure: Slack's webhook URI is invalid.", exception.Message); | ||
} | ||
|
||
[Fact] | ||
public void ComposeMessageBuildsExceptionMessage() | ||
{ | ||
var message = new SlackMessageBuilder(_slackConfiguration, _exception, _notifierOptions, null).ComposeMessage(); | ||
|
||
Assert.IsType<SlackMessage>(message); | ||
Assert.Equal("the chicken channel", message.Channel); | ||
Assert.Equal("achicken", message.Username); | ||
Assert.Contains("[Fried Chicken - Development] EXCEPTION!", message.Text); | ||
Assert.Contains("This is an exception!", message.Text); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
ExceptionNotification.Core.Tests/Slack/SlackNotifierTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
using ExceptionNotification.Core.Exceptions; | ||
using ExceptionNotification.Core.Slack; | ||
using Xunit; | ||
|
||
namespace ExceptionNotification.Core.Tests.Slack | ||
{ | ||
[Trait("Category", "Unit")] | ||
public class SlackNotifierTests | ||
{ | ||
[Fact] | ||
public void FireExceptionThrowsExceptionWhenConfigurationIsNull() | ||
{ | ||
var notifier = new SlackNotifier(null); | ||
|
||
var exception = Assert.Throws<ConfigurationMissingException>(() => notifier.FireNotification(new Exception(), null)); | ||
Assert.Equal("FireNotification failure: configuration is null.", exception.Message); | ||
} | ||
|
||
[Fact] | ||
public void FireExceptionThrowsExceptionWhenExceptionIsNull() | ||
{ | ||
var notifier = new SlackNotifier(new SlackConfiguration()); | ||
|
||
var exception = Assert.Throws<ExceptionMissingException>(() => notifier.FireNotification(null, null)); | ||
Assert.Equal("FireNotification failure: exception is null.", exception.Message); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
ExceptionNotification.Core/Exceptions/MalformedUriException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
|
||
namespace ExceptionNotification.Core.Exceptions | ||
{ | ||
public class MalformedUriException : Exception | ||
{ | ||
public MalformedUriException(string message) : this(message, null) | ||
{ } | ||
|
||
public MalformedUriException(string message, Exception innerException) : base(message, innerException) | ||
{ } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using ExceptionNotification.Core.Exceptions; | ||
using Newtonsoft.Json; | ||
|
||
namespace ExceptionNotification.Core.Slack | ||
{ | ||
public class SlackClient | ||
{ | ||
private readonly Uri _webhookUri; | ||
|
||
public SlackClient(Uri webhookUri) | ||
{ | ||
_webhookUri = webhookUri; | ||
} | ||
|
||
public SlackClient(string webhookUri) | ||
{ | ||
if (!Uri.IsWellFormedUriString(webhookUri, UriKind.Absolute)) | ||
{ | ||
throw new MalformedUriException("SlackClient failure: Slack's webhook URI is invalid."); | ||
} | ||
|
||
_webhookUri = new Uri(webhookUri); | ||
} | ||
|
||
public async Task<HttpResponseMessage> SendNotificationAsync(SlackMessage message) | ||
{ | ||
using (var client = new HttpClient()) | ||
{ | ||
var serializedPayload = JsonConvert.SerializeObject(message); | ||
var payload = new StringContent(serializedPayload, Encoding.UTF8, "application/json"); | ||
var response = await client.PostAsync(_webhookUri, payload); | ||
|
||
return response; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace ExceptionNotification.Core.Slack | ||
{ | ||
public class SlackConfiguration | ||
{ | ||
public string WebhookUri { get; set; } | ||
|
||
public string Channel { get; set; } | ||
|
||
public string Username { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace ExceptionNotification.Core.Slack | ||
{ | ||
public class SlackMessage | ||
{ | ||
[JsonProperty("channel")] | ||
public string Channel { get; set; } | ||
|
||
[JsonProperty("username")] | ||
public string Username { get; set; } | ||
|
||
[JsonProperty("text")] | ||
public string Text { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using ExceptionNotification.Core.Exceptions; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace ExceptionNotification.Core.Slack | ||
{ | ||
public class SlackMessageBuilder : ExceptionMessageBuilder | ||
{ | ||
private readonly SlackConfiguration _configuration; | ||
|
||
public SlackMessageBuilder(SlackConfiguration configuration, Exception exception, | ||
NotifierOptions notifierOptions, HttpRequest request) : base(exception, notifierOptions, request) | ||
{ | ||
if (!Uri.IsWellFormedUriString(configuration.WebhookUri, UriKind.Absolute)) | ||
{ | ||
throw new MalformedUriException("SlackMessageBuilder failure: Slack's webhook URI is invalid."); | ||
} | ||
|
||
_configuration = configuration; | ||
} | ||
|
||
public SlackMessage ComposeMessage() | ||
{ | ||
var messageBody = $"{ComposeSubject()}\n\n {ComposeContent()}"; | ||
var message = new SlackMessage | ||
{ | ||
Channel = _configuration.Channel, | ||
Username = _configuration.Username, | ||
Text = messageBody | ||
}; | ||
|
||
return message; | ||
} | ||
} | ||
} |
Oops, something went wrong.