Example #1
0
    def post(self, user, data=None):
        if user.is_anonymous and not User.objects.filter(
                display_name='AnonymousUser').exists():
            UserF.create(display_name='AnonymousUser')

        if data is None:
            data = {
                'name': 'A test image',
                'description': 'Test image description',
                'file': get_image()
            }

        url = reverse(
            'api:project_media',
            kwargs={
                'project_id': self.project.id,
                'contribution_id': self.contribution.id
            }
        )

        request = self.factory.post(url, data)
        force_authenticate(request, user)
        view = AllContributionsMediaAPIView.as_view()
        return view(
            request,
            project_id=self.project.id,
            contribution_id=self.contribution.id
        ).render()
Example #2
0
    def setUp(self):
        self.factory = APIRequestFactory()
        self.admin = UserF.create()
        self.creator = UserF.create()
        self.viewer = UserF.create()
        self.project = ProjectF(
            add_admins=[self.admin],
            add_contributors=[self.creator]
        )

        self.contribution = ObservationFactory.create(
            **{'project': self.project, 'creator': self.creator}
        )

        self.viewer = UserF.create()
        self.grouping = GroupingFactory.create(
            add_viewers=[self.viewer],
            **{'project': self.project}
        )
        RuleFactory.create(**{
            'grouping': self.grouping,
            'category': self.contribution.category
        })

        self.image_file = ImageFileFactory.create(
            **{'contribution': self.contribution, 'creator': self.creator}
        )
Example #3
0
    def setUp(self):
        self.factory = APIRequestFactory()
        self.admin = UserF.create()
        self.contributor = UserF.create()
        self.view_member = UserF.create()
        self.non_member = UserF.create()

        self.project = ProjectF(
            add_admins=[self.admin],
            add_contributors=[self.contributor],
            add_viewers=[self.view_member]
        )
        self.other_project = ProjectF.create()

        # Create 20 locations, 10 should be accessible for the project
        for x in range(0, 5):
            LocationFactory()
            LocationFactory(**{
                'private': True,
                'private_for_project': self.other_project
            })
            LocationFactory(**{
                'private': True,
                'private_for_project': self.project
            })
            LocationFactory(**{
                'private': True
            })
Example #4
0
 def setUp(self):
     self.contributor = UserF.create()
     self.admin = UserF.create()
     self.non_member = UserF.create()
     self.project = ProjectF(
         add_admins=[self.admin],
         add_contributors=[self.contributor]
     )
     self.observation = ObservationFactory.create(**{
         'project': self.project,
         'creator': self.contributor
     })
     comment = CommentFactory.create(**{
         'commentto': self.observation
     })
     response = CommentFactory.create(**{
         'commentto': self.observation,
         'respondsto': comment
     })
     CommentFactory.create(**{
         'commentto': self.observation,
         'respondsto': response
     })
     CommentFactory.create(**{
         'commentto': self.observation,
         'respondsto': comment
     })
     CommentFactory.create(**{
         'commentto': self.observation
     })
Example #5
0
    def setUp(self):
        self.admin = UserF.create()
        self.contributor = UserF.create()
        self.view_member = UserF.create()
        self.non_member = UserF.create()

        self.project = ProjectF(
            add_admins=[self.admin],
            add_contributors=[self.contributor],
            add_viewers=[self.view_member]
        )
        self.category = CategoryFactory(**{
            'status': 'active',
            'project': self.project
        })

        TextFieldFactory.create(**{
            'key': 'key_1',
            'category': self.category,
            'order': 0
        })
        NumericFieldFactory.create(**{
            'key': 'key_2',
            'category': self.category,
            'order': 1
        })
Example #6
0
    def setUp(self):
        self.factory = APIRequestFactory()
        self.admin = UserF.create()
        self.contributor = UserF.create()

        self.project = ProjectF.create(
            add_admins=[self.admin],
            add_contributors=[self.contributor]
        )
Example #7
0
 def setUp(self):
     self.admin = UserF.create()
     self.creator = UserF.create()
     self.project = ProjectF(
         add_admins=[self.admin],
         add_contributors=[self.creator]
     )
     self.observation = ObservationFactory.create(**{
         'project': self.project,
         'creator': self.creator
     })
Example #8
0
    def setUp(self):
        self.factory = APIRequestFactory()
        self.admin = UserF.create()
        self.creator = UserF.create()
        self.project = ProjectF(
            add_admins=[self.admin],
            add_contributors=[self.creator]
        )

        self.contribution = ObservationFactory.create(
            **{'project': self.project}
        )
Example #9
0
    def setUp(self):
        self.factory = APIRequestFactory()
        self.admin = UserF.create()
        self.contributor = UserF.create()
        self.non_member = UserF.create()

        self.project = ProjectF(
            add_admins=[self.admin],
            add_contributors=[self.contributor]
        )

        self.category = CategoryFactory(**{
            'status': 'active',
            'project': self.project
        })

        TextFieldFactory.create(**{
            'key': 'key_1',
            'category': self.category,
            'required': True,
            'order': 1
        })
        NumericFieldFactory.create(**{
            'key': 'key_2',
            'category': self.category,
            'minval': 0,
            'maxval': 1000,
            'order': 2
        })

        self.data = {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -0.13404607772827148,
                    51.52439200896907
                ]
            },
            "properties": {
                "key_1": "value 1",
                "key_2": 12
            },
            "meta": {
                "category": self.category.id,
            },
            "location": {
                "name": "UCL",
                "description": "UCL's main quad",
                "private": True
            }
        }
Example #10
0
 def setUp(self):
     self.contributor = UserF.create()
     self.admin = UserF.create()
     self.non_member = UserF.create()
     self.project = ProjectF(
         add_admins=[self.admin],
         add_contributors=[self.contributor],
         **{'isprivate': False}
     )
     self.observation = ObservationFactory.create(**{
         'project': self.project,
         'creator': self.contributor
     })
Example #11
0
    def test_get_isowner(self):
        user = UserF.create()
        comment = CommentFactory.create(**{'creator': user})

        serializer = CommentSerializer(comment, context={'user': user})
        self.assertTrue(serializer.get_isowner(comment))

        serializer = CommentSerializer(
            comment, context={'user': UserF.create()})
        self.assertFalse(serializer.get_isowner(comment))

        serializer = CommentSerializer(
            comment, context={'user': AnonymousUser()})
        self.assertFalse(serializer.get_isowner(comment))
Example #12
0
    def setUp(self):
        self.factory = APIRequestFactory()
        self.admin = UserF.create()
        self.creator = UserF.create()
        self.viewer = UserF.create()
        self.project = ProjectF(
            add_admins=[self.admin],
            add_contributors=[self.creator]
        )

        self.contribution = ObservationFactory.create(
            **{'project': self.project, 'creator': self.creator}
        )

        ImageFileFactory.create_batch(5, **{'contribution': self.contribution})
Example #13
0
 def _post(self, data, user):
     if user.is_anonymous and not User.objects.filter(
             display_name='AnonymousUser').exists():
         UserF.create(display_name='AnonymousUser')
     url = reverse(
         'api:project_observations',
         kwargs={
             'project_id': self.project.id
         }
     )
     request = self.factory.post(
         url, json.dumps(data), content_type='application/json')
     force_authenticate(request, user=user)
     view = ProjectObservations.as_view()
     return view(request, project_id=self.project.id).render()
Example #14
0
    def test_delete_with_user(self):
        user = UserF.create()
        view = RuleDelete.as_view()
        project = ProjectF.create(add_contributors=[user])
        grouping = GroupingFactory.create(**{'project': project})
        rule = RuleFactory.create(**{'grouping': grouping})

        url = reverse(
            'admin:rule_delete',
            kwargs={
                'project_id': project.id,
                'grouping_id': grouping.id,
                'rule_id': rule.id
            }
        )

        request = APIRequestFactory().get(url)
        request.user = user

        response = view(
            request,
            project_id=project.id,
            grouping_id=grouping.id,
            rule_id=rule.id).render()

        self.assertEqual(response.status_code, 200)
        self.assertEqual(Rule.objects.count(), 1)
        self.assertContains(
            response,
            'You are not member of the administrators group of this project '
            'and therefore not allowed to alter the settings of the project'
        )
Example #15
0
 def test_get_with_unconnected_user(self):
     view = ApplicationDisconnect.as_view()
     url = reverse('admin:app_disconnect', kwargs={'app_id': self.app.id})
     request = APIRequestFactory().get(url)
     request.user = UserF.create()
     response = view(request, app_id=self.app.id)
     self.assertTrue(isinstance(response, HttpResponseRedirect))
Example #16
0
    def test_post_with_user(self):
        data = {
            'name': 'test app',
            'description': '',
            'download_url': 'http://example.com/download',
            'redirect_uris': 'http://example.com/redirect',
            'authorization_grant_type': 'password'
        }

        view = ApplicationSettings.as_view()
        url = reverse('admin:app_settings', kwargs={'app_id': self.app.id})
        request = APIRequestFactory().post(url, data)
        request.user = UserF.create()
        response = view(request, app_id=self.app.id).render()
        self.assertEqual(response.status_code, 200)
        self.assertContains(
            response,
            'You are not the owner of this application and therefore not '
            'allowed to access this app.'
        )

        ref = Application.objects.get(pk=self.app.id)
        self.assertNotEqual(ref.name, data.get('name'))
        self.assertNotEqual(ref.description, data.get('description'))
        self.assertNotEqual(ref.download_url, data.get('download_url'))
        self.assertNotEqual(ref.redirect_uris, data.get('redirect_uris'))
        self.assertNotEqual(
            ref.authorization_grant_type,
            data.get('authorization_grant_type')
        )
Example #17
0
 def test_get_with_user(self):
     view = ApplicationOverview.as_view()
     url = reverse('admin:app_overview')
     request = APIRequestFactory().get(url)
     request.user = UserF.create()
     response = view(request).render()
     self.assertEqual(response.status_code, 200)
Example #18
0
 def setUp(self):
     self.user1 = UserF.create()
     self.user2 = UserF.create()
     self.app1 = ApplicationFactory(**{
         'user': self.user1
     })
     self.app2 = ApplicationFactory(**{
         'user': self.user1
     })
     self.app3 = ApplicationFactory(**{
         'user': self.user2
     })
     self.deleted_app = ApplicationFactory(**{
         'user': self.user1,
         'status': 'deleted'
     })
Example #19
0
    def test(self):
        admin = UserF.create()
        project = ProjectF(add_admins=[admin])
        observation = ObservationFactory.create(**{
            'project': project
        })
        comment = CommentFactory.create()

        factory = APIRequestFactory()
        request = factory.post(
            '/api/projects/%s/observations/%s/comments/' %
            (project.id, observation.id),
            {
                'text': 'Response to a comment',
                'respondsto': comment.id
            }
        )
        force_authenticate(request, user=admin)
        view = AllContributionsCommentsAPIView.as_view()
        response = view(
            request,
            project_id=project.id,
            observation_id=observation.id
        ).render()

        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(
            json.loads(response.content).get('error'),
            'The comment you try to respond to is not a comment to the '
            'observation.'
        )
Example #20
0
    def setUp(self):
        self.factory = APIRequestFactory()
        self.admin = UserF.create()
        self.contributor = UserF.create()
        self.view_member = UserF.create()
        self.non_member = UserF.create()

        self.project = ProjectF(
            add_admins=[self.admin],
            add_contributors=[self.contributor]
        )

        self.location = LocationFactory(**{
            'private': True,
            'private_for_project': self.project
        })
Example #21
0
 def test_for_moderator(self):
     observations = Observation.objects.all().for_moderator(UserF.create())
     self.assertEqual(len(observations), 10)
     for observation in observations:
         self.assertNotIn(
             observation.status,
             ['draft', 'deleted']
         )
Example #22
0
 def test_for_viewer(self):
     observations = Observation.objects.all().for_viewer(UserF.create())
     self.assertEqual(len(observations), 5)
     for observation in observations:
         self.assertNotIn(
             observation.status,
             ['draft', 'pending', 'deleted']
         )
Example #23
0
    def setUp(self):
        self.factory = APIRequestFactory()
        self.admin = UserF.create()
        self.contributor = UserF.create()
        self.non_member = UserF.create()

        self.project = ProjectF(
            add_admins=[self.admin],
            add_contributors=[self.contributor]
        )
        self.category = CategoryFactory(**{
            'status': 'active',
            'project': self.project
        })

        TextFieldFactory.create(**{
            'key': 'key_1',
            'category': self.category,
            'order': 0
        })
        NumericFieldFactory.create(**{
            'key': 'key_2',
            'category': self.category,
            'order': 1
        })

        location = LocationFactory()

        self.observation = ObservationFactory.create(**{
            'properties': {
                "key_1": "value 1",
                "key_2": 12,
            },
            'category': self.category,
            'project': self.project,
            'location': location,
            'creator': self.admin,
            'status': 'active'
        })

        self.update_data = {
            "properties": {
                "version": 1,
                "key_2": 15
            }
        }
Example #24
0
    def get_response(self, user):
        if user.is_anonymous and not User.objects.filter(
                display_name='AnonymousUser').exists():
            UserF.create(display_name='AnonymousUser')

        factory = APIRequestFactory()
        request = factory.post(
            '/api/projects/%s/maps/all-contributions/%s/comments/' %
            (self.project.id, self.observation.id),
            {'text': 'A comment to the observation'}
        )
        force_authenticate(request, user=user)
        view = AllContributionsCommentsAPIView.as_view()
        return view(
            request,
            project_id=self.project.id,
            observation_id=self.observation.id
        ).render()
Example #25
0
    def test_flag_with_anonymous(self):
        if not User.objects.filter(display_name='AnonymousUser').exists():
            UserF.create(display_name='AnonymousUser')

        url = reverse('api:project_single_observation', kwargs={
            'project_id': self.project.id,
            'observation_id': self.observation.id
        })
        request = self.factory.patch(url)
        request.data = {'meta': {'status': "pending"}}
        request.user = AnonymousUser()

        view = SingleContributionAPIView()
        view.update_and_respond(request, self.observation)
        self.assertEqual(
            Observation.objects.get(pk=self.observation.id).status,
            'active'
        )
Example #26
0
 def setUp(self):
     self.user = UserF.create()
     self.app = ApplicationFactory.create()
     self.token = AccessToken.objects.create(
         user=self.user,
         application=self.app,
         token='df0af6a395b4cd072445b3832e9379bfee257da0',
         scope=1,
         expires='2030-12-31T23:59:01+00:00'
     )
Example #27
0
 def setUp(self):
     self.creator = UserF.create()
     ObservationFactory.create_batch(
         5, **{'status': 'active', 'creator': self.creator})
     ObservationFactory.create_batch(
         5, **{'status': 'draft', 'creator': self.creator})
     ObservationFactory.create_batch(
         5, **{'status': 'pending', 'creator': self.creator})
     ObservationFactory.create_batch(
         5, **{'status': 'deleted', 'creator': self.creator})
Example #28
0
 def setUp(self):
     self.factory = APIRequestFactory()
     self.admin = UserF.create()
     self.creator = UserF.create()
     self.moderator = UserF.create()
     self.viewer = UserF.create()
     self.project = ProjectF(
         add_admins=[self.admin],
         add_contributors=[self.creator],
         add_viewer=[self.viewer]
     )
     self.moderators = UserGroupF(add_users=[self.moderator], **{
         'project': self.project,
         'can_moderate': True
     })
     self.observation = ObservationFactory.create(**{
         'project': self.project,
         'creator': self.creator,
         'status': 'active'
     })
Example #29
0
 def test_access_inactive_with_admin(self):
     admin = UserF.create()
     project = ProjectF.create(
         add_admins=[admin],
         **{'isprivate': True}
     )
     inactive_type = CategoryFactory(**{
         'project': project,
         'status': 'inactive'
     })
     self.assertEqual(inactive_type, Category.objects.get_single(
         admin, project.id, inactive_type.id))
Example #30
0
    def test_admin_access_with_non_member(self):
        user = UserF.create()

        project = ProjectF.create(**{
            'isprivate': True
        })

        active_type = CategoryFactory(**{
            'project': project
        })

        Category.objects.as_admin(user, project.id, active_type.id)