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