@@ -6,11 +6,13 @@ import org.apache.http.HttpStatus
6
6
import spock.lang.Specification
7
7
8
8
@TestFor (UnsubscribeController )
9
- @Mock ([Notification , User , Query ])
9
+ @Mock ([Notification , User , Query , QueryResult ])
10
10
class UnsubscribeControllerSpec extends Specification {
11
11
12
12
def setup () {
13
13
controller. userService = Mock (UserService )
14
+ controller. queryService = Mock (QueryService )
15
+ controller. notificationService = Mock (NotificationService )
14
16
}
15
17
16
18
def " index() should return a HTTP 400 (BAD_REQUEST) if there is no logged in user and no token" () {
@@ -249,6 +251,7 @@ class UnsubscribeControllerSpec extends Specification {
249
251
def " unsubscribe() should delete all the user's notifications if the token matches the token for a user with notifications" () {
250
252
setup :
251
253
controller. userService. getUser() >> null
254
+ controller. queryService. createMyAnnotationQuery(_ as String ) >> new Query ([name : ' emptyquery' ])
252
255
Query query1 = newQuery(" query1" )
253
256
Query query2 = newQuery(" query2" )
254
257
User user1 = new User (userId : " user1" , email : " fred@bla.com" )
@@ -282,9 +285,44 @@ class UnsubscribeControllerSpec extends Specification {
282
285
Notification . count() == 1
283
286
}
284
287
288
+ def " unsubscribe() should delete my annotaions alert if the token matches the token for a user with notifications" () {
289
+ setup :
290
+ User user = new User (userId : " userid" , email : " fred@bla.com" , frequency : new Frequency ([name : ' hourly' ]))
291
+ Query query = new Query ([
292
+ name : ' testquery' ,
293
+ queryPath : ' /occurrences/search?fq=assertion_user_id:' + ' userid' + ' &dir=desc&facets=basis_of_record' ,
294
+ updateMessage : ' updateMessage' ,
295
+ baseUrl : ' baseUrl' ,
296
+ baseUrlForUI : ' baseUrlForUI' ,
297
+ resourceName : ' resourceName' ,
298
+ queryPathForUI : ' queryPathForUI'
299
+ ])
300
+
301
+ controller. userService. getUser() >> null
302
+ controller. queryService. constructMyAnnotationQueryPath(_ as String ) >> ' /occurrences/search?fq=assertion_user_id:' + user. userId + ' &dir=desc&facets=basis_of_record'
303
+
304
+ user. save(failOnError : true , flush : true )
305
+ query. save(failOnError : true , flush : true )
306
+ Notification notification = new Notification (user : user, query : query)
307
+ notification. save(failOnError : true , flush : true )
308
+ QueryResult queryResult = new QueryResult ([query : query, frequency : user. frequency])
309
+ queryResult. save(failOnError : true , flush : true )
310
+
311
+ when :
312
+ params. token = notification. unsubscribeToken
313
+ request. method = ' POST'
314
+ controller. unsubscribe()
315
+
316
+ then :
317
+ log. info " token = ${ params.token} "
318
+ 1 * controller. notificationService. deleteMyAnnotation(user)
319
+ response. status == HttpStatus . SC_OK
320
+ }
321
+
285
322
def " unsubscribe() should delete only 1 notification if the token matches the token for a notification" () {
286
323
setup :
287
324
controller. userService. getUser() >> null
325
+ controller. queryService. createMyAnnotationQuery(_ as String ) >> new Query ([name : ' emptyquery' ])
288
326
Query query1 = newQuery(" query1" )
289
327
Query query2 = newQuery(" query2" )
290
328
User user = new User (userId : " user1" , email : " fred@bla.com" )
@@ -323,6 +361,7 @@ class UnsubscribeControllerSpec extends Specification {
323
361
user. addToNotifications(notification2)
324
362
user. save(failOnError : true , flush : true )
325
363
controller. userService. getUser() >> user
364
+ controller. queryService. createMyAnnotationQuery(_ as String ) >> new Query ([name : ' emptyquery' ])
326
365
327
366
when :
328
367
params. token = notification1. unsubscribeToken
0 commit comments