-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpuzzles_controller_test.rb
More file actions
55 lines (44 loc) · 1.71 KB
/
puzzles_controller_test.rb
File metadata and controls
55 lines (44 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require "test_helper"
class Slack::PuzzlesControllerTest < ActionDispatch::IntegrationTest
setup do
ENV["SLACK_SIGNING_SECRET"] = "test_signing_secret"
end
test "renders ok when puzzle fails validation" do
params = { payload: puzzle_payload(question: "") }
post slack_puzzle_path, params: params,
headers: slack_headers(body: params.to_query)
assert_response :ok
end
test "renders ok even when Slack notification fails after puzzle is saved" do
# Notification failure should only log — it must not affect the response to Slack.
original = Slack::ApplicationController.instance_method(:send_message)
Slack::ApplicationController.define_method(:send_message) { |*| nil }
params = { payload: puzzle_payload(question: "What is unique about Ruby's blocks?") }
post slack_puzzle_path, params: params,
headers: slack_headers(body: params.to_query)
assert_response :ok
ensure
Slack::ApplicationController.define_method(:send_message, original)
end
private
def puzzle_payload(question: "What is Ruby?")
{
user: { id: "U123" },
view: {
state: {
values: {
question: { question: { value: question } },
answer: { answer: { selected_option: { value: "ruby" } } },
explanation: { explanation: { value: "It is a programming language." } },
link: { link: { value: nil } }
}
}
}
}.to_json
end
def slack_headers(secret: ENV["SLACK_SIGNING_SECRET"], timestamp: Time.now.to_i, body: "")
ts = timestamp.to_s
sig = "v0=" + OpenSSL::HMAC.hexdigest("SHA256", secret, "v0:#{ts}:#{body}")
{ "X-Slack-Request-Timestamp" => ts, "X-Slack-Signature" => sig }
end
end