-
Notifications
You must be signed in to change notification settings - Fork 2k
/
test_vocabulary.py
1135 lines (990 loc) · 46.1 KB
/
test_vocabulary.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
import ckan
from pylons.test import pylonsapp
import paste.fixture
from ckan.common import json
import ckan.lib.dictization.model_dictize as model_dictize
import sqlalchemy
from nose.tools import raises, assert_raises
class TestVocabulary(object):
@classmethod
def setup_class(self):
self.app = paste.fixture.TestApp(pylonsapp)
@classmethod
def teardown_class(self):
ckan.model.repo.rebuild_db()
def setup(self):
self.clean_vocab()
model = ckan.model
context = {'model': model}
genre = model.Vocabulary("Genre")
time_period = ckan.model.Vocabulary("Time Period")
composers = ckan.model.Vocabulary("Composers")
model.Session.add_all([genre, time_period, composers])
self.genre_vocab = model_dictize.vocabulary_dictize(genre, context)
self.timeperiod_vocab = model_dictize.vocabulary_dictize(time_period, context)
self.composers_vocab = model_dictize.vocabulary_dictize(composers, context)
ckan.model.Session.commit()
self.sysadmin_user = ckan.model.User.get('admin')
self.normal_user = ckan.model.User.get('normal')
if not self.sysadmin_user:
normal_user = ckan.model.User(name=u'normal', password=u'annafan')
sysadmin_user = ckan.model.User(name=u'admin', password=u'testsysadmin')
sysadmin_user.sysadmin = True
ckan.model.Session.add(normal_user)
ckan.model.Session.add(sysadmin_user)
ckan.model.Session.commit()
self.sysadmin_user = ckan.model.User.get('admin')
self.normal_user = ckan.model.User.get('normal')
self.sysadmin_apikey = self.sysadmin_user.apikey
def clean_vocab(self):
ckan.model.Session.execute('delete from package_tag_revision')
ckan.model.Session.execute('delete from package_tag')
ckan.model.Session.execute('delete from tag')
ckan.model.Session.execute('delete from vocabulary')
ckan.model.Session.commit()
@classmethod
def _post(self, url, params=None, extra_environ=None):
if params is None:
params = {}
param_string = json.dumps(params)
response = self.app.post(url, params=param_string,
extra_environ=extra_environ)
assert not response.errors
return response.json
@classmethod
def _create_vocabulary(self, vocab_name=None, user=None):
# Create a new vocabulary.
params = {'name': vocab_name}
if user:
extra_environ = {'Authorization' : str(user.apikey)}
else:
extra_environ = None
response = self._post('/api/action/vocabulary_create', params=params,
extra_environ=extra_environ)
# Check the values of the response.
assert response['success'] == True
assert response['result']
created_vocab = response['result']
assert created_vocab['name'] == vocab_name
assert created_vocab['id']
# Get the list of vocabularies.
response = self._post('/api/action/vocabulary_list')
# Check that the vocabulary we created is in the list.
assert response['success'] == True
assert response['result']
assert response['result'].count(created_vocab) == 1
# Get the created vocabulary.
params = {'id': created_vocab['id']}
response = self._post('/api/action/vocabulary_show', params)
# Check that retrieving the vocab by name gives the same result.
by_name_params = {'id': created_vocab['name']}
assert response == self._post('/api/action/vocabulary_show',
by_name_params)
# Check that it matches what we created.
assert response['success'] == True
assert response['result'] == created_vocab
return created_vocab
def _update_vocabulary(self, params, user=None):
if user:
extra_environ = {'Authorization' : str(user.apikey)}
else:
extra_environ = None
original_vocab = self._post('/api/action/vocabulary_show',
{'id': params.get('id') or params.get('name')})['result']
response = self._post('/api/action/vocabulary_update', params=params,
extra_environ=extra_environ)
# Check the values of the response.
assert response['success'] == True
assert response['result']
updated_vocab = response['result']
# id should never change.
assert updated_vocab['id'] == original_vocab['id']
if params.has_key('id'):
assert updated_vocab['id'] == params['id']
# name should change only if given in params.
if params.has_key('name'):
assert updated_vocab['name'] == params['name']
else:
assert updated_vocab['name'] == original_vocab['name']
# tags should change only if given in params.
if params.has_key('tags'):
assert sorted([tag['name'] for tag in params['tags']]) \
== sorted([tag['name'] for tag in updated_vocab['tags']])
else:
assert updated_vocab['tags'] == original_vocab['tags']
# Get the list of vocabularies.
response = self._post('/api/action/vocabulary_list')
# Check that the vocabulary we created is in the list.
assert response['success'] == True
assert response['result']
assert response['result'].count(updated_vocab) == 1
# Get the created vocabulary.
params = {'id': updated_vocab['id']}
response = self._post('/api/action/vocabulary_show', params)
# Check that retrieving the vocab by name gives the same result.
by_name_params = {'id': updated_vocab['name']}
assert response == self._post('/api/action/vocabulary_show',
by_name_params)
# Check that it matches what we created.
assert response['success'] == True
assert response['result'] == updated_vocab
return updated_vocab
def _delete_vocabulary(self, vocab_id, user=None):
if user:
extra_environ = {'Authorization' : str(user.apikey)}
else:
extra_environ = None
params = {'id': vocab_id}
response = self._post('/api/action/vocabulary_delete', params=params,
extra_environ=extra_environ)
# Check the values of the response.
assert response['success'] == True
assert response['result'] is None
response['result']
# Get the list of vocabularies.
response = self._post('/api/action/vocabulary_list')
assert response['success'] == True
assert response['result']
# Check that the vocabulary we deleted is not in the list.
assert vocab_id not in [vocab['id'] for vocab in response['result']]
# Check that the deleted vocabulary can no longer be retrieved.
response = self.app.post('/api/action/vocabulary_show',
params=json.dumps(params),
extra_environ = {'Authorization':
str(self.sysadmin_user.apikey)},
status=404)
assert response.json['success'] == False
def _list_tags(self, vocabulary=None, user=None):
params = {}
if vocabulary:
params['vocabulary_id'] = vocabulary['id']
if user:
extra_environ = {'Authorization' : str(user.apikey)}
else:
extra_environ = None
response = self._post('/api/action/tag_list', params=params,
extra_environ=extra_environ)
assert response['success'] == True
return response['result']
def _create_tag(self, user, tag_name, vocabulary=None):
tag_dict = {'name': tag_name}
if vocabulary:
tag_dict['vocabulary_id'] = vocabulary['id']
if user:
extra_environ = {'Authorization' : str(user.apikey)}
else:
extra_environ = None
response = self._post('/api/action/tag_create', params=tag_dict,
extra_environ=extra_environ)
assert response['success'] == True
return response['result']
def _delete_tag(self, user, tag_id_or_name, vocab_id_or_name=None):
params = {'id': tag_id_or_name}
if vocab_id_or_name:
params['vocabulary_id'] = vocab_id_or_name
if user:
extra_environ = {'Authorization' : str(user.apikey)}
else:
extra_environ = None
response = self._post('/api/action/tag_delete', params=params,
extra_environ=extra_environ)
assert response['success'] == True
return response['result']
def test_vocabulary_create(self):
'''Test adding a new vocabulary to a CKAN instance via the action
API.
'''
self._create_vocabulary(vocab_name="My cool vocab",
user=self.sysadmin_user)
def test_vocabulary_create_with_tags(self):
'''Test adding a new vocabulary with some tags.
'''
params = {'name': 'foobar'}
tag1 = {'name': 'foo'}
tag2 = {'name': 'bar'}
params['tags'] = [tag1, tag2]
response = self._post('/api/action/vocabulary_create',
params=params,
extra_environ = {'Authorization':
str(self.sysadmin_apikey)})
assert response['success'] == True
assert response['result']
created_vocab = response['result']
assert created_vocab['name'] == 'foobar'
assert created_vocab['id']
# Get the list of vocabularies.
response = self._post('/api/action/vocabulary_list')
# Check that the vocabulary we created is in the list.
assert response['success'] == True
assert response['result']
assert response['result'].count(created_vocab) == 1
# Get the created vocabulary.
params = {'id': created_vocab['id']}
response = self._post('/api/action/vocabulary_show', params)
# Check that retrieving the vocab by name gives the same result.
by_name_params = {'id': created_vocab['name']}
assert response == self._post('/api/action/vocabulary_show',
by_name_params)
# Check that it matches what we created.
assert response['success'] == True
assert response['result'] == created_vocab
# Get the list of tags for the vocabulary.
tags = self._list_tags(created_vocab)
assert len(tags) == 2
assert tags.count('foo') == 1
assert tags.count('bar') == 1
def test_vocabulary_create_bad_tags(self):
'''Test creating new vocabularies with invalid tags.
'''
for tags in (
[{'id': 'xxx'}, {'name': 'foo'}],
[{'name': 'foo'}, {'name': None}],
[{'name': 'foo'}, {'name': ''}],
[{'name': 'foo'}, {'name': 'f'}],
[{'name': 'f'*200}, {'name': 'foo'}],
[{'name': 'Invalid!'}, {'name': 'foo'}],
):
params = {'name': 'foobar', 'tags': tags}
response = self.app.post('/api/action/vocabulary_create',
params=json.dumps(params),
extra_environ = {'Authorization':
str(self.sysadmin_apikey)},
status=409)
assert response.json['success'] == False
assert response.json['error'].has_key('tags')
assert len(response.json['error']) == 2
def test_vocabulary_create_none_tags(self):
'''Test creating new vocabularies with None for 'tags'.
'''
params = {'name': 'foobar', 'tags': None}
response = self.app.post('/api/action/vocabulary_create',
params=json.dumps(params),
extra_environ = {'Authorization':
str(self.sysadmin_user.apikey)},
status=400)
assert "Integrity Error" in response.body
def test_vocabulary_create_empty_tags(self):
'''Test creating new vocabularies with [] for 'tags'.
'''
params = {'name': 'foobar', 'tags': []}
response = self.app.post('/api/action/vocabulary_create',
params=json.dumps(params),
extra_environ = {'Authorization':
str(self.sysadmin_user.apikey)},
status=200)
assert response.json['success'] == True
assert response.json['result']
created_vocab = response.json['result']
assert created_vocab['name'] == 'foobar'
assert created_vocab['id']
assert created_vocab['tags'] == []
params = {'id': created_vocab['id']}
response = self._post('/api/action/vocabulary_show', params)
assert response['success'] == True
assert response['result'] == created_vocab
tags = self._list_tags(created_vocab)
assert tags == []
def test_vocabulary_create_id(self):
'''Test error response when user tries to supply their own ID when
creating a vocabulary.
'''
params = {'id': 'xxx', 'name': 'foobar'}
param_string = json.dumps(params)
response = self.app.post('/api/action/vocabulary_create',
params=param_string,
extra_environ = {'Authorization':
str(self.sysadmin_user.apikey)},
status=409)
assert response.json['success'] == False
assert response.json['error']['id'] == [u'The input field id was '
'not expected.']
def test_vocabulary_create_no_name(self):
'''Test error response when user tries to create a vocab without a
name.
'''
params = {}
param_string = json.dumps(params)
response = self.app.post('/api/action/vocabulary_create',
params=param_string,
extra_environ = {'Authorization':
str(self.sysadmin_user.apikey)},
status=409)
assert response.json['success'] == False
assert response.json['error']['name'] == [u'Missing value']
def test_vocabulary_create_invalid_name(self):
'''Test error response when user tries to create a vocab with an
invalid name.
'''
for name in (None, '', 'a', 'foobar'*100):
params = {'name': name}
param_string = json.dumps(params)
response = self.app.post('/api/action/vocabulary_create',
params=param_string,
extra_environ = {'Authorization':
str(self.sysadmin_apikey)},
status=409)
assert response.json['success'] == False
assert response.json['error']['name']
def test_vocabulary_create_exists(self):
'''Test error response when user tries to create a vocab that already
exists.
'''
params = {'name': self.genre_vocab['name']}
param_string = json.dumps(params)
response = self.app.post('/api/action/vocabulary_create',
params=param_string,
extra_environ = {'Authorization':
str(self.sysadmin_user.apikey)},
status=409)
assert response.json['success'] == False
assert response.json['error']['name'] == [u'That vocabulary name is '
'already in use.']
def test_vocabulary_create_not_logged_in(self):
'''Test that users who are not logged in cannot create vocabularies.'''
params = {'name':
"Spam Vocabulary: SpamCo Duck Rental: Rent Your Ducks From Us!"}
param_string = json.dumps(params)
response = self.app.post('/api/action/vocabulary_create',
params=param_string,
status=403)
assert response.json['success'] == False
assert response.json['error']['message'] == 'Access denied'
def test_vocabulary_create_not_authorized(self):
'''Test that users who are not authorized cannot create vocabs.'''
params = {'name': 'My Unauthorised Vocabulary'}
param_string = json.dumps(params)
response = self.app.post('/api/action/vocabulary_create',
params=param_string,
extra_environ = {'Authorization':
str(self.normal_user.apikey)},
status=403)
assert response.json['success'] == False
assert response.json['error']['message'] == 'Access denied'
def test_vocabulary_update_id_only(self):
self._update_vocabulary({'id': self.genre_vocab['id']},
self.sysadmin_user)
def test_vocabulary_update_id_and_same_name(self):
self._update_vocabulary({'id': self.genre_vocab['id'],
'name': self.genre_vocab['name']}, self.sysadmin_user)
def test_vocabulary_update_id_and_new_name(self):
self._update_vocabulary({'id': self.genre_vocab['id'],
'name': 'new name'}, self.sysadmin_user)
def test_vocabulary_update_id_and_same_tags(self):
self._update_vocabulary({'id': self.genre_vocab['id'],
'tags': self.genre_vocab['tags']}, self.sysadmin_user)
def test_vocabulary_update_id_and_new_tags(self):
tags = [
{'name': 'new test tag one'},
{'name': 'new test tag two'},
{'name': 'new test tag three'},
]
self._update_vocabulary({'id': self.genre_vocab['id'], 'tags': tags},
self.sysadmin_user)
def test_vocabulary_update_id_same_name_and_same_tags(self):
self._update_vocabulary({'id': self.genre_vocab['id'],
'name': self.genre_vocab['name'],
'tags': self.genre_vocab['tags']}, self.sysadmin_user)
def test_vocabulary_update_id_same_name_and_new_tags(self):
tags = [
{'name': 'new test tag one'},
{'name': 'new test tag two'},
{'name': 'new test tag three'},
]
self._update_vocabulary({'id': self.genre_vocab['id'],
'name': self.genre_vocab['name'],
'tags': tags}, self.sysadmin_user)
def test_vocabulary_update_id_new_name_and_same_tags(self):
self._update_vocabulary({'id': self.genre_vocab['id'],
'name': 'new name',
'tags': self.genre_vocab['tags']}, self.sysadmin_user)
def test_vocabulary_update_not_exists(self):
'''Test the error response given when a user tries to update a
vocabulary that doesn't exist.
'''
params = {'id': 'xxxxxxx', 'name': 'updated_name'}
param_string = json.dumps(params)
response = self.app.post('/api/action/vocabulary_update',
params=param_string,
extra_environ = {'Authorization':
str(self.sysadmin_user.apikey)},
status=404)
assert response.json['success'] == False
assert response.json['error']['message'].startswith('Not found: ')
def test_vocabulary_update_no_id(self):
params = {'name': 'bagel radio'}
param_string = json.dumps(params)
response = self.app.post('/api/action/vocabulary_update',
params=param_string,
extra_environ = {'Authorization':
str(self.sysadmin_user.apikey)},
status=409)
assert response.json['success'] == False
assert response.json['error'].has_key('id')
assert response.json['error']['id'] == 'id not in data'
def test_vocabulary_update_not_logged_in(self):
'''Test that users who are not logged in cannot update vocabularies.'''
params = {'id': self.genre_vocab['id']}
param_string = json.dumps(params)
response = self.app.post('/api/action/vocabulary_update',
params=param_string,
status=403)
assert response.json['success'] == False
assert response.json['error']['message'] == 'Access denied'
def test_vocabulary_update_with_tags(self):
tags = [
{'name': 'drone'},
{'name': 'noise'},
{'name': 'fuzz'},
{'name': 'field recordings'},
{'name': 'hypnagogia'},
{'name': 'textures without rhythm'},
]
self._update_vocabulary(
{
'id': self.genre_vocab['id'],
'name': self.genre_vocab['name'],
'tags': tags
},
self.sysadmin_user)
params = {'id': self.genre_vocab['id']}
response = self._post('/api/action/vocabulary_show', params)
# Check that retrieving the vocab by name gives the same result.
assert len(response['result']['tags']) == len(tags)
def test_vocabulary_update_not_authorized(self):
'''Test that users who are not authorized cannot update vocabs.'''
params = {'id': self.genre_vocab['id']}
param_string = json.dumps(params)
response = self.app.post('/api/action/vocabulary_update',
params=param_string,
extra_environ = {'Authorization':
str(self.normal_user.apikey)},
status=403)
assert response.json['success'] == False
assert response.json['error']['message'] == 'Access denied'
def test_vocabulary_update_bad_tags(self):
'''Test updating vocabularies with invalid tags.
'''
apikey = str(self.sysadmin_user.apikey)
for tags in (
[{'id': 'xxx'}, {'name': 'foo'}],
[{'name': 'foo'}, {'name': None}],
[{'name': 'foo'}, {'name': ''}],
[{'name': 'foo'}, {'name': 'f'}],
[{'name': 'f'*200}, {'name': 'foo'}],
[{'name': 'Invalid!'}, {'name': 'foo'}],
):
params = {'id': self.genre_vocab['name'], 'tags': tags}
response = self.app.post('/api/action/vocabulary_update',
params=json.dumps(params),
extra_environ = {'Authorization': apikey},
status=409)
assert response.json['success'] == False
assert response.json['error']['tags']
def test_vocabulary_update_none_tags(self):
'''Test updating vocabularies with None for 'tags'.
'''
params = {'id': self.genre_vocab['id'], 'tags': None}
response = self.app.post('/api/action/vocabulary_update',
params=json.dumps(params),
extra_environ = {'Authorization':
str(self.sysadmin_user.apikey)},
status=400)
assert "Integrity Error" in response.body, response.body
def test_vocabulary_update_empty_tags(self):
'''Test updating vocabularies with [] for 'tags'.
'''
params = {'id': self.genre_vocab['id'], 'tags': []}
response = self.app.post('/api/action/vocabulary_update',
params=json.dumps(params),
extra_environ = {'Authorization':
str(self.sysadmin_user.apikey)},
status=200)
assert response.json['success'] == True
assert response.json['result']
updated_vocab = response.json['result']
assert updated_vocab['name'] == self.genre_vocab['name']
assert updated_vocab['id'] == self.genre_vocab['id']
assert updated_vocab['tags'] == []
params = {'id': updated_vocab['id']}
response = self._post('/api/action/vocabulary_show', params)
assert response['success'] == True
assert response['result'] == updated_vocab
tags = self._list_tags(updated_vocab)
assert tags == []
def test_vocabulary_delete(self):
self._delete_vocabulary(self.genre_vocab['id'], self.sysadmin_user)
def test_vocabulary_delete_not_exists(self):
'''Test the error response given when a user tries to delete a
vocabulary that doesn't exist.
'''
params = {'id': 'xxxxxxx'}
param_string = json.dumps(params)
response = self.app.post('/api/action/vocabulary_delete',
params=param_string,
extra_environ = {'Authorization':
str(self.sysadmin_user.apikey)},
status=404)
assert response.json['success'] == False
assert response.json['error']['message'].startswith('Not found: '
'Could not find vocabulary')
def test_vocabulary_delete_no_id(self):
'''Test the error response given when a user tries to delete a
vocabulary without giving the vocabulary id.
'''
params = {}
param_string = json.dumps(params)
response = self.app.post('/api/action/vocabulary_delete',
params=param_string,
extra_environ = {'Authorization':
str(self.sysadmin_user.apikey)},
status=409)
assert response.json['success'] == False
assert response.json['error'].has_key('id')
assert response.json['error']['id'] == 'id not in data'
def test_vocabulary_delete_not_logged_in(self):
'''Test that users who are not logged in cannot delete vocabularies.'''
params = {'id': self.genre_vocab['id']}
param_string = json.dumps(params)
response = self.app.post('/api/action/vocabulary_delete',
params=param_string,
status=403)
assert response.json['success'] == False
assert response.json['error']['message'] == 'Access denied'
def test_vocabulary_delete_not_authorized(self):
'''Test that users who are not authorized cannot delete vocabs.'''
params = {'id': self.genre_vocab['id']}
param_string = json.dumps(params)
response = self.app.post('/api/action/vocabulary_delete',
params=param_string,
extra_environ = {'Authorization':
str(self.normal_user.apikey)},
status=403)
assert response.json['success'] == False
assert response.json['error']['message'] == 'Access denied'
def test_add_tag_to_vocab(self):
'''Test that a tag can be added to and then retrieved from a vocab.'''
vocab = self.genre_vocab
tags_before = self._list_tags(vocab)
tag_created = self._create_tag(self.sysadmin_user, 'noise', vocab)
tags_after = self._list_tags(vocab)
new_tag_names = [tag_name for tag_name in tags_after if tag_name not in
tags_before]
assert len(new_tag_names) == 1
assert tag_created['name'] in new_tag_names
def test_add_tag_no_vocab(self):
'''Test the error response when a user tries to create a tag without
specifying a vocab.
'''
tag_dict = {'name': 'noise'}
tag_string = json.dumps(tag_dict)
response = self.app.post('/api/action/tag_create',
params=tag_string,
extra_environ = {'Authorization':
str(self.sysadmin_user.apikey)},
status=409)
assert response.json['success'] == False
assert response.json['error']['vocabulary_id'] == ['Missing value']
def test_add_tag_vocab_not_exists(self):
'''Test the error response when a user tries to add a tag to a vocab
that doesn't exist.
'''
tag_dict = {'name': 'noise', 'vocabulary_id': 'does not exist'}
tag_string = json.dumps(tag_dict)
response = self.app.post('/api/action/tag_create',
params=tag_string,
extra_environ = {'Authorization':
str(self.sysadmin_user.apikey)},
status=409)
assert response.json['success'] == False
assert response.json['error']['vocabulary_id'] == [
'Tag vocabulary was not found.']
def test_add_tag_already_added(self):
'''Test the error response when a user tries to add a tag to a vocab
that already has a tag with the same name.
'''
self.test_add_tag_to_vocab()
vocab = self.genre_vocab
tag_dict = {'name': 'noise', 'vocabulary_id': vocab['id']}
tag_string = json.dumps(tag_dict)
response = self.app.post('/api/action/tag_create',
params=tag_string,
extra_environ = {'Authorization':
str(self.sysadmin_user.apikey)},
status=409)
assert response.json['success'] == False
assert response.json['error']['vocabulary_id'][0].startswith(
'Tag noise already belongs to vocabulary')
def test_add_tag_with_id(self):
'''Test the error response when a user tries to specify the tag ID when
adding a tag to a vocab.
'''
tag_dict = {
'id': 'dsagdsgsgsd',
'name': 'noise',
'vocabulary_id': self.genre_vocab['id']
}
tag_string = json.dumps(tag_dict)
response = self.app.post('/api/action/tag_create',
params=tag_string,
extra_environ = {'Authorization':
str(self.sysadmin_user.apikey)},
status=409)
assert response.json['success'] == False
assert response.json['error']['id'] == [u'The input field id was not '
'expected.']
def test_add_tag_without_name(self):
'''Test the error response when a user tries to create a tag without a
name.
'''
tag_dict = {
'vocabulary_id': self.genre_vocab['id']
}
tag_string = json.dumps(tag_dict)
response = self.app.post('/api/action/tag_create',
params=tag_string,
extra_environ = {'Authorization':
str(self.sysadmin_user.apikey)},
status=409)
assert response.json['success'] == False
assert response.json['error']['name'] == [u'Missing value']
def test_add_tag_invalid_name(self):
for name in ('Not a valid tag name!', '', None):
tag_dict = {
'name': name,
'vocabulary_id': self.genre_vocab['id']
}
tag_string = json.dumps(tag_dict)
response = self.app.post('/api/action/tag_create',
params=tag_string,
extra_environ = {'Authorization':
str(self.sysadmin_apikey)},
status=409)
assert response.json['success'] == False
assert response.json['error']['name']
def test_add_tag_invalid_vocab_id(self):
tag_dict = {
'name': 'noise',
'vocabulary_id': 'xxcxzczxczxc',
}
tag_string = json.dumps(tag_dict)
response = self.app.post('/api/action/tag_create',
params=tag_string,
extra_environ = {'Authorization':
str(self.sysadmin_user.apikey)},
status=409)
assert response.json['success'] == False
assert response.json['error']['vocabulary_id'] == [
u'Tag vocabulary was not found.']
def test_add_tag_not_logged_in(self):
tag_dict = {
'name': 'noise',
'vocabulary_id': self.genre_vocab['id']
}
tag_string = json.dumps(tag_dict)
response = self.app.post('/api/action/tag_create',
params=tag_string,
status=403)
assert response.json['success'] == False
assert response.json['error']['message'] == 'Access denied'
def test_add_tag_not_authorized(self):
tag_dict = {
'name': 'noise',
'vocabulary_id': self.genre_vocab['id']
}
tag_string = json.dumps(tag_dict)
response = self.app.post('/api/action/tag_create',
params=tag_string,
extra_environ = {'Authorization':
str(self.normal_user.apikey)},
status=403)
assert response.json['success'] == False
assert response.json['error']['message'] == 'Access denied'
def test_add_vocab_tag_to_dataset(self):
'''Test that a tag belonging to a vocab can be added to a dataset,
retrieved from the dataset, and then removed from the dataset.'''
ckan.model.repo.rebuild_db()
self.setup()
ckan.tests.CreateTestData.create()
# First add a tag to the vocab.
vocab = self.genre_vocab
tag = self._create_tag(self.sysadmin_user, 'noise', vocab)
# Get a package from the API.
package = (self._post('/api/action/package_show',
{'id': self._post('/api/action/package_list')['result'][0]})
['result'])
# Add the new vocab tag to the package.
package['tags'].append(tag)
updated_package = (self._post('/api/action/package_update',
params={'id': package['id'], 'tags': package['tags']},
extra_environ={'Authorization': str(self.sysadmin_user.apikey)})
['result'])
# Test that the new vocab tag was added to the package.
tags_in_pkg = [tag_in_pkg for tag_in_pkg in updated_package['tags'] if
tag_in_pkg['name'] == tag['name'] and
tag_in_pkg['vocabulary_id'] == tag['vocabulary_id']]
assert len(tags_in_pkg) == 1
# Test that the package appears vocabulary_list.
vocabs = self._post('/api/action/vocabulary_list')['result']
genre_vocab = [vocab for vocab in vocabs if vocab['name'] == 'Genre']
assert len(genre_vocab) == 1
genre_vocab = genre_vocab[0]
noise_tag = [tag_ for tag_ in genre_vocab['tags']
if tag_['name'] == 'noise']
assert len(noise_tag) == 1
noise_tag = noise_tag[0]
assert len([p for p in noise_tag['packages'] if
p['id'] == updated_package['id']]) == 1
# Test that the tagged package appears in vocabulary_show.
genre_vocab = self._post('/api/action/vocabulary_show',
params={'id': genre_vocab['id']})['result']
noise_tag = [tag_ for tag_ in genre_vocab['tags']
if tag_['name'] == 'noise']
assert len(noise_tag) == 1
noise_tag = noise_tag[0]
assert len([p for p in noise_tag['packages'] if
p['id'] == updated_package['id']]) == 1
# Remove the new vocab tag from the package.
package['tags'].remove(tag)
updated_package = (self._post('/api/action/package_update',
params={'id': package['id'], 'tags': package['tags']},
extra_environ={'Authorization': str(self.sysadmin_user.apikey)})
['result'])
# Test that the tag no longer appears in the list of tags for the
# package.
package = (self._post('/api/action/package_show',
{'id': self._post('/api/action/package_list')['result'][0]})
['result'])
tags_in_pkg = [tag_in_pkg for tag_in_pkg in package['tags'] if
tag_in_pkg['name'] == tag['name'] and
tag_in_pkg['vocabulary_id'] == tag['vocabulary_id']]
assert len(tags_in_pkg) == 0
def test_delete_tag_from_vocab(self):
'''Test that a tag can be deleted from a vocab.'''
ckan.model.repo.rebuild_db()
self.setup()
ckan.tests.CreateTestData.create()
vocab = self.genre_vocab
# First add some tags to the vocab.
noise_tag = self._create_tag(self.sysadmin_user, 'noise', vocab)
ragga_tag = self._create_tag(self.sysadmin_user, 'ragga', vocab)
grunge_tag = self._create_tag(self.sysadmin_user, 'grunge', vocab)
funk_tag = self._create_tag(self.sysadmin_user, 'funk', vocab)
tags = (noise_tag, ragga_tag, grunge_tag, funk_tag)
# Get a package from the API.
package = (self._post('/api/action/package_show',
{'id': self._post('/api/action/package_list')['result'][0]})
['result'])
# Add the new vocab tags to the package.
for tag in tags:
package['tags'].append(tag)
updated_package = (self._post('/api/action/package_update',
params={'id': package['id'], 'tags': package['tags']},
extra_environ={'Authorization': str(self.sysadmin_user.apikey)})
['result'])
# Test that the new vocab tags were added to the package.
for tag in tags:
tags_in_pkg = [tag_in_pkg for tag_in_pkg in
updated_package['tags'] if tag_in_pkg['name'] ==
tag['name'] and tag_in_pkg['vocabulary_id'] ==
tag['vocabulary_id']]
assert len(tags_in_pkg) == 1
# Now delete the tags from the vocab.
tags_before = self._list_tags(vocab)
self._delete_tag(self.sysadmin_user, noise_tag['name'], vocab['name'])
self._delete_tag(self.sysadmin_user, ragga_tag['id'], vocab['name'])
self._delete_tag(self.sysadmin_user, grunge_tag['id'], vocab['id'])
self._delete_tag(self.sysadmin_user, funk_tag['name'], vocab['id'])
# Test that the tags no longer appear in the list of tags for the
# vocab.
tags_after = self._list_tags(vocab)
assert len(tags_after) == len(tags_before) - 4
assert tag['name'] not in tags_after
difference = [tag_name for tag_name in tags_before if tag_name not in
tags_after]
assert sorted(difference) == sorted([tag['name'] for tag in tags])
# Test that the tags no longer appear in the list of tags for the
# package.
package = (self._post('/api/action/package_show',
{'id': self._post('/api/action/package_list')['result'][0]})
['result'])
for tag in tags:
tags_in_pkg = [tag_in_pkg for tag_in_pkg in package['tags'] if
tag_in_pkg['name'] == tag['name'] and
tag_in_pkg['vocabulary_id'] == tag['vocabulary_id']]
assert len(tags_in_pkg) == 0
def test_delete_free_tag(self):
'''Test that a free tag can be deleted via the API, and is
automatically removed from datasets.
'''
ckan.model.repo.rebuild_db()
self.setup()
ckan.tests.CreateTestData.create()
# Get a package from the API.
package = (self._post('/api/action/package_show',
{'id': self._post('/api/action/package_list')['result'][0]})
['result'])
package_id = package['id']
# Add some new free tags to the package.
tags = package['tags']
tags.append({'name': 'ducks'})
tags.append({'name': 'birds'})
self._post('/api/action/package_update',
params={'id': package['id'], 'tags': tags},
extra_environ={'Authorization':
str(self.sysadmin_user.apikey)})
# Test that the new tags appear in the list of tags.
tags = self._list_tags()
assert [tag for tag in tags].count('ducks') == 1
assert [tag for tag in tags].count('birds') == 1
# Test that the new tags appear in the package's list of tags.
package = (self._post('/api/action/package_show',
{'id': package_id})['result'])
packages_tags = [tag['name'] for tag in package['tags']]
assert [tag for tag in packages_tags].count('ducks') == 1
assert [tag for tag in packages_tags].count('birds') == 1
# Now delete the tags.
self._delete_tag(self.sysadmin_user, 'ducks')
birds_tag_id = self._post('/api/action/tag_show',
{'id': 'birds'})['result']['id']
self._delete_tag(self.sysadmin_user, birds_tag_id)
# Test that the tags no longer appear in the list of tags.
tags = self._list_tags()
assert [tag for tag in tags].count('ducks') == 0
assert [tag for tag in tags].count('birds') == 0
# Test that the tags no longer appear in the package's list of tags.
package = (self._post('/api/action/package_show',
{'id': package_id})['result'])
packages_tags = [tag['name'] for tag in package['tags']]
assert [tag for tag in packages_tags].count('ducks') == 0
assert [tag for tag in packages_tags].count('birds') == 0
def test_delete_tag_no_id(self):
'''Test the error response when a user tries to delete a tag without
giving the tag id.
'''
vocab = self.genre_vocab
tag = self._create_tag(self.sysadmin_user, 'noise', vocab)
for tag_id in ('missing', '', None):
# Now try to delete the tag from the vocab.
params = {'vocabulary_id': vocab['name']}
if tag_id != 'missing':
params['id'] = tag_id
response = self.app.post('/api/action/tag_delete',
params=json.dumps(params),
extra_environ = {'Authorization':
str(self.sysadmin_user.apikey)},