Skip to content
Snippets Groups Projects
Unverified Commit c892b4b4 authored by Noah Moss's avatar Noah Moss Committed by GitHub
Browse files

clear slack token if saved with a non-existant files channel (#29397)

parent 0896fc2d
No related branches found
No related tags found
No related merge requests found
......@@ -40,6 +40,10 @@
(slack/clear-channel-cache!))
(let [processed-files-channel (slack/process-files-channel-name slack-files-channel)]
(when (and processed-files-channel (not (slack/channel-exists? processed-files-channel)))
;; Files channel could not be found; clear the token we had previously set since the integration should not be
;; enabled.
(slack/slack-token-valid?! false)
(slack/slack-app-token! nil)
(throw (ex-info (tru "Slack channel not found.")
{:errors {:slack-files-channel (tru "channel not found")}})))
(slack/slack-files-channel! processed-files-channel))
......
......@@ -192,12 +192,13 @@
params)))
(defn channel-exists?
"Returns true if the channel it exists."
"Returns a Boolean indicating whether a channel with a given name exists in the cache."
[channel-name]
(let [channel-names (into #{} (comp (map (juxt :name :id))
cat)
(:channels (slack-cached-channels-and-usernames)))]
(and channel-name (contains? channel-names channel-name))))
(boolean
(let [channel-names (into #{} (comp (map (juxt :name :id))
cat)
(:channels (slack-cached-channels-and-usernames)))]
(and channel-name (contains? channel-names channel-name)))))
(s/defn valid-token?
"Check whether a Slack token is valid by checking if the `conversations.list` Slack api accepts it."
......
......@@ -45,16 +45,22 @@
(mt/user-http-request :crowberto :put 200 "slack/settings" {:slack-files-channel "#fake-channel"})
(is (= "fake-channel" (slack/slack-files-channel))))))
(testing "An error is returned if the Slack files channel cannot be found"
(with-redefs [slack/channel-exists? (constantly nil)]
(testing "An error is returned if the Slack files channel cannot be found, and the integration is not enabled"
(with-redefs [slack/channel-exists? (constantly nil)
slack/valid-token? (constantly true)
slack/refresh-channels-and-usernames! (constantly nil)
slack/refresh-channels-and-usernames-when-needed! (constantly nil)]
(let [response (mt/user-http-request :crowberto :put 400 "slack/settings"
{:slack-files-channel "fake-channel"})]
(is (= {:slack-files-channel "channel not found"} (:errors response))))))
{:slack-files-channel "fake-channel"
:slack-app-token "fake-token"})]
(is (= {:slack-files-channel "channel not found"} (:errors response)))
(is (nil? (slack/slack-app-token)))
(is (= "metabase_files" (slack/slack-files-channel))))))
(testing "The Slack app token or files channel settings are cleared if no value is sent in the request"
(mt/with-temporary-setting-values [slack-app-token "fake-token"
slack-files-channel "fake-channel"
slack/slack-cached-channels-and-usernames ["fake_channel"]
(mt/with-temporary-setting-values [slack-app-token "fake-token"
slack-files-channel "fake-channel"
slack/slack-cached-channels-and-usernames ["fake_channel"]
slack/slack-channels-and-usernames-last-updated (t/zoned-date-time)]
(mt/user-http-request :crowberto :put 200 "slack/settings" {})
(is (= nil (slack/slack-app-token)))
......
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