示例#1
0
class EntityViewSetTest(TestCase):
    def setUp(self):
        super().setUp()

        self.collection = Collection.objects.create(name="Test Collection", contributor=self.contributor)
        self.collection2 = Collection.objects.create(name="Test Collection 2", contributor=self.contributor)
        self.entity = Entity.objects.create(name="Test entity", contributor=self.contributor)
        process = Process.objects.create(name="Test process", contributor=self.contributor)
        self.data = Data.objects.create(
            name="Test data",
            contributor=self.contributor,
            process=process,
            status=Data.STATUS_DONE
        )
        data_location = DataLocation.objects.create(subpath='')
        data_location.subpath = str(data_location.id)
        data_location.save()
        data_location.data.add(self.data)
        self.data_2 = Data.objects.create(
            name="Test data 2",
            contributor=self.contributor,
            process=process,
            status=Data.STATUS_DONE
        )
        data_location = DataLocation.objects.create(subpath='')
        data_location.subpath = str(data_location.id)
        data_location.save()
        data_location.data.add(self.data_2)

        # another Data object to make sure that other objects are not processed
        data = Data.objects.create(name="Dummy data", contributor=self.contributor, process=process)
        data_location = DataLocation.objects.create(subpath='')
        data_location.subpath = str(data_location.id)
        data_location.save()
        data_location.data.add(data)

        self.entity.data.add(self.data)
        self.entity.collections.add(self.collection2)

        assign_perm('add_collection', self.contributor, self.collection)
        assign_perm('add_entity', self.contributor, self.entity)
        assign_perm('view_collection', self.contributor, self.collection)
        assign_perm('view_collection', self.contributor, self.collection2)
        assign_perm('view_entity', self.contributor, self.entity)

        self.entityviewset = EntityViewSet()

        self.duplicate_viewset = EntityViewSet.as_view(actions={
            'post': 'duplicate',
        })
        self.move_to_collection_viewset = EntityViewSet.as_view(actions={
            'post': 'move_to_collection',
        })
        self.entity_detail_viewset = EntityViewSet.as_view(actions={
            'get': 'retrieve',
            'put': 'update',
            'patch': 'partial_update',
            'delete': 'destroy',
        })
        self.entity_list_viewset = EntityViewSet.as_view(actions={
            'get': 'list',
            'post': 'create',
        })

        self.detail_url = lambda pk: reverse('resolwe-api:entity-detail', kwargs={'pk': pk})

    def _create_data(self):
        process = Process.objects.create(
            name='Test process',
            contributor=self.contributor,
        )

        return Data.objects.create(
            name='Test data',
            contributor=self.contributor,
            process=process,
        )

    def test_list_filter_collections(self):
        request = factory.get('/', {}, format='json')
        force_authenticate(request, self.contributor)
        resp = self.entity_list_viewset(request)
        self.assertEqual(len(resp.data), 1)

        request = factory.get('/', {'collections': 999999}, format='json')
        force_authenticate(request, self.contributor)
        resp = self.entity_list_viewset(request)
        self.assertEqual(len(resp.data), 0)

        request = factory.get('/', {'collections': self.collection.pk}, format='json')
        force_authenticate(request, self.contributor)
        resp = self.entity_list_viewset(request)
        self.assertEqual(len(resp.data), 0)

        request = factory.get('/', {'collections': self.collection2.pk}, format='json')
        force_authenticate(request, self.contributor)
        resp = self.entity_list_viewset(request)
        self.assertEqual(len(resp.data), 1)

    def test_add_to_collection(self):
        request_mock = mock.MagicMock(data={'ids': [self.collection.pk]}, user=self.contributor)
        self.entityviewset.get_object = lambda: self.entity

        self.assertEqual(self.entity.collections.count(), 1)

        self.entityviewset.add_to_collection(request_mock)

        self.assertEqual(self.collection.data.count(), 1)
        self.assertEqual(self.entity.collections.count(), 2)

    def test_remove_from_collection(self):
        # Manually add Entity and it's Data objects to the Collection
        self.entity.collections.add(self.collection.pk)
        self.collection.data.add(self.data)

        request_mock = mock.MagicMock(data={'ids': [self.collection.pk]}, user=self.contributor)
        self.entityviewset.get_object = lambda: self.entity

        self.assertEqual(self.entity.collections.count(), 2)

        self.entityviewset.remove_from_collection(request_mock)

        self.assertEqual(self.collection.data.count(), 0)
        self.assertEqual(self.entity.collections.count(), 1)

    def test_add_remove_permissions(self):
        request_mock = mock.MagicMock(data={'ids': [self.collection.pk]}, user=self.contributor)
        self.entityviewset.get_object = lambda: self.entity

        remove_perm('add_collection', self.contributor, self.collection)

        with self.assertRaises(exceptions.PermissionDenied):
            self.entityviewset.remove_from_collection(request_mock)

        with self.assertRaises(exceptions.PermissionDenied):
            self.entityviewset.add_to_collection(request_mock)

    def test_add_data(self):
        self.entity.collections.add(self.collection)

        request_mock = mock.MagicMock(data={'ids': [self.data.pk]}, user=self.contributor)
        self.entityviewset.get_object = lambda: self.entity

        self.entityviewset.add_data(request_mock)

        self.assertEqual(self.entity.data.count(), 1)
        self.assertEqual(self.collection.data.count(), 1)

    def test_move_to_collection(self):
        entity = Entity.objects.create(contributor=self.contributor)
        assign_perm('view_entity', self.contributor, entity)
        data = self._create_data()
        assign_perm('view_data', self.contributor, data)
        entity.data.add(data)

        source_collection = Collection.objects.create(contributor=self.contributor)
        assign_perm('view_collection', self.contributor, source_collection)
        assign_perm('add_collection', self.contributor, source_collection)
        entity.collections.add(source_collection)
        data.collection_set.add(source_collection)

        destination_collection = Collection.objects.create(contributor=self.contributor)
        assign_perm('view_collection', self.contributor, destination_collection)
        assign_perm('add_collection', self.contributor, destination_collection)

        request = factory.post(reverse('resolwe-api:entity-move-to-collection'), {
            'ids': [entity.id],
            'source_collection': source_collection.id,
            'destination_collection': destination_collection.id,
        }, format='json')
        force_authenticate(request, self.contributor)

        self.assertEqual(source_collection.entity_set.count(), 1)
        self.assertEqual(source_collection.data.count(), 1)
        self.assertEqual(destination_collection.entity_set.count(), 0)
        self.assertEqual(destination_collection.data.count(), 0)

        self.move_to_collection_viewset(request)

        self.assertEqual(source_collection.entity_set.count(), 0)
        self.assertEqual(source_collection.data.count(), 0)
        self.assertEqual(destination_collection.entity_set.count(), 1)
        self.assertEqual(destination_collection.entity_set.first().id, entity.id)
        self.assertEqual(destination_collection.data.first().id, data.id)

    def test_delete(self):
        entity = Entity.objects.create(
            name="Test entity",
            contributor=self.contributor,
        )

        data_1, data_2 = self._create_data(), self._create_data()

        entity.data.add(data_1, data_2)

        assign_perm('view_entity', self.user, entity)
        assign_perm('edit_entity', self.user, entity)
        assign_perm('view_data', self.user, data_1)
        assign_perm('view_data', self.user, data_2)
        assign_perm('edit_data', self.user, data_1)

        request = factory.delete(self.detail_url(entity.pk))
        force_authenticate(request, self.user)
        self.entity_detail_viewset(request, pk=entity.pk)

        self.assertTrue(Data.objects.filter(pk=data_1.pk).exists())
        self.assertTrue(Data.objects.filter(pk=data_2.pk).exists())

        # Recreate the initial state and test with `delete_content` flag.
        entity = Entity.objects.create(
            name="Test entity",
            contributor=self.contributor,
        )

        entity.data.add(data_1, data_2)

        assign_perm('view_entity', self.user, entity)
        assign_perm('edit_entity', self.user, entity)

        request = factory.delete('{}?delete_content=1'.format(self.detail_url(entity.pk)))
        force_authenticate(request, self.user)
        self.entity_detail_viewset(request, pk=entity.pk)

        # Only objects with `edit` permission can be deleted.
        self.assertFalse(Data.objects.filter(pk=data_1.pk).exists())
        self.assertTrue(Data.objects.filter(pk=data_2.pk).exists())

        # Ensure that deletion works correctly when all data objects of an entity
        # are deleted.
        entity = Entity.objects.create(
            name="Test entity",
            contributor=self.contributor,
        )

        assign_perm('view_entity', self.user, entity)
        assign_perm('edit_entity', self.user, entity)
        assign_perm('edit_data', self.user, data_2)

        entity.data.add(data_2)

        request = factory.delete('{}?delete_content=1'.format(self.detail_url(entity.pk)))
        force_authenticate(request, self.user)
        response = self.entity_detail_viewset(request, pk=entity.pk)

        self.assertEqual(response.status_code, 204)
        self.assertFalse(Entity.objects.filter(pk=entity.pk).exists())
        self.assertFalse(Data.objects.filter(pk=data_2.pk).exists())

    def test_duplicate(self):
        entity = Entity.objects.first()
        collection = Collection.objects.create(contributor=self.contributor)
        assign_perm('add_collection', self.contributor, collection)
        collection.entity_set.add(entity)
        data = entity.data.all()
        for datum in data:
            assign_perm('view_data', self.contributor, datum)
        collection.data.add(*data)

        request = factory.post(reverse('resolwe-api:entity-duplicate'), {'ids': [entity.id]}, format='json')
        force_authenticate(request, self.contributor)
        response = self.duplicate_viewset(request)

        duplicate = Entity.objects.get(id=response.data[0]['id'])
        self.assertTrue(duplicate.is_duplicate())
        self.assertEqual(collection.entity_set.count(), 1)
        self.assertEqual(collection.data.count(), 1)

        # Assert collection membership.
        collection_without_perm = Collection.objects.create(contributor=self.contributor)
        collection_without_perm.entity_set.add(entity)
        collection_without_perm.data.add(*entity.data.all())

        request = factory.post(reverse('resolwe-api:entity-duplicate'), {
            'ids': [entity.id],
            'inherit_collections': True
        }, format='json')
        force_authenticate(request, self.contributor)
        response = self.duplicate_viewset(request)

        self.assertEqual(collection.entity_set.count(), 2)
        self.assertEqual(collection.data.count(), 2)

        self.assertEqual(collection_without_perm.entity_set.count(), 1)
        self.assertEqual(collection_without_perm.data.count(), 1)

    def test_duplicate_not_auth(self):
        request = factory.post(reverse('resolwe-api:entity-duplicate'), format='json')
        response = self.duplicate_viewset(request)

        self.assertEqual(response.data['detail'], MESSAGES['NOT_FOUND'])
示例#2
0
class EntityViewSetTest(TestCase):
    def setUp(self):
        super(EntityViewSetTest, self).setUp()

        self.collection = Collection.objects.create(
            name="Test Collection", contributor=self.contributor)
        self.entity = Entity.objects.create(name="Test entity",
                                            contributor=self.contributor)
        process = Process.objects.create(name="Test process",
                                         contributor=self.contributor)
        self.data = Data.objects.create(name="Test data",
                                        contributor=self.contributor,
                                        process=process)
        self.data_2 = Data.objects.create(name="Test data 2",
                                          contributor=self.contributor,
                                          process=process)

        # another Data object to make sure that other objects are not processed
        Data.objects.create(name="Dummy data",
                            contributor=self.contributor,
                            process=process)

        self.entity.data.add(self.data)

        assign_perm('add_collection', self.contributor, self.collection)
        assign_perm('add_entity', self.contributor, self.entity)

        self.entityviewset = EntityViewSet()

    def test_add_to_collection(self):
        request_mock = mock.MagicMock(data={'ids': [self.collection.pk]},
                                      user=self.contributor)
        self.entityviewset.get_object = lambda: self.entity

        self.entityviewset.add_to_collection(request_mock)

        self.assertEqual(self.collection.data.count(), 1)
        self.assertEqual(self.entity.collections.count(), 1)

    def test_remove_from_collection(self):
        # Manually add Entity and it's Data objects to the Collection
        self.entity.collections.add(self.collection.pk)
        self.collection.data.add(self.data)

        request_mock = mock.MagicMock(data={'ids': [self.collection.pk]},
                                      user=self.contributor)
        self.entityviewset.get_object = lambda: self.entity

        self.entityviewset.remove_from_collection(request_mock)

        self.assertEqual(self.collection.data.count(), 0)
        self.assertEqual(self.entity.collections.count(), 0)

    def test_add_remove_permissions(self):
        request_mock = mock.MagicMock(data={'ids': [self.collection.pk]},
                                      user=self.contributor)
        self.entityviewset.get_object = lambda: self.entity

        remove_perm('add_collection', self.contributor, self.collection)

        with self.assertRaises(exceptions.PermissionDenied):
            self.entityviewset.remove_from_collection(request_mock)

        with self.assertRaises(exceptions.PermissionDenied):
            self.entityviewset.add_to_collection(request_mock)

    def test_add_data(self):
        self.entity.collections.add(self.collection)

        request_mock = mock.MagicMock(data={'ids': [self.data.pk]},
                                      user=self.contributor)
        self.entityviewset.get_object = lambda: self.entity

        self.entityviewset.add_data(request_mock)

        self.assertEqual(self.entity.data.count(), 1)
        self.assertEqual(self.collection.data.count(), 1)

    def test_remove_data(self):
        self.entity.data.add(self.data_2)
        self.entityviewset.get_object = lambda: self.entity

        # entity is removed only when last data object is removed
        request_mock = mock.MagicMock(data={'ids': [self.data.pk]},
                                      user=self.contributor)
        self.entityviewset.remove_data(request_mock)
        self.assertEqual(Entity.objects.count(), 1)
        request_mock = mock.MagicMock(data={'ids': [self.data_2.pk]},
                                      user=self.contributor)
        self.entityviewset.remove_data(request_mock)
        self.assertEqual(Entity.objects.count(), 0)
示例#3
0
class EntityViewSetTest(TestCase):
    def setUp(self):
        super().setUp()

        self.collection = Collection.objects.create(
            name="Test Collection", contributor=self.contributor)
        self.collection2 = Collection.objects.create(
            name="Test Collection 2", contributor=self.contributor)
        self.entity = Entity.objects.create(name="Test entity",
                                            contributor=self.contributor)
        process = Process.objects.create(name="Test process",
                                         contributor=self.contributor)
        self.data = Data.objects.create(name="Test data",
                                        contributor=self.contributor,
                                        process=process)
        self.data_2 = Data.objects.create(name="Test data 2",
                                          contributor=self.contributor,
                                          process=process)

        # another Data object to make sure that other objects are not processed
        Data.objects.create(name="Dummy data",
                            contributor=self.contributor,
                            process=process)

        self.entity.data.add(self.data)
        self.entity.collections.add(self.collection2)

        assign_perm('add_collection', self.contributor, self.collection)
        assign_perm('add_entity', self.contributor, self.entity)
        assign_perm('view_collection', self.contributor, self.collection)
        assign_perm('view_collection', self.contributor, self.collection2)
        assign_perm('view_entity', self.contributor, self.entity)

        self.entityviewset = EntityViewSet()

        self.entity_detail_viewset = EntityViewSet.as_view(
            actions={
                'get': 'retrieve',
                'put': 'update',
                'patch': 'partial_update',
                'delete': 'destroy',
            })
        self.entity_list_viewset = EntityViewSet.as_view(actions={
            'get': 'list',
            'post': 'create',
        })

        self.detail_url = lambda pk: reverse('resolwe-api:entity-detail',
                                             kwargs={'pk': pk})

    def _create_data(self):
        process = Process.objects.create(
            name='Test process',
            contributor=self.contributor,
        )

        return Data.objects.create(
            name='Test data',
            contributor=self.contributor,
            process=process,
        )

    def test_list_filter_collections(self):
        request = factory.get('/', {}, format='json')
        force_authenticate(request, self.contributor)
        resp = self.entity_list_viewset(request)
        self.assertEqual(len(resp.data), 1)

        request = factory.get('/', {'collections': 999999}, format='json')
        force_authenticate(request, self.contributor)
        resp = self.entity_list_viewset(request)
        self.assertEqual(len(resp.data), 0)

        request = factory.get('/', {'collections': self.collection.pk},
                              format='json')
        force_authenticate(request, self.contributor)
        resp = self.entity_list_viewset(request)
        self.assertEqual(len(resp.data), 0)

        request = factory.get('/', {'collections': self.collection2.pk},
                              format='json')
        force_authenticate(request, self.contributor)
        resp = self.entity_list_viewset(request)
        self.assertEqual(len(resp.data), 1)

    def test_add_to_collection(self):
        request_mock = mock.MagicMock(data={'ids': [self.collection.pk]},
                                      user=self.contributor)
        self.entityviewset.get_object = lambda: self.entity

        self.assertEqual(self.entity.collections.count(), 1)

        self.entityviewset.add_to_collection(request_mock)

        self.assertEqual(self.collection.data.count(), 1)
        self.assertEqual(self.entity.collections.count(), 2)

    def test_remove_from_collection(self):
        # Manually add Entity and it's Data objects to the Collection
        self.entity.collections.add(self.collection.pk)
        self.collection.data.add(self.data)

        request_mock = mock.MagicMock(data={'ids': [self.collection.pk]},
                                      user=self.contributor)
        self.entityviewset.get_object = lambda: self.entity

        self.assertEqual(self.entity.collections.count(), 2)

        self.entityviewset.remove_from_collection(request_mock)

        self.assertEqual(self.collection.data.count(), 0)
        self.assertEqual(self.entity.collections.count(), 1)

    def test_add_remove_permissions(self):
        request_mock = mock.MagicMock(data={'ids': [self.collection.pk]},
                                      user=self.contributor)
        self.entityviewset.get_object = lambda: self.entity

        remove_perm('add_collection', self.contributor, self.collection)

        with self.assertRaises(exceptions.PermissionDenied):
            self.entityviewset.remove_from_collection(request_mock)

        with self.assertRaises(exceptions.PermissionDenied):
            self.entityviewset.add_to_collection(request_mock)

    def test_add_data(self):
        self.entity.collections.add(self.collection)

        request_mock = mock.MagicMock(data={'ids': [self.data.pk]},
                                      user=self.contributor)
        self.entityviewset.get_object = lambda: self.entity

        self.entityviewset.add_data(request_mock)

        self.assertEqual(self.entity.data.count(), 1)
        self.assertEqual(self.collection.data.count(), 1)

    def test_delete(self):
        entity = Entity.objects.create(
            name="Test entity",
            contributor=self.contributor,
        )

        data_1, data_2 = self._create_data(), self._create_data()

        entity.data.add(data_1, data_2)

        assign_perm('view_entity', self.user, entity)
        assign_perm('edit_entity', self.user, entity)
        assign_perm('view_data', self.user, data_1)
        assign_perm('view_data', self.user, data_2)
        assign_perm('edit_data', self.user, data_1)

        request = factory.delete(self.detail_url(entity.pk))
        force_authenticate(request, self.user)
        self.entity_detail_viewset(request, pk=entity.pk)

        self.assertTrue(Data.objects.filter(pk=data_1.pk).exists())
        self.assertTrue(Data.objects.filter(pk=data_2.pk).exists())

        # Recreate the initial state and test with `delete_content` flag.
        entity = Entity.objects.create(
            name="Test entity",
            contributor=self.contributor,
        )

        entity.data.add(data_1, data_2)

        assign_perm('view_entity', self.user, entity)
        assign_perm('edit_entity', self.user, entity)

        request = factory.delete('{}?delete_content=1'.format(
            self.detail_url(entity.pk)))
        force_authenticate(request, self.user)
        self.entity_detail_viewset(request, pk=entity.pk)

        # Only objects with `edit` permission can be deleted.
        self.assertFalse(Data.objects.filter(pk=data_1.pk).exists())
        self.assertTrue(Data.objects.filter(pk=data_2.pk).exists())

        # Ensure that deletion works correctly when all data objects of an entity
        # are deleted.
        entity = Entity.objects.create(
            name="Test entity",
            contributor=self.contributor,
        )

        assign_perm('view_entity', self.user, entity)
        assign_perm('edit_entity', self.user, entity)
        assign_perm('edit_data', self.user, data_2)

        entity.data.add(data_2)

        request = factory.delete('{}?delete_content=1'.format(
            self.detail_url(entity.pk)))
        force_authenticate(request, self.user)
        response = self.entity_detail_viewset(request, pk=entity.pk)

        self.assertEqual(response.status_code, 204)
        self.assertFalse(Entity.objects.filter(pk=entity.pk).exists())
        self.assertFalse(Data.objects.filter(pk=data_2.pk).exists())