Exemplo n.º 1
0
 def setUp(self):
     self.photograph = TerraUserFactory()
     self.user = TerraUserFactory()
     self._set_permissions([
         'manage_all_campaigns',
         'add_campaign',
     ])
Exemplo n.º 2
0
    def test_results_must_not_duplicate(self):
        self._set_permissions(['can_read_self_requests', ])
        request_to_review = UserRequestFactory(owner=self.user)
        request_to_review.reviewers.add(TerraUserFactory())
        request_to_review.reviewers.add(TerraUserFactory())

        response = self.client.get(reverse('trrequests:request-list'))
        self.assertEqual(self.user.userrequests.all().count(),
                         response.json().get('count'))
        self._clean_permissions()
Exemplo n.º 3
0
 def test_has_object_permission_with_staff(self):
     user = TerraUserFactory()
     user.is_staff = True
     request = factory.get('/')
     request.user = user
     self.assertTrue(
         self.permission.has_object_permission(
             request,
             self.view,
             self.userrequest,
         )
     )
Exemplo n.º 4
0
 def setUp(self):
     self.permission = IsOwnerOrStaff()
     self.view = TestView.as_view()
     self.request = factory.get('/')  # we don't care about the path
     self.user = TerraUserFactory()
     self.request.user = self.user
     self.userrequest = UserRequestFactory()
Exemplo n.º 5
0
    def setUp(self):
        self.client = APIClient()

        self.user = TerraUserFactory()
        self._set_permissions([
            'can_read_self_requests',
        ])
        self.client.force_authenticate(user=self.user)
Exemplo n.º 6
0
    def setUp(self):

        examples = [{
            'key': 'terracommon.test.data.store',
            'value': {'key': 'value', }
        }, {
            'key': 'terracommon.test.data.new_store',
            'value': {}
        }, {
            'key': 'terracommon.prefix.dot.com',
            'value': {}
        }]

        for example in examples:
            DataStore.objects.create(**example)

        self.client = APIClient()
        self.user = TerraUserFactory()
        self.client.force_authenticate(user=self.user)
Exemplo n.º 7
0
    def test_forbidden_create_item(self):
        user = TerraUserFactory()
        client = APIClient()
        client.force_authenticate(user=user)

        response = client.post(reverse('datastore:datastore-detail',
                                       args=['terracommon.prefix.forbidden_creation']),
                               data={'test_key': 'test value'})
        self.assertEqual(response.status_code, HTTP_403_FORBIDDEN)
        self.assertFalse(DataStore.objects.filter(key='terracommon.prefix.forbidden_creation').exists())
Exemplo n.º 8
0
    def setUp(self):
        template_path = os.path.join('terracommon', 'document_generator',
                                     'tests', 'test_template.odt')
        self.documenttemplate = DocumentTemplate.objects.create(
            name='test_template', documenttemplate=template_path)
        self.user = TerraUserFactory()
        self.userrequest = UserRequestFactory()
        self.downloadable_document = DownloadableDocument.objects.create(
            user=self.user,
            document=self.documenttemplate,
            linked_object=self.userrequest)

        self.serializer = DownloadableDocumentSerializer(
            self.downloadable_document)
Exemplo n.º 9
0
 def setUpTestData(cls):
     cls.feature = FeatureFactory()
     cls.user = TerraUserFactory()
     # Create viewpoint with draft picture attached to it
     cls.viewpoint = ViewpointFactory(label="Basic viewpoint")
     # Create viewpoint with accepted picture attached to it
     cls.viewpoint_with_accepted_picture = ViewpointFactory(
         label="Viewpoint with accepted picture",
         pictures__state=STATES.ACCEPTED,
         properties={'test_update': 'ko'},
     )
     # Create viewpoints with no picture attached to it
     cls.viewpoint_without_picture = ViewpointFactory(
         label="Viewpoint without picture",
         pictures=None,
         properties={'test_update': 'ko'},
     )
Exemplo n.º 10
0
 def setUp(self):
     self.user = TerraUserFactory()
     self.userrequest = UserRequestFactory()
     self.docx_file = os.path.join(os.path.dirname(__file__), 'empty.docx')
     with open(self.docx_file, 'rb') as docx:
         self.template = DocumentTemplate.objects.create(
             name='emptydocx',
             documenttemplate=SimpleUploadedFile(
                 str(uuid4()),
                 docx.read()
             )
         )
     self.downloadable = DownloadableDocument.objects.create(
         user=self.user,
         document=self.template,
         linked_object=self.userrequest
     )
Exemplo n.º 11
0
 def setUp(self):
     self.client = APIClient()
     self.user = TerraUserFactory.create(password=self.USER_PASSWORD)
Exemplo n.º 12
0
    def setUp(self):
        self.client = APIClient()
        self.user = TerraUserFactory()

        self.client.force_authenticate(user=self.user)
Exemplo n.º 13
0
class DataStoreTestCase(TestCase):
    def setUp(self):

        examples = [{
            'key': 'terracommon.test.data.store',
            'value': {'key': 'value', }
        }, {
            'key': 'terracommon.test.data.new_store',
            'value': {}
        }, {
            'key': 'terracommon.prefix.dot.com',
            'value': {}
        }]

        for example in examples:
            DataStore.objects.create(**example)

        self.client = APIClient()
        self.user = TerraUserFactory()
        self.client.force_authenticate(user=self.user)

    def test_not_authenticated(self):
        client = APIClient()
        response = client.get(reverse('datastore:datastore-list'))
        self.assertEqual(HTTP_401_UNAUTHORIZED, response.status_code)

    def test_no_permission(self):
        client = APIClient()
        client.force_authenticate(user=self.user)
        response = client.get(reverse('datastore:datastore-list'))

        self.assertEqual(HTTP_200_OK, response.status_code)
        self.assertEqual(0, response.json()['count'])

    def test_can_readonly(self):
        # Add the user to a group
        group = Group.objects.create(name='can_read')
        self.user.groups.add(group)
        self.user.save()

        # Allow this group to read the terracommon.prefix prefix
        perm = Permission.objects.get(codename='can_read_datastore')
        DataStorePermission.objects.create(
            group=group,
            permission=perm,
            prefix='terracommon.prefix',
        )

        response = self.client.get(reverse('datastore:datastore-list'))
        self.assertEqual(HTTP_200_OK, response.status_code)
        self.assertEqual(1, response.json()['count'])

        # test that write is not allowed
        test_value = {'a': 'TEST'}
        ds = DataStore.objects.get(key='terracommon.prefix.dot.com')
        response = self.client.put(reverse('datastore:datastore-detail',
                                           args=[ds.key]),
                                   data={'value': test_value})

        self.assertEqual(HTTP_403_FORBIDDEN, response.status_code)
        self.user.groups.clear()

    def test_can_readwrite(self):
        # Add the user to a group
        group = Group.objects.create(name='can_readwrite')
        self.user.groups.add(group)

        # Allow this group to read the terracommon.prefix prefix
        perm = Permission.objects.get(codename='can_readwrite_datastore')
        DataStorePermission.objects.get_or_create(
            group=group,
            permission=perm,
            prefix='terracommon.prefix',
        )

        response = self.client.get(reverse('datastore:datastore-list'))
        self.assertEqual(HTTP_200_OK, response.status_code)
        self.assertEqual(1, response.json()['count'])

        # Test writing
        test_value = {'a': 'b'}
        ds = DataStore.objects.get(key='terracommon.prefix.dot.com')
        response = self.client.put(reverse('datastore:datastore-detail',
                                           args=[ds.key]),
                                   data=test_value)

        self.assertEqual(HTTP_200_OK, response.status_code)

        ds.refresh_from_db()
        self.assertDictEqual(ds.value, test_value)

        self.user.groups.clear()

    def test_create_item(self):
        # Add the user to a group
        group = Group.objects.create(name='can_readwrite')
        self.user.groups.add(group)

        # Allow this group to read the terracommon.prefix prefix
        perm = Permission.objects.get(codename='can_readwrite_datastore')
        DataStorePermission.objects.get_or_create(
            group=group,
            permission=perm,
            prefix='terracommon.prefix',
        )

        test_value = {'test_key': 'test value'}
        response = self.client.post(reverse('datastore:datastore-detail', args=['terracommon.prefix.blurp']),
                                    data=test_value)
        self.assertEqual(response.status_code, HTTP_200_OK)

        self.assertTrue(DataStore.objects.filter(key='terracommon.prefix.blurp').exists())
        self.assertDictEqual(response.json(), test_value)

    def test_forbidden_create_item(self):
        user = TerraUserFactory()
        client = APIClient()
        client.force_authenticate(user=user)

        response = client.post(reverse('datastore:datastore-detail',
                                       args=['terracommon.prefix.forbidden_creation']),
                               data={'test_key': 'test value'})
        self.assertEqual(response.status_code, HTTP_403_FORBIDDEN)
        self.assertFalse(DataStore.objects.filter(key='terracommon.prefix.forbidden_creation').exists())

    def test_perms(self):
        # Add the user to a group
        group = Group.objects.create(name='can_readwrite')
        self.user.groups.add(group)

        # Allow this group to read the terracommon.prefix prefix
        perm = Permission.objects.get(codename='can_readwrite_datastore')
        DataStorePermission.objects.get_or_create(
            group=group,
            permission=perm,
            prefix='terracommon.testprefix',
        )

        test_value = {'test_key': 'test value'}
        response = self.client.post(reverse('datastore:datastore-detail', args=['terracommon.testprefix.blurp']),
                                    data=test_value)
        self.assertEqual(response.status_code, HTTP_200_OK)

        response = self.client.post(reverse('datastore:datastore-detail', args=['terracommon.forbiddenprefix']),
                                    data=test_value)
        self.assertEqual(response.status_code, HTTP_403_FORBIDDEN)