Skip to content

Commit 54f6266

Browse files
committed
Adding and updating webhook controller tests
1 parent 0fda3be commit 54f6266

File tree

3 files changed

+251
-2
lines changed

3 files changed

+251
-2
lines changed

app/controllers/class_marker/webhooks_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class ClassMarker::WebhooksController < ApplicationController
22
skip_before_action :verify_authenticity_token, :authenticate
3+
before_action :verify_hmac_signature
34

45
def assessment
56
test_id = params.dig(:test, :test_id)

spec/requests/class_marker/webhook/assessment_spec.rb

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
RSpec.describe ClassMarker::WebhooksController do
44
let(:passing_json_body) { File.read("spec/support/class_marker/passing_webhook.json") }
5+
let(:missing_params_json_body) { File.read("spec/support/class_marker/missing_params_webhook.json") }
56

67
describe "#assessment" do
78
context "with a valid webhook signature" do
@@ -28,11 +29,35 @@
2829
end
2930
end
3031

32+
context "with missing params" do
33+
before do
34+
allow_any_instance_of(described_class)
35+
.to receive(:verify_hmac_signature).and_return(true)
36+
end
37+
38+
it "returns 400 bad request response" do
39+
post class_marker_assessment_webhook_path, params: JSON.parse(missing_params_json_body)
40+
expect(response.status).to eq(400)
41+
end
42+
43+
it "raises a sentry error" do
44+
allow(Sentry).to receive(:capture_message)
45+
46+
post class_marker_assessment_webhook_path, params: JSON.parse(missing_params_json_body)
47+
expect(Sentry).to have_received(:capture_message)
48+
end
49+
end
50+
3151
context "with an invalid webhook signature" do
32-
it "raises an error" do
52+
before do
53+
allow_any_instance_of(described_class)
54+
.to receive(:hmac_header_valid?).and_return(false)
55+
end
56+
57+
it "raises InvalidHMACError" do
3358
expect do
3459
post class_marker_assessment_webhook_path, params: JSON.parse(passing_json_body)
35-
end.to raise_error(StandardError, /Invalid HMAC signature/)
60+
end.to raise_error(ClassMarker::WebhooksController::InvalidHMACError, "Invalid HMAC signature")
3661
end
3762
end
3863
end
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
{
2+
"payload_type": "single_user_test_results_link",
3+
"payload_status": "live",
4+
"test": {
5+
"test_id": 100,
6+
"test_name": "Sample Test Name"
7+
},
8+
"link": {
9+
"link_id": 101,
10+
"link_name": "Sample Link Name",
11+
"link_url_id": "sample_quiz_id_123"
12+
},
13+
"result": {
14+
"link_result_id": 8127364,
15+
"first": "John",
16+
"last": "Smith",
17+
"email": "john@example.com",
18+
"percentage": "",
19+
"points_scored": 9.0,
20+
"points_available": 12.0,
21+
"requires_grading": "Yes",
22+
"time_started": 1436263522,
23+
"time_finished": 1436264122,
24+
"duration": "00:05:20",
25+
"percentage_passmark": 50,
26+
"passed": true,
27+
"feedback": "Thanks for completing our Exam!",
28+
"give_certificate_only_when_passed": false,
29+
"certificate_url": "https://www.classmarker.com/pdf/certificate/SampleCertificate.pdf",
30+
"view_results_url": "https://www.classmarker.com/view/results/?required_parameters_here",
31+
"access_code_question": "What is your Employee ID?",
32+
"access_code_used": "12345",
33+
"extra_info_question": "Which sales department are you assigned to?",
34+
"extra_info_answer": "New York Product 7 Divisiaon",
35+
"extra_info2_question": "Extra Information Question 2 here",
36+
"extra_info2_answer": "Extra Information Answer 2 here",
37+
"extra_info3_question": "Extra Information Question 3 here",
38+
"extra_info3_answer": "Extra Information Answer 3 here",
39+
"extra_info4_question": "Extra Information Question 4 here",
40+
"extra_info4_answer": "Extra Information Answer 4 here",
41+
"extra_info5_question": "Extra Information Question 5 here",
42+
"extra_info5_answer": "Extra Information Answer 5 here",
43+
"cm_user_id": "123456",
44+
"ip_address": "192.168.0.1"
45+
},
46+
"questions": [
47+
{
48+
"question_id": 3542854,
49+
"question_type": "multiplechoice",
50+
"category_id": 1,
51+
"points_available": 2,
52+
"question": "What is the first step for treating a skin burn?",
53+
"options": {
54+
"A": "Apply oil or butter",
55+
"B": "Nothing should be done",
56+
"C": "Soak in water for five minutes",
57+
"D": "Apply antibiotic ointment"
58+
},
59+
"correct_option": "C",
60+
"points_scored": 2,
61+
"user_response": "C",
62+
"result": "correct",
63+
"feedback": "Great, and remember, never use oil on Skin burns!"
64+
},
65+
{
66+
"question_id": 10254859,
67+
"question_type": "multiplechoice",
68+
"category_id": 2,
69+
"points_available": 2,
70+
"question": "Select the options you should take when the fire alarm sounds:",
71+
"options": {
72+
"A": "Call you manager to see if you can leave the building",
73+
"B": "Exit the building immediately",
74+
"C": "Use the Lifts to exit faster",
75+
"D": "Use the stairwell to exit"
76+
},
77+
"correct_option": "B,D",
78+
"points_scored": 1,
79+
"user_response": "B",
80+
"result": "partial_correct",
81+
"feedback": "That is incorrect, the correct answers are A and C"
82+
},
83+
{
84+
"question_id": 5485962,
85+
"question_type": "truefalse",
86+
"category_id": 3,
87+
"points_available": 1,
88+
"question": "Our Support staff work 7 day a week",
89+
"options": {
90+
"A": "True",
91+
"B": "False"
92+
},
93+
"correct_option": "A",
94+
"points_scored": 1,
95+
"user_response": "A",
96+
"result": "correct",
97+
"feedback": "That is correct, we provide 7 day support"
98+
},
99+
{
100+
"question_id": 3896152,
101+
"question_type": "freetext",
102+
"category_id": 5,
103+
"points_available": 1,
104+
"question": "Our company website is: www.______.com",
105+
"options": {
106+
"exact_match": [
107+
{
108+
"content": "example"
109+
},
110+
{
111+
"content": "example.com"
112+
},
113+
{
114+
"content": "www.example.com"
115+
},
116+
{
117+
"content": "http://www.example.com"
118+
},
119+
{
120+
"content": "https://www.example.com"
121+
}
122+
]
123+
},
124+
"points_scored": 1,
125+
"user_response": "example",
126+
"result": "correct",
127+
"feedback": "Correct, always send our customers to our main website: www.example.com"
128+
},
129+
{
130+
"question_id": 6403973,
131+
"question_type": "matching",
132+
"category_id": 2,
133+
"points_available": 4,
134+
"question": "Match the options below:",
135+
"points_scored": 3,
136+
"options": {
137+
"A": {
138+
"clue": "Product faulty",
139+
"match": "Exchange or Refund",
140+
"correct_option": "A",
141+
"user_response": "A"
142+
},
143+
"B": {
144+
"clue": "Customer mis-used and broke product",
145+
"match": "No refund",
146+
"correct_option": "B",
147+
"user_response": "B"
148+
},
149+
"C": {
150+
"clue": "Customer broke factory seal",
151+
"match": "No refund",
152+
"correct_option": "B",
153+
"user_response": "B"
154+
},
155+
"D": {
156+
"clue": "Incorrect product size purchased",
157+
"match": "Exchange",
158+
"correct_option": "D",
159+
"user_response": "A"
160+
},
161+
"E": {
162+
"match": "Have customer removed by security"
163+
}
164+
},
165+
"result": "partial_correct",
166+
"feedback": "Please check your incorrect matches"
167+
},
168+
{
169+
"question_id": 444564,
170+
"question_type": "essay",
171+
"category_id": 5,
172+
"points_available": 1,
173+
"question": "Describe some advantages of having test papers graded instantly:",
174+
"points_scored": 0,
175+
"user_response": "Users can see their results instantly, grading is accurate, save time from manual grading",
176+
"result": "requires_grading",
177+
"custom_feedback": "",
178+
"feedback": "Generic feedback here"
179+
},
180+
{
181+
"question_id": 442810,
182+
"question_type": "grammar",
183+
"category_id": 3,
184+
"points_available": 1,
185+
"question": "The car was parkked over their!",
186+
"answer": "The car was parked over there!",
187+
"points_scored": 1,
188+
"user_response": "The car was parked over there!",
189+
"result": "correct",
190+
"feedback": "Well done!"
191+
}
192+
],
193+
"category_results": [
194+
{
195+
"category_id": 1,
196+
"name": "Health and Safety",
197+
"percentage": 66.7,
198+
"points_available": 6,
199+
"points_scored": 4
200+
},
201+
{
202+
"category_id": 2,
203+
"name": "Exit Procedure",
204+
"percentage": 100,
205+
"points_available": 2,
206+
"points_scored": 2
207+
},
208+
{
209+
"category_id": 3,
210+
"name": "General Knowledge",
211+
"percentage": 100,
212+
"points_available": 2,
213+
"points_scored": 2
214+
},
215+
{
216+
"category_id": 5,
217+
"name": "Sales",
218+
"percentage": 50,
219+
"points_available": 2,
220+
"points_scored": 1
221+
}
222+
]
223+
}

0 commit comments

Comments
 (0)