moved channel test function the handle message, better testability

pull/121/head
Karolis Rusenas 2017-12-12 10:11:58 +00:00
parent affd2ba760
commit 0792c77ce8
2 changed files with 8 additions and 48 deletions

View File

@ -213,11 +213,6 @@ func (b *Bot) postMessage(title, message, color string, fields []slack.Attachmen
}
func (b *Bot) isApproval(event *slack.MessageEvent, eventText string) (resp *approvalResponse, ok bool) {
// only accepting approvals from approvals channel
if !b.isApprovalsChannel(event) {
return nil, false
}
if strings.HasPrefix(strings.ToLower(eventText), approvalResponseKeyword) {
return &approvalResponse{
User: event.User,
@ -265,10 +260,14 @@ func (b *Bot) handleMessage(event *slack.MessageEvent) {
}
eventText = b.trimBot(eventText)
approval, ok := b.isApproval(event, eventText)
if ok {
b.approvalsRespCh <- approval
return
// only accepting approvals from approvals channel
if b.isApprovalsChannel(event) {
approval, ok := b.isApproval(event, eventText)
if ok {
b.approvalsRespCh <- approval
return
}
}
// Responses that are just a canned string response

View File

@ -352,42 +352,3 @@ func TestIsApproval(t *testing.T) {
t.Errorf("event expected to be an approval")
}
}
func TestIsApprovalDifferentChannel(t *testing.T) {
f8s := &testutil.FakeK8sImplementer{}
mem := memory.NewMemoryCache(100*time.Hour, 100*time.Hour, 100*time.Hour)
identifier := "k8s/project/repo:1.2.3"
am := approvals.New(mem, codecs.DefaultSerializer())
// creating initial approve request
err := am.Create(&types.Approval{
Identifier: identifier,
VotesRequired: 2,
CurrentVersion: "2.3.4",
NewVersion: "3.4.5",
Event: &types.Event{
Repository: types.Repository{
Name: "project/repo",
Tag: "2.3.4",
},
},
})
if err != nil {
t.Fatalf("unexpected error while creating : %s", err)
}
bot := New("keel", "random", "approvals", f8s, am)
_, isApproval := bot.isApproval(&slack.MessageEvent{
Msg: slack.Msg{
Channel: "general", // should be ignored
User: "user-x",
},
}, "approve k8s/project/repo:1.2.3")
if isApproval {
t.Errorf("event unexpected to be an approval")
}
}