@@ -19,18 +19,13 @@ package ru.mystamps.web.feature.site
1919
2020import org.slf4j.helpers.NOPLogger
2121import ru.mystamps.web.feature.site.SiteDb.SuspiciousActivity
22- import ru.mystamps.web.feature.site.SiteDb.SuspiciousActivityType
2322import ru.mystamps.web.service.TestObjects
24- import ru.mystamps.web.tests.DateUtils
2523import spock.lang.Specification
26- import spock.lang.Unroll
2724
2825class SiteServiceImplTest extends Specification {
2926 private static final String TEST_TYPE = TestObjects . TEST_ACTIVITY_TYPE
3027 private static final String TEST_PAGE = TestObjects . TEST_ACTIVITY_PAGE
31- private static final String TEST_IP = TestObjects . TEST_ACTIVITY_IP
3228 private static final String TEST_METHOD = TestObjects . TEST_ACTIVITY_METHOD
33- private static final String TEST_REFERER_PAGE = TestObjects . TEST_ACTIVITY_REFERER
3429 private static final String TEST_USER_AGENT = TestObjects . TEST_ACTIVITY_AGENT
3530
3631 private final SuspiciousActivityDao suspiciousActivityDao = Mock ()
@@ -44,140 +39,10 @@ class SiteServiceImplTest extends Specification {
4439 )
4540 }
4641
47- //
48- // Tests for logAboutAbsentPage()
49- //
50-
51- def " logAboutAbsentPage() should pass arguments to logEvent()" () {
52- given :
53- Integer expectedUserId = 17
54- when :
55- serviceImpl. logAboutAbsentPage(
56- TEST_PAGE ,
57- TEST_METHOD ,
58- expectedUserId,
59- TEST_IP ,
60- TEST_REFERER_PAGE ,
61- TEST_USER_AGENT
62- )
63- then :
64- 1 * serviceImpl. logEvent(
65- SuspiciousActivityType . PAGE_NOT_FOUND ,
66- TEST_PAGE ,
67- TEST_METHOD ,
68- expectedUserId,
69- TEST_IP ,
70- TEST_REFERER_PAGE ,
71- TEST_USER_AGENT ,
72- { Date date ->
73- assert DateUtils . roughlyEqual(date, new Date ())
74- return true
75- }
76- )
77- }
78-
79- //
80- // Tests for logAboutFailedAuthentication()
81- //
82-
83- def " logAboutFailedAuthentication() should pass arguments to logEvent()" () {
84- given :
85- Integer expectedUserId = 18
86- Date expectedDate = new Date ()
87- when :
88- serviceImpl. logAboutFailedAuthentication(
89- TEST_PAGE ,
90- TEST_METHOD ,
91- expectedUserId,
92- TEST_IP ,
93- TEST_REFERER_PAGE ,
94- TEST_USER_AGENT ,
95- expectedDate
96- )
97- then :
98- 1 * serviceImpl. logEvent(
99- SuspiciousActivityType . AUTHENTICATION_FAILED ,
100- TEST_PAGE ,
101- TEST_METHOD ,
102- expectedUserId,
103- TEST_IP ,
104- TEST_REFERER_PAGE ,
105- TEST_USER_AGENT ,
106- expectedDate
107- )
108- }
109-
11042 //
11143 // Tests for logEvent()
11244 //
11345
114- def " logEvent() should throw exception when type is null" () {
115- when :
116- serviceImpl. logEvent(null , TEST_PAGE , TEST_METHOD , null , null , null , null , null )
117- then :
118- IllegalArgumentException ex = thrown()
119- ex. message == ' Type of suspicious activity must be non null'
120- }
121-
122- def " logEvent() should throw exception when page is null" () {
123- when :
124- serviceImpl. logEvent(TEST_TYPE , null , TEST_METHOD , null , null , null , null , null )
125- then :
126- IllegalArgumentException ex = thrown()
127- ex. message == ' Page must be non null'
128- }
129-
130- def " logEvent() should call dao" () {
131- when :
132- serviceImpl. logEvent(TEST_TYPE , TEST_PAGE , TEST_METHOD , null , null , null , null , null )
133- then :
134- 1 * suspiciousActivityDao. add(_ as AddSuspiciousActivityDbDto )
135- }
136-
137- def " logEvent() should pass activity type to dao" () {
138- given :
139- String expectedType = ' expectedType'
140- when :
141- serviceImpl. logEvent(expectedType, TEST_PAGE , TEST_METHOD , null , null , null , null , null )
142- then :
143- 1 * suspiciousActivityDao. add({ AddSuspiciousActivityDbDto activity ->
144- assert activity?. type == expectedType
145- return true
146- })
147- }
148-
149- def " logEvent() should assign occurred at to specified date when date was provided" () {
150- given :
151- Date expectedDate = new Date () - 100
152- when :
153- serviceImpl. logEvent(TEST_TYPE , TEST_PAGE , TEST_METHOD , null , null , null , null , expectedDate)
154- then :
155- 1 * suspiciousActivityDao. add({ AddSuspiciousActivityDbDto activity ->
156- assert DateUtils . roughlyEqual(activity?. occurredAt, expectedDate)
157- return true
158- })
159- }
160-
161- def " logEvent() should assign occurred at to current date when date wasn't provided" () {
162- when :
163- serviceImpl. logEvent(TEST_TYPE , TEST_PAGE , TEST_METHOD , null , null , null , null , null )
164- then :
165- 1 * suspiciousActivityDao. add({ AddSuspiciousActivityDbDto activity ->
166- assert DateUtils . roughlyEqual(activity?. occurredAt, new Date ())
167- return true
168- })
169- }
170-
171- def " logEvent() should pass page to dao" () {
172- when :
173- serviceImpl. logEvent(TEST_TYPE , TEST_PAGE , TEST_METHOD , null , null , null , null , null )
174- then :
175- 1 * suspiciousActivityDao. add({ AddSuspiciousActivityDbDto activity ->
176- assert activity?. page == TEST_PAGE
177- return true
178- })
179- }
180-
18146 def " logAboutAbsentPage() should pass abbreviated page when it's too long" () {
18247 given :
18348 String longPageUrl = ' /long/url/' + (' x' * SuspiciousActivity . PAGE_URL_LENGTH )
@@ -191,20 +56,6 @@ class SiteServiceImplTest extends Specification {
19156 return true
19257 })
19358 }
194-
195- @Unroll
196- def " logEvent() should pass method to dao" (String expectedMethod) {
197- when :
198- serviceImpl. logEvent(TEST_TYPE , TEST_PAGE , expectedMethod, null , null , null , null , null )
199- then :
200- 1 * suspiciousActivityDao. add({ AddSuspiciousActivityDbDto activity ->
201- assert activity?. method == expectedMethod
202- return true
203- })
204- where : expectedMethod | _
205- ' OPTIONS' | _
206- null | _
207- }
20859
20960 def " logEvent() should pass abbreviated method when it's too long" () {
21061 given :
@@ -220,58 +71,6 @@ class SiteServiceImplTest extends Specification {
22071 })
22172 }
22273
223- def " logEvent() should pass null to dao for unknown user id" () {
224- when :
225- serviceImpl. logEvent(TEST_TYPE , TEST_PAGE , TEST_METHOD , null , null , null , null , null )
226- then :
227- 1 * suspiciousActivityDao. add({ AddSuspiciousActivityDbDto activity ->
228- assert activity?. userId == null
229- return true
230- })
231- }
232-
233- def " logEvent() should pass user id to dao" () {
234- given :
235- Integer expectedUserId = 20
236- when :
237- serviceImpl. logEvent(TEST_TYPE , TEST_PAGE , TEST_METHOD , expectedUserId, null , null , null , null )
238- then :
239- 1 * suspiciousActivityDao. add({ AddSuspiciousActivityDbDto activity ->
240- assert activity?. userId == expectedUserId
241- return true
242- })
243- }
244-
245- def " logEvent() should pass ip to dao" () {
246- when :
247- serviceImpl. logEvent(TEST_TYPE , TEST_PAGE , TEST_METHOD , null , TEST_IP , null , null , null )
248- then :
249- 1 * suspiciousActivityDao. add({ AddSuspiciousActivityDbDto activity ->
250- assert activity?. ip == TEST_IP
251- return true
252- })
253- }
254-
255- def " logEvent() should pass empty string to dao for unknown ip" () {
256- when :
257- serviceImpl. logEvent(TEST_TYPE , TEST_PAGE , TEST_METHOD , null , null , null , null , null )
258- then :
259- 1 * suspiciousActivityDao. add({ AddSuspiciousActivityDbDto activity ->
260- assert activity?. ip?. empty
261- return true
262- })
263- }
264-
265- def " logEvent() should pass referer to dao" () {
266- when :
267- serviceImpl. logEvent(TEST_TYPE , TEST_PAGE , TEST_METHOD , null , null , TEST_REFERER_PAGE , null , null )
268- then :
269- 1 * suspiciousActivityDao. add({ AddSuspiciousActivityDbDto activity ->
270- assert activity?. refererPage == TEST_REFERER_PAGE
271- return true
272- })
273- }
274-
27574 def " logEvent() should pass abbreviated referer when it's too long" () {
27675 given :
27776 String longRefererUrl = ' /long/url/' + (' x' * SuspiciousActivity . REFERER_PAGE_LENGTH )
@@ -286,30 +85,6 @@ class SiteServiceImplTest extends Specification {
28685 })
28786 }
28887
289- def " logEvent() should pass null to dao for unknown referer" (String refererPage) {
290- when :
291- serviceImpl. logEvent(TEST_TYPE , TEST_PAGE , TEST_METHOD , null , null , refererPage, null , null )
292- then :
293- 1 * suspiciousActivityDao. add({ AddSuspiciousActivityDbDto activity ->
294- assert activity?. refererPage == null
295- return true
296- })
297- where : refererPage | _
298- ' ' | _
299- ' ' | _
300- null | _
301- }
302-
303- def " logEvent() should pass user agent to dao" () {
304- when :
305- serviceImpl. logEvent(TEST_TYPE , TEST_PAGE , TEST_METHOD , null , null , null , TEST_USER_AGENT , null )
306- then :
307- 1 * suspiciousActivityDao. add({ AddSuspiciousActivityDbDto activity ->
308- assert activity?. userAgent == TEST_USER_AGENT
309- return true
310- })
311- }
312-
31388 def " logEvent() should pass abbreviated user agent when it's too long" () {
31489 given :
31590 String longUserAgent = ' Mozilla/5.0 (Windows NT 6.1) AppleWebKit/' + (' x' * SuspiciousActivity . USER_AGENT_LENGTH )
@@ -324,18 +99,4 @@ class SiteServiceImplTest extends Specification {
32499 })
325100 }
326101
327- def " logEvent() should pass null to dao for unknown user agent" (String userAgent) {
328- when :
329- serviceImpl. logEvent(TEST_TYPE , TEST_PAGE , TEST_METHOD , null , null , null , userAgent, null )
330- then :
331- 1 * suspiciousActivityDao. add({ AddSuspiciousActivityDbDto activity ->
332- assert activity?. userAgent == null
333- return true
334- })
335- where : userAgent | _
336- ' ' | _
337- ' ' | _
338- null | _
339- }
340-
341102}
0 commit comments