-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathlink_checker_api_controller_test.rb
More file actions
59 lines (45 loc) · 1.8 KB
/
link_checker_api_controller_test.rb
File metadata and controls
59 lines (45 loc) · 1.8 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
56
57
58
59
require "test_helper"
require "gds_api/test_helpers/link_checker_api"
class Admin::LinkCheckerApiControllerTest < ActionController::TestCase
include GdsApi::TestHelpers::LinkCheckerApi
def generate_signature(body, key)
OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha1"), key, body)
end
setup do
@link = "http://www.example.com"
publication = create(:publication, body: "[link](#{@link})")
report = link_checker_api_batch_report_hash(
id: 5,
links: [{ uri: @link }],
).with_indifferent_access
LinkCheckerApiReport.create_in_progress_report(report, publication)
end
test "POST :callback updates LinkCheckerApiReport" do
body = link_checker_api_batch_report_hash(
id: 5,
links: [
{ uri: @link, status: "ok" },
],
)
headers = {
"Content-Type": "application/json",
"X-LinkCheckerApi-Signature": generate_signature(body.to_json, Rails.application.credentials.link_checker_api_secret_token),
}
request.headers.merge! headers
post :callback, params: body
assert_response :success
end
test "POST :callback returns 503 when the webhook secret is not configured" do
Rails.application.credentials.stubs(:link_checker_api_secret_token).returns(nil)
LinkCheckerApiReport.any_instance.expects(:mark_report_as_completed).never
body = link_checker_api_batch_report_hash(id: 5, links: [{ uri: @link, status: "ok" }])
post :callback, params: body
assert_response :service_unavailable
end
test "POST :callback returns 400 when the signature header is missing" do
LinkCheckerApiReport.any_instance.expects(:mark_report_as_completed).never
body = link_checker_api_batch_report_hash(id: 5, links: [{ uri: @link, status: "ok" }])
post :callback, params: body
assert_response :bad_request
end
end