/
test_follow.py
1348 lines (1116 loc) · 58.7 KB
/
test_follow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# encoding: utf-8
'''Test for the follower API.
This module tests following, unfollowing, getting a list of what you're
following or the number of things you're following, getting a list of who's
following you or the number of followers you have, testing whether or not
you're following something, etc.
This module _does not_ test the user dashboard activity stream (which shows
activities from everything you're following), that is tested in
test_dashboard.py.
'''
import datetime
import ckan
from ckan.tests.legacy import are_foreign_keys_supported, SkipTest, CreateTestData, call_action_api
import ckan.tests.helpers as helpers
def datetime_from_string(s):
'''Return a standard datetime.datetime object initialised from a string in
the same format used for timestamps in dictized activities (the format
produced by datetime.datetime.isoformat())
'''
return datetime.datetime.strptime(s, '%Y-%m-%dT%H:%M:%S.%f')
def follow(func):
'''Return a wrapper function for a follow_* function.
The wrapper functions test the `followee_list` and `followee_count` API
calls, in addition to any tests carried out by the wrapped function.
'''
def wrapped_func(app, follower_id, apikey, object_id, object_arg,
sysadmin_apikey):
followee_count_before = call_action_api(app,
'followee_count', id=follower_id)
followees_before = call_action_api(app, 'followee_list',
id=follower_id, apikey=sysadmin_apikey)
func(app, follower_id, apikey, object_id, object_arg, sysadmin_apikey)
followee_count_after = call_action_api(app,
'followee_count', id=follower_id)
followees_after = call_action_api(app, 'followee_list',
id=follower_id, apikey=sysadmin_apikey)
assert followee_count_after == followee_count_before + 1, (
"After a user follows an object, the user's `followee_count` "
"should increase by 1")
assert len(followees_after) == len(followees_before) + 1, (
"After a user follows an object, the object should appear in "
"the user's `followee_list`")
assert len([followee for followee in followees_after
if followee['dict']['id'] == object_id]) == 1, (
"After a user follows an object, the object should appear in "
"the user's `followee_list`")
return wrapped_func
@follow
def follow_user(app, follower_id, apikey, object_id, object_arg,
sysadmin_apikey):
'''Test a user starting to follow another user via the API.
:param follower_id: id of the user that will be following something.
:param apikey: API key of the user that will be following something.
:param object_id: id of the user that will be followed.
:param object_arg: the argument to pass to follow_user as the id of
the object that will be followed, could be the object's id or name.
'''
# Record the object's followers count before.
follower_count_before = call_action_api(app,
'user_follower_count', id=object_id)
# Record the follower's followees count before.
followee_count_before = call_action_api(app,
'user_followee_count', id=follower_id)
# Check that the user is not already following the object.
result = call_action_api(app, 'am_following_user',
id=object_id, apikey=apikey)
assert result is False
# Make the user start following the object.
before = datetime.datetime.now()
follower = call_action_api(app, 'follow_user', id=object_arg,
apikey=apikey)
after = datetime.datetime.now()
assert follower['follower_id'] == follower_id
assert follower['object_id'] == object_id
timestamp = datetime_from_string(follower['datetime'])
assert (timestamp >= before and timestamp <= after), str(timestamp)
# Check that am_following_user now returns True.
result = call_action_api(app, 'am_following_user',
id=object_id, apikey=apikey)
assert result is True
# Check that the follower appears in the object's list of followers.
followers = call_action_api(app, 'user_follower_list',
id=object_id, apikey=sysadmin_apikey)
assert len(followers) == follower_count_before + 1
assert len([follower for follower in followers if follower['id'] == follower_id]) == 1
# Check that the object appears in the follower's list of followees.
followees = call_action_api(app, 'user_followee_list',
apikey=sysadmin_apikey, id=follower_id)
assert len(followees) == followee_count_before + 1
assert len([followee for followee in followees if followee['id'] == object_id]) == 1
# Check that the object's follower count has increased by 1.
follower_count_after = call_action_api(app,
'user_follower_count', id=object_id)
assert follower_count_after == follower_count_before + 1
# Check that the follower's followee count has increased by 1.
followee_count_after = call_action_api(app,
'user_followee_count', id=follower_id)
assert followee_count_after == followee_count_before + 1
@follow
def follow_dataset(app, follower_id, apikey, dataset_id, dataset_arg,
sysadmin_apikey):
'''Test a user starting to follow a dataset via the API.
:param follower_id: id of the user.
:param apikey: API key of the user.
:param dataset_id: id of the dataset.
:param dataset_arg: the argument to pass to follow_dataset as the id of
the dataset that will be followed, could be the dataset's id or name.
'''
# Record the dataset's followers count before.
follower_count_before = call_action_api(app,
'dataset_follower_count', id=dataset_id)
# Record the follower's followees count before.
followee_count_before = call_action_api(app,
'dataset_followee_count', id=follower_id)
# Check that the user is not already following the dataset.
result = call_action_api(app, 'am_following_dataset',
id=dataset_id, apikey=apikey)
assert result is False
# Make the user start following the dataset.
before = datetime.datetime.now()
follower = call_action_api(app, 'follow_dataset',
id=dataset_arg, apikey=apikey)
after = datetime.datetime.now()
assert follower['follower_id'] == follower_id
assert follower['object_id'] == dataset_id
timestamp = datetime_from_string(follower['datetime'])
assert (timestamp >= before and timestamp <= after), str(timestamp)
# Check that am_following_dataset now returns True.
result = call_action_api(app, 'am_following_dataset',
id=dataset_id, apikey=apikey)
assert result is True
# Check that the follower appears in the dataset's list of followers.
followers = call_action_api(app, 'dataset_follower_list',
id=dataset_id, apikey=sysadmin_apikey)
assert len(followers) == follower_count_before + 1
assert len([follower for follower in followers if follower['id'] == follower_id]) == 1
# Check that the dataset appears in the follower's list of followees.
followees = call_action_api(app, 'dataset_followee_list',
apikey=sysadmin_apikey, id=follower_id)
assert len(followees) == followee_count_before + 1
assert len([followee for followee in followees if followee['id'] == dataset_id]) == 1
# Check that the dataset's follower count has increased by 1.
follower_count_after = call_action_api(app,
'dataset_follower_count', id=dataset_id)
assert follower_count_after == follower_count_before + 1
# Check that the follower's followee count has increased by 1.
followee_count_after = call_action_api(app,
'dataset_followee_count', id=follower_id)
assert followee_count_after == followee_count_before + 1
@follow
def follow_group(app, user_id, apikey, group_id, group_arg, sysadmin_apikey):
'''Test a user starting to follow a group via the API.
:param user_id: id of the user
:param apikey: API key of the user
:param group_id: id of the group
:param group_arg: the argument to pass to follow_group as the id of
the group that will be followed, could be the group's id or name
'''
# Record the group's followers count before.
follower_count_before = call_action_api(app,
'group_follower_count', id=group_id)
# Record the user's followees count before.
followee_count_before = call_action_api(app,
'group_followee_count', id=user_id)
# Check that the user is not already following the group.
result = call_action_api(app, 'am_following_group',
id=group_id, apikey=apikey)
assert result is False
# Make the user start following the group.
before = datetime.datetime.now()
follower = call_action_api(app, 'follow_group', id=group_id,
apikey=apikey)
after = datetime.datetime.now()
assert follower['follower_id'] == user_id
assert follower['object_id'] == group_id
timestamp = datetime_from_string(follower['datetime'])
assert (timestamp >= before and timestamp <= after), str(timestamp)
# Check that am_following_group now returns True.
result = call_action_api(app, 'am_following_group',
id=group_id, apikey=apikey)
assert result is True
# Check that the user appears in the group's list of followers.
followers = call_action_api(app, 'group_follower_list',
id=group_id, apikey=sysadmin_apikey)
assert len(followers) == follower_count_before + 1
assert len([follower for follower in followers
if follower['id'] == user_id]) == 1
# Check that the group appears in the user's list of followees.
followees = call_action_api(app, 'group_followee_list',
apikey=sysadmin_apikey, id=user_id)
assert len(followees) == followee_count_before + 1
assert len([followee for followee in followees
if followee['id'] == group_id]) == 1
# Check that the group's follower count has increased by 1.
follower_count_after = call_action_api(app,
'group_follower_count', id=group_id)
assert follower_count_after == follower_count_before + 1
# Check that the user's followee count has increased by 1.
followee_count_after = call_action_api(app,
'group_followee_count', id=user_id)
assert followee_count_after == followee_count_before + 1
class TestFollow(object):
'''Tests for the follower API.'''
@classmethod
def setup_class(self):
CreateTestData.create()
self.testsysadmin = {
'id': ckan.model.User.get('testsysadmin').id,
'apikey': ckan.model.User.get('testsysadmin').apikey,
'name': ckan.model.User.get('testsysadmin').name,
}
self.annafan = {
'id': ckan.model.User.get('annafan').id,
'apikey': ckan.model.User.get('annafan').apikey,
'name': ckan.model.User.get('annafan').name,
}
self.russianfan = {
'id': ckan.model.User.get('russianfan').id,
'apikey': ckan.model.User.get('russianfan').apikey,
'name': ckan.model.User.get('russianfan').name,
}
self.joeadmin = {
'id': ckan.model.User.get('joeadmin').id,
'apikey': ckan.model.User.get('joeadmin').apikey,
'name': ckan.model.User.get('joeadmin').name,
}
self.warandpeace = {
'id': ckan.model.Package.get('warandpeace').id,
'name': ckan.model.Package.get('warandpeace').name,
}
self.annakarenina = {
'id': ckan.model.Package.get('annakarenina').id,
'name': ckan.model.Package.get('annakarenina').name,
}
self.rogers_group = {
'id': ckan.model.Group.get('roger').id,
'name': ckan.model.Group.get('roger').name,
}
self.davids_group = {
'id': ckan.model.Group.get('david').id,
'name': ckan.model.Group.get('david').name,
}
self.app = helpers._get_test_app()
@classmethod
def teardown_class(self):
ckan.model.repo.rebuild_db()
def test_00_visitor_cannot_get_user_follower_list(self):
call_action_api(self.app, 'user_follower_list',
id=self.russianfan['id'], status=403)
def test_00_user_cannot_get_user_follower_list(self):
call_action_api(self.app, 'user_follower_list',
id=self.russianfan['id'], status=403,
apikey=self.annafan['apikey'])
def test_00_sysadmin_can_get_user_follower_list(self):
call_action_api(self.app, 'user_follower_list',
id=self.russianfan['id'], status=200,
apikey=self.testsysadmin['apikey'])
def test_00_visitor_cannot_get_dataset_follower_list(self):
call_action_api(self.app, 'dataset_follower_list',
id='warandpeace', status=403)
def test_00_user_cannot_get_dataset_follower_list(self):
call_action_api(self.app, 'dataset_follower_list',
id='warandpeace', status=403, apikey=self.annafan['apikey'])
def test_00_sysadmin_can_get_dataset_follower_list(self):
call_action_api(self.app, 'dataset_follower_list',
id='warandpeace', status=200,
apikey=self.testsysadmin['apikey'])
def test_00_visitor_cannot_get_group_follower_list(self):
call_action_api(self.app, 'group_follower_list',
id='roger', status=403)
def test_00_user_cannot_get_group_follower_list(self):
call_action_api(self.app, 'group_follower_list',
id='roger', status=403, apikey=self.annafan['apikey'])
def test_00_sysadmin_can_get_group_follower_list(self):
call_action_api(self.app, 'group_follower_list',
id='roger', status=200, apikey=self.testsysadmin['apikey'])
def test_00_visitor_cannot_get_followee_list(self):
call_action_api(self.app, 'followee_list',
id=self.russianfan['id'], status=403)
def test_00_user_cannot_get_followee_list(self):
call_action_api(self.app, 'followee_list',
id=self.russianfan['id'], status=403,
apikey=self.annafan['apikey'])
def test_00_sysadmin_can_get_followee_list(self):
call_action_api(self.app, 'followee_list',
id=self.russianfan['id'], status=200,
apikey=self.testsysadmin['apikey'])
def test_00_visitor_cannot_get_user_followee_list(self):
'''A visitor cannot see what users a user is following.'''
call_action_api(self.app, 'user_followee_list',
id=self.russianfan['id'], status=403)
def test_00_user_cannot_get_user_followee_list(self):
'''A user cannot see what users another user is following.'''
call_action_api(self.app, 'user_followee_list',
id=self.russianfan['id'], status=403,
apikey=self.annafan['apikey'])
def test_00_sysadmin_can_get_user_followee_list(self):
'''A sysadmin can see what users another user is following.'''
call_action_api(self.app, 'user_followee_list',
id=self.russianfan['id'], status=200,
apikey=self.testsysadmin['apikey'])
def test_00_user_can_get_own_user_followee_list(self):
'''A user can see what users she herself is following.'''
call_action_api(self.app, 'user_followee_list',
id=self.russianfan['id'], status=200,
apikey=self.russianfan['apikey'])
def test_00_visitor_cannot_get_dataset_followee_list(self):
'''A visitor cannot see what datasets a user is following.'''
call_action_api(self.app, 'dataset_followee_list',
id=self.russianfan['id'], status=403)
def test_00_user_cannot_get_dataset_followee_list(self):
'''A user cannot see what datasets another user is following.'''
call_action_api(self.app, 'dataset_followee_list',
id='russianfan', status=403, apikey=self.annafan['apikey'])
def test_00_sysadmin_can_get_dataset_followee_list(self):
'''A sysadmin can see what datasets another user is following.'''
call_action_api(self.app, 'dataset_followee_list',
id='russianfan', status=200,
apikey=self.testsysadmin['apikey'])
def test_00_user_can_get_own_dataset_followee_list(self):
'''A user can see what datasets she herself is following.'''
call_action_api(self.app, 'dataset_followee_list',
id=self.russianfan['id'], status=200,
apikey=self.russianfan['apikey'])
def test_00_visitor_cannot_get_group_followee_list(self):
'''A visitor cannot see what groups a user is following.'''
call_action_api(self.app, 'group_followee_list',
id='roger', status=403)
def test_00_user_cannot_get_group_followee_list(self):
'''A user cannot see what groups another user is following.'''
call_action_api(self.app, 'group_followee_list',
id='roger', status=403, apikey=self.annafan['apikey'])
def test_00_sysadmin_can_get_group_followee_list(self):
'''A sysadmin can see what groups another user is following.'''
call_action_api(self.app, 'group_followee_list',
id=self.annafan['id'], status=200,
apikey=self.testsysadmin['apikey'])
def test_00_user_can_get_own_group_followee_list(self):
'''A user can see what groups she herself is following.'''
call_action_api(self.app, 'group_followee_list',
id=self.russianfan['id'], status=200,
apikey=self.russianfan['apikey'])
def test_01_user_follow_user_bad_apikey(self):
for apikey in ('bad api key', '', ' ', 'None', '3', '35.7', 'xxx'):
error = call_action_api(self.app, 'follow_user',
id=self.russianfan['id'], apikey=apikey,
status=403)
assert error['__type'] == 'Authorization Error'
def test_01_user_follow_dataset_bad_apikey(self):
for apikey in ('bad api key', '', ' ', 'None', '3', '35.7', 'xxx'):
error = call_action_api(self.app, 'follow_dataset',
id=self.warandpeace['id'], apikey=apikey,
status=403)
assert error['__type'] == 'Authorization Error'
def test_01_user_follow_group_bad_apikey(self):
for apikey in ('bad api key', '', ' ', 'None', '3', '35.7', 'xxx'):
error = call_action_api(self.app, 'follow_group',
id=self.rogers_group['id'], apikey=apikey,
status=403)
assert error['__type'] == 'Authorization Error'
def test_01_user_follow_user_missing_apikey(self):
error = call_action_api(self.app, 'follow_user',
id=self.russianfan['id'], status=403)
assert error['__type'] == 'Authorization Error'
def test_01_user_follow_dataset_missing_apikey(self):
error = call_action_api(self.app, 'follow_dataset',
id=self.warandpeace['id'], status=403)
assert error['__type'] == 'Authorization Error'
def test_01_user_follow_group_missing_apikey(self):
error = call_action_api(self.app, 'follow_group',
id=self.rogers_group['id'], status=403)
assert error['__type'] == 'Authorization Error'
def test_01_follow_bad_object_id(self):
for action in ('follow_user', 'follow_dataset', 'follow_group'):
for object_id in ('bad id', ' ', 3, 35.7, 'xxx'):
error = call_action_api(self.app, action,
id=object_id,
apikey=self.annafan['apikey'], status=409)
assert error['id'][0].startswith('Not found')
def test_01_follow_empty_object_id(self):
for action in ('follow_user', 'follow_dataset', 'follow_group'):
for object_id in ('', None):
error = call_action_api(self.app, action,
id=object_id,
apikey=self.annafan['apikey'], status=409)
assert error['id'] == ['Missing value']
def test_01_follow_missing_object_id(self):
for action in ('follow_user', 'follow_dataset', 'follow_group'):
error = call_action_api(self.app, action,
apikey=self.annafan['apikey'], status=409)
assert error['id'] == ['Missing value']
def test_02_user_follow_user_by_id(self):
follow_user(self.app, self.annafan['id'], self.annafan['apikey'],
self.russianfan['id'], self.russianfan['id'],
self.testsysadmin['apikey'])
def test_02_user_follow_dataset_by_id(self):
follow_dataset(self.app, self.annafan['id'], self.annafan['apikey'],
self.warandpeace['id'], self.warandpeace['id'],
self.testsysadmin['apikey'])
def test_02_user_follow_group_by_id(self):
follow_group(self.app, self.annafan['id'], self.annafan['apikey'],
self.rogers_group['id'], self.rogers_group['id'],
self.testsysadmin['apikey'])
def test_02_user_follow_user_by_name(self):
follow_user(self.app, self.annafan['id'], self.annafan['apikey'],
self.testsysadmin['id'], self.testsysadmin['name'],
self.testsysadmin['apikey'])
def test_02_user_follow_dataset_by_name(self):
follow_dataset(self.app, self.joeadmin['id'], self.joeadmin['apikey'],
self.warandpeace['id'], self.warandpeace['name'],
self.testsysadmin['apikey'])
def test_02_user_follow_group_by_name(self):
follow_group(self.app, self.joeadmin['id'], self.joeadmin['apikey'],
self.rogers_group['id'], self.rogers_group['name'],
self.testsysadmin['apikey'])
def test_03_user_follow_user_already_following(self):
for object_id in (self.russianfan['id'], self.russianfan['name'],
self.testsysadmin['id'], self.testsysadmin['name']):
error = call_action_api(self.app, 'follow_user',
id=object_id, apikey=self.annafan['apikey'],
status=409)
assert error['message'].startswith('You are already following ')
def test_03_user_follow_dataset_already_following(self):
for object_id in (self.warandpeace['id'], self.warandpeace['name']):
error = call_action_api(self.app, 'follow_dataset',
id=object_id, apikey=self.annafan['apikey'],
status=409)
assert error['message'].startswith('You are already following ')
def test_03_user_follow_group_already_following(self):
for group_id in (self.rogers_group['id'], self.rogers_group['name']):
error = call_action_api(self.app, 'follow_group',
id=group_id, apikey=self.annafan['apikey'],
status=409)
assert error['message'].startswith('You are already following ')
def test_03_user_cannot_follow_herself(self):
error = call_action_api(self.app, 'follow_user',
apikey=self.annafan['apikey'], status=409,
id=self.annafan['id'])
assert error['message'] == 'You cannot follow yourself'
def test_04_follower_count_bad_id(self):
for action in ('user_follower_count', 'dataset_follower_count',
'group_follower_count'):
for object_id in ('bad id', ' ', 3, 35.7, 'xxx', ''):
error = call_action_api(self.app, action,
status=409, id=object_id)
assert 'id' in error
def test_04_follower_count_missing_id(self):
for action in ('user_follower_count', 'dataset_follower_count',
'group_follower_count'):
error = call_action_api(self.app, action, status=409)
assert error['id'] == ['Missing value']
def test_04_user_follower_count_no_followers(self):
follower_count = call_action_api(self.app,
'user_follower_count', id=self.annafan['id'])
assert follower_count == 0
def test_04_dataset_follower_count_no_followers(self):
follower_count = call_action_api(self.app,
'dataset_follower_count', id=self.annakarenina['id'])
assert follower_count == 0
def test_04_group_follower_count_no_followers(self):
follower_count = call_action_api(self.app,
'group_follower_count', id=self.davids_group['id'])
assert follower_count == 0
def _followee_count_bad_id(self, action):
for object_id in ('bad id', ' ', 3, 35.7, 'xxx', ''):
error = call_action_api(self.app, action,
status=409, id=object_id)
assert 'id' in error
def test_04_followee_count_bad_id(self):
self._followee_count_bad_id('followee_count')
def test_04_user_followee_count_bad_id(self):
self._followee_count_bad_id('user_followee_count')
def test_04_dataset_followee_count_bad_id(self):
self._followee_count_bad_id('dataset_followee_count')
def test_04_group_followee_count_bad_id(self):
self._followee_count_bad_id('group_followee_count')
def _followee_count_missing_id(self, action):
error = call_action_api(self.app, action, status=409)
assert error['id'] == ['Missing value']
def test_04_followee_count_missing_id(self):
self._followee_count_missing_id('followee_count')
def test_04_user_followee_count_missing_id(self):
self._followee_count_missing_id('user_followee_count')
def test_04_dataset_followee_count_missing_id(self):
self._followee_count_missing_id('dataset_followee_count')
def test_04_group_followee_count_missing_id(self):
self._followee_count_missing_id('group_followee_count')
def _followee_count_not_following_anything(self, action):
followee_count = call_action_api(self.app, action,
id=self.russianfan['id'])
assert followee_count == 0
def test_04_followee_count_not_following_anything(self):
self._followee_count_not_following_anything('followee_count')
def test_04_user_followee_count_not_following_anything(self):
self._followee_count_not_following_anything('user_followee_count')
def test_04_dataset_followee_count_not_following_anything(self):
self._followee_count_not_following_anything('dataset_followee_count')
def test_04_group_followee_count_not_following_anything(self):
self._followee_count_not_following_anything('group_followee_count')
def test_04_follower_list_bad_id(self):
for action in ('user_follower_list', 'dataset_follower_list',
'group_follower_list'):
for object_id in ('bad id', ' ', 3, 35.7, 'xxx', ''):
error = call_action_api(self.app, action,
status=409, id=object_id,
apikey=self.testsysadmin['apikey'])
assert error['id']
def test_04_follower_list_missing_id(self):
for action in ('user_follower_list', 'dataset_follower_list',
'group_follower_list'):
error = call_action_api(self.app, action, status=409,
apikey=self.testsysadmin['apikey'])
assert error['id'] == ['Missing value']
def test_04_user_follower_list_no_followers(self):
followers = call_action_api(self.app, 'user_follower_list',
id=self.annafan['id'], apikey=self.testsysadmin['apikey'])
assert followers == []
def test_04_dataset_follower_list_no_followers(self):
followers = call_action_api(self.app,
'dataset_follower_list', id=self.annakarenina['id'],
apikey=self.testsysadmin['apikey'])
assert followers == []
def test_04_group_follower_list_no_followers(self):
followers = call_action_api(self.app, 'group_follower_list',
id=self.davids_group['id'], apikey=self.testsysadmin['apikey'])
assert followers == []
def _followee_list_bad_id(self, action):
for object_id in ('bad id', ' ', 3, 35.7, 'xxx', ''):
error = call_action_api(self.app, action,
status=409, id=object_id,
apikey=self.testsysadmin['apikey'])
assert error['id']
def test_04_followee_list_bad_id(self):
self._followee_list_bad_id('followee_list')
def test_04_user_followee_list_bad_id(self):
self._followee_list_bad_id('user_followee_list')
def test_04_dataset_followee_list_bad_id(self):
self._followee_list_bad_id('dataset_followee_list')
def test_04_group_followee_list_bad_id(self):
self._followee_list_bad_id('group_followee_list')
def _followee_list_missing_id(self, action):
error = call_action_api(self.app, action, status=409,
apikey=self.testsysadmin['apikey'])
assert error['id'] == ['Missing value']
def test_04_followee_list_missing_id(self):
self._followee_list_missing_id('followee_list')
def test_04_user_followee_list_missing_id(self):
self._followee_list_missing_id('user_followee_list')
def test_04_dataset_followee_missing_bad_id(self):
self._followee_list_missing_id('dataset_followee_list')
def test_04_group_followee_missing_bad_id(self):
self._followee_list_missing_id('group_followee_list')
def _followee_list_not_following_anything(self, action):
followees = call_action_api(self.app, action,
id=self.russianfan['id'], apikey=self.russianfan['apikey'])
assert followees == []
def test_04_followee_list_not_following_anything(self):
self._followee_list_not_following_anything('followee_list')
def test_04_user_followee_list_not_following_anything(self):
self._followee_list_not_following_anything('user_followee_list')
def test_04_dataset_followee_not_following_anything(self):
self._followee_list_not_following_anything('dataset_followee_list')
def test_04_group_followee_not_following_anything(self):
self._followee_list_not_following_anything('group_followee_list')
def test_04_am_following_bad_id(self):
for action in ('am_following_dataset', 'am_following_user',
'am_following_group'):
for object_id in ('bad id', ' ', 3, 35.7, 'xxx'):
error = call_action_api(self.app, action,
apikey=self.annafan['apikey'], status=409, id=object_id)
assert error['id'][0].startswith('Not found: ')
def test_04_am_following_missing_id(self):
for action in ('am_following_dataset', 'am_following_user',
'am_following_group'):
for id in ('missing', None, ''):
if id == 'missing':
error = call_action_api(self.app, action,
apikey=self.annafan['apikey'], status=409)
else:
error = call_action_api(self.app, action,
apikey=self.annafan['apikey'], status=409, id=id)
assert error['id'] == [u'Missing value']
def test_04_am_following_dataset_bad_apikey(self):
for apikey in ('bad api key', '', ' ', 'None', '3', '35.7', 'xxx'):
error = call_action_api(self.app,
'am_following_dataset', apikey=apikey, status=403,
id=self.warandpeace['id'])
assert error['message'] == 'Access denied'
def test_04_am_following_dataset_missing_apikey(self):
error = call_action_api(self.app, 'am_following_dataset',
status=403, id=self.warandpeace['id'])
assert error['message'] == 'Access denied'
def test_04_am_following_user_bad_apikey(self):
for apikey in ('bad api key', '', ' ', 'None', '3', '35.7', 'xxx'):
error = call_action_api(self.app, 'am_following_user',
apikey=apikey, status=403, id=self.annafan['id'])
assert error['message'] == 'Access denied'
def test_04_am_following_user_missing_apikey(self):
error = call_action_api(self.app, 'am_following_user',
status=403, id=self.annafan['id'])
assert error['message'] == 'Access denied'
def test_04_am_following_group_bad_apikey(self):
for apikey in ('bad api key', '', ' ', 'None', '3', '35.7', 'xxx'):
error = call_action_api(self.app, 'am_following_group',
apikey=apikey, status=403, id=self.rogers_group['id'])
assert error['message'] == 'Access denied'
def test_04_am_following_group_missing_apikey(self):
error = call_action_api(self.app, 'am_following_group',
status=403, id=self.rogers_group['id'])
assert error['message'] == 'Access denied'
class TestFollowerDelete(object):
'''Tests for the unfollow_* APIs.'''
@classmethod
def setup_class(self):
CreateTestData.create()
self.tester = {
'id': ckan.model.User.get('tester').id,
'apikey': ckan.model.User.get('tester').apikey,
'name': ckan.model.User.get('tester').name,
}
self.testsysadmin = {
'id': ckan.model.User.get('testsysadmin').id,
'apikey': ckan.model.User.get('testsysadmin').apikey,
'name': ckan.model.User.get('testsysadmin').name,
}
self.annafan = {
'id': ckan.model.User.get('annafan').id,
'apikey': ckan.model.User.get('annafan').apikey,
'name': ckan.model.User.get('annafan').name,
}
self.russianfan = {
'id': ckan.model.User.get('russianfan').id,
'apikey': ckan.model.User.get('russianfan').apikey,
'name': ckan.model.User.get('russianfan').name,
}
self.joeadmin = {
'id': ckan.model.User.get('joeadmin').id,
'apikey': ckan.model.User.get('joeadmin').apikey,
'name': ckan.model.User.get('joeadmin').name,
}
self.warandpeace = {
'id': ckan.model.Package.get('warandpeace').id,
'name': ckan.model.Package.get('warandpeace').name,
}
self.annakarenina = {
'id': ckan.model.Package.get('annakarenina').id,
'name': ckan.model.Package.get('annakarenina').name,
}
self.rogers_group = {
'id': ckan.model.Group.get('roger').id,
'name': ckan.model.Group.get('roger').name,
}
self.davids_group = {
'id': ckan.model.Group.get('david').id,
'name': ckan.model.Group.get('david').name,
}
self.app = helpers._get_test_app()
follow_user(self.app, self.testsysadmin['id'],
self.testsysadmin['apikey'], self.joeadmin['id'],
self.joeadmin['id'], self.testsysadmin['apikey'])
follow_user(self.app, self.tester['id'], self.tester['apikey'],
self.joeadmin['id'], self.joeadmin['id'],
self.testsysadmin['apikey'])
follow_user(self.app, self.russianfan['id'], self.russianfan['apikey'],
self.joeadmin['id'], self.joeadmin['id'],
self.testsysadmin['apikey'])
follow_user(self.app, self.annafan['id'], self.annafan['apikey'],
self.joeadmin['id'], self.joeadmin['id'],
self.testsysadmin['apikey'])
follow_user(self.app, self.annafan['id'], self.annafan['apikey'],
self.tester['id'], self.tester['id'],
self.testsysadmin['apikey'])
follow_dataset(self.app, self.testsysadmin['id'],
self.testsysadmin['apikey'], self.warandpeace['id'],
self.warandpeace['id'], self.testsysadmin['apikey'])
follow_dataset(self.app, self.tester['id'], self.tester['apikey'],
self.warandpeace['id'], self.warandpeace['id'],
self.testsysadmin['apikey'])
follow_dataset(self.app, self.russianfan['id'], self.russianfan['apikey'],
self.warandpeace['id'], self.warandpeace['id'],
self.testsysadmin['apikey'])
follow_dataset(self.app, self.annafan['id'], self.annafan['apikey'],
self.warandpeace['id'], self.warandpeace['id'],
self.testsysadmin['apikey'])
follow_group(self.app, self.annafan['id'], self.annafan['apikey'],
self.davids_group['id'], self.davids_group['id'],
self.testsysadmin['apikey'])
@classmethod
def teardown_class(self):
ckan.model.repo.rebuild_db()
def test_01_unfollow_user_not_exists(self):
'''Test the error response when a user tries to unfollow a user that
she is not following.
'''
error = call_action_api(self.app, 'unfollow_user',
apikey=self.annafan['apikey'], status=404,
id=self.russianfan['id'])
assert error['message'].startswith('Not found: You are not following ')
def test_01_unfollow_dataset_not_exists(self):
'''Test the error response when a user tries to unfollow a dataset that
she is not following.
'''
error = call_action_api(self.app, 'unfollow_dataset',
apikey=self.annafan['apikey'], status=404,
id=self.annakarenina['id'])
assert error['message'].startswith('Not found: You are not following')
def test_01_unfollow_group_not_exists(self):
'''Test the error response when a user tries to unfollow a group that
she is not following.
'''
error = call_action_api(self.app, 'unfollow_group',
apikey=self.annafan['apikey'], status=404,
id=self.rogers_group['id'])
assert error['message'].startswith('Not found: You are not following')
def test_01_unfollow_bad_apikey(self):
'''Test the error response when a user tries to unfollow something
but provides a bad API key.
'''
for action in ('unfollow_user', 'unfollow_dataset', 'unfollow_group'):
for apikey in ('bad api key', '', ' ', 'None', '3', '35.7',
'xxx'):
error = call_action_api(self.app, action,
apikey=apikey, status=403, id=self.joeadmin['id'])
assert error['__type'] == 'Authorization Error'
def test_01_unfollow_missing_apikey(self):
'''Test error response when calling unfollow_* without api key.'''
for action in ('unfollow_user', 'unfollow_dataset', 'unfollow_group'):
error = call_action_api(self.app, action, status=403,
id=self.joeadmin['id'])
assert error['__type'] == 'Authorization Error'
def test_01_unfollow_bad_object_id(self):
'''Test error response when calling unfollow_* with bad object id.'''
for action in ('unfollow_user', 'unfollow_dataset', 'unfollow_group'):
for object_id in ('bad id', ' ', 3, 35.7, 'xxx'):
error = call_action_api(self.app, action,
apikey=self.annafan['apikey'], status=409,
id=object_id)
assert error['id'][0].startswith('Not found')
def test_01_unfollow_missing_object_id(self):
for action in ('unfollow_user', 'unfollow_dataset', 'unfollow_group'):
for id in ('missing', None, ''):
if id == 'missing':
error = call_action_api(self.app, action,
apikey=self.annafan['apikey'], status=409)
else:
error = call_action_api(self.app, action,
apikey=self.annafan['apikey'], status=409, id=id)
assert error['id'] == [u'Missing value']
def _unfollow_user(self, follower_id, apikey, object_id, object_arg):
'''Test a user unfollowing a user via the API.
:param follower_id: id of the follower.
:param apikey: API key of the follower.
:param object_id: id of the object to unfollow.
:param object_arg: the argument to pass to unfollow_user as the id of
the object to unfollow, could be the object's id or name.
'''
# Record the user's number of followers before.
count_before = call_action_api(self.app,
'user_follower_count', id=object_id)
followee_count_before = call_action_api(self.app,
'followee_count', id=follower_id)
user_followee_count_before = call_action_api(self.app,
'user_followee_count', id=follower_id)
# Check that the user is following the object.
am_following = call_action_api(self.app,
'am_following_user', apikey=apikey, id=object_id)
assert am_following is True
# Make the user unfollow the object.
call_action_api(self.app, 'unfollow_user', apikey=apikey,
id=object_arg)
# Check that am_following_user now returns False.
am_following = call_action_api(self.app,
'am_following_user', apikey=apikey, id=object_id)
assert am_following is False
# Check that the user doesn't appear in the object's list of followers.
followers = call_action_api(self.app, 'user_follower_list',
id=object_id, apikey=self.testsysadmin['apikey'])
assert len([follower for follower in followers if follower['id'] ==
follower_id]) == 0
# Check that the object's follower count has decreased by 1.
count_after = call_action_api(self.app,
'user_follower_count', id=object_id)
assert count_after == count_before - 1
# Check that the user doesn't appear in the subject's list of
# followees.
followees = call_action_api(self.app, 'followee_list',
id=follower_id, apikey=apikey)
assert len([followee for followee in followees
if followee['dict']['id'] == object_id]) == 0
followees = call_action_api(self.app, 'user_followee_list',
id=follower_id, apikey=apikey)
assert len([followee for followee in followees
if followee['id'] == object_id]) == 0
# Check the the subject's followee cont has decreased by 1.
count_after = call_action_api(self.app, 'followee_count',
id=follower_id)
assert count_after == followee_count_before - 1
count_after = call_action_api(self.app,
'user_followee_count', id=follower_id)
assert count_after == user_followee_count_before - 1
def _unfollow_dataset(self, user_id, apikey, dataset_id, dataset_arg):
'''Test a user unfollowing a dataset via the API.
:param user_id: id of the follower.
:param apikey: API key of the follower.
:param dataset_id: id of the object to unfollow.
:param dataset_arg: the argument to pass to unfollow_dataset as the id
of the object to unfollow, could be the object's id or name.
'''
# Record the dataset's number of followers before.
count_before = call_action_api(self.app,
'dataset_follower_count', id=dataset_id)
followee_count_before = call_action_api(self.app,
'followee_count', id=user_id)
dataset_followee_count_before = call_action_api(self.app,
'dataset_followee_count', id=user_id)
# Check that the user is following the dataset.
am_following = call_action_api(self.app,
'am_following_dataset', apikey=apikey, id=dataset_id)
assert am_following is True
# Make the user unfollow the dataset.
call_action_api(self.app, 'unfollow_dataset', apikey=apikey,
id=dataset_arg)
# Check that am_following_dataset now returns False.
am_following = call_action_api(self.app,