| 1 | package slack |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "net/url" |
| 7 | "reflect" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func TestSlash_ServeHTTP(t *testing.T) { |
| 13 | once.Do(startServer) |
| 14 | serverURL := fmt.Sprintf("http://%s/slash", serverAddr) |
| 15 | |
| 16 | tests := []struct { |
| 17 | body url.Values |
| 18 | wantParams SlashCommand |
| 19 | wantStatusCode int |
| 20 | }{ |
| 21 | { |
| 22 | body: url.Values{ |
| 23 | "command": []string{"/command"}, |
| 24 | "team_domain": []string{"team"}, |
| 25 | "enterprise_id": []string{"E0001"}, |
| 26 | "enterprise_name": []string{"Globular%20Construct%20Inc"}, |
| 27 | "channel_id": []string{"C1234ABCD"}, |
| 28 | "text": []string{"text"}, |
| 29 | "team_id": []string{"T1234ABCD"}, |
| 30 | "user_id": []string{"U1234ABCD"}, |
| 31 | "user_name": []string{"username"}, |
| 32 | "response_url": []string{"https://hooks.slack.com/commands/XXXXXXXX/00000000000/YYYYYYYYYYYYYY"}, |
| 33 | "token": []string{"valid"}, |
| 34 | "channel_name": []string{"channel"}, |
| 35 | "trigger_id": []string{"0000000000.1111111111.222222222222aaaaaaaaaaaaaa"}, |
| 36 | "api_app_id": []string{"A123456"}, |
| 37 | }, |
| 38 | wantParams: SlashCommand{ |
| 39 | Command: "/command", |
| 40 | TeamDomain: "team", |
| 41 | EnterpriseID: "E0001", |
| 42 | EnterpriseName: "Globular%20Construct%20Inc", |
| 43 | ChannelID: "C1234ABCD", |
| 44 | Text: "text", |
| 45 | TeamID: "T1234ABCD", |
| 46 | UserID: "U1234ABCD", |
| 47 | UserName: "username", |
| 48 | ResponseURL: "https://hooks.slack.com/commands/XXXXXXXX/00000000000/YYYYYYYYYYYYYY", |
| 49 | Token: "valid", |
| 50 | ChannelName: "channel", |
| 51 | TriggerID: "0000000000.1111111111.222222222222aaaaaaaaaaaaaa", |
| 52 | APIAppID: "A123456", |
| 53 | }, |
| 54 | wantStatusCode: http.StatusOK, |
| 55 | }, |
| 56 | { |
| 57 | body: url.Values{ |
| 58 | "token": []string{"invalid"}, |
| 59 | }, |
| 60 | wantParams: SlashCommand{ |
| 61 | Token: "invalid", |
| 62 | }, |
| 63 | wantStatusCode: http.StatusUnauthorized, |
| 64 | }, |
| 65 | } |
| 66 | |
| 67 | var slashCommand SlashCommand |
| 68 | client := &http.Client{} |
| 69 | http.HandleFunc("/slash", func(w http.ResponseWriter, r *http.Request) { |
| 70 | var err error |
| 71 | slashCommand, err = SlashCommandParse(r) |
| 72 | if err != nil { |
| 73 | w.WriteHeader(http.StatusInternalServerError) |
| 74 | } |
| 75 | acceptableTokens := []string{"valid", "valid2"} |
| 76 | if !slashCommand.ValidateToken(acceptableTokens...) { |
| 77 | w.WriteHeader(http.StatusUnauthorized) |
| 78 | } |
| 79 | }) |
| 80 | |
| 81 | for i, test := range tests { |
| 82 | req, err := http.NewRequest(http.MethodPost, serverURL, strings.NewReader(test.body.Encode())) |
| 83 | if err != nil { |
| 84 | t.Fatalf("%d: Unexpected error: %s", i, err) |
| 85 | } |
| 86 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 87 | |
| 88 | resp, err := client.Do(req) |
| 89 | if err != nil { |
| 90 | t.Fatalf("%d: Unexpected error: %s", i, err) |
| 91 | } |
| 92 | |
| 93 | if resp.StatusCode != test.wantStatusCode { |
| 94 | t.Errorf("%d: Got status code %d, want %d", i, resp.StatusCode, test.wantStatusCode) |
| 95 | } |
| 96 | if !reflect.DeepEqual(slashCommand, test.wantParams) { |
| 97 | t.Errorf("%d: Got params %#v, want %#v", i, slashCommand, test.wantParams) |
| 98 | } |
| 99 | resp.Body.Close() |
| 100 | } |
| 101 | } |