Skip to content
Snippets Groups Projects
Commit 088aea67 authored by Ryan Senior's avatar Ryan Senior
Browse files

Add tests around slack integration (specifically user/channel calls)

parent 2ffc1f07
No related branches found
No related tags found
No related merge requests found
......@@ -140,7 +140,8 @@
:docstring-checker {:include [#"^metabase"]
:exclude [#"test"
#"^metabase\.http-client$"]}
:profiles {:dev {:dependencies [[expectations "2.2.0-beta2"] ; unit tests
:profiles {:dev {:dependencies [[clj-http-fake "1.0.3"] ; Library to mock clj-http responses
[expectations "2.2.0-beta2"] ; unit tests
[ring/ring-mock "0.3.0"]] ; Library to create mock Ring requests for unit tests
:plugins [[docstring-checker "1.0.2"] ; Check that all public vars have docstrings. Run with 'lein docstring-checker'
[jonase/eastwood "0.3.1"
......
(ns metabase.integrations.slack-test
(:require [metabase.integrations.slack :as slack-integ :refer :all]
[expectations :refer :all]
[clj-http.fake :as http-fake]
[metabase.test.util :as tu]
[cheshire.core :as json]
[clojure.java.io :as io]))
(def ^:private default-channels-response
(delay (slurp (io/resource "slack_channels_response.json"))))
(def ^:private default-channels
(delay (:channels (json/parse-string @default-channels-response keyword))))
(def ^:private channels-request
{:address "https://slack.com/api/channels.list"
:query-params {:token "test-token"
:exclude_archived "1"}})
(defn- expected-200-response [body]
(fn [_]
{:status 200
:body (if (string? body)
body
(json/generate-string body))}))
(def ^:private invalid-token-response
(expected-200-response
{:ok false
:error "invalid_auth"}))
(defn- exception-if-called [_]
(throw (Exception. "Failure, route should not have been invoked")))
;; Channels should return nil if no Slack token has been configured
(expect
nil
(http-fake/with-fake-routes {channels-request exception-if-called}
(tu/with-temporary-setting-values [slack-token nil]
(channels-list))))
;; Test the channels call and expected response
(expect
@default-channels
(http-fake/with-fake-routes {channels-request (expected-200-response @default-channels-response)}
(tu/with-temporary-setting-values [slack-token "test-token"]
(channels-list))))
;; Test the invalid token auth flow
(expect
{:ex-class clojure.lang.ExceptionInfo
:msg nil
:data {:errors {:slack-token "Invalid token"}}}
(http-fake/with-fake-routes {channels-request invalid-token-response}
(tu/with-temporary-setting-values [slack-token "test-token"]
(tu/exception-and-message
(channels-list)))))
(def ^:private default-users-response
(delay (slurp (io/resource "slack_users_response.json"))))
(def ^:private default-users
(delay (:members (json/parse-string @default-users-response keyword))))
(def ^:private users-request
{:address "https://slack.com/api/users.list"
:query-params {:token "test-token"}})
;; Users should return nil if no Slack token has been configured
(expect
nil
(http-fake/with-fake-routes {users-request exception-if-called}
(tu/with-temporary-setting-values [slack-token nil]
(users-list))))
;; Test the users call and the expected response
(expect
@default-users
(http-fake/with-fake-routes {users-request (expected-200-response @default-users-response)}
(tu/with-temporary-setting-values [slack-token "test-token"]
(users-list))))
;; Test the invalid token auth flow for users
(expect
{:ex-class clojure.lang.ExceptionInfo
:msg nil
:data {:errors {:slack-token "Invalid token"}}}
(http-fake/with-fake-routes {users-request invalid-token-response}
(tu/with-temporary-setting-values [slack-token "test-token"]
(tu/exception-and-message
(users-list)))))
(def ^:private files-request
(assoc-in channels-request [:query-params :exclude_archived] "0"))
;; Asking for the files channel when slack is not configured throws an exception
(expect
{:ex-class clojure.lang.ExceptionInfo
:msg (var-get #'slack-integ/channel-missing-msg)
:data {:status-code 400}}
(http-fake/with-fake-routes {files-request exception-if-called}
(tu/exception-and-message
(files-channel))))
(defn- create-files-channel []
(let [channel-name (var-get #'slack-integ/files-channel-name)]
(-> @default-channels
first
(assoc
:name channel-name, :name_normalized channel-name,
:purpose {:value "Metabase file upload location", :creator "", :last_set 0}))))
;; Testing the call that finds the metabase files channel
(expect
(create-files-channel)
(http-fake/with-fake-routes {files-request (-> @default-channels-response
json/parse-string
(update :channels conj (create-files-channel))
expected-200-response)}
(tu/with-temporary-setting-values [slack-token "test-token"]
(files-channel))))
{
"ok": true,
"channels": [
{
"id": "C0G9QF9GW",
"name": "random",
"is_channel": true,
"created": 1449709280,
"creator": "U0G9QF9C6",
"is_archived": false,
"is_general": false,
"name_normalized": "random",
"is_shared": false,
"is_org_shared": false,
"is_member": true,
"is_private": false,
"is_mpim": false,
"members": [
"U0G9QF9C6",
"U0G9WFXNZ"
],
"topic": {
"value": "Other stuff",
"creator": "U0G9QF9C6",
"last_set": 1449709352
},
"purpose": {
"value": "A place for non-work-related flimflam, faffing, hodge-podge or jibber-jabber you'd prefer to keep out of more focused work-related channels.",
"creator": "",
"last_set": 0
},
"previous_names": [],
"num_members": 2
},
{
"id": "C0G9QKBBL",
"name": "general",
"is_channel": true,
"created": 1449709280,
"creator": "U0G9QF9C6",
"is_archived": false,
"is_general": true,
"name_normalized": "general",
"is_shared": false,
"is_org_shared": false,
"is_member": true,
"is_private": false,
"is_mpim": false,
"members": [
"U0G9QF9C6",
"U0G9WFXNZ"
],
"topic": {
"value": "Talk about anything!",
"creator": "U0G9QF9C6",
"last_set": 1449709364
},
"purpose": {
"value": "To talk about anything!",
"creator": "U0G9QF9C6",
"last_set": 1449709334
},
"previous_names": [],
"num_members": 2
}
],
"response_metadata": {
"next_cursor": "dGVhbTpDMUg5UkVTR0w="
}
}
{
"ok": true,
"members": [
{
"id": "W012A3CDE",
"team_id": "T012AB3C4",
"name": "spengler",
"deleted": false,
"color": "9f69e7",
"real_name": "spengler",
"tz": "America/Los_Angeles",
"tz_label": "Pacific Daylight Time",
"tz_offset": -25200,
"profile": {
"avatar_hash": "ge3b51ca72de",
"status_text": "Print is dead",
"status_emoji": ":books:",
"real_name": "Egon Spengler",
"display_name": "spengler",
"real_name_normalized": "Egon Spengler",
"display_name_normalized": "spengler",
"email": "spengler@ghostbusters.example.com",
"image_24": "https://.../avatar/e3b51ca72dee4ef87916ae2b9240df50.jpg",
"image_32": "https://.../avatar/e3b51ca72dee4ef87916ae2b9240df50.jpg",
"image_48": "https://.../avatar/e3b51ca72dee4ef87916ae2b9240df50.jpg",
"image_72": "https://.../avatar/e3b51ca72dee4ef87916ae2b9240df50.jpg",
"image_192": "https://.../avatar/e3b51ca72dee4ef87916ae2b9240df50.jpg",
"image_512": "https://.../avatar/e3b51ca72dee4ef87916ae2b9240df50.jpg",
"team": "T012AB3C4"
},
"is_admin": true,
"is_owner": false,
"is_primary_owner": false,
"is_restricted": false,
"is_ultra_restricted": false,
"is_bot": false,
"updated": 1502138686,
"is_app_user": false,
"has_2fa": false
},
{
"id": "W07QCRPA4",
"team_id": "T0G9PQBBK",
"name": "glinda",
"deleted": false,
"color": "9f69e7",
"real_name": "Glinda Southgood",
"tz": "America/Los_Angeles",
"tz_label": "Pacific Daylight Time",
"tz_offset": -25200,
"profile": {
"avatar_hash": "8fbdd10b41c6",
"image_24": "https://a.slack-edge.com...png",
"image_32": "https://a.slack-edge.com...png",
"image_48": "https://a.slack-edge.com...png",
"image_72": "https://a.slack-edge.com...png",
"image_192": "https://a.slack-edge.com...png",
"image_512": "https://a.slack-edge.com...png",
"image_1024": "https://a.slack-edge.com...png",
"image_original": "https://a.slack-edge.com...png",
"first_name": "Glinda",
"last_name": "Southgood",
"title": "Glinda the Good",
"phone": "",
"skype": "",
"real_name": "Glinda Southgood",
"real_name_normalized": "Glinda Southgood",
"display_name": "Glinda the Fairly Good",
"display_name_normalized": "Glinda the Fairly Good",
"email": "glenda@south.oz.coven"
},
"is_admin": true,
"is_owner": false,
"is_primary_owner": false,
"is_restricted": false,
"is_ultra_restricted": false,
"is_bot": false,
"updated": 1480527098,
"has_2fa": false
}
],
"cache_ts": 1498777272,
"response_metadata": {
"next_cursor": "dXNlcjpVMEc5V0ZYTlo="
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment