1+ package slackhttp
2+
3+ import (
4+ "encoding/json"
5+ "github.com/slack-go/slack"
6+ "net/http"
7+ "net/http/httptest"
8+ "testing"
9+ )
10+
11+ func Test_handleSlackCommand (t * testing.T ) {
12+ t .Run ("invalid command" , func (t * testing.T ) {
13+ s := & server {}
14+ r := & NamedRepositoryRegistry {}
15+
16+ tests := []struct {
17+ name string
18+ command string
19+ expect string
20+ }{
21+ {
22+ name : "when using missing bits" ,
23+ command : "totally wrong" ,
24+ expect : "Incorrect usage, expected /gitops-commit [command] [name] [tag]" ,
25+ },
26+ {
27+ name : "when using an invalid command" ,
28+ command : "wrong thing v1.2.3" ,
29+ expect : "Unknown command 'wrong', expected /gitops-commit [command] [name] [tag]" ,
30+ },
31+ {
32+ name : "when using a valid command " ,
33+ command : "deploy thing v1.2.3" ,
34+ expect : "Unknown named repository, cannot handle \" thing\" , availabe options ()" ,
35+ },
36+ }
37+
38+ for _ , tt := range tests {
39+ slackCommand := slack.SlashCommand {
40+ Text : tt .command ,
41+ }
42+
43+ t .Run (tt .name , func (t * testing.T ) {
44+ rr := httptest .NewRecorder ()
45+
46+ s .handleSlackCommand (r )(rr , slackCommand )
47+
48+ if c := rr .Code ; c != http .StatusOK {
49+ t .Errorf ("handleSlackCommand() = %v, want %v" , c , http .StatusOK )
50+ }
51+
52+ response := make (map [string ]interface {})
53+ err := json .Unmarshal (rr .Body .Bytes (), & response )
54+
55+ if err != nil {
56+ t .Errorf ("handleSlackCommand() invalid json: %s" , err )
57+ }
58+
59+ if got , ok := response ["text" ].(string ); ok {
60+ if got != tt .expect {
61+ t .Errorf ("expected '%s', got '%s'" , tt .expect , got )
62+ }
63+ }
64+ })
65+ }
66+ })
67+ }
0 commit comments