forked from m1ome/narada
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslack_test.go
More file actions
106 lines (84 loc) · 2.13 KB
/
slack_test.go
File metadata and controls
106 lines (84 loc) · 2.13 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package narada
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
)
func TestNewClient(t *testing.T) {
gock.Clean()
// Reading fixture
buf, err := os.ReadFile("./fixtures/slack/request.json")
assert.NoError(t, err)
t.Run("Errors", func(t *testing.T) {
t.Run("Bad url", func(t *testing.T) {
client := NewClient("i am a bad url!")
msg := &SlackMessage{}
err := client.SendMessage(msg)
assert.Error(t, err)
})
t.Run("Bad response code", func(t *testing.T) {
defer gock.Off()
gock.New("http://slack_404_testing.slack.com").
Post("/").
Reply(404)
client := NewClient("http://slack_404_testing.slack.com")
msg := &SlackMessage{}
err := client.SendMessage(msg)
assert.Error(t, err)
})
})
t.Run("Success", func(t *testing.T) {
defer gock.Off()
gock.New("http://slack_testing.slack.com").
Post("/").
MatchType("json").
BodyString(string(buf)).
Reply(200)
client := NewClient("http://slack_testing.slack.com")
assert.NotNil(t, client)
msg := &SlackMessage{
Text: "Hello",
Username: "Bot",
IconEmoji: "icon-emoji",
IconUrl: "icon-url",
Channel: "general",
UnfurlLinks: false,
}
attach := NewAttachment()
attach.Text = "Attachment #1"
attach.Color = "red"
attach.Fallback = "http://google.com"
attach.Pretext = "pre text"
attach.Title = "Attachment title"
field1 := NewField()
field1.Title = "Field 1"
field1.Value = "Field 1 - Value"
field1.Short = true
field2 := NewField()
field2.Title = "Field 2"
field2.Value = "Field 2 - Value"
field2.Short = false
attach.AddField(field1)
attach.AddField(field2)
msg.AddAttachment(attach)
{
err := client.SendMessage(msg)
assert.NoError(t, err)
}
})
}
func TestMessage(t *testing.T) {
message := SlackMessage{}
assert.Equal(t, 0, len(message.Attachments))
attachment := NewAttachment()
message.AddAttachment(attachment)
assert.Equal(t, 1, len(message.Attachments))
}
func TestSlackError_Error(t *testing.T) {
err := SlackError{
Code: 100,
Body: "Error body",
}
assert.Equal(t, "SlackError: 100 Error body", err.Error())
}