|
9 | 9 | let(:interrupt_params) do |
10 | 10 | { |
11 | 11 | 'type' => type, |
12 | | - 'status_code' => status_code |
| 12 | + 'status_code' => status_code, |
| 13 | + 'security_response_id' => security_response_id |
13 | 14 | } |
14 | 15 | end |
15 | 16 |
|
16 | 17 | let(:type) { 'html' } |
17 | 18 | let(:status_code) { '100' } |
| 19 | + let(:security_response_id) { '73bb7b99-52f6-43ea-998c-6cbc6b80f520' } |
18 | 20 |
|
19 | 21 | context 'status_code' do |
20 | 22 | subject(:status) { described_class.from_interrupt_params(interrupt_params, http_accept_header).status } |
|
31 | 33 | context 'body' do |
32 | 34 | subject(:body) { described_class.from_interrupt_params(interrupt_params, http_accept_header).body } |
33 | 35 |
|
34 | | - it { is_expected.to eq [Datadog::AppSec::Assets.blocked(format: :html)] } |
| 36 | + it 'includes security response ID in the response body' do |
| 37 | + expect(body).to match_array([include(security_response_id)]) |
| 38 | + end |
35 | 39 |
|
36 | 40 | context 'type is auto it uses the HTTP_ACCEPT to decide the result' do |
37 | 41 | let(:type) { 'auto' } |
38 | 42 | let(:http_accept_header) { 'application/json' } |
39 | 43 |
|
40 | | - it { is_expected.to eq [Datadog::AppSec::Assets.blocked(format: :json)] } |
| 44 | + it 'includes security response ID in the response body' do |
| 45 | + expect(body).to match_array([include(security_response_id)]) |
| 46 | + end |
| 47 | + |
| 48 | + it 'returns the response body with correct content type' do |
| 49 | + expect(body).to eq([ |
| 50 | + Datadog::AppSec::Assets |
| 51 | + .blocked(format: :json) |
| 52 | + .gsub(Datadog::AppSec::Response::SECURITY_RESPONSE_ID_PLACEHOLDER, security_response_id) |
| 53 | + ]) |
| 54 | + end |
41 | 55 | end |
42 | 56 | end |
43 | 57 |
|
|
60 | 74 | let(:interrupt_params) { {} } |
61 | 75 | subject(:response) { described_class.from_interrupt_params(interrupt_params, http_accept_header) } |
62 | 76 |
|
63 | | - it 'uses default response' do |
| 77 | + it 'uses default response replaces placeholders in the template' do |
64 | 78 | expect(response.status).to eq 403 |
65 | | - expect(response.body).to eq [Datadog::AppSec::Assets.blocked(format: :html)] |
66 | 79 | expect(response.headers['Content-Type']).to eq 'text/html' |
67 | 80 | end |
| 81 | + |
| 82 | + it 'does not render security response ID placeholders' do |
| 83 | + expect(response.body).not_to match_array([include(Datadog::AppSec::Response::SECURITY_RESPONSE_ID_PLACEHOLDER)]) |
| 84 | + end |
68 | 85 | end |
69 | 86 | end |
70 | 87 |
|
|
116 | 133 | end |
117 | 134 |
|
118 | 135 | describe '.body' do |
119 | | - subject(:body) { described_class.from_interrupt_params({}, http_accept_header).body } |
| 136 | + let(:security_response_id) { SecureRandom.uuid } |
| 137 | + |
| 138 | + subject(:body) do |
| 139 | + described_class.from_interrupt_params( |
| 140 | + {'security_response_id' => security_response_id}, |
| 141 | + http_accept_header |
| 142 | + ).body |
| 143 | + end |
120 | 144 |
|
121 | 145 | shared_examples_for 'with custom response body' do |type| |
122 | 146 | before do |
|
135 | 159 | context 'with unsupported Accept headers' do |
136 | 160 | let(:http_accept_header) { 'application/xml' } |
137 | 161 |
|
138 | | - it { is_expected.to eq [Datadog::AppSec::Assets.blocked(format: :json)] } |
| 162 | + it 'returns default json template with security response ID' do |
| 163 | + expect(body).to eq([ |
| 164 | + Datadog::AppSec::Assets |
| 165 | + .blocked(format: :json) |
| 166 | + .gsub(Datadog::AppSec::Response::SECURITY_RESPONSE_ID_PLACEHOLDER, security_response_id) |
| 167 | + ]) |
| 168 | + end |
139 | 169 | end |
140 | 170 |
|
141 | 171 | context('with Accept: text/html') do |
142 | 172 | let(:http_accept_header) { 'text/html' } |
143 | 173 |
|
144 | | - it { is_expected.to eq [Datadog::AppSec::Assets.blocked(format: :html)] } |
| 174 | + it 'returns default html template with security response ID' do |
| 175 | + expect(body).to eq([ |
| 176 | + Datadog::AppSec::Assets |
| 177 | + .blocked(format: :html) |
| 178 | + .gsub(Datadog::AppSec::Response::SECURITY_RESPONSE_ID_PLACEHOLDER, security_response_id) |
| 179 | + ]) |
| 180 | + end |
145 | 181 |
|
146 | 182 | it_behaves_like 'with custom response body', :html |
147 | 183 | end |
148 | 184 |
|
149 | 185 | context('with Accept: application/json') do |
150 | 186 | let(:http_accept_header) { 'application/json' } |
151 | 187 |
|
152 | | - it { is_expected.to eq [Datadog::AppSec::Assets.blocked(format: :json)] } |
| 188 | + it 'returns default json template with security response ID' do |
| 189 | + expect(body).to eq([ |
| 190 | + Datadog::AppSec::Assets |
| 191 | + .blocked(format: :json) |
| 192 | + .gsub(Datadog::AppSec::Response::SECURITY_RESPONSE_ID_PLACEHOLDER, security_response_id) |
| 193 | + ]) |
| 194 | + end |
153 | 195 |
|
154 | 196 | it_behaves_like 'with custom response body', :json |
155 | 197 | end |
156 | 198 |
|
157 | 199 | context('with Accept: text/plain') do |
158 | 200 | let(:http_accept_header) { 'text/plain' } |
159 | 201 |
|
160 | | - it { is_expected.to eq [Datadog::AppSec::Assets.blocked(format: :text)] } |
| 202 | + it 'returns default text template with security response ID' do |
| 203 | + expect(body).to eq([ |
| 204 | + Datadog::AppSec::Assets |
| 205 | + .blocked(format: :text) |
| 206 | + .gsub(Datadog::AppSec::Response::SECURITY_RESPONSE_ID_PLACEHOLDER, security_response_id) |
| 207 | + ]) |
| 208 | + end |
161 | 209 |
|
162 | 210 | it_behaves_like 'with custom response body', :text |
163 | 211 | end |
|
0 commit comments