slack_service_spec.rb
1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
describe NotificationServices::SlackService, type: 'model' do
let(:notice) { Fabricate :notice }
let(:service_url) do
"https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXX"
end
let(:service) do
Fabricate :slack_notification_service, app: notice.app,
service_url: service_url
end
it "should have icon for slack" do
expect(Rails.root.join("docs/notifications/slack/errbit.png")).to exist
end
it "should send a notification to Slack with hook url" do
# setup
problem = notice.problem
# faraday stubbing
payload = {
username: "Errbit",
icon_url: "https://raw.githubusercontent.com/errbit/errbit/master/docs/notifications/slack/errbit.png",
attachments: [
{
fallback: service.message_for_slack(problem),
title: problem.message.to_s.truncate(100),
title_link: problem.url,
text: problem.where,
color: "#D00000",
fields: [
{
title: "Application",
value: problem.app.name,
short: true
},
{
title: "Environment",
value: problem.environment,
short: true
},
{
title: "Times Occurred",
value: problem.notices_count,
short: true
},
{
title: "First Noticed",
value: problem.first_notice_at.try(:to_s, :db),
short: true
}
]
}
]
}.to_json
expect(HTTParty).to receive(:post).with(service.service_url, body: payload, headers: { "Content-Type" => "application/json" }).and_return(true)
service.create_notification(problem)
end
end