| 1 | package slack |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "testing" |
| 7 | |
| 8 | "net/http" |
| 9 | |
| 10 | "github.com/stretchr/testify/assert" |
| 11 | ) |
| 12 | |
| 13 | // Dialogs |
| 14 | var simpleDialog = `{ |
| 15 | "callback_id":"ryde-46e2b0", |
| 16 | "title":"Request a Ride", |
| 17 | "submit_label":"Request", |
| 18 | "notify_on_cancel":true |
| 19 | }` |
| 20 | |
| 21 | var simpleTextElement = `{ |
| 22 | "label": "testing label", |
| 23 | "name": "testing name", |
| 24 | "type": "text", |
| 25 | "placeholder": "testing placeholder", |
| 26 | "optional": true, |
| 27 | "value": "testing value", |
| 28 | "max_length": 1000, |
| 29 | "min_length": 10, |
| 30 | "hint": "testing hint", |
| 31 | "subtype": "email" |
| 32 | }` |
| 33 | |
| 34 | var simpleSelectElement = `{ |
| 35 | "label": "testing label", |
| 36 | "name": "testing name", |
| 37 | "type": "select", |
| 38 | "placeholder": "testing placeholder", |
| 39 | "optional": true, |
| 40 | "value": "testing value", |
| 41 | "data_source": "users", |
| 42 | "selected_options": [], |
| 43 | "options": [{"label": "option 1", "value": "1"}], |
| 44 | "option_groups": [] |
| 45 | }` |
| 46 | |
| 47 | func unmarshalDialog() (*Dialog, error) { |
| 48 | dialog := &Dialog{} |
| 49 | // Unmarshall the simple dialog json |
| 50 | if err := json.Unmarshal([]byte(simpleDialog), &dialog); err != nil { |
| 51 | return nil, err |
| 52 | } |
| 53 | |
| 54 | // Unmarshall and append the text element |
| 55 | textElement := &TextInputElement{} |
| 56 | if err := json.Unmarshal([]byte(simpleTextElement), &textElement); err != nil { |
| 57 | return nil, err |
| 58 | } |
| 59 | |
| 60 | // Unmarshall and append the select element |
| 61 | selectElement := &DialogInputSelect{} |
| 62 | if err := json.Unmarshal([]byte(simpleSelectElement), &selectElement); err != nil { |
| 63 | return nil, err |
| 64 | } |
| 65 | |
| 66 | dialog.Elements = []DialogElement{ |
| 67 | textElement, |
| 68 | selectElement, |
| 69 | } |
| 70 | |
| 71 | return dialog, nil |
| 72 | } |
| 73 | |
| 74 | func TestSimpleDialog(t *testing.T) { |
| 75 | dialog, err := unmarshalDialog() |
| 76 | assert.Nil(t, err) |
| 77 | assertSimpleDialog(t, dialog) |
| 78 | } |
| 79 | |
| 80 | func TestCreateSimpleDialog(t *testing.T) { |
| 81 | dialog := &Dialog{} |
| 82 | dialog.CallbackID = "ryde-46e2b0" |
| 83 | dialog.Title = "Request a Ride" |
| 84 | dialog.SubmitLabel = "Request" |
| 85 | dialog.NotifyOnCancel = true |
| 86 | |
| 87 | textElement := &TextInputElement{} |
| 88 | textElement.Label = "testing label" |
| 89 | textElement.Name = "testing name" |
| 90 | textElement.Type = "text" |
| 91 | textElement.Placeholder = "testing placeholder" |
| 92 | textElement.Optional = true |
| 93 | textElement.Value = "testing value" |
| 94 | textElement.MaxLength = 1000 |
| 95 | textElement.MinLength = 10 |
| 96 | textElement.Hint = "testing hint" |
| 97 | textElement.Subtype = "email" |
| 98 | |
| 99 | selectElement := &DialogInputSelect{} |
| 100 | selectElement.Label = "testing label" |
| 101 | selectElement.Name = "testing name" |
| 102 | selectElement.Type = "select" |
| 103 | selectElement.Placeholder = "testing placeholder" |
| 104 | selectElement.Optional = true |
| 105 | selectElement.Value = "testing value" |
| 106 | selectElement.DataSource = "users" |
| 107 | selectElement.SelectedOptions = []DialogSelectOption{} |
| 108 | selectElement.Options = []DialogSelectOption{ |
| 109 | {Label: "option 1", Value: "1"}, |
| 110 | } |
| 111 | selectElement.OptionGroups = []DialogOptionGroup{} |
| 112 | |
| 113 | dialog.Elements = []DialogElement{ |
| 114 | textElement, |
| 115 | selectElement, |
| 116 | } |
| 117 | |
| 118 | assertSimpleDialog(t, dialog) |
| 119 | } |
| 120 | |
| 121 | func assertSimpleDialog(t *testing.T, dialog *Dialog) { |
| 122 | assert.NotNil(t, dialog) |
| 123 | |
| 124 | // Test the main dialog fields |
| 125 | assert.Equal(t, "ryde-46e2b0", dialog.CallbackID) |
| 126 | assert.Equal(t, "Request a Ride", dialog.Title) |
| 127 | assert.Equal(t, "Request", dialog.SubmitLabel) |
| 128 | assert.Equal(t, true, dialog.NotifyOnCancel) |
| 129 | |
| 130 | // Test the text element is correctly parsed |
| 131 | textElement := dialog.Elements[0].(*TextInputElement) |
| 132 | assert.Equal(t, "testing label", textElement.Label) |
| 133 | assert.Equal(t, "testing name", textElement.Name) |
| 134 | assert.Equal(t, InputTypeText, textElement.Type) |
| 135 | assert.Equal(t, "testing placeholder", textElement.Placeholder) |
| 136 | assert.Equal(t, true, textElement.Optional) |
| 137 | assert.Equal(t, "testing value", textElement.Value) |
| 138 | assert.Equal(t, 1000, textElement.MaxLength) |
| 139 | assert.Equal(t, 10, textElement.MinLength) |
| 140 | assert.Equal(t, "testing hint", textElement.Hint) |
| 141 | assert.Equal(t, InputSubtypeEmail, textElement.Subtype) |
| 142 | |
| 143 | // Test the select element is correctly parsed |
| 144 | selectElement := dialog.Elements[1].(*DialogInputSelect) |
| 145 | assert.Equal(t, "testing label", selectElement.Label) |
| 146 | assert.Equal(t, "testing name", selectElement.Name) |
| 147 | assert.Equal(t, InputTypeSelect, selectElement.Type) |
| 148 | assert.Equal(t, "testing placeholder", selectElement.Placeholder) |
| 149 | assert.Equal(t, true, selectElement.Optional) |
| 150 | assert.Equal(t, "testing value", selectElement.Value) |
| 151 | assert.Equal(t, DialogDataSourceUsers, selectElement.DataSource) |
| 152 | assert.Equal(t, []DialogSelectOption{}, selectElement.SelectedOptions) |
| 153 | assert.Equal(t, "option 1", selectElement.Options[0].Label) |
| 154 | assert.Equal(t, "1", selectElement.Options[0].Value) |
| 155 | assert.Equal(t, 0, len(selectElement.OptionGroups)) |
| 156 | } |
| 157 | |
| 158 | // Callbacks |
| 159 | var simpleCallback = `{ |
| 160 | "type": "dialog_submission", |
| 161 | "submission": { |
| 162 | "name": "Sigourney Dreamweaver", |
| 163 | "email": "[email protected]", |
| 164 | "phone": "+1 800-555-1212", |
| 165 | "meal": "burrito", |
| 166 | "comment": "No sour cream please", |
| 167 | "team_channel": "C0LFFBKPB", |
| 168 | "who_should_sing": "U0MJRG1AL" |
| 169 | }, |
| 170 | "callback_id": "employee_offsite_1138b", |
| 171 | "team": { |
| 172 | "id": "T1ABCD2E12", |
| 173 | "domain": "coverbands" |
| 174 | }, |
| 175 | "user": { |
| 176 | "id": "W12A3BCDEF", |
| 177 | "name": "dreamweaver" |
| 178 | }, |
| 179 | "channel": { |
| 180 | "id": "C1AB2C3DE", |
| 181 | "name": "coverthon-1999" |
| 182 | }, |
| 183 | "action_ts": "936893340.702759", |
| 184 | "token": "M1AqUUw3FqayAbqNtsGMch72", |
| 185 | "response_url": "https://hooks.slack.com/app/T012AB0A1/123456789/JpmK0yzoZDeRiqfeduTBYXWQ" |
| 186 | }` |
| 187 | |
| 188 | func unmarshalCallback(j string) (*DialogCallback, error) { |
| 189 | callback := &DialogCallback{} |
| 190 | if err := json.Unmarshal([]byte(j), &callback); err != nil { |
| 191 | return nil, err |
| 192 | } |
| 193 | return callback, nil |
| 194 | } |
| 195 | |
| 196 | func TestSimpleCallback(t *testing.T) { |
| 197 | callback, err := unmarshalCallback(simpleCallback) |
| 198 | assert.Nil(t, err) |
| 199 | assertSimpleCallback(t, callback) |
| 200 | } |
| 201 | |
| 202 | func assertSimpleCallback(t *testing.T, callback *DialogCallback) { |
| 203 | assert.NotNil(t, callback) |
| 204 | assert.Equal(t, InteractionTypeDialogSubmission, callback.Type) |
| 205 | assert.Equal(t, "employee_offsite_1138b", callback.CallbackID) |
| 206 | assert.Equal(t, "T1ABCD2E12", callback.Team.ID) |
| 207 | assert.Equal(t, "coverbands", callback.Team.Domain) |
| 208 | assert.Equal(t, "C1AB2C3DE", callback.Channel.ID) |
| 209 | assert.Equal(t, "coverthon-1999", callback.Channel.Name) |
| 210 | assert.Equal(t, "W12A3BCDEF", callback.User.ID) |
| 211 | assert.Equal(t, "dreamweaver", callback.User.Name) |
| 212 | assert.Equal(t, "936893340.702759", callback.ActionTs) |
| 213 | assert.Equal(t, "M1AqUUw3FqayAbqNtsGMch72", callback.Token) |
| 214 | assert.Equal(t, "https://hooks.slack.com/app/T012AB0A1/123456789/JpmK0yzoZDeRiqfeduTBYXWQ", callback.ResponseURL) |
| 215 | assert.Equal(t, "Sigourney Dreamweaver", callback.Submission["name"]) |
| 216 | assert.Equal(t, "[email protected]", callback.Submission["email"]) |
| 217 | assert.Equal(t, "+1 800-555-1212", callback.Submission["phone"]) |
| 218 | assert.Equal(t, "burrito", callback.Submission["meal"]) |
| 219 | assert.Equal(t, "No sour cream please", callback.Submission["comment"]) |
| 220 | assert.Equal(t, "C0LFFBKPB", callback.Submission["team_channel"]) |
| 221 | assert.Equal(t, "U0MJRG1AL", callback.Submission["who_should_sing"]) |
| 222 | } |
| 223 | |
| 224 | // Suggestion Callbacks |
| 225 | var simpleSuggestionCallback = `{ |
| 226 | "type": "dialog_suggestion", |
| 227 | "token": "W3VDvuzi2nRLsiaDOsmJranO", |
| 228 | "action_ts": "1528203589.238335", |
| 229 | "team": { |
| 230 | "id": "T24BK35ML", |
| 231 | "domain": "hooli-hq" |
| 232 | }, |
| 233 | "user": { |
| 234 | "id": "U900MV5U7", |
| 235 | "name": "gbelson" |
| 236 | }, |
| 237 | "channel": { |
| 238 | "id": "C012AB3CD", |
| 239 | "name": "triage-platform" |
| 240 | }, |
| 241 | "name": "external_data", |
| 242 | "value": "test", |
| 243 | "callback_id": "bugs" |
| 244 | }` |
| 245 | |
| 246 | func unmarshalSuggestionCallback(j string) (*InteractionCallback, error) { |
| 247 | callback := &InteractionCallback{} |
| 248 | if err := json.Unmarshal([]byte(j), &callback); err != nil { |
| 249 | return nil, err |
| 250 | } |
| 251 | return callback, nil |
| 252 | } |
| 253 | |
| 254 | func TestSimpleSuggestionCallback(t *testing.T) { |
| 255 | callback, err := unmarshalSuggestionCallback(simpleSuggestionCallback) |
| 256 | assert.Nil(t, err) |
| 257 | assertSimpleSuggestionCallback(t, callback) |
| 258 | } |
| 259 | |
| 260 | func assertSimpleSuggestionCallback(t *testing.T, callback *InteractionCallback) { |
| 261 | assert.NotNil(t, callback) |
| 262 | assert.Equal(t, InteractionTypeDialogSuggestion, callback.Type) |
| 263 | assert.Equal(t, "W3VDvuzi2nRLsiaDOsmJranO", callback.Token) |
| 264 | assert.Equal(t, "1528203589.238335", callback.ActionTs) |
| 265 | assert.Equal(t, "T24BK35ML", callback.Team.ID) |
| 266 | assert.Equal(t, "hooli-hq", callback.Team.Domain) |
| 267 | assert.Equal(t, "U900MV5U7", callback.User.ID) |
| 268 | assert.Equal(t, "gbelson", callback.User.Name) |
| 269 | assert.Equal(t, "C012AB3CD", callback.Channel.ID) |
| 270 | assert.Equal(t, "triage-platform", callback.Channel.Name) |
| 271 | assert.Equal(t, "external_data", callback.Name) |
| 272 | assert.Equal(t, "test", callback.Value) |
| 273 | assert.Equal(t, "bugs", callback.CallbackID) |
| 274 | } |
| 275 | |
| 276 | func openDialogHandler(rw http.ResponseWriter, r *http.Request) { |
| 277 | rw.Header().Set("Content-Type", "application/json") |
| 278 | response, _ := json.Marshal(struct { |
| 279 | SlackResponse |
| 280 | }{ |
| 281 | SlackResponse: SlackResponse{Ok: true}, |
| 282 | }) |
| 283 | rw.Write(response) |
| 284 | } |
| 285 | |
| 286 | func TestOpenDialog(t *testing.T) { |
| 287 | http.HandleFunc("/dialog.open", openDialogHandler) |
| 288 | once.Do(startServer) |
| 289 | api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/")) |
| 290 | dialog, err := unmarshalDialog() |
| 291 | if err != nil { |
| 292 | t.Errorf("Unexpected error: %s", err) |
| 293 | return |
| 294 | } |
| 295 | err = api.OpenDialog("TXXXXXXXX", *dialog) |
| 296 | if err != nil { |
| 297 | t.Errorf("Unexpected error: %s", err) |
| 298 | return |
| 299 | } |
| 300 | err = api.OpenDialog("", *dialog) |
| 301 | if err == nil { |
| 302 | t.Errorf("Did not error with empty trigger, %s", err) |
| 303 | return |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | const ( |
| 308 | triggerID = "trigger_xyz" |
| 309 | callbackID = "callback_xyz" |
| 310 | notifyOnCancel = false |
| 311 | title = "Dialog_title" |
| 312 | submitLabel = "Send" |
| 313 | token = "xoxa-123-123-123-213" |
| 314 | ) |
| 315 | |
| 316 | func _mocDialog() *Dialog { |
| 317 | triggerID := triggerID |
| 318 | callbackID := callbackID |
| 319 | notifyOnCancel := notifyOnCancel |
| 320 | title := title |
| 321 | submitLabel := submitLabel |
| 322 | |
| 323 | return &Dialog{ |
| 324 | TriggerID: triggerID, |
| 325 | CallbackID: callbackID, |
| 326 | NotifyOnCancel: notifyOnCancel, |
| 327 | Title: title, |
| 328 | SubmitLabel: submitLabel, |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | func TestDialogCreate(t *testing.T) { |
| 333 | dialog := _mocDialog() |
| 334 | if dialog == nil { |
| 335 | t.Errorf("Should be able to construct a dialog") |
| 336 | t.Fail() |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | func ExampleDialog() { |
| 341 | dialog := _mocDialog() |
| 342 | fmt.Println(*dialog) |
| 343 | // Output: |
| 344 | // {trigger_xyz callback_xyz Dialog_title Send false []} |
| 345 | } |