-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathacceptance_tests.rb
More file actions
152 lines (127 loc) · 5.83 KB
/
acceptance_tests.rb
File metadata and controls
152 lines (127 loc) · 5.83 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# frozen_string_literal: true
FAKE_HMAC_SECRET = "octoawesome-secret"
FAKE_ALT_HMAC_SECRET = "octoawesome-2-secret"
FAKE_SHARED_SECRET = "octoawesome-shared-secret"
require "rspec"
require "net/http"
require "json"
MAX_WAIT_TIME = 30 # how long to wait for the server to start
describe "Hooks" do
let(:http) { Net::HTTP.new("0.0.0.0", 8080) }
before(:all) do
start_time = Time.now
loop do
begin
response = Net::HTTP.new("0.0.0.0", 8080).get("/health")
break if response.is_a?(Net::HTTPSuccess)
rescue Errno::ECONNREFUSED, SocketError, StandardError
# Server not ready yet, continue waiting
end
if Time.now - start_time > MAX_WAIT_TIME
raise "Server did not return a 200 within #{MAX_WAIT_TIME} seconds"
end
sleep 1
end
end
describe "operational endpoints" do
it "responds to the /health check" do
response = http.get("/health")
expect(response).to be_a(Net::HTTPSuccess)
body = JSON.parse(response.body)
expect(body["status"]).to eq("healthy")
expect(body["version"]).to eq(Hooks::VERSION)
expect(body).to have_key("timestamp")
expect(body).to have_key("uptime_seconds")
end
it "responds to the /version endpoint" do
response = http.get("/version")
expect(response).to be_a(Net::HTTPSuccess)
body = JSON.parse(response.body)
expect(body["version"]).to eq(Hooks::VERSION)
expect(body).to have_key("timestamp")
end
end
describe "endpoints" do
describe "team1" do
it "responds to the /webhooks/team1 endpoint" do
response = http.get("/webhooks/team1")
expect(response).to be_a(Net::HTTPMethodNotAllowed)
expect(response.body).to include("405 Not Allowed")
end
it "processes a POST request with JSON payload" do
payload = { event: "test_event", data: "test_data", event_type: "alert" }
response = http.post("/webhooks/team1", payload.to_json, { "Content-Type" => "application/json" })
expect(response).to be_a(Net::HTTPSuccess)
body = JSON.parse(response.body)
expect(body["status"]).to eq("alert_processed")
expect(body["handler"]).to eq("Team1Handler")
expect(body["channels_notified"]).to include("#team1-alerts")
expect(body).to have_key("timestamp")
end
end
describe "github" do
it "receives a POST request but contains an invalid HMAC signature" do
payload = { action: "push", repository: { name: "test-repo" } }
headers = { "Content-Type" => "application/json", "X-Hub-Signature-256" => "sha256=invalidsignature" }
response = http.post("/webhooks/github", payload.to_json, headers)
expect(response).to be_a(Net::HTTPUnauthorized)
expect(response.body).to include("request validation failed")
end
it "receives a POST request but there is no HMAC related header" do
payload = { action: "push", repository: { name: "test-repo" } }
headers = { "Content-Type" => "application/json" }
response = http.post("/webhooks/github", payload.to_json, headers)
expect(response).to be_a(Net::HTTPUnauthorized)
expect(response.body).to include("request validation failed")
end
it "receives a POST request but it uses the wrong algo" do
payload = { action: "push", repository: { name: "test-repo" } }
headers = {
"Content-Type" => "application/json",
"X-Hub-Signature-256" => "sha512=" + OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha512"), FAKE_HMAC_SECRET, payload.to_json)
}
response = http.post("/webhooks/github", payload.to_json, headers)
expect(response).to be_a(Net::HTTPUnauthorized)
expect(response.body).to include("request validation failed")
end
it "successfully processes a valid POST request with HMAC signature" do
payload = { action: "push", repository: { name: "test-repo" } }
headers = {
"Content-Type" => "application/json",
"X-Hub-Signature-256" => "sha256=" + OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha256"), FAKE_HMAC_SECRET, payload.to_json)
}
response = http.post("/webhooks/github", payload.to_json, headers)
expect(response).to be_a(Net::HTTPSuccess)
body = JSON.parse(response.body)
expect(body["status"]).to eq("success")
end
end
describe "slack" do
it "receives a POST request but contains an invalid HMAC signature" do
payload = { text: "Hello, Slack!" }
digest = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha256"), FAKE_ALT_HMAC_SECRET, payload.to_json)
headers = { "Content-Type" => "application/json", "Signature-256" => "sha256=#{digest}" }
response = http.post("/webhooks/slack", payload.to_json, headers)
expect(response).to be_a(Net::HTTPUnauthorized)
expect(response.body).to include("request validation failed")
end
end
describe "okta" do
it "receives a POST request but contains an invalid shared secret" do
payload = { event: "user.login", user: { id: "12345" } }
headers = { "Content-Type" => "application/json", "Authorization" => "badvalue" }
response = http.post("/webhooks/okta", payload.to_json, headers)
expect(response).to be_a(Net::HTTPUnauthorized)
expect(response.body).to include("request validation failed")
end
it "successfully processes a valid POST request with shared secret" do
payload = { event: "user.login", user: { id: "12345" } }
headers = { "Content-Type" => "application/json", "Authorization" => FAKE_SHARED_SECRET }
response = http.post("/webhooks/okta", payload.to_json, headers)
expect(response).to be_a(Net::HTTPSuccess)
body = JSON.parse(response.body)
expect(body["status"]).to eq("success")
end
end
end
end