def test_remove_tag_from_collection(self): c = CollectionFactory() r = self.c.get(c.get_absolute_url() + "remove_tag/foo/") self.assertEqual(r.status_code, 200) r = self.c.get(c.get_absolute_url() + "remove_tag/foo/?ajax=1") self.assertEqual(r.status_code, 200) self.assertEqual(r.content, "ok")
def test_edit_collection(self): c = CollectionFactory() r = self.c.get(c.get_absolute_url() + "edit/") self.assertEqual(r.status_code, 200) r = self.c.post(c.get_absolute_url() + "edit/", data=dict(title="updated title")) self.assertEqual(r.status_code, 302)
def test_404_when_annotation_not_in_collection(self): """Test 404 when Annotation is not in the Collection.""" collection1 = CollectionFactory() collection2 = CollectionFactory() annotation = AnnotationFactory(collection=collection1) endpoint = u'/annotations/{}/{}/'.format(collection2.id, annotation.id) res = self.app_get_json_ld(endpoint) assert_equal(res.status_code, 404, res.data)
def test_non_empty_collection_cannot_be_deleted(self): """Test non-empty Collection cannot be deleted.""" collection = CollectionFactory() CollectionFactory() annotation = AnnotationFactory(collection=collection) endpoint = u'/annotations/{}/'.format(collection.id) res = self.app_delete_json_ld(endpoint) assert_equal(res.status_code, 400, res.data) assert_equal(collection.deleted, False)
def test_collection_toggle_active(self): c = CollectionFactory(active=True) r = self.c.post(c.get_absolute_url() + "toggle_active/") c = Collection.objects.get(id=c.id) self.assertEqual(r.status_code, 302) self.assertFalse(c.active) r = self.c.post(c.get_absolute_url() + "toggle_active/") self.assertEqual(r.status_code, 302) c = Collection.objects.get(id=c.id) self.assertTrue(c.active)
def test_list_all_collections(self): """Test a list of all Annotation Collections is returned.""" collection1 = CollectionFactory() collection2 = CollectionFactory() AnnotationFactory.create_batch(3, collection=collection1) expected = { '@context': 'http://www.w3.org/ns/anno.jsonld', 'id': url_for('api.index', _external=True), 'label': 'All collections', 'type': ['AnnotationCollection', 'BasicContainer'], 'total': 2, 'items': [{ 'id': url_for('api.collections', collection_id=collection1.id, _external=True), 'type': collection1.data['type'], 'created': '1984-11-19T00:00:00Z', 'generated': '1984-11-19T00:00:00Z', 'total': 3 }, { 'id': url_for('api.collections', collection_id=collection2.id, _external=True), 'type': collection2.data['type'], 'created': '1984-11-19T00:00:00Z', 'generated': '1984-11-19T00:00:00Z', 'total': 0 }] } endpoint = '/annotations/' res = self.app_get_json_ld(endpoint) data = json.loads(res.data.decode('utf8')) assert_dict_equal(data, expected)
def test_get_minimal_container_with_iris(self): """Test get Collection with PreferMinimalContainer and IRIs.""" collection = CollectionFactory() annotation = AnnotationFactory(collection=collection) expected = { '@context': 'http://www.w3.org/ns/anno.jsonld', 'id': url_for('api.collections', collection_id=collection.id, iris=1), 'type': collection.data['type'], 'created': '1984-11-19T00:00:00Z', 'generated': '1984-11-19T00:00:00Z', 'total': 1, 'first': url_for('api.collections', collection_id=collection.id, **dict(page=0, iris=1)) } endpoint = u'/annotations/{}/'.format(collection.id) prefer = ('return=representation;include=' '"http://www.w3.org/ns/ldp#PreferMinimalContainer' ' http://www.w3.org/ns/oa#PreferContainedIRIs"') headers = dict(prefer=prefer) res = self.app_get_json_ld(endpoint, headers=headers) data = json.loads(res.data.decode('utf8')) assert_dict_equal(data, expected)
def test_get_iri_container_using_query_string(self): """Test get Collection with PreferContainedIRIs using query.""" collection = CollectionFactory() annotation = AnnotationFactory(collection=collection) expected = { '@context': 'http://www.w3.org/ns/anno.jsonld', 'id': url_for('api.collections', collection_id=collection.id, iris=1), 'type': collection.data['type'], 'created': '1984-11-19T00:00:00Z', 'generated': '1984-11-19T00:00:00Z', 'total': 1, 'first': { 'id': url_for('api.collections', collection_id=collection.id, **dict(page=0, iris=1)), 'type': 'AnnotationPage', 'startIndex': 0, 'items': [ url_for('api.annotations', collection_id=collection.id, annotation_id=annotation.id) ] } } endpoint = u'/annotations/{}/'.format(collection.id) res = self.app_get_json_ld(endpoint + '?iris=1') data = json.loads(res.data.decode('utf8')) assert_dict_equal(data, expected)
def test_410_when_annotation_used_to_exist(self): """Test 410 when Annotation used to exist.""" collection = CollectionFactory() annotation = AnnotationFactory(collection=collection, deleted=True) endpoint = u'/annotations/{}/{}/'.format(collection.id, annotation.id) res = self.app_get_json_ld(endpoint) assert_equal(res.status_code, 410, res.data)
def test_annotation_created(self): """Test Annotation created.""" collection = CollectionFactory(id='foo') endpoint = '/annotations/{}/'.format(collection.id) data = dict(type='Annotation', body='bar', target='http://example.org') res = self.app_post_json_ld(endpoint, data=data) annotation = repo.get(Annotation, 1) assert_not_equal(annotation, None) _id = url_for('api.annotations', collection_id=collection.id, annotation_id=annotation.id) data = json.loads(res.data.decode('utf8')) assert_dict_equal( data, { '@context': 'http://www.w3.org/ns/anno.jsonld', 'id': _id, 'type': 'Annotation', 'body': data['body'], 'target': data['target'], 'created': '1984-11-19T00:00:00Z', 'generated': '1984-11-19T00:00:00Z', 'generator': current_app.config.get('GENERATOR') }) # Test 201 assert_equal(res.status_code, 201, res.data) # Test Location header contains Annotation IRI assert_equal(res.headers.get('Location'), _id) # Test Link header link = '<http://www.w3.org/ns/ldp#Resource>; rel="type"' assert_equal(res.headers.get('Link'), link)
def test_annotation_deleted(self): """Test Annotation deleted.""" collection = CollectionFactory() annotation = AnnotationFactory(collection=collection) endpoint = u'/annotations/{}/{}/'.format(collection.id, annotation.id) res = self.app_delete_json_ld(endpoint) assert_equal(annotation.deleted, True) assert_equal(res.status_code, 204, res.data)
def test_collection_deleted(self): """Test Collection deleted.""" collections = CollectionFactory.create_batch(2) collection = collections[0] endpoint = u'/annotations/{}/'.format(collection.id) res = self.app_delete_json_ld(endpoint) assert_equal(res.status_code, 204, res.data) assert_equal(collection.deleted, True)
def test_collection_modified_when_annotation_deleted(self): """Test Collection modified time updated when Annotation deleted.""" collection = CollectionFactory() annotation = AnnotationFactory(collection=collection) endpoint = u'/annotations/{}/{}/'.format(collection.id, annotation.id) self.app_delete_json_ld(endpoint) assert_equal(annotation.deleted, True) assert_not_equal(annotation.modified, None) assert_equal(collection.modified, annotation.modified)
def test_collection_modified_when_annotation_created(self): """Test Collection modified time updated when Annotation created.""" collection = CollectionFactory() endpoint = u'/annotations/{}/'.format(collection.id) data = dict(type='Annotation', body='bar', target='http://example.org') self.app_post_json_ld(endpoint, data=data) annotation = repo.get(Annotation, 1) assert_not_equal(annotation.created, None) assert_equal(collection.modified, annotation.created)
def test_max_age(self): """Test CORS Access-Control-Max-Age.""" collection = CollectionFactory() endpoint = u'/annotations/{}'.format(collection.id) headers = { 'Access-Control-Request-Method': 'GET', 'Access-Control-Request-Headers': 'Authorization' } res = self.app.options(endpoint, headers=headers) assert_equal(res.headers.get('Access-Control-Max-Age'), '21600')
def test_origin(self): """Test CORS Access-Control-Allow-Origin.""" collection = CollectionFactory() endpoint = u'/annotations/{}'.format(collection.id) headers = { 'Access-Control-Request-Method': 'GET', 'Access-Control-Request-Headers': 'Authorization' } res = self.app.options(endpoint, headers=headers) assert_equal(res.headers['Access-Control-Allow-Origin'], '*')
def test_annotation_created_with_slug(self): """Test Annotation created with slug.""" collection = CollectionFactory(id='foo') endpoint = '/annotations/{}/'.format(collection.id) data = dict(type='Annotation', body='bar', target='http://example.org') slug = 'baz' headers = dict(slug=slug) res = self.app_post_json_ld(endpoint, data=data, headers=headers) annotation = repo.get(Annotation, 1) assert_equal(annotation.id, slug)
def test_collection_modified_when_annotation_updated(self): """Test Collection modified time updated when Annotation updated.""" collection = CollectionFactory() annotation = AnnotationFactory(collection=collection) data = annotation.dictize().copy() data['body'] = "My new body" endpoint = u'/annotations/{}/{}/'.format(collection.id, annotation.id) self.app_put_json_ld(endpoint, data=data) assert_not_equal(annotation.modified, None) assert_equal(collection.modified, annotation.modified)
def test_headers(self): """Test CORS headers.""" collection = CollectionFactory() endpoint = u'/annotations/{}'.format(collection.id) test_headers = ['Content-Type', 'Authorization'] for hdr in test_headers: headers = { 'Access-Control-Request-Method': 'GET', 'Access-Control-Request-Headers': hdr } res = self.app.options(endpoint, headers=headers) assert_equal(res.headers.get('Access-Control-Allow-Headers'), hdr)
def test_collection_validated_before_update(self, mock_validate): """Test Collection validated before update.""" collection = CollectionFactory() endpoint = u'/annotations/{}/'.format(collection.id) bad_data = {'foo': 'bar'} mock_validate.side_effect = ValidationError('Bad Data') res = self.app_put_json_ld(endpoint, data=bad_data) assert_equal(res.status_code, 400, res.data) schema_path = os.path.join(current_app.root_path, 'schemas', 'collection.json') schema = json.load(open(schema_path)) mock_validate.assert_called_once_with(bad_data, schema) assert_not_equal(collection._data, bad_data)
def test_methods(self): """Test CORS Access-Control-Allow-Methods.""" collection = CollectionFactory() endpoint = u'/annotations/{}'.format(collection.id) headers = { 'Access-Control-Request-Method': 'GET', 'Access-Control-Request-Headers': 'Authorization' } res = self.app.options(endpoint, headers=headers) methods = ['PUT', 'HEAD', 'DELETE', 'OPTIONS', 'GET'] assert_not_equal(res.headers.get('Access-Control-Allow-Methods'), None) for m in methods: assert_in(m, res.headers['Access-Control-Allow-Methods'])
def test_404_when_page_does_not_exist(self): """Test 404 when AnnotationPage does not exist.""" collection = CollectionFactory() endpoint = u'/annotations/{0}/?page={1}'.format(collection.id, 0) res = self.app_get_json_ld(endpoint) assert_equal(res.status_code, 404, res.data) per_page = current_app.config.get('ANNOTATIONS_PER_PAGE') AnnotationFactory.create_batch(per_page, collection=collection) endpoint = u'/annotations/{0}/?page={1}'.format(collection.id, 1) res = self.app_get_json_ld(endpoint) assert_equal(res.status_code, 404, res.data)
def test_get_annotation_headers(self): """Test Annotation headers.""" collection = CollectionFactory() annotation = AnnotationFactory(collection=collection) endpoint = u'/annotations/{}/{}/'.format(collection.id, annotation.id) res = self.app_get_json_ld(endpoint) link = '<http://www.w3.org/ns/ldp#Resource>; rel="type"' assert_equal(res.headers.get('Link'), link) ct = 'application/ld+json; profile="http://www.w3.org/ns/anno.jsonld"' assert_equal(res.headers.get('Content-Type'), ct) allow = 'GET,PUT,DELETE,OPTIONS,HEAD' assert_equal(res.headers.get('Allow'), allow) assert_not_equal(res.headers.get('ETag'), None)
def test_annotation_created_with_id_moved_to_via(self): """Test Annotation created with ID moved to via.""" collection = CollectionFactory(id='foo') endpoint = '/annotations/{}/'.format(collection.id) old_id = 'bar' data = dict(type='Annotation', body='baz', target='http://example.org', id=old_id) res = self.app_post_json_ld(endpoint, data=data) annotation = repo.get(Annotation, 1) assert_equal(annotation._data.get('via'), old_id) assert_not_equal(annotation.id, old_id) assert_not_equal(annotation.dictize()['id'], old_id)
def test_get_default_container(self): """Test get Collection with default container preferences. Should default to PreferContainedDescriptions. """ collection = CollectionFactory() annotation = AnnotationFactory(collection=collection) expected = { '@context': 'http://www.w3.org/ns/anno.jsonld', 'id': url_for('api.collections', collection_id=collection.id), 'type': collection.data['type'], 'created': '1984-11-19T00:00:00Z', 'generated': '1984-11-19T00:00:00Z', 'total': 1, 'first': { 'id': url_for('api.collections', collection_id=collection.id, page=0), 'type': 'AnnotationPage', 'startIndex': 0, 'items': [{ 'id': url_for('api.annotations', collection_id=collection.id, annotation_id=annotation.id), 'type': 'Annotation', 'body': annotation.data['body'], 'target': annotation.data['target'], 'created': '1984-11-19T00:00:00Z', 'generated': '1984-11-19T00:00:00Z', 'generator': current_app.config.get('GENERATOR') }] } } endpoint = u'/annotations/{}/'.format(collection.id) res = self.app_get_json_ld(endpoint) data = json.loads(res.data.decode('utf8')) assert_dict_equal(data, expected)
def test_annotation_updated(self): """Test Annotation updated.""" collection = CollectionFactory() annotation = AnnotationFactory(collection=collection) data = annotation.dictize().copy() data['body'] = "My new body" assert_equal(annotation.modified, None) endpoint = u'/annotations/{}/{}/'.format(collection.id, annotation.id) res = self.app_put_json_ld(endpoint, data=data) # Test object updated assert_equal(annotation.modified, '1984-11-19T00:00:00Z') # Test data data = json.loads(res.data.decode('utf8')) assert_equal( data, { '@context': 'http://www.w3.org/ns/anno.jsonld', 'id': url_for('api.annotations', collection_id=collection.id, annotation_id=annotation.id), 'type': 'Annotation', 'body': data['body'], 'target': data['target'], 'created': '1984-11-19T00:00:00Z', 'generated': '1984-11-19T00:00:00Z', 'modified': '1984-11-19T00:00:00Z', 'generator': current_app.config.get('GENERATOR') }) # Test 200 assert_equal(res.status_code, 200, res.data) # Test Link header link = '<http://www.w3.org/ns/ldp#Resource>; rel="type"' assert_equal(res.headers.get('Link'), link)
def test_get_page(self): """Test get AnnotationPage.""" collection = CollectionFactory() annotation = AnnotationFactory(collection=collection) expected = { '@context': 'http://www.w3.org/ns/anno.jsonld', 'id': url_for('api.collections', collection_id=collection.id, page=0), 'type': 'AnnotationPage', 'startIndex': 0, 'items': [{ 'id': url_for('api.annotations', collection_id=collection.id, annotation_id=annotation.id), 'type': 'Annotation', 'body': annotation.data['body'], 'target': annotation.data['target'], 'created': '1984-11-19T00:00:00Z', 'generated': '1984-11-19T00:00:00Z', 'generator': current_app.config.get('GENERATOR') }], 'partOf': { 'id': url_for('api.collections', collection_id=collection.id), 'total': 1, 'type': ['BasicContainer', 'AnnotationCollection'], 'created': '1984-11-19T00:00:00Z', 'generated': '1984-11-19T00:00:00Z' } } endpoint = u'/annotations/{}/'.format(collection.id) res = self.app_get_json_ld(endpoint + '?page=0') data = json.loads(res.data.decode('utf8')) assert_dict_equal(data, expected)
def test_deleted_annotations_not_returned(self): """Test deleted Annotation not returned in AnnotationCollection.""" collection = CollectionFactory() annotation = AnnotationFactory(collection=collection) AnnotationFactory(collection=collection, deleted=True) expected = { '@context': 'http://www.w3.org/ns/anno.jsonld', 'id': url_for('api.collections', collection_id=collection.id), 'type': collection.data['type'], 'created': '1984-11-19T00:00:00Z', 'generated': '1984-11-19T00:00:00Z', 'total': 1, 'first': { 'id': url_for('api.collections', collection_id=collection.id, page=0), 'type': 'AnnotationPage', 'startIndex': 0, 'items': [{ 'id': url_for('api.annotations', collection_id=collection.id, annotation_id=annotation.id), 'type': 'Annotation', 'body': annotation.data['body'], 'target': annotation.data['target'], 'created': '1984-11-19T00:00:00Z', 'generated': '1984-11-19T00:00:00Z', 'generator': current_app.config.get('GENERATOR') }] } } endpoint = u'/annotations/{}/'.format(collection.id) res = self.app_get_json_ld(endpoint) data = json.loads(res.data.decode('utf8')) assert_dict_equal(data, expected)
def test_get_collection_headers(self): """Test Collection headers.""" collection_data = {'type': ['BasicContainer', 'AnnotationCollection']} collection = CollectionFactory(data=collection_data) endpoint = u'/annotations/{}/'.format(collection.id) res = self.app_get_json_ld(endpoint) assert_equal(res.headers.getlist('Link'), [ '<http://www.w3.org/ns/oa#AnnotationCollection>; rel="type"', '<http://www.w3.org/ns/ldp#BasicContainer>; rel="type"', '<http://www.w3.org/TR/annotation-protocol/>; ' + 'rel="http://www.w3.org/ns/ldp#constrainedBy"' ]) ct = 'application/ld+json; profile="http://www.w3.org/ns/anno.jsonld"' assert_equal(res.headers.get('Content-Type'), ct) allow = 'GET,POST,PUT,DELETE,OPTIONS,HEAD' assert_equal(res.headers.get('Allow'), allow) assert_not_equal(res.headers.get('ETag'), None)
def test_get_page_with_iris(self): """Test get AnnotationPage with IRIs.""" collection = CollectionFactory() annotation = AnnotationFactory(collection=collection) expected = { '@context': 'http://www.w3.org/ns/anno.jsonld', 'id': url_for('api.collections', collection_id=collection.id, **dict(page=0, iris=1)), 'type': 'AnnotationPage', 'startIndex': 0, 'items': [ url_for('api.annotations', collection_id=collection.id, annotation_id=annotation.id) ], 'partOf': { 'id': url_for('api.collections', collection_id=collection.id, iris=1), 'total': 1, 'type': ['BasicContainer', 'AnnotationCollection'], 'created': '1984-11-19T00:00:00Z', 'generated': '1984-11-19T00:00:00Z' } } endpoint = u'/annotations/{}/'.format(collection.id) res = self.app_get_json_ld(endpoint + '?page=0&iris=1') data = json.loads(res.data.decode('utf8')) assert_dict_equal(data, expected)
def test_get_annotation(self): """Test Annotation returned.""" collection = CollectionFactory() annotation = AnnotationFactory(collection=collection) _id = url_for('api.annotations', collection_id=collection.id, annotation_id=annotation.id) expected = { '@context': 'http://www.w3.org/ns/anno.jsonld', 'id': _id, 'type': 'Annotation', 'body': annotation.data['body'], 'target': annotation.data['target'], 'created': '1984-11-19T00:00:00Z', 'generated': '1984-11-19T00:00:00Z', 'generator': current_app.config.get('GENERATOR') } endpoint = u'/annotations/{}/{}/'.format(collection.id, annotation.id) res = self.app_get_json_ld(endpoint) data = json.loads(res.data.decode('utf8')) assert_equal(data, expected) assert_equal(res.status_code, 200, res.data)
def test_forms(self): self.collection = CollectionFactory() add_form = self.collection.add_video_form() assert "id_title" in str(add_form) assert 'title' in add_form.fields
def test_delete_collection(self): c = CollectionFactory() r = self.c.get(c.get_absolute_url() + "delete/") self.assertEqual(r.status_code, 200) r = self.c.post(c.get_absolute_url() + "delete/") self.assertEqual(r.status_code, 302)
def test_404_when_annotation_not_found(self): """Test 404 when Annotation does not exist.""" collection = CollectionFactory() endpoint = u'/annotations/{}/{}/'.format(collection.id, 'foo') res = self.app_get_json_ld(endpoint) assert_equal(res.status_code, 404, res.data)
def test_edit_collection_workflows(self): c = CollectionFactory() r = self.c.post(c.get_absolute_url() + "workflows/") self.assertEqual(r.status_code, 302)
def test_get_multiple_pages(self): """Test get multiple AnnotationPage.""" collection = CollectionFactory() n_pages = 3 per_page = current_app.config.get('ANNOTATIONS_PER_PAGE') last_page = n_pages - 1 annotations = AnnotationFactory.create_batch(per_page * n_pages, collection=collection) current_page = 1 start = current_page * per_page items = [] for anno in annotations[start:start + per_page]: items.append({ 'id': url_for('api.annotations', collection_id=collection.id, annotation_id=anno.id), 'type': 'Annotation', 'body': anno.data['body'], 'target': anno.data['target'], 'created': '1984-11-19T00:00:00Z', 'generated': '1984-11-19T00:00:00Z', 'generator': current_app.config.get('GENERATOR') }) expected = { '@context': 'http://www.w3.org/ns/anno.jsonld', 'id': url_for('api.collections', collection_id=collection.id, page=current_page), 'type': 'AnnotationPage', 'startIndex': 0, 'items': items, 'partOf': { 'id': url_for('api.collections', collection_id=collection.id), 'total': len(annotations), 'type': ['BasicContainer', 'AnnotationCollection'], 'created': '1984-11-19T00:00:00Z', 'generated': '1984-11-19T00:00:00Z' }, 'next': url_for('api.collections', collection_id=collection.id, page=current_page + 1), 'prev': url_for('api.collections', collection_id=collection.id, page=current_page - 1), } endpoint = u'/annotations/{}/'.format(collection.id) res = self.app_get_json_ld(endpoint + '?page={}'.format(current_page)) data = json.loads(res.data.decode('utf8')) assert_dict_equal(data, expected)