-
Notifications
You must be signed in to change notification settings - Fork 2k
/
test_activity.py
2123 lines (1822 loc) · 88.3 KB
/
test_activity.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
'''Functional tests for the public activity streams API.
This module tests the contents of the various public activity streams:
use activity streams, dataset activity streams, group activity streams, etc.
This module _does not_ test the private user dashboard activity stream (which
is different because the contents depend on what the user is following), that
is tested in test_dashboard.py.
'''
import datetime
import logging
logger = logging.getLogger(__name__)
import pylons.test
from pylons import config
from paste.deploy.converters import asbool
import paste.fixture
from nose import SkipTest
from ckan.common import json
##def package_update(context, data_dict):
## # These tests call package_update directly which is really bad
## # setting api_version in context make things seem like the api key
## # is ok etc
## context['api_version'] = 3
## context['ignore_auth'] = True
## return _package_update(context, data_dict)
##
##def package_create(context, data_dict):
## # These tests call package_update directly which is really bad
## # setting api_version in context make things seem like the api key
## # is ok etc
## context['api_version'] = 3
## context['ignore_auth'] = True
## return _package_create(context, data_dict)
def package_show(app, data_dict, apikey=None):
if apikey:
extra_environ = {'Authorization': str(apikey)}
else:
extra_environ = None
response = app.post('/api/action/package_show', json.dumps(data_dict),
extra_environ=extra_environ)
response_dict = json.loads(response.body)
assert response_dict['success'] is True
package = response_dict['result']
return package
def package_list(app, data_dict=None, apikey=None):
if data_dict is None:
data_dict = {}
if apikey:
extra_environ = {'Authorization': str(apikey)}
else:
extra_environ = None
response = app.post('/api/action/package_list',
json.dumps(data_dict), extra_environ=extra_environ)
response_dict = json.loads(response.body)
assert response_dict['success'] is True
packages = response_dict['result']
return packages
def group_list(app, data_dict=None, apikey=None):
if data_dict is None:
data_dict = {}
if 'all_fields' not in data_dict:
data_dict['all_fields'] = True
if apikey:
extra_environ = {'Authorization': str(apikey)}
else:
extra_environ = None
response = app.post('/api/action/group_list',
json.dumps(data_dict), extra_environ=extra_environ)
response_dict = json.loads(response.body)
assert response_dict['success'] is True
groups = response_dict['result']
return groups
def package_update(app, data_dict, apikey=None):
if apikey:
extra_environ = {'Authorization': str(apikey)}
else:
extra_environ = None
response = app.post('/api/action/package_update',
json.dumps(data_dict), extra_environ=extra_environ)
response_dict = json.loads(response.body)
assert response_dict['success'] is True
updated_package = response_dict['result']
return updated_package
def group_update(app, data_dict, apikey=None):
if apikey:
extra_environ = {'Authorization': str(apikey)}
else:
extra_environ = None
response = app.post('/api/action/group_update',
json.dumps(data_dict), extra_environ=extra_environ)
response_dict = json.loads(response.body)
assert response_dict['success'] is True
updated_group = response_dict['result']
return updated_group
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 make_resource():
'''Return a test resource in dictionary form.'''
return {
'url': 'http://www.example.com',
'description': 'example resource description',
'format': 'txt',
'name': 'example resource',
}
def make_package(name=None):
'''Return a test package in dictionary form.'''
if name is None:
name = "test_package"
# A package with no resources, tags, extras or groups.
pkg = {
'name': name,
'title': 'My Test Package',
'author': 'test author',
'author_email': 'test_author@test_author.com',
'maintainer': 'test maintainer',
'maintainer_email': 'test_maintainer@test_maintainer.com',
'notes': 'some test notes',
'url': 'www.example.com',
}
# Add some resources to the package.
res1 = {
'url': 'http://www.example-resource.info',
'description': 'an example resource description',
'format': 'HTML',
'name': 'an example resource',
}
res2 = {
'url': 'http://www.example-resource2.info',
'description': 'another example resource description',
'format': 'PDF',
'name': 'another example resource',
}
pkg['resources'] = [res1, res2]
# Add some tags to the package.
tag1 = {'name': 'a_test_tag'}
tag2 = {'name': 'another_test_tag'}
pkg['tags'] = [tag1, tag2]
# Add the package to a group.
pkg['groups'] = [{'name': 'roger'}]
return pkg
def find_new_activities(before, after):
return [activity for activity in after if activity not in before]
class TestActivity:
@classmethod
def setup_class(self):
if not asbool(config.get('ckan.activity_streams_enabled', 'true')):
raise SkipTest('Activity streams not enabled')
import ckan
import ckan.model as model
ckan.tests.CreateTestData.create()
sysadmin_user = model.User.get('testsysadmin')
self.sysadmin_user = {
'id': sysadmin_user.id,
'apikey': sysadmin_user.apikey,
'name': sysadmin_user.name,
}
normal_user = model.User.get('annafan')
self.normal_user = {
'id': normal_user.id,
'apikey': normal_user.apikey,
'name': normal_user.name,
}
warandpeace = model.Package.get('warandpeace')
self.warandpeace = {
'id': warandpeace.id,
}
annakarenina = model.Package.get('annakarenina')
self.annakarenina = {
'id': annakarenina.id,
}
self.users = [self.sysadmin_user, self.normal_user]
self.app = paste.fixture.TestApp(pylons.test.pylonsapp)
@classmethod
def teardown_class(self):
import ckan.model as model
model.repo.rebuild_db()
def user_activity_stream(self, user_id, apikey=None):
if apikey:
extra_environ = {'Authorization': str(apikey)}
else:
extra_environ = None
response = self.app.get("/api/2/rest/user/%s/activity" % user_id,
extra_environ=extra_environ)
return json.loads(response.body)
def package_activity_stream(self, package_id, apikey=None):
if apikey:
extra_environ = {'Authorization': str(apikey)}
else:
extra_environ = None
print '@@@@@@@@', extra_environ
response = self.app.get("/api/2/rest/dataset/%s/activity" % package_id,
extra_environ=extra_environ)
return json.loads(response.body)
def group_activity_stream(self, group_id, apikey=None):
if apikey:
extra_environ = {'Authorization': str(apikey)}
else:
extra_environ = None
response = self.app.get("/api/2/rest/group/%s/activity" % group_id,
extra_environ=extra_environ)
return json.loads(response.body)
def recently_changed_datasets_stream(self, apikey=None):
if apikey:
extra_environ = {'Authorization': str(apikey)}
else:
extra_environ = None
response = self.app.post(
'/api/action/recently_changed_packages_activity_list',
params=json.dumps({}),
extra_environ=extra_environ,
status=200)
assert response.json['success'] is True
activities = response.json['result']
return activities
def activity_details(self, activity):
response = self.app.get(
"/api/2/rest/activity/%s/details" % activity['id'])
return json.loads(response.body)
def record_details(self, user_id, package_id=None, group_ids=None,
apikey=None):
details = {}
details['user activity stream'] = self.user_activity_stream(user_id,
apikey)
if package_id is not None:
details['package activity stream'] = (
self.package_activity_stream(package_id, apikey))
if group_ids is not None:
details['group activity streams'] = {}
for group_id in group_ids:
details['group activity streams'][group_id] = (
self.group_activity_stream(group_id, apikey))
details['recently changed datasets stream'] = \
self.recently_changed_datasets_stream(apikey)
details['time'] = datetime.datetime.now()
return details
def _create_package(self, user, name=None):
if user:
user_id = user['id']
apikey = user['apikey']
else:
user_id = 'not logged in'
apikey = None
before = self.record_details(user_id, apikey=apikey)
# Create a new package.
request_data = make_package(name)
before = self.record_details(user_id=user_id,
group_ids=[group['name'] for group in request_data['groups']],
apikey=apikey)
extra_environ = {'Authorization': str(user['apikey'])}
response = self.app.post('/api/action/package_create',
json.dumps(request_data), extra_environ=extra_environ)
response_dict = json.loads(response.body)
assert response_dict['success'] is True
package_created = response_dict['result']
after = self.record_details(user_id=user_id,
package_id=package_created['id'],
group_ids=[group['name'] for group in package_created['groups']],
apikey=apikey)
# Find the new activity in the user's activity stream.
user_new_activities = (find_new_activities(
before['user activity stream'], after['user activity stream']))
assert len(user_new_activities) == 1, ("There should be 1 new "
" activity in the user's activity stream, but found %i" %
len(user_new_activities))
activity = user_new_activities[0]
# The same new activity should appear in the package's activity stream.
pkg_new_activities = after['package activity stream']
assert pkg_new_activities == [activity]
# The same new activity should appear in the recently changed datasets
# stream.
new_rcd_activities = find_new_activities(
before['recently changed datasets stream'],
after['recently changed datasets stream'])
assert new_rcd_activities == [activity]
# The same new activity should appear in the activity streams of the
# package's groups.
for group_dict in package_created['groups']:
grp_new_activities = find_new_activities(
before['group activity streams'][group_dict['name']],
after['group activity streams'][group_dict['name']])
assert [activity['id'] for activity in grp_new_activities] == [
activity['id']]
# Check that the new activity has the right attributes.
assert activity['object_id'] == package_created['id'], \
str(activity['object_id'])
assert activity['user_id'] == user_id, str(activity['user_id'])
assert activity['activity_type'] == 'new package', \
str(activity['activity_type'])
if 'id' not in activity:
assert False, "activity object should have an id value"
# TODO: Test for the _correct_ revision_id value.
if 'revision_id' not in activity:
assert False, "activity object should have a revision_id value"
timestamp = datetime_from_string(activity['timestamp'])
assert timestamp >= before['time'] and timestamp <= \
after['time'], str(activity['timestamp'])
details = self.activity_details(activity)
# There should be five activity details: one for the package itself,
# one for each of its two resources, and one for each of its two tags.
assert len(details) == 5, "There should be five activity details."
detail_ids = [detail['object_id'] for detail in details]
assert detail_ids.count(package_created['id']) == 1, (
"There should be one activity detail for the package itself.")
for resource in package_created['resources']:
assert detail_ids.count(resource['id']) == 1, (
"There should be one activity detail for each of the "
"package's resources")
for detail in details:
assert detail['activity_id'] == activity['id'], \
str(detail['activity_id'])
if detail['object_id'] == package_created['id']:
assert detail['activity_type'] == "new", (
str(detail['activity_type']))
assert detail['object_type'] == "Package", \
str(detail['object_type'])
elif (detail['object_id'] in
[resource['id'] for resource in package_created['resources']]):
assert detail['activity_type'] == "new", (
str(detail['activity_type']))
assert detail['object_type'] == "Resource", (
str(detail['object_type']))
else:
assert detail['activity_type'] == "added", (
str(detail['activity_type']))
assert detail['object_type'] == "tag", (
str(detail['object_type']))
def _add_resource(self, package, user):
if user:
user_id = user['id']
apikey = user['apikey']
else:
user_id = 'not logged in'
apikey = None
before = self.record_details(user_id, package['id'],
[group['name'] for group in package['groups']], apikey=apikey)
resource_ids_before = [resource['id'] for resource in
package['resources']]
# Create a new resource.
resources = package['resources']
resources.append(make_resource())
updated_package = package_update(self.app, package, user['apikey'])
after = self.record_details(user_id, package['id'],
[group['name'] for group in package['groups']], apikey=apikey)
resource_ids_after = [resource['id'] for resource in
updated_package['resources']]
assert len(resource_ids_after) == len(resource_ids_before) + 1
# Find the new activity in the user's activity stream.
user_new_activities = (find_new_activities(
before['user activity stream'], after['user activity stream']))
assert len(user_new_activities) == 1, ("There should be 1 new "
" activity in the user's activity stream, but found %i" %
len(user_new_activities))
activity = user_new_activities[0]
# The same new activity should appear in the package's activity stream.
pkg_new_activities = (find_new_activities(
before['package activity stream'],
after['package activity stream']))
assert pkg_new_activities == user_new_activities
# The same new activity should appear in the recently changed datasets
# stream.
assert find_new_activities(
before['recently changed datasets stream'],
after['recently changed datasets stream']) \
== user_new_activities
# If the package has any groups, the same new activity should appear
# in the activity stream of each group.
for group_dict in package['groups']:
grp_new_activities = find_new_activities(
before['group activity streams'][group_dict['name']],
after['group activity streams'][group_dict['name']])
assert grp_new_activities == [activity]
# Check that the new activity has the right attributes.
assert activity['object_id'] == updated_package['id'], \
str(activity['object_id'])
assert activity['user_id'] == user_id, str(activity['user_id'])
assert activity['activity_type'] == 'changed package', \
str(activity['activity_type'])
if 'id' not in activity:
assert False, "activity object should have an id value"
# TODO: Test for the _correct_ revision_id value.
if 'revision_id' not in activity:
assert False, "activity object should have a revision_id value"
timestamp = datetime_from_string(activity['timestamp'])
assert (timestamp >= before['time'] and
timestamp <= after['time']), str(activity['timestamp'])
# Test for the presence of a correct activity detail item.
details = self.activity_details(activity)
assert len(details) == 1, [(detail['activity_type'],
detail['object_type']) for detail in details]
detail = details[0]
assert detail['activity_id'] == activity['id'], \
str(detail['activity_id'])
new_resource_ids = [id for id in resource_ids_after if id not in
resource_ids_before]
assert len(new_resource_ids) == 1
new_resource_id = new_resource_ids[0]
assert detail['object_id'] == new_resource_id, (
str(detail['object_id']))
assert detail['object_type'] == "Resource", (
str(detail['object_type']))
assert detail['activity_type'] == "new", (
str(detail['activity_type']))
def _delete_extra(self, package_dict, user):
if user:
user_id = user['id']
apikey = user['apikey']
else:
user_id = 'not logged in'
apikey = None
before = self.record_details(user_id, package_dict['id'],
[group['name'] for group in package_dict['groups']],
apikey=apikey)
extras_before = list(package_dict['extras'])
assert len(extras_before) > 0, (
"Can't update an extra if the package doesn't have any")
# Update the package's first extra.
del package_dict['extras'][0]
updated_package = package_update(self.app, package_dict,
user['apikey'])
after = self.record_details(user_id, package_dict['id'],
[group['name'] for group in package_dict['groups']],
apikey=apikey)
extras_after = updated_package['extras']
assert len(extras_after) == len(extras_before) - 1, (
"%s != %s" % (len(extras_after), len(extras_before) - 1))
# Find the new activity in the user's activity stream.
user_new_activities = (find_new_activities(
before['user activity stream'], after['user activity stream']))
assert len(user_new_activities) == 1, ("There should be 1 new "
" activity in the user's activity stream, but found %i" %
len(user_new_activities))
activity = user_new_activities[0]
# The same new activity should appear in the package's activity stream.
pkg_new_activities = (find_new_activities(
before['package activity stream'],
after['package activity stream']))
assert pkg_new_activities == user_new_activities
# The same new activity should appear in the recently changed datasets
# stream.
assert find_new_activities(
before['recently changed datasets stream'],
after['recently changed datasets stream']) \
== user_new_activities
# If the package has any groups, the same new activity should appear
# in the activity stream of each group.
for group_dict in package_dict['groups']:
grp_new_activities = find_new_activities(
before['group activity streams'][group_dict['name']],
after['group activity streams'][group_dict['name']])
assert grp_new_activities == [activity]
# Check that the new activity has the right attributes.
assert activity['object_id'] == updated_package['id'], \
str(activity['object_id'])
assert activity['user_id'] == user_id, str(activity['user_id'])
assert activity['activity_type'] == 'changed package', \
str(activity['activity_type'])
if 'id' not in activity:
assert False, "activity object should have an id value"
# TODO: Test for the _correct_ revision_id value.
if 'revision_id' not in activity:
assert False, "activity object should have a revision_id value"
timestamp = datetime_from_string(activity['timestamp'])
assert (timestamp >= before['time'] and
timestamp <= after['time']), str(activity['timestamp'])
# Test for the presence of a correct activity detail item.
details = self.activity_details(activity)
assert len(details) == 1, (
"There should be 1 activity detail but found %s"
% len(details))
detail = details[0]
assert detail['activity_id'] == activity['id'], \
str(detail['activity_id'])
deleted_extras = [extra for extra in extras_before if extra not in
extras_after]
assert len(deleted_extras) == 1, "%s != 1" % len(deleted_extras)
deleted_extra = deleted_extras[0]
assert detail['object_type'] == "PackageExtra", (
str(detail['object_type']))
assert detail['activity_type'] == "deleted", (
str(detail['activity_type']))
def _update_extra(self, package_dict, user):
if user:
user_id = user['id']
apikey = user['apikey']
else:
user_id = 'not logged in'
apikey=None
before = self.record_details(user_id, package_dict['id'],
[group['name'] for group in package_dict['groups']],
apikey=apikey)
extras_before = package_dict['extras']
assert len(extras_before) > 0, (
"Can't update an extra if the package doesn't have any")
# Update the package's first extra.
extras = list(extras_before)
if extras[0]['value'] != '"edited"':
extras[0]['value'] = '"edited"'
else:
assert extras[0]['value'] != '"edited again"'
extras[0]['value'] = '"edited again"'
updated_package = package_update(self.app, package_dict,
user['apikey'])
after = self.record_details(user_id, package_dict['id'],
[group['name'] for group in package_dict['groups']],
apikey=apikey)
extras_after = updated_package['extras']
assert len(extras_after) == len(extras_before), (
"%s != %s" % (len(extras_after), len(extras_before)))
# Find the new activity in the user's activity stream.
user_new_activities = (find_new_activities(
before['user activity stream'], after['user activity stream']))
assert len(user_new_activities) == 1, ("There should be 1 new "
" activity in the user's activity stream, but found %i" %
len(user_new_activities))
activity = user_new_activities[0]
# The same new activity should appear in the package's activity stream.
pkg_new_activities = (find_new_activities(
before['package activity stream'],
after['package activity stream']))
assert pkg_new_activities == user_new_activities
# The same new activity should appear in the recently changed datasets
# stream.
assert find_new_activities(
before['recently changed datasets stream'],
after['recently changed datasets stream']) \
== user_new_activities
# If the package has any groups, the same new activity should appear
# in the activity stream of each group.
for group_dict in package_dict['groups']:
grp_new_activities = find_new_activities(
before['group activity streams'][group_dict['name']],
after['group activity streams'][group_dict['name']])
assert grp_new_activities == [activity]
# Check that the new activity has the right attributes.
assert activity['object_id'] == updated_package['id'], \
str(activity['object_id'])
assert activity['user_id'] == user_id, str(activity['user_id'])
assert activity['activity_type'] == 'changed package', \
str(activity['activity_type'])
if 'id' not in activity:
assert False, "activity object should have an id value"
# TODO: Test for the _correct_ revision_id value.
if 'revision_id' not in activity:
assert False, "activity object should have a revision_id value"
timestamp = datetime_from_string(activity['timestamp'])
assert (timestamp >= before['time'] and
timestamp <= after['time']), str(activity['timestamp'])
# Test for the presence of a correct activity detail item.
details = self.activity_details(activity)
assert len(details) == 1, (
"There should be 1 activity detail but found %s"
% len(details))
detail = details[0]
assert detail['activity_id'] == activity['id'], \
str(detail['activity_id'])
new_extras = [extra for extra in extras_after if extra not in
extras_before]
assert len(new_extras) == 1, "%s != 1" % len(new_extras)
new_extra = new_extras[0]
assert detail['object_type'] == "PackageExtra", (
str(detail['object_type']))
assert detail['activity_type'] == "changed", (
str(detail['activity_type']))
def _add_extra(self, package_dict, user, key=None):
if key is None:
key = 'quality'
if user:
user_id = user['id']
apikey = user['apikey']
else:
user_id = 'not logged in'
apikey = None
before = self.record_details(user_id, package_dict['id'],
[group['name'] for group in package_dict['groups']],
apikey=apikey)
# Make a copy of the package's extras before we add a new extra,
# so we can compare the extras before and after updating the package.
extras_before = list(package_dict['extras'])
# Create a new extra.
extras = package_dict['extras']
extras.append({'key': key, 'value': '10000'})
updated_package = package_update(self.app, package_dict,
user['apikey'])
after = self.record_details(user_id, package_dict['id'],
[group['name'] for group in package_dict['groups']],
apikey=apikey)
extras_after = updated_package['extras']
assert len(extras_after) == len(extras_before) + 1, (
"%s != %s" % (len(extras_after), len(extras_before) + 1))
# Find the new activity in the user's activity stream.
user_new_activities = (find_new_activities(
before['user activity stream'], after['user activity stream']))
assert len(user_new_activities) == 1, ("There should be 1 new "
" activity in the user's activity stream, but found %i" %
len(user_new_activities))
activity = user_new_activities[0]
# The same new activity should appear in the package's activity stream.
pkg_new_activities = (find_new_activities(
before['package activity stream'],
after['package activity stream']))
assert pkg_new_activities == user_new_activities
# The same new activity should appear in the recently changed datasets
# stream.
assert find_new_activities(
before['recently changed datasets stream'],
after['recently changed datasets stream']) \
== user_new_activities
# If the package has any groups, the same new activity should appear
# in the activity stream of each group.
for group_dict in package_dict['groups']:
grp_new_activities = find_new_activities(
before['group activity streams'][group_dict['name']],
after['group activity streams'][group_dict['name']])
assert grp_new_activities == [activity]
# Check that the new activity has the right attributes.
assert activity['object_id'] == updated_package['id'], \
str(activity['object_id'])
assert activity['user_id'] == user_id, str(activity['user_id'])
assert activity['activity_type'] == 'changed package', \
str(activity['activity_type'])
if 'id' not in activity:
assert False, "activity object should have an id value"
# TODO: Test for the _correct_ revision_id value.
if 'revision_id' not in activity:
assert False, "activity object should have a revision_id value"
timestamp = datetime_from_string(activity['timestamp'])
assert (timestamp >= before['time'] and
timestamp <= after['time']), str(activity['timestamp'])
# Test for the presence of a correct activity detail item.
details = self.activity_details(activity)
assert len(details) == 1, (
"There should be 1 activity detail but found %s"
% len(details))
detail = details[0]
assert detail['activity_id'] == activity['id'], \
str(detail['activity_id'])
new_extras = [extra for extra in extras_after if extra not in
extras_before]
assert len(new_extras) == 1, "%s != 1" % len(new_extras)
new_extra = new_extras[0]
assert detail['object_type'] == "PackageExtra", (
str(detail['object_type']))
assert detail['activity_type'] == "new", (
str(detail['activity_type']))
def _create_activity(self, user, package, params):
before = self.record_details(user['id'], package['id'],
apikey=user['apikey'])
response = self.app.post('/api/action/activity_create',
params=json.dumps(params),
extra_environ={'Authorization': str(self.sysadmin_user['apikey'])})
assert response.json['success'] is True
after = self.record_details(user['id'], package['id'],
apikey=user['apikey'])
# Find the new activity in the user's activity stream.
user_new_activities = (find_new_activities(
before['user activity stream'], after['user activity stream']))
assert len(user_new_activities) == 1, ("There should be 1 new "
" activity in the user's activity stream, but found %i" %
len(user_new_activities))
activity = user_new_activities[0]
# The same new activity should appear in the package's activity stream.
pkg_new_activities = (find_new_activities(
before['package activity stream'],
after['package activity stream']))
assert pkg_new_activities == user_new_activities
# Check that the new activity has the right attributes.
assert activity['object_id'] == params['object_id'], (
str(activity['object_id']))
assert activity['user_id'] == params['user_id'], (
str(activity['user_id']))
assert activity['activity_type'] == params['activity_type'], (
str(activity['activity_type']))
if 'id' not in activity:
assert False, "activity object has no id value"
# TODO: Test for the _correct_ revision_id value.
if 'revision_id' not in activity:
assert False, "activity has no revision_id value"
timestamp = datetime_from_string(activity['timestamp'])
assert (timestamp >= before['time'] and
timestamp <= after['time']), str(activity['timestamp'])
def _delete_group(self, group, user):
"""
Delete the given group and test that the correct activity stream
item and detail are emitted.
"""
before = self.record_details(user['id'], group_ids=[group['id']],
apikey=user['apikey'])
# Deleted the group.
group_dict = {'id': group['id'], 'state': 'deleted'}
group_update(self.app, group_dict, user['apikey'])
after = self.record_details(user['id'], group_ids=[group['id']],
apikey=user['apikey'])
# Find the new activity.
new_activities = find_new_activities(before['user activity stream'],
after['user activity stream'])
assert len(new_activities) == 1, ("There should be 1 new activity in "
"the user's activity stream, but found %i" % len(new_activities))
activity = new_activities[0]
assert find_new_activities(
before["group activity streams"][group['id']],
after['group activity streams'][group['id']]) == \
new_activities, ("The same activity should also "
"appear in the group's activity stream.")
# Check that the new activity has the right attributes.
assert activity['object_id'] == group['id'], str(activity['object_id'])
assert activity['user_id'] == user['id'], str(activity['user_id'])
assert activity['activity_type'] == 'deleted group', \
str(activity['activity_type'])
if 'id' not in activity:
assert False, "activity object has no id value"
# TODO: Test for the _correct_ revision_id value.
if 'revision_id' not in activity:
assert False, "activity has no revision_id value"
timestamp = datetime_from_string(activity['timestamp'])
assert timestamp >= before['time'] and timestamp <= after['time'], \
str(activity['timestamp'])
def _update_group(self, group, user):
"""
Update the given group and test that the correct activity stream
item and detail are emitted.
"""
before = self.record_details(user['id'], group_ids=[group['id']],
apikey=user['apikey'])
# Update the group.
group_dict = {'id': group['id'], 'title': 'edited'}
group_update(self.app, group_dict, user['apikey'])
after = self.record_details(user['id'], group_ids=[group['id']],
apikey=user['apikey'])
# Find the new activity.
new_activities = find_new_activities(before['user activity stream'],
after['user activity stream'])
assert len(new_activities) == 1, ("There should be 1 new activity in "
"the user's activity stream, but found %i" % len(new_activities))
activity = new_activities[0]
assert find_new_activities(
before["group activity streams"][group['id']],
after['group activity streams'][group['id']]) == \
new_activities, ("The same activity should also "
"appear in the group's activity stream.")
# Check that the new activity has the right attributes.
assert activity['object_id'] == group['id'], str(activity['object_id'])
assert activity['user_id'] == user['id'], str(activity['user_id'])
assert activity['activity_type'] == 'changed group', \
str(activity['activity_type'])
if 'id' not in activity:
assert False, "activity object has no id value"
# TODO: Test for the _correct_ revision_id value.
if 'revision_id' not in activity:
assert False, "activity has no revision_id value"
timestamp = datetime_from_string(activity['timestamp'])
assert timestamp >= before['time'] and timestamp <= after['time'], \
str(activity['timestamp'])
def _update_user(self, user):
"""
Update the given user and test that the correct activity stream item
and detail are emitted.
"""
response = self.app.post('/api/action/user_show',
json.dumps({'id': user['id']}),
extra_environ={'Authorization': str(user['apikey'])})
response_dict = json.loads(response.body)
assert response_dict['success'] is True
user_dict = response_dict['result']
before = self.record_details(user_dict['id'],
apikey=user_dict['apikey'])
# Update the user.
user_dict['about'] = 'edited'
if not user_dict.get('email'):
user_dict['email'] = 'there has to be a value in email'
self.app.post('/api/action/user_update', json.dumps(user_dict),
extra_environ={'Authorization': str(user['apikey'])})
after = self.record_details(user_dict['id'],
apikey=user_dict['apikey'])
# Find the new activity.
new_activities = find_new_activities(before['user activity stream'],
after['user activity stream'])
assert len(new_activities) == 1, ("There should be 1 new activity in "
"the user's activity stream, but found %i" % len(new_activities))
activity = new_activities[0]
# Check that the new activity has the right attributes.
assert activity['object_id'] == user_dict['id'], (
str(activity['object_id']))
assert activity['user_id'] == user_dict['id'], str(activity['user_id'])
assert activity['activity_type'] == 'changed user', (
str(activity['activity_type']))
if 'id' not in activity:
assert False, "activity object has no id value"
# TODO: Test for the _correct_ revision_id value.
if 'revision_id' not in activity:
assert False, "activity has no revision_id value"
timestamp = datetime_from_string(activity['timestamp'])
assert timestamp >= before['time'] and timestamp <= after['time'], \
str(activity['timestamp'])
def _delete_resources(self, package):
"""
Remove all resources (if any) from the given package, and test that
correct activity item and detail items are emitted.
"""
before = self.record_details(self.normal_user['id'], package['id'],
[group['name'] for group in package['groups']],
apikey=self.normal_user['apikey'])
num_resources = len(package['resources'])
assert num_resources > 0, \
"Cannot delete resources if there aren't any."
resource_ids = [resource['id'] for resource in package['resources']]
package['resources'] = []
package_update(self.app, package, self.normal_user['apikey'])
after = self.record_details(self.normal_user['id'], package['id'],
[group['name'] for group in package['groups']],
apikey=self.normal_user['apikey'])
# Find the new activity in the user's activity stream.
user_new_activities = (find_new_activities(
before['user activity stream'], after['user activity stream']))
assert len(user_new_activities) == 1, ("There should be 1 new "
" activity in the user's activity stream, but found %i" %
len(user_new_activities))
activity = user_new_activities[0]
# The same new activity should appear in the package's activity stream.
pkg_new_activities = (find_new_activities(
before['package activity stream'],
after['package activity stream']))
assert pkg_new_activities == user_new_activities
# The same new activity should appear in the recently changed datasets
# stream.
assert find_new_activities(
before['recently changed datasets stream'],
after['recently changed datasets stream']) \
== user_new_activities
# If the package has any groups, the same new activity should appear
# in the activity stream of each group.
for group_dict in package['groups']:
grp_new_activities = find_new_activities(
before['group activity streams'][group_dict['name']],
after['group activity streams'][group_dict['name']])
assert grp_new_activities == [activity]
# Check that the new activity has the right attributes.
assert activity['object_id'] == package['id'], (
str(activity['object_id']))
assert activity['user_id'] == self.normal_user['id'], (
str(activity['user_id']))
assert activity['activity_type'] == 'changed package', (
str(activity['activity_type']))
if 'id' not in activity:
assert False, "activity object has no id value"
# TODO: Test for the _correct_ revision_id value.
if 'revision_id' not in activity:
assert False, "activity has no revision_id value"
timestamp = datetime_from_string(activity['timestamp'])
assert timestamp >= before['time'], str(activity['timestamp'])
assert timestamp <= after['time'], str(activity['timestamp'])
# Test for the presence of correct activity detail items.
details = self.activity_details(activity)
assert len(details) == num_resources
for detail in details:
assert detail['activity_id'] == activity['id'], (
"activity_id should be %s but is %s"
% (activity['id'], detail['activity_id']))
assert detail['object_id'] in resource_ids, (
str(detail['object_id']))
assert detail['object_type'] == "Resource", (