@@ -67,154 +67,112 @@ class BlockingActionHelperSpecification extends DDSpecification {
6767 null | null
6868 }
6969
70- void ' getTemplate returning default html template' () {
70+ void ' getTemplate returning default #templateType template' () {
7171 expect :
72- new String (BlockingActionHelper . getTemplate(HTML ), StandardCharsets . UTF_8 )
73- .contains(" <title>You've been blocked</title>" )
74- }
72+ new String (BlockingActionHelper . getTemplate(templateType), StandardCharsets . UTF_8 )
73+ .contains(expectedContent)
7574
76- void ' getTemplate returning default JSON template ' () {
77- expect :
78- new String ( BlockingActionHelper . getTemplate( JSON ), StandardCharsets . UTF_8 )
79- .contains( ' "You\' ve been blocked"' )
75+ where :
76+ templateType | expectedContent
77+ HTML | " <title>You've been blocked</title> "
78+ JSON | ' "You\' ve been blocked"'
8079 }
8180
82- void ' getTemplate returning custom html template' () {
81+ void ' getTemplate returning custom #templateType template' () {
8382 setup :
8483 File tempDir = File . createTempDir(' testTempDir-' , ' ' )
8584 Config config = Mock (Config )
86- File tempFile = new File (tempDir, ' template.html ' )
87- tempFile << ' <body>My template</body> '
85+ File tempFile = new File (tempDir, fileName )
86+ tempFile << templateContent
8887
8988 when :
9089 BlockingActionHelper . reset(config)
9190
9291 then :
93- 1 * config. getAppSecHttpBlockedTemplateHtml() >> tempFile. toString()
94- 1 * config. getAppSecHttpBlockedTemplateJson() >> null
95- new String (BlockingActionHelper . getTemplate(HTML ), StandardCharsets . UTF_8 )
96- .contains(' <body>My template</body> ' )
92+ 1 * config. getAppSecHttpBlockedTemplateHtml() >> (templateType == HTML ? tempFile. toString() : null )
93+ 1 * config. getAppSecHttpBlockedTemplateJson() >> (templateType == JSON ? tempFile . toString() : null )
94+ new String (BlockingActionHelper . getTemplate(templateType ), StandardCharsets . UTF_8 )
95+ .contains(templateContent )
9796
9897 cleanup :
9998 BlockingActionHelper . reset(Config . get())
10099 tempDir. deleteDir()
101- }
102-
103- void ' getTemplate returning custom json template' () {
104- setup :
105- File tempDir = File . createTempDir(' testTempDir-' , ' ' )
106- Config config = Mock (Config )
107- File tempFile = new File (tempDir, ' template.json' )
108- tempFile << ' {"foo":"bar"}'
109100
110- when :
111- BlockingActionHelper . reset(config)
112-
113- then :
114- 1 * config. getAppSecHttpBlockedTemplateHtml() >> null
115- 1 * config. getAppSecHttpBlockedTemplateJson() >> tempFile. toString()
116- new String (BlockingActionHelper . getTemplate(JSON ), StandardCharsets . UTF_8 )
117- .contains(' {"foo":"bar"}' )
118-
119- cleanup :
120- BlockingActionHelper . reset(Config . get())
121- tempDir. deleteDir()
101+ where :
102+ templateType | fileName | templateContent
103+ HTML | ' template.html' | ' <body>My template</body>'
104+ JSON | ' template.json' | ' {"foo":"bar"}'
122105 }
123106
124107 void ' getTemplate with null argument' () {
125108 expect :
126109 BlockingActionHelper . getTemplate(null ) == null
127110 }
128111
129- void ' will use default templates if custom files do not exist ' () {
112+ void ' will use default #templateType template if #reason ' () {
130113 setup :
131114 Config config = Mock (Config )
115+ File tempDir = tempDirSetup ? File . createTempDir(' testTempDir-' , ' ' ) : null
116+ File template = tempFile ? new File (tempDir, ' template' ) : null
117+ if (template) {
118+ template << ' a' * (500 * 1024 + 1 )
119+ }
132120
133121 when :
134122 BlockingActionHelper . reset(config)
135123
136124 then :
137- 1 * config. getAppSecHttpBlockedTemplateHtml() >> ' /bad/file.html'
138- 1 * config. getAppSecHttpBlockedTemplateJson() >> ' /bad/file.json'
139- new String (BlockingActionHelper . getTemplate(HTML ), StandardCharsets . UTF_8 )
140- .contains(" <title>You've been blocked</title>" )
141- new String (BlockingActionHelper . getTemplate(JSON ), StandardCharsets . UTF_8 )
142- .contains(' "You\' ve been blocked' )
125+ 1 * config. getAppSecHttpBlockedTemplateHtml() >> htmlConfigValue?. call(template)
126+ 1 * config. getAppSecHttpBlockedTemplateJson() >> jsonConfigValue?. call(template)
127+ new String (BlockingActionHelper . getTemplate(templateType), StandardCharsets . UTF_8 )
128+ .contains(expectedContent)
143129
144130 cleanup :
145131 BlockingActionHelper . reset(Config . get())
146- }
147-
148- void ' will use default templates if custom files are too big' () {
149- setup :
150- Config config = Mock (Config )
151- File tempDir = File . createTempDir(' testTempDir-' , ' ' )
152- File template = new File (tempDir, ' template' )
153- template << ' a' * (500 * 1024 + 1 )
154-
155- when :
156- BlockingActionHelper . reset(config)
157-
158- then :
159- 1 * config. getAppSecHttpBlockedTemplateHtml() >> template. toString()
160- 1 * config. getAppSecHttpBlockedTemplateJson() >> template. toString()
161- new String (BlockingActionHelper . getTemplate(HTML ), StandardCharsets . UTF_8 )
162- .contains(" <title>You've been blocked</title>" )
163- new String (BlockingActionHelper . getTemplate(JSON ), StandardCharsets . UTF_8 )
164- .contains(' "You\' ve been blocked' )
132+ if (tempDir) {
133+ tempDir. deleteDir()
134+ }
165135
166- cleanup :
167- BlockingActionHelper . reset(Config . get())
168- tempDir. deleteDir()
136+ where :
137+ templateType | reason | tempDirSetup | tempFile | htmlConfigValue | jsonConfigValue | expectedContent
138+ HTML | ' custom file does not exist' | false | false | { _ -> ' /bad/file.html' } | { _ -> ' /bad/file.json' } | " <title>You've been blocked</title>"
139+ JSON | ' custom file does not exist' | false | false | { _ -> ' /bad/file.html' } | { _ -> ' /bad/file.json' } | ' "You\' ve been blocked'
140+ HTML | ' custom file is too big' | true | true | { it -> it. toString() } | { it -> it. toString() } | " <title>You've been blocked</title>"
141+ JSON | ' custom file is too big' | true | true | { it -> it. toString() } | { it -> it. toString() } | ' "You\' ve been blocked'
169142 }
170143
171144
172- void ' getTemplate with security_response_id replaces placeholder in HTML template' () {
145+ void ' getTemplate with security_response_id replaces placeholder in #templateType template' () {
173146 given :
174147 def securityResponseId = ' 12345678-1234-1234-1234-123456789abc'
175148
176149 when :
177- def template = BlockingActionHelper . getTemplate(HTML , securityResponseId)
150+ def template = BlockingActionHelper . getTemplate(templateType , securityResponseId)
178151 def templateStr = new String (template, StandardCharsets . UTF_8 )
179152
180153 then :
181- templateStr. contains(" Security Response ID: ${ securityResponseId} " )
182154 ! templateStr. contains(' [security_response_id]' )
183- }
155+ templateStr . contains(expectedContent . replace( ' [id] ' , securityResponseId))
184156
185- void ' getTemplate with security_response_id replaces placeholder in JSON template' () {
186- given :
187- def securityResponseId = ' 12345678-1234-1234-1234-123456789abc'
188-
189- when :
190- def template = BlockingActionHelper . getTemplate(JSON , securityResponseId)
191- def templateStr = new String (template, StandardCharsets . UTF_8 )
192-
193- then :
194- templateStr. contains(" \" security_response_id\" :\" ${ securityResponseId} \" " )
195- ! templateStr. contains(' [security_response_id]' )
157+ where :
158+ templateType | expectedContent
159+ HTML | ' Security Response ID: [id]'
160+ JSON | ' "security_response_id":"[id]"'
196161 }
197162
198- void ' getTemplate without security_response_id uses empty string in HTML template' () {
163+ void ' getTemplate without security_response_id uses empty string in #templateType template' () {
199164 when :
200- def template = BlockingActionHelper . getTemplate(HTML , null )
165+ def template = BlockingActionHelper . getTemplate(templateType , null )
201166 def templateStr = new String (template, StandardCharsets . UTF_8 )
202167
203168 then :
204169 ! templateStr. contains(' [security_response_id]' )
205- templateStr. contains(' Security Response ID:' )
206- // The placeholder is replaced with empty string
207- }
208-
209- void ' getTemplate without security_response_id uses empty string in JSON template' () {
210- when :
211- def template = BlockingActionHelper . getTemplate(JSON , null )
212- def templateStr = new String (template, StandardCharsets . UTF_8 )
170+ expectedContents. every { content -> templateStr. contains(content) }
213171
214- then :
215- ! templateStr . contains( ' [security_response_id] ' )
216- templateStr . contains( ' "security_response_id" ' )
217- templateStr . contains( ' "" ' ) // Empty string value
172+ where :
173+ templateType | expectedContents
174+ HTML | [ ' Security Response ID: ' ]
175+ JSON | [ ' "security_response_id" ' , ' "" ' ]
218176 }
219177
220178 void ' getTemplate with empty security_response_id uses empty string' () {
@@ -225,54 +183,34 @@ class BlockingActionHelperSpecification extends DDSpecification {
225183 then :
226184 ! new String (htmlTemplate, StandardCharsets . UTF_8 ). contains(' [security_response_id]' )
227185 ! new String (jsonTemplate, StandardCharsets . UTF_8 ). contains(' [security_response_id]' )
228- // Both templates have placeholders replaced with empty string
229186 }
230187
231- void ' getTemplate with security_response_id works with custom HTML template' () {
188+ void ' getTemplate with security_response_id works with custom #templateType template' () {
232189 setup :
233190 File tempDir = File . createTempDir(' testTempDir-' , ' ' )
234191 Config config = Mock (Config )
235- File tempFile = new File (tempDir, ' template.html ' )
236- tempFile << ' <body>Custom template with security_response_id: [security_response_id]</body> '
192+ File tempFile = new File (tempDir, fileName )
193+ tempFile << templateContent
237194 def securityResponseId = ' test-block-id-123'
238195
239196 when :
240197 BlockingActionHelper . reset(config)
241- def template = BlockingActionHelper . getTemplate(HTML , securityResponseId)
198+ def template = BlockingActionHelper . getTemplate(templateType , securityResponseId)
242199 def templateStr = new String (template, StandardCharsets . UTF_8 )
243200
244201 then :
245- 1 * config. getAppSecHttpBlockedTemplateHtml() >> tempFile. toString()
246- 1 * config. getAppSecHttpBlockedTemplateJson() >> null
247- templateStr. contains(" Custom template with security_response_id: ${ securityResponseId} " )
202+ 1 * config. getAppSecHttpBlockedTemplateHtml() >> (templateType == HTML ? tempFile. toString() : null )
203+ 1 * config. getAppSecHttpBlockedTemplateJson() >> (templateType == JSON ? tempFile . toString() : null )
204+ templateStr. contains(expectedContent . replace( ' [id] ' , securityResponseId) )
248205 ! templateStr. contains(' [security_response_id]' )
249206
250207 cleanup :
251208 BlockingActionHelper . reset(Config . get())
252209 tempDir. deleteDir()
253- }
254-
255- void ' getTemplate with security_response_id works with custom JSON template' () {
256- setup :
257- File tempDir = File . createTempDir(' testTempDir-' , ' ' )
258- Config config = Mock (Config )
259- File tempFile = new File (tempDir, ' template.json' )
260- tempFile << ' {"error":"blocked","id":"[security_response_id]"}'
261- def securityResponseId = ' test-block-id-456'
262-
263- when :
264- BlockingActionHelper . reset(config)
265- def template = BlockingActionHelper . getTemplate(JSON , securityResponseId)
266- def templateStr = new String (template, StandardCharsets . UTF_8 )
267-
268- then :
269- 1 * config. getAppSecHttpBlockedTemplateHtml() >> null
270- 1 * config. getAppSecHttpBlockedTemplateJson() >> tempFile. toString()
271- templateStr. contains(" \" error\" :\" blocked\" ,\" id\" :\" ${ securityResponseId} \" " )
272- ! templateStr. contains(' [security_response_id]' )
273210
274- cleanup :
275- BlockingActionHelper . reset(Config . get())
276- tempDir. deleteDir()
211+ where :
212+ templateType | fileName | templateContent | expectedContent
213+ HTML | ' template.html' | ' <body>Custom template with security_response_id: [security_response_id]</body>' | ' Custom template with security_response_id: [id]'
214+ JSON | ' template.json' | ' {"error":"blocked","id":"[security_response_id]"}' | ' "error":"blocked","id":"[id]"'
277215 }
278216}
0 commit comments