def setUp(self): self.contributor = UserFactory.create() self.admin = UserFactory.create() self.non_member = UserFactory.create() self.project = ProjectFactory( add_admins=[self.admin], add_contributors=[self.contributor] ) self.contribution = ObservationFactory.create(**{ 'project': self.project, 'creator': self.contributor }) comment = CommentFactory.create(**{ 'commentto': self.contribution }) response = CommentFactory.create(**{ 'commentto': self.contribution, 'respondsto': comment }) CommentFactory.create(**{ 'commentto': self.contribution, 'respondsto': response }) CommentFactory.create(**{ 'commentto': self.contribution, 'respondsto': comment }) CommentFactory.create(**{ 'commentto': self.contribution })
def test_validate_full_invalid(self): creator = UserFactory() location = LocationFactory() category = CategoryFactory() TextFieldFactory.create(**{ 'key': 'text', 'category': category, 'order': 0 }) NumericFieldFactory.create(**{ 'key': 'number', 'category': category, 'order': 1 }) data = {'text': 'Text', 'number': 12} observation = Observation.create(properties=data, creator=creator, location=location, category=category, project=category.project, status='active') updater = UserFactory() update = {'text': 'Udpated Text', 'number': 'abc', 'version': 1} Observation.validate_full(category=category, data=update) observation.update(properties=update, updator=updater) self.assertEqual(observation.properties, data) self.assertEqual(observation.version, 1)
def post(self, user, data=None): if user.is_anonymous and not User.objects.filter( display_name='AnonymousUser').exists(): UserFactory.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 = MediaAPIView.as_view() return view( request, project_id=self.project.id, contribution_id=self.contribution.id ).render()
def setUp(self): self.factory = APIRequestFactory() self.admin = UserFactory.create() self.contributor = UserFactory.create() self.project = ProjectFactory.create( add_admins=[self.admin], add_contributors=[self.contributor])
def setUp(self): self.factory = APIRequestFactory() self.admin = UserFactory.create() self.contributor = UserFactory.create() self.non_member = UserFactory.create() self.project = ProjectFactory( add_admins=[self.admin], add_contributors=[self.contributor] ) self.other_project = ProjectFactory.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 })
def test_update_draft_observation(self): creator = UserFactory() location = LocationFactory() category = CategoryFactory() TextFieldFactory.create(**{ 'key': 'text', 'category': category, 'required': True, 'order': 0 }) NumericFieldFactory.create(**{ 'key': 'number', 'category': category, 'order': 1 }) data = {'number': 12} observation = Observation.create(properties=data, creator=creator, location=location, category=category, project=category.project, status='draft') updater = UserFactory() update = {'number': 13} observation.update(properties=update, updator=updater, status='draft') self.assertEqual(observation.properties.get('number'), 13) self.assertEqual(observation.version, 1)
def setUp(self): self.factory = APIRequestFactory() self.admin = UserFactory.create() self.creator = UserFactory.create() self.moderator = UserFactory.create() self.commenter = UserFactory.create() self.project = ProjectFactory( add_admins=[self.admin], add_contributors=[self.creator, self.commenter]) self.moderators = UserGroupFactory(add_users=[self.moderator], **{ 'project': self.project, 'can_moderate': True }) self.contribution = ObservationFactory.create(**{ 'project': self.project, 'creator': self.creator, 'status': 'review' }) self.comment = CommentFactory.create( **{ 'commentto': self.contribution, 'creator': self.commenter, 'review_status': 'open' })
def post(self, user, data=None): if user.is_anonymous and not User.objects.filter( display_name='AnonymousUser').exists(): UserFactory.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()
def setUp(self): self.contributor = UserFactory.create() self.admin = UserFactory.create() self.non_member = UserFactory.create() self.project = ProjectFactory( add_admins=[self.admin], add_contributors=[self.contributor], **{"isprivate": False} ) self.contribution = ObservationFactory.create(**{"project": self.project, "creator": self.contributor})
def setUp(self): self.factory = APIRequestFactory() self.admin = UserFactory.create() self.contributor = UserFactory.create() self.project = ProjectFactory.create( add_admins=[self.admin], add_contributors=[self.contributor] )
def setUp(self): self.admin = UserFactory.create() self.creator = UserFactory.create() self.project = ProjectFactory(add_admins=[self.admin], add_contributors=[self.creator]) self.contribution = ObservationFactory.create(**{ 'project': self.project, 'creator': self.creator })
def setUp(self): """Set up test.""" self.superuser = UserFactory.create(**{'is_superuser': True}) self.admin = UserFactory.create(**{'is_superuser': False}) self.contributor = UserFactory.create(**{'is_superuser': False}) self.regular_user = UserFactory.create(**{'is_superuser': False}) self.project = ProjectFactory.create( add_admins=[self.admin], add_contributors=[self.contributor], **{'isprivate': True})
def setUp(self): self.admin = UserFactory.create() self.creator = UserFactory.create() self.project = ProjectFactory( add_admins=[self.admin], add_contributors=[self.creator] ) self.contribution = ObservationFactory.create(**{ 'project': self.project, 'creator': self.creator })
def setUp(self): self.contributor = UserFactory.create() self.admin = UserFactory.create() self.non_member = UserFactory.create() self.project = ProjectFactory(add_admins=[self.admin], add_contributors=[self.contributor], **{'isprivate': False}) self.contribution = ObservationFactory.create( **{ 'project': self.project, 'creator': self.contributor })
def _post(self, data, user): if user.is_anonymous and not User.objects.filter( display_name='AnonymousUser').exists(): UserFactory.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()
def get_response(self, user): if user.is_anonymous and not User.objects.filter(display_name="AnonymousUser").exists(): UserFactory.create(display_name="AnonymousUser") factory = APIRequestFactory() request = factory.post( "/api/projects/%s/maps/all-contributions/%s/comments/" % (self.project.id, self.contribution.id), {"text": "A comment to the contribution."}, ) force_authenticate(request, user=user) view = CommentsAPIView.as_view() return view(request, project_id=self.project.id, contribution_id=self.contribution.id).render()
def setUp(self): self.factory = APIRequestFactory() self.admin = UserFactory.create() self.contributor = UserFactory.create() self.non_member = UserFactory.create() self.project = ProjectFactory( 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 } }
def setUp(self): self.contributor = UserFactory.create() self.admin = UserFactory.create() self.non_member = UserFactory.create() self.project = ProjectFactory( add_admins=[self.admin], add_contributors=[self.contributor], **{'isprivate': False} ) self.observation = ObservationFactory.create(**{ 'project': self.project, 'creator': self.contributor })
def test_get_isowner(self): user = UserFactory.create() comment = CommentFactory.create(**{'creator': user}) serializer = CommentSerializer(comment, context={'user': user}) self.assertTrue(serializer.get_isowner(comment)) serializer = CommentSerializer(comment, context={'user': UserFactory.create()}) self.assertFalse(serializer.get_isowner(comment)) serializer = CommentSerializer(comment, context={'user': AnonymousUser()}) self.assertFalse(serializer.get_isowner(comment))
def setUp(self): self.factory = APIRequestFactory() self.admin = UserFactory.create() self.creator = UserFactory.create() self.moderator = UserFactory.create() self.commenter = UserFactory.create() self.project = ProjectFactory(add_admins=[self.admin], add_contributors=[self.creator, self.commenter]) self.moderators = UserGroupFactory( add_users=[self.moderator], **{"project": self.project, "can_moderate": True} ) self.contribution = ObservationFactory.create( **{"project": self.project, "creator": self.creator, "status": "active"} ) self.comment = CommentFactory.create(**{"commentto": self.contribution, "creator": self.commenter})
def setUp(self): self.factory = APIRequestFactory() self.admin = UserFactory.create() self.creator = UserFactory.create() self.viewer = UserFactory.create() self.project = ProjectFactory(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})
def test_get_isowner(self): user = UserFactory.create() comment = CommentFactory.create(**{'creator': user}) serializer = CommentSerializer(comment, context={'user': user}) self.assertTrue(serializer.get_isowner(comment)) serializer = CommentSerializer( comment, context={'user': UserFactory.create()}) self.assertFalse(serializer.get_isowner(comment)) serializer = CommentSerializer( comment, context={'user': AnonymousUser()}) self.assertFalse(serializer.get_isowner(comment))
def setUp(self): self.factory = APIRequestFactory() self.admin = UserFactory.create() self.contributor = UserFactory.create() self.view_member = UserFactory.create() self.non_member = UserFactory.create() self.project = ProjectFactory(add_admins=[self.admin], add_contributors=[self.contributor]) self.location = LocationFactory(**{ 'private': True, 'private_for_project': self.project })
def get_response(self, user): if user.is_anonymous and not User.objects.filter( display_name='AnonymousUser').exists(): UserFactory.create(display_name='AnonymousUser') factory = APIRequestFactory() request = factory.post( '/api/projects/%s/maps/all-contributions/%s/comments/' % (self.project.id, self.contribution.id), {'text': 'A comment to the contribution.'}) force_authenticate(request, user=user) view = CommentsAPIView.as_view() return view(request, project_id=self.project.id, contribution_id=self.contribution.id).render()
def setUp(self): self.factory = APIRequestFactory() self.admin = UserFactory.create() self.creator = UserFactory.create() self.viewer = UserFactory.create() self.project = ProjectFactory( 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})
def _post(self, data, user): if user.is_anonymous and not User.objects.filter( display_name='AnonymousUser').exists(): UserFactory.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()
def test_get_with_user(self): view = ApplicationOverview.as_view() url = reverse('admin:app_overview') request = APIRequestFactory().get(url) request.user = UserFactory.create() response = view(request).render() self.assertEqual(response.status_code, 200)
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 = UserFactory.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') )
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 = UserFactory.create() response = view(request, app_id=self.app.id) self.assertTrue(isinstance(response, HttpResponseRedirect))
def test(self): admin = UserFactory.create() project = ProjectFactory(add_admins=[admin]) observation = ObservationFactory.create(**{ 'project': project }) comment = CommentFactory.create(**{ 'commentto': observation }) 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_201_CREATED) self.assertEqual( json.loads(response.content).get('respondsto'), comment.id )
def setUp(self): self.factory = APIRequestFactory() self.admin = UserFactory.create() self.contributor = UserFactory.create() self.view_member = UserFactory.create() self.non_member = UserFactory.create() self.project = ProjectFactory( add_admins=[self.admin], add_contributors=[self.contributor] ) self.location = LocationFactory(**{ 'private': True, 'private_for_project': self.project })
def test_update_observation(self): category = CategoryFactory() TextFieldFactory(**{'key': 'text', 'category': category, 'order': 0}) NumericFieldFactory(**{ 'key': 'number', 'category': category, 'order': 1 }) observation = ObservationFactory.create( **{ 'properties': { 'text': 'Text', 'number': 12 }, 'category': category, 'project': category.project }) updater = UserFactory() update = {'text': 'Udpated Text', 'number': 13} observation.update(properties=update, updator=updater) # ref_observation = Observation.objects.get(pk=observation.id) self.assertEqual(observation.properties, { 'text': 'Udpated Text', 'number': 13 }) self.assertEqual(observation.version, 2)
def test_for_viewer(self): observations = Observation.objects.all().for_viewer( UserFactory.create()) self.assertEqual(len(observations), 5) for observation in observations: self.assertNotIn(observation.status, ['draft', 'pending', 'deleted'])
def setUp(self): self.user1 = UserFactory.create() self.user2 = UserFactory.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' })
def test(self): admin = UserFactory.create() project = ProjectFactory(add_admins=[admin]) contribution = ObservationFactory.create(**{ 'project': project }) comment = CommentFactory.create() factory = APIRequestFactory() request = factory.post( '/api/projects/%s/contributions/%s/comments/' % (project.id, contribution.id), { 'text': 'Response to a comment', 'respondsto': comment.id } ) force_authenticate(request, user=admin) view = CommentsAPIView.as_view() response = view( request, project_id=project.id, contribution_id=contribution.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 ' 'contribution.' )
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', 'skip_authorization': True, } view = ApplicationSettings.as_view() url = reverse('admin:app_settings', kwargs={'app_id': self.app.id}) request = APIRequestFactory().post(url, data) request.user = UserFactory.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'))
def test_access_fields_with_non_member(self): user = UserFactory.create() project = ProjectFactory.create(**{'isprivate': True}) category = CategoryFactory(**{'project': project, 'status': 'active'}) TextFieldFactory.create(**{'status': 'active', 'category': category}) TextFieldFactory.create(**{'status': 'inactive', 'category': category}) Field.objects.get_list(user, project.id, category.id)
def test_admin_access_with_non_member(self): user = UserFactory.create() project = ProjectFactory.create(**{'isprivate': True}) active_type = CategoryFactory(**{'project': project}) Category.objects.as_admin(user, project.id, active_type.id)
def test_get_contribution_with_some_dude(self): some_dude = UserFactory.create() view = SingleCommentAPIView() view.get_contribution( some_dude, self.project.id, self.contribution.id )
def setUp(self): self.factory = APIRequestFactory() self.admin = UserFactory.create() self.contributor = UserFactory.create() self.non_member = UserFactory.create() self.project = ProjectFactory( 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 } }
def test_flag_with_anonymous(self): if not User.objects.filter(display_name='AnonymousUser').exists(): UserFactory.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')
def test_access_with_projct_non_member(self): contributor = UserFactory.create() project = ProjectFactory.create() CategoryFactory(**{'project': project, 'status': 'active'}) CategoryFactory(**{'project': project, 'status': 'inactive'}) Category.objects.get_list(contributor, project.id)
def test_for_viewer(self): observations = Observation.objects.all().for_viewer( UserFactory.create()) self.assertEqual(len(observations), 5) for observation in observations: self.assertNotIn( observation.status, ['draft', 'pending', 'deleted'] )
def test_flag_with_anonymous(self): if not User.objects.filter(display_name='AnonymousUser').exists(): UserFactory.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' )
def get_response(self, user): if user.is_anonymous and not User.objects.filter( display_name='AnonymousUser').exists(): UserFactory.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()
def setUp(self): self.factory = APIRequestFactory() self.admin = UserFactory.create() self.creator = UserFactory.create() self.viewer = UserFactory.create() self.project = ProjectFactory.create( add_admins=[self.admin], add_contributors=[self.creator], **{'isprivate': False} ) self.contribution = ObservationFactory.create( **{'project': self.project, 'creator': self.creator} ) self.image_file = ImageFileFactory.create( **{'contribution': self.contribution, 'creator': self.creator} )
def test_access_fields_with_admin(self): admin = UserFactory.create() project = ProjectFactory.create(add_admins=[admin], **{'isprivate': True}) category = CategoryFactory(**{'project': project, 'status': 'active'}) TextFieldFactory.create(**{'status': 'active', 'category': category}) TextFieldFactory.create(**{'status': 'inactive', 'category': category}) self.assertEqual( len(Field.objects.get_list(admin, project.id, category.id)), 2)
def test_admin_access_active_field_with_non_member(self): user = UserFactory.create() project = ProjectFactory.create(**{'isprivate': True}) category = CategoryFactory(**{'project': project, 'status': 'active'}) field = TextFieldFactory.create(**{ 'status': 'active', 'category': category }) Field.objects.as_admin(user, project.id, category.id, field.id)
def setUp(self): self.creator = UserFactory.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})
def setUp(self): self.user = UserFactory.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' )
def setUp(self): self.factory = APIRequestFactory() self.admin = UserFactory.create() self.creator = UserFactory.create() self.moderator = UserFactory.create() self.viewer = UserFactory.create() self.project = ProjectFactory( add_admins=[self.admin], add_contributors=[self.creator], add_viewer=[self.viewer] ) self.moderators = UserGroupFactory(add_users=[self.moderator], **{ 'project': self.project, 'can_moderate': True }) self.observation = ObservationFactory.create(**{ 'project': self.project, 'creator': self.creator, 'status': 'active' })