예제 #1
0
class CollectionLogoViewTest(OCLAPITestCase):
    def setUp(self):
        super().setUp()
        self.user = UserProfileFactory(username='******')
        self.token = self.user.get_token()
        self.collection = UserCollectionFactory(mnemonic='coll1',
                                                user=self.user)

    @patch('core.common.services.S3.upload_base64')
    def test_post_200(self, upload_base64_mock):
        upload_base64_mock.return_value = 'users/username/collections/coll1/logo.png'
        self.assertIsNone(self.collection.logo_url)
        self.assertIsNone(self.collection.logo_path)

        response = self.client.post(self.collection.uri + 'logo/',
                                    dict(base64='base64-data'),
                                    HTTP_AUTHORIZATION='Token ' + self.token,
                                    format='json')

        self.assertEqual(response.status_code, 200)
        expected_logo_url = 'http://oclapi2-dev.s3.amazonaws.com/users/username/collections/coll1/logo.png'
        self.assertEqual(
            response.data['logo_url'].replace('https://', 'http://'),
            expected_logo_url)
        self.collection.refresh_from_db()
        self.assertEqual(
            self.collection.logo_url.replace('https://', 'http://'),
            expected_logo_url)
        self.assertEqual(self.collection.logo_path,
                         'users/username/collections/coll1/logo.png')
        upload_base64_mock.assert_called_once_with(
            'base64-data', 'users/username/collections/coll1/logo.png', False,
            True)
예제 #2
0
 def setUp(self):
     super().setUp()
     self.user = UserProfileFactory(username='******')
     self.token = self.user.get_token()
     self.collection = UserCollectionFactory(mnemonic='coll', user=self.user)
     self.collection_v1 = UserCollectionFactory(version='v1', mnemonic='coll', user=self.user)
     self.v1_updated_at = self.collection_v1.updated_at.strftime('%Y%m%d%H%M%S')
예제 #3
0
 def setUp(self):
     super().setUp()
     self.user = UserProfileFactory()
     self.token = self.user.get_token()
     self.extras = dict(foo='bar', tao='ching')
     self.collection = UserCollectionFactory(mnemonic='coll',
                                             user=self.user,
                                             extras=self.extras)
예제 #4
0
 def setUp(self):
     super().setUp()
     self.user = UserProfileFactory()
     self.token = self.user.get_token()
     self.collection = UserCollectionFactory(mnemonic='coll',
                                             user=self.user)
     self.collection_v1 = UserCollectionFactory(version='v1',
                                                mnemonic='coll',
                                                user=self.user)
예제 #5
0
class CollectionConceptsViewTest(OCLAPITestCase):
    def setUp(self):
        super().setUp()
        self.user = UserProfileFactory()
        self.collection = UserCollectionFactory(user=self.user)
        self.token = self.user.get_token()

    def test_get_200(self):
        response = self.client.get(
            self.collection.concepts_url,
            HTTP_AUTHORIZATION='Token ' + self.token,
            format='json'
        )

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 0)

        source1 = OrganizationSourceFactory()
        source2 = OrganizationSourceFactory()
        concept1 = ConceptFactory(parent=source1, mnemonic='concept')
        concept2 = ConceptFactory(parent=source2, mnemonic='concept')
        self.collection.add_references([concept1.uri, concept2.uri])

        response = self.client.get(
            self.collection.concepts_url,
            HTTP_AUTHORIZATION='Token ' + self.token,
            format='json'
        )

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 2)

    def test_get_duplicate_concept_name_from_multiple_sources_200(self):
        source1 = OrganizationSourceFactory()
        source2 = OrganizationSourceFactory()
        concept1 = ConceptFactory(parent=source1, mnemonic='concept')
        concept2 = ConceptFactory(parent=source2, mnemonic='concept')
        self.collection.add_references([concept1.uri, concept2.uri])

        response = self.client.get(
            self.collection.concepts_url + 'concept/',
            HTTP_AUTHORIZATION='Token ' + self.token,
            format='json'
        )

        self.assertEqual(response.status_code, 409)

        response = self.client.get(
            self.collection.concepts_url + 'concept/?uri=' + concept2.uri,
            HTTP_AUTHORIZATION='Token ' + self.token,
            format='json'
        )

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data['uuid'], str(concept2.get_latest_version().id))
예제 #6
0
 def setUp(self):
     super().setUp()
     self.user = UserProfileFactory()
     self.token = self.user.get_token()
     self.collection = UserCollectionFactory(mnemonic='coll', user=self.user)
     self.concept = ConceptFactory()
     self.reference = CollectionReference(expression=self.concept.uri)
     self.reference.full_clean()
     self.reference.save()
     self.collection.references.add(self.reference)
     self.collection.concepts.set(self.reference.concepts)
     self.assertEqual(self.collection.references.count(), 1)
     self.assertEqual(self.collection.concepts.count(), 1)
예제 #7
0
class CollectionVersionListViewTest(OCLAPITestCase):
    def setUp(self):
        super().setUp()
        self.user = UserProfileFactory()
        self.token = self.user.get_token()
        self.collection = UserCollectionFactory(mnemonic='coll',
                                                user=self.user)
        self.concept = ConceptFactory()
        self.reference = CollectionReference(expression=self.concept.uri)
        self.reference.full_clean()
        self.reference.save()
        self.collection.references.add(self.reference)
        self.collection.concepts.set(self.reference.concepts)
        self.assertEqual(self.collection.references.count(), 1)
        self.assertEqual(self.collection.concepts.count(), 1)

    def test_get_200(self):
        response = self.client.get('/collections/coll/versions/?verbose=true',
                                   HTTP_AUTHORIZATION='Token ' + self.token,
                                   format='json')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 1)
        self.assertEqual(response.data[0]['version'], 'HEAD')

        UserCollectionFactory(mnemonic=self.collection.mnemonic,
                              user=self.user,
                              version='v1',
                              released=True)

        response = self.client.get('/collections/coll/versions/?released=true',
                                   HTTP_AUTHORIZATION='Token ' + self.token,
                                   format='json')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 1)
        self.assertEqual(response.data[0]['version'], 'v1')

    @patch('core.collections.views.export_collection')
    def test_post_201(self, export_collection_mock):
        export_collection_mock.delay = Mock()
        response = self.client.post('/collections/coll/versions/',
                                    dict(id='v1', description='version1'),
                                    HTTP_AUTHORIZATION='Token ' + self.token,
                                    format='json')

        self.assertEqual(response.status_code, 201)
        self.assertEqual(response.data['version'], 'v1')
        self.assertEqual(self.collection.versions.count(), 2)

        last_created_version = self.collection.versions.order_by(
            'created_at').last()
        self.assertEqual(last_created_version.version, 'v1')
        self.assertEqual(last_created_version.description, 'version1')
        self.assertEqual(last_created_version.concepts.count(), 1)
        self.assertEqual(last_created_version.references.count(), 1)
        self.assertEqual(last_created_version,
                         self.collection.get_latest_version())
        export_collection_mock.delay.assert_called_once_with(
            str(last_created_version.id))
예제 #8
0
class CollectionExtraRetrieveUpdateDestroyViewTest(OCLAPITestCase):
    def setUp(self):
        super().setUp()
        self.user = UserProfileFactory()
        self.token = self.user.get_token()
        self.extras = dict(foo='bar', tao='ching')
        self.collection = UserCollectionFactory(mnemonic='coll',
                                                user=self.user,
                                                extras=self.extras)

    def test_get_200(self):
        response = self.client.get('/collections/coll/extras/foo/',
                                   HTTP_AUTHORIZATION='Token ' + self.token,
                                   format='json')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, dict(foo='bar'))

    def test_get_404(self):
        response = self.client.get('/collections/coll/extras/bar/',
                                   HTTP_AUTHORIZATION='Token ' + self.token,
                                   format='json')
        self.assertEqual(response.status_code, 404)

    def test_put_200(self):
        response = self.client.put('/collections/coll/extras/foo/',
                                   dict(foo='barbar'),
                                   HTTP_AUTHORIZATION='Token ' + self.token,
                                   format='json')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, dict(foo='barbar'))
        self.collection.refresh_from_db()
        self.assertEqual(self.collection.extras['foo'], 'barbar')

    def test_put_400(self):
        response = self.client.put('/collections/coll/extras/foo/',
                                   dict(foo=None),
                                   HTTP_AUTHORIZATION='Token ' + self.token,
                                   format='json')
        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.data, ['Must specify foo param in body.'])
        self.collection.refresh_from_db()
        self.assertEqual(self.collection.extras, self.extras)

    def test_delete_204(self):
        response = self.client.delete('/collections/coll/extras/foo/',
                                      HTTP_AUTHORIZATION='Token ' + self.token,
                                      format='json')
        self.assertEqual(response.status_code, 204)
        self.collection.refresh_from_db()
        self.assertEqual(self.collection.extras, dict(tao='ching'))

    def test_delete_404(self):
        response = self.client.delete('/collections/coll/extras/bar/',
                                      HTTP_AUTHORIZATION='Token ' + self.token,
                                      format='json')
        self.assertEqual(response.status_code, 404)
예제 #9
0
    def test_get_200(self):
        response = self.client.get('/collections/coll/versions/?verbose=true',
                                   HTTP_AUTHORIZATION='Token ' + self.token,
                                   format='json')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 1)
        self.assertEqual(response.data[0]['version'], 'HEAD')

        UserCollectionFactory(mnemonic=self.collection.mnemonic,
                              user=self.user,
                              version='v1',
                              released=True)

        response = self.client.get('/collections/coll/versions/?released=true',
                                   HTTP_AUTHORIZATION='Token ' + self.token,
                                   format='json')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 1)
        self.assertEqual(response.data[0]['version'], 'v1')
예제 #10
0
 def setUp(self):
     super().setUp()
     self.user = UserProfileFactory()
     self.collection = UserCollectionFactory(user=self.user)
     self.token = self.user.get_token()
예제 #11
0
class CollectionLatestVersionRetrieveUpdateViewTest(OCLAPITestCase):
    def setUp(self):
        super().setUp()
        self.user = UserProfileFactory()
        self.token = self.user.get_token()
        self.collection = UserCollectionFactory(mnemonic='coll',
                                                user=self.user)
        self.collection_v1 = UserCollectionFactory(version='v1',
                                                   mnemonic='coll',
                                                   user=self.user)

    def test_get_404(self):
        response = self.client.get('/collections/coll/latest/',
                                   HTTP_AUTHORIZATION='Token ' + self.token,
                                   format='json')

        self.assertEqual(response.status_code, 404)

    def test_get_200(self):
        self.collection_v1.released = True
        self.collection_v1.save()

        response = self.client.get('/collections/coll/latest/',
                                   HTTP_AUTHORIZATION='Token ' + self.token,
                                   format='json')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data['uuid'], str(self.collection_v1.id))
        self.assertEqual(response.data['id'], 'v1')
        self.assertEqual(response.data['short_code'], 'coll')

    def test_put_200(self):
        self.collection_v1.released = True
        self.collection_v1.save()
        self.assertEqual(self.collection.versions.count(), 2)
        self.assertIsNone(self.collection_v1.external_id)

        external_id = 'EXT-123'
        response = self.client.put('/collections/coll/latest/',
                                   dict(external_id=external_id),
                                   HTTP_AUTHORIZATION='Token ' + self.token,
                                   format='json')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data['uuid'], str(self.collection_v1.id))
        self.assertEqual(response.data['id'], 'v1')
        self.assertEqual(response.data['short_code'], 'coll')
        self.assertEqual(response.data['external_id'], external_id)
        self.collection_v1.refresh_from_db()
        self.assertEqual(self.collection_v1.external_id, external_id)
        self.assertEqual(self.collection.versions.count(), 2)

    def test_put_400(self):
        self.collection_v1.released = True
        self.collection_v1.save()

        response = self.client.put('/collections/coll/latest/',
                                   dict(version=None),
                                   HTTP_AUTHORIZATION='Token ' + self.token,
                                   format='json')

        self.assertEqual(response.status_code, 400)
        self.assertEqual(
            response.data, {
                'version': [
                    ErrorDetail(string='This field may not be null.',
                                code='null')
                ]
            })
예제 #12
0
class CollectionVersionRetrieveUpdateDestroyViewTest(OCLAPITestCase):
    def setUp(self):
        super().setUp()
        self.user = UserProfileFactory()
        self.token = self.user.get_token()
        self.collection = UserCollectionFactory(mnemonic='coll',
                                                user=self.user)
        self.collection_v1 = UserCollectionFactory(version='v1',
                                                   mnemonic='coll',
                                                   user=self.user)

    def test_get_200(self):
        response = self.client.get('/collections/coll/v1/',
                                   HTTP_AUTHORIZATION='Token ' + self.token,
                                   format='json')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data['uuid'], str(self.collection_v1.id))
        self.assertEqual(response.data['id'], 'v1')
        self.assertEqual(response.data['short_code'], 'coll')
        self.assertEqual(response.data['type'], 'Collection Version')

    def test_get_404(self):
        response = self.client.get('/collections/coll/v2/',
                                   HTTP_AUTHORIZATION='Token ' + self.token,
                                   format='json')

        self.assertEqual(response.status_code, 404)

    def test_put_200(self):
        self.assertEqual(self.collection.versions.count(), 2)
        self.assertIsNone(self.collection_v1.external_id)

        external_id = 'EXT-123'
        response = self.client.put('/collections/coll/v1/',
                                   dict(external_id=external_id),
                                   HTTP_AUTHORIZATION='Token ' + self.token,
                                   format='json')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data['uuid'], str(self.collection_v1.id))
        self.assertEqual(response.data['id'], 'v1')
        self.assertEqual(response.data['short_code'], 'coll')
        self.assertEqual(response.data['external_id'], external_id)
        self.collection_v1.refresh_from_db()
        self.assertEqual(self.collection_v1.external_id, external_id)
        self.assertEqual(self.collection.versions.count(), 2)

    def test_put_400(self):
        response = self.client.put('/collections/coll/v1/',
                                   dict(version=None),
                                   HTTP_AUTHORIZATION='Token ' + self.token,
                                   format='json')

        self.assertEqual(response.status_code, 400)
        self.assertEqual(
            response.data, {
                'version': [
                    ErrorDetail(string='This field may not be null.',
                                code='null')
                ]
            })

    @patch('core.common.services.S3.delete_objects', Mock())
    def test_delete(self):
        response = self.client.delete('/collections/coll/v1/',
                                      HTTP_AUTHORIZATION='Token ' + self.token,
                                      format='json')

        self.assertEqual(response.status_code, 204)
        self.assertEqual(self.collection.versions.count(), 1)
        self.assertTrue(self.collection.versions.first().is_latest_version)

        response = self.client.delete('/collections/coll/{}/'.format(
            self.collection.version),
                                      HTTP_AUTHORIZATION='Token ' + self.token,
                                      format='json')

        self.assertEqual(response.status_code, 204)
        self.assertFalse(
            Collection.objects.filter(id=self.collection.id).exists())
예제 #13
0
class CollectionReferencesViewTest(OCLAPITestCase):
    def setUp(self):
        super().setUp()
        self.user = UserProfileFactory()
        self.token = self.user.get_token()
        self.collection = UserCollectionFactory(mnemonic='coll',
                                                user=self.user)
        self.concept = ConceptFactory()
        self.reference = CollectionReference(expression=self.concept.uri)
        self.reference.full_clean()
        self.reference.save()
        self.collection.references.add(self.reference)
        self.collection.concepts.set(self.reference.concepts)
        self.assertEqual(self.collection.references.count(), 1)
        self.assertEqual(self.collection.concepts.count(), 1)

    def test_get_404(self):
        response = self.client.get('/collections/foobar/references/',
                                   format='json')

        self.assertEqual(response.status_code, 404)

    def test_get_200(self):
        response = self.client.get('/collections/coll/references/',
                                   format='json')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 1)
        self.assertEqual(response.data[0]['expression'],
                         self.concept.get_latest_version().uri)
        self.assertEqual(response.data[0]['reference_type'], 'concepts')

        response = self.client.get(
            '/collections/coll/references/?q={}&search_sort=desc'.format(
                self.concept.get_latest_version().uri),
            format='json')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 1)
        self.assertEqual(response.data[0]['expression'],
                         self.concept.get_latest_version().uri)
        self.assertEqual(response.data[0]['reference_type'], 'concepts')

        response = self.client.get(
            '/collections/coll/references/?q=/concepts/&search_sort=desc',
            format='json')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 1)

        response = self.client.get(
            '/collections/coll/references/?q=/mappings/&search_sort=desc',
            format='json')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 0)

    def test_delete_400(self):
        response = self.client.delete('/collections/coll/references/',
                                      HTTP_AUTHORIZATION='Token ' + self.token,
                                      format='json')

        self.assertEqual(response.status_code, 400)

    def test_delete_204_random(self):
        response = self.client.delete('/collections/coll/references/',
                                      dict(expressions=['/foo/']),
                                      HTTP_AUTHORIZATION='Token ' + self.token,
                                      format='json')

        self.assertEqual(response.status_code, 204)
        self.assertEqual(self.collection.references.count(), 1)
        self.assertEqual(self.collection.concepts.count(), 1)

    def test_delete_204_all_expressions(self):
        response = self.client.delete('/collections/coll/references/',
                                      dict(expressions='*'),
                                      HTTP_AUTHORIZATION='Token ' + self.token,
                                      format='json')

        self.assertEqual(response.status_code, 204)
        self.assertEqual(self.collection.references.count(), 0)
        self.assertEqual(self.collection.concepts.count(), 0)

    def test_delete_204_specific_expression(self):
        response = self.client.delete(
            '/collections/coll/references/',
            dict(expressions=[self.concept.get_latest_version().uri]),
            HTTP_AUTHORIZATION='Token ' + self.token,
            format='json')

        self.assertEqual(response.status_code, 204)
        self.assertEqual(self.collection.references.count(), 0)
        self.assertEqual(self.collection.concepts.count(), 0)

        concept = ConceptFactory()
        latest_version = concept.get_latest_version()
        MappingFactory(from_concept=latest_version, parent=concept.parent)
        response = self.client.put(
            '/collections/coll/references/?cascade=sourcemappings',
            dict(data=dict(mappings=[latest_version.uri])),
            HTTP_AUTHORIZATION='Token ' + self.token,
            format='json')
        self.assertEqual(response.status_code, 200)
        self.collection.refresh_from_db()
        self.assertEqual(self.collection.references.count(), 2)
        self.assertEqual(self.collection.concepts.count(), 1)
        self.assertEqual(self.collection.mappings.count(), 1)

        response = self.client.delete(
            '/collections/coll/references/?cascade=sourcemappings',
            dict(expressions=[latest_version.uri]),
            HTTP_AUTHORIZATION='Token ' + self.token,
            format='json')
        self.assertEqual(response.status_code, 204)
        self.collection.refresh_from_db()
        self.assertEqual(self.collection.references.count(), 0)
        self.assertEqual(self.collection.concepts.count(), 0)
        self.assertEqual(self.collection.mappings.count(), 0)

    @patch('core.collections.views.add_references')
    def test_put_202_all(self, add_references_mock):
        add_references_mock.delay = Mock()

        response = self.client.put('/collections/coll/references/',
                                   dict(data=dict(concepts='*')),
                                   HTTP_AUTHORIZATION='Token ' + self.token,
                                   format='json')

        self.assertEqual(response.status_code, 202)
        self.assertEqual(response.data, [])
        add_references_mock.delay.assert_called_once_with(
            self.user, dict(concepts='*'), self.collection,
            'http://testserver', False)

    def test_put_200_specific_expression(self):
        response = self.client.put(
            '/collections/coll/references/',
            dict(data=dict(concepts=[self.concept.uri])),
            HTTP_AUTHORIZATION='Token ' + self.token,
            format='json')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, [
            dict(
                added=False,
                expression=self.concept.uri,
                message=[
                    'Concept or Mapping reference name must be unique in a collection.'
                ])
        ])

        concept2 = ConceptFactory()
        response = self.client.put('/collections/coll/references/',
                                   dict(data=dict(concepts=[concept2.uri])),
                                   HTTP_AUTHORIZATION='Token ' + self.token,
                                   format='json')

        self.assertEqual(response.status_code, 200)
        self.collection.refresh_from_db()
        self.assertEqual(self.collection.references.count(), 2)
        self.assertEqual(self.collection.concepts.count(), 2)
        self.assertEqual(self.collection.active_concepts, 2)
        self.assertEqual(self.collection.active_mappings, 0)
        self.assertTrue(
            self.collection.references.filter(
                expression=concept2.get_latest_version().uri).exists())
        self.assertEqual(response.data, [
            dict(
                added=True,
                expression=concept2.get_latest_version().uri,
                message=
                'Added the latest versions of concept to the collection. Future updates will not be added'
                ' automatically.')
        ])

        mapping = MappingFactory(from_concept=concept2,
                                 to_concept=self.concept,
                                 parent=self.concept.parent)

        response = self.client.put('/collections/coll/references/',
                                   dict(data=dict(mappings=[mapping.uri])),
                                   HTTP_AUTHORIZATION='Token ' + self.token,
                                   format='json')

        self.assertEqual(response.status_code, 200)
        self.collection.refresh_from_db()
        self.assertEqual(self.collection.references.count(), 3)
        self.assertEqual(self.collection.concepts.count(), 2)
        self.assertEqual(self.collection.mappings.count(), 1)
        self.assertEqual(self.collection.active_concepts, 2)
        self.assertEqual(self.collection.active_mappings, 1)
        self.assertTrue(
            self.collection.references.filter(
                expression=mapping.get_latest_version().uri).exists())
        self.assertEqual(response.data, [
            dict(
                added=True,
                expression=mapping.get_latest_version().uri,
                message=
                'Added the latest versions of mapping to the collection. Future updates will not be added'
                ' automatically.')
        ])

        concept3 = ConceptFactory()
        latest_version = concept3.get_latest_version()
        mapping2 = MappingFactory(from_concept=latest_version,
                                  parent=concept3.parent)

        response = self.client.put(
            '/collections/coll/references/?cascade=sourcemappings',
            dict(data=dict(concepts=[latest_version.uri])),
            HTTP_AUTHORIZATION='Token ' + self.token,
            format='json')
        self.assertEqual(response.status_code, 200)
        self.collection.refresh_from_db()
        self.assertEqual(self.collection.references.count(), 5)
        self.assertEqual(self.collection.concepts.count(), 3)
        self.assertEqual(self.collection.mappings.count(), 2)
        self.assertEqual(self.collection.active_concepts, 3)
        self.assertEqual(self.collection.active_mappings, 2)
        self.assertTrue(
            self.collection.references.filter(
                expression=mapping2.get_latest_version().uri).exists())
        self.assertTrue(
            self.collection.references.filter(
                expression=latest_version.uri).exists())
예제 #14
0
 def setUp(self):
     super().setUp()
     self.user = UserProfileFactory(username='******')
     self.token = self.user.get_token()
     self.collection = UserCollectionFactory(mnemonic='coll1',
                                             user=self.user)