def test_get_context_data(self): user = UserF.create(**{'is_superuser': True}) ProjectF.create_batch(5, add_admins=[user]) ProjectF.create_batch(5) view = ProjectsList() context = view.get_context_data() self.assertEqual(len(context.get('projects')), 10)
def test_get_update_projects(self): # Setup everything updated_project_1 = ProjectF.create() updated_project_2 = ProjectF.create() not_updated_project = ProjectF.create() for x in range(0, 2): for project in [ updated_project_1, updated_project_2, not_updated_project ]: ObservationFactory.create(project=project) updated = ObservationFactory.create(project=updated_project_2) yesterday = datetime.utcnow().replace(tzinfo=utc) ObservationFactory.create(project=updated_project_1) updated.update(properties={'key': 'value'}, updator=UserF.create()) # the tests command = Command() updated_projects = command.get_updated_projects(yesterday) self.assertIn(updated_project_1, updated_projects) self.assertIn(updated_project_2, updated_projects) self.assertNotIn(not_updated_project, updated_projects)
def test_post_with_admin(self): user = UserF.create() project_1 = ProjectF.create(**{'creator': user}) project_2 = ProjectF.create(**{'creator': user}) data = { str(project_1.id): 'on' } view = UserNotifications.as_view() url = reverse('admin:notifications') request = APIRequestFactory().post(url, data) request.user = user from django.contrib.messages.storage.fallback import FallbackStorage setattr(request, 'session', 'session') messages = FallbackStorage(request) setattr(request, '_messages', messages) response = view(request).render() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue( Admins.objects.get(project=project_1, user=user).contact ) self.assertFalse( Admins.objects.get(project=project_2, user=user).contact )
def setUp(self): self.creator = UserF.create() self.admin = UserF.create() self.view_member = UserF.create() self.contributor = UserF.create() ProjectF.create(add_admins=[self.admin], add_contributors=[self.contributor]) ProjectF.create(add_admins=[self.admin, self.contributor])
def setUp(self): self.admin = UserF.create() self.project1 = ProjectF(add_admins=[self.admin]) self.project2 = ProjectF(add_admins=[self.admin]) self.public_location = LocationFactory(**{'private': False}) self.private_location = LocationFactory(**{'private': True}) self.private_for_project_location = LocationFactory( **{ 'private': True, 'private_for_project': self.project1 })
def setUp(self): self.creator = UserF.create() self.admin = UserF.create() self.view_member = UserF.create() self.contributor = UserF.create() ProjectF.create( add_admins=[self.admin], add_contributors=[self.contributor] ) ProjectF.create( add_admins=[self.admin, self.contributor] )
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})
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 } }
def test_post_with_admin_no_rules(self): view = RuleSettings.as_view() project = ProjectF.create() category = CategoryFactory.create(**{'project': project}) grouping = GroupingFactory.create(**{'project': project}) rule = RuleFactory.create(**{'grouping': grouping}) data = {'category': category.id} url = reverse('admin:rule_settings', kwargs={ 'project_id': project.id, 'grouping_id': grouping.id, 'rule_id': rule.id }) request = APIRequestFactory().post(url, data) request.user = project.creator from django.contrib.messages.storage.fallback import FallbackStorage setattr(request, 'session', 'session') messages = FallbackStorage(request) setattr(request, '_messages', messages) response = view(request, project_id=project.id, grouping_id=grouping.id, rule_id=rule.id) self.assertEqual(response.status_code, 302)
def test_get_with_admin(self): view = RuleSettings.as_view() project = ProjectF.create() grouping = GroupingFactory.create(**{'project': project}) rule = RuleFactory.create(**{'grouping': grouping}) url = reverse('admin:rule_settings', kwargs={ 'project_id': project.id, 'grouping_id': grouping.id, 'rule_id': rule.id }) request = APIRequestFactory().get(url) request.user = project.creator response = view(request, project_id=project.id, grouping_id=grouping.id, rule_id=rule.id).render() self.assertEqual(response.status_code, 200) self.assertNotContains( response, 'You are not member of the administrators group of this project ' 'and therefore not allowed to alter the settings of the project')
def test_get_update_stats(self): moderator = UserF.create() contributor = UserF.create() some_dude = UserF.create() project = ProjectF.create(add_admins=[moderator], add_contributors=[contributor]) suspended = ObservationFactory.create( created_at=(datetime.utcnow() - timedelta(2)).replace(tzinfo=utc), project=project, creator=contributor, status='active') updated = ObservationFactory.create(project=project, creator=contributor) reported = ObservationFactory.create(project=project, creator=contributor) approved = ObservationFactory.create(project=project, creator=contributor, status='pending') yesterday = datetime.utcnow().replace(tzinfo=utc) suspended.update(properties=None, status='pending', updator=UserF.create()) reported.update(properties=None, status='review', updator=UserF.create()) updated.update(properties={'key': 'value'}, updator=UserF.create()) approved.update(properties=None, status='active', updator=moderator) for user in [moderator, contributor]: ObservationFactory.create(project=project, creator=user, status='pending') command = Command() report = command.get_updated_items(project, moderator, yesterday) to_moderate = report.get('to_moderate') self.assertEqual(len(to_moderate.get('new')), 2) self.assertEqual(len(to_moderate.get('suspended')), 1) self.assertIsNone(report.get('yours')) report = command.get_updated_items(project, contributor, yesterday) yours = report.get('yours') self.assertEqual(len(yours.get('changed')), 4) self.assertEqual(len(yours.get('approved')), 1) self.assertEqual(len(yours.get('reported')), 1) self.assertEqual(len(yours.get('suspended')), 1) report = command.get_updated_items(project, some_dude, yesterday) self.assertEqual(report, None)
def test_post_non_existing_with_admin(self): """ Update the view with project admin should render an error message """ project = ProjectF.create() user = project.creator self.request.user = user self.request.method = 'POST' self.request.POST = { 'name': 'Name', 'description': 'Description', } response = self.view( self.request, project_id=project.id, subset_id=634842156456).render() rendered = render_to_string( 'subsets/subsets_settings.html', { 'error_description': 'Subset matching query does not exist.', 'error': 'Not found.', 'user': user, 'PLATFORM_NAME': get_current_site(self.request).name, 'GEOKEY_VERSION': version.get_version() } ) self.assertEqual(response.status_code, 200) self.assertEqual(response.content.decode('utf-8'), rendered)
def test_post_with_admin(self): """ Updating with project admin should create the subset and redirect to the subset data page. """ self.request.method = 'POST' self.request.POST = { 'name': 'Name', 'description': 'Description', } project = ProjectF.create() user = project.creator self.request.user = user response = self.view(self.request, project_id=project.id) self.assertEqual(1, Subset.objects.count()) subset = Subset.objects.first() self.assertEqual(subset.name, 'Name') self.assertEqual(subset.description, 'Description') self.assertEqual(subset.project, project) self.assertEqual(subset.creator, user) self.assertEqual(response.status_code, 302) self.assertIn( '/admin/projects/%s/subsets/%s/' % (project.id, subset.id), response['location'] )
def test_post_with_user(self): """ Updating with normal user should render the page with an error message """ self.request.method = 'POST' self.request.POST = { 'name': 'Name', 'description': 'Description', } user = UserF.create() project = ProjectF.create() self.request.user = user response = self.view(self.request, project_id=project.id).render() rendered = render_to_string( 'subsets/subsets_create.html', { 'error_description': 'Project matching query does not exist.', 'error': 'Not found.', 'user': user, 'PLATFORM_NAME': get_current_site(self.request).name, 'GEOKEY_VERSION': version.get_version() } ) self.assertEqual(response.status_code, 200) self.assertEqual(response.content.decode('utf-8'), rendered) self.assertEqual(0, Subset.objects.count())
def test_single_lookup(self): project = ProjectF.create() category = CategoryFactory.create(**{'project': project}) lookup = LookupFieldFactory.create( **{'category': category, 'key': 'lookup'} ) kermit = LookupValueFactory.create(**{ 'field': lookup, 'name': 'Kermit' }) gonzo = LookupValueFactory.create(**{ 'field': lookup, 'name': 'Gonzo' }) ObservationFactory.create_batch(3, **{ 'project': project, 'category': category, 'properties': {'lookup': kermit.id} }) ObservationFactory.create_batch(3, **{ 'project': project, 'category': category, 'properties': {'lookup': gonzo.id} }) result = project.observations.all().search('kermit') self.assertEqual(len(result), 3) for o in result: self.assertEqual(o.properties.get('lookup'), kermit.id)
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 })
def test_get_viewgroup(self): project = ProjectF.create() grouping = GroupingFactory.create(**{'project': project}) group = UserGroupF(**{'project': project}) html = get_view_group(grouping, group) self.assertEqual( html, '<li>' '<button type="button" name="%s" class="btn btn-default ' 'pull-right grant-single" data-toggle="button" >' '<span class="text-success">Grant access</span></button>' '<strong>%s</strong><p>%s</p>' '</li>' % (grouping.id, grouping.name, grouping.description)) GroupingUserGroupFactory.create(**{ 'usergroup': group, 'grouping': grouping }) html = get_view_group(grouping, group) self.assertEqual( html, '<li>' '<button type="button" name="%s" class="btn btn-default ' 'pull-right active grant-single" data-toggle="button" >' '<span class="text-danger">Revoke access</span></button>' '<strong>%s</strong><p>%s</p>' '</li>' % (grouping.id, grouping.name, grouping.description)) project.isprivate = False project.save() grouping.isprivate = False grouping.save() html = get_view_group(grouping, group) self.assertIn('This data grouping is public.', html)
def test_delete_with_admin(self): view = RuleDelete.as_view() project = ProjectF.create() 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 = project.creator from django.contrib.messages.storage.fallback import FallbackStorage setattr(request, 'session', 'session') messages = FallbackStorage(request) setattr(request, '_messages', messages) response = view(request, project_id=project.id, grouping_id=grouping.id, rule_id=rule.id) self.assertEqual(response.status_code, 302) self.assertEqual(Rule.objects.count(), 0)
def test(self): admin = UserF.create() project = ProjectF.create(add_admins=[admin]) category_1 = CategoryFactory(**{'project': project}) TextFieldFactory(**{'key': 'text', 'category': category_1}) category_2 = CategoryFactory(**{'project': project}) TextFieldFactory(**{'key': 'bla', 'category': category_2}) ObservationFactory.create_batch( 5, **{ 'project': project, 'category': category_1, 'properties': { 'text': 'yes' } }) viewer = UserF.create() view = GroupingFactory(add_viewers=[viewer], **{'project': project}) RuleFactory( **{ 'grouping': view, 'category': category_1, 'constraints': { 'text': 'yes' } }) serializer = GroupingSerializer(view, context={'user': admin}) self.assertEqual(len(serializer.get_data(view).get('features')), 5) self.assertEqual(serializer.get_num_contributions(view), 5)
def test_access_fields_with_non_member(self): user = UserF.create() project = ProjectF.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_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')
def test(self): admin = UserF.create() project = ProjectF.create(add_admins=[admin]) category_1 = CategoryFactory(**{'project': project}) TextFieldFactory(**{ 'key': 'text', 'category': category_1 }) category_2 = CategoryFactory(**{'project': project}) TextFieldFactory(**{ 'key': 'bla', 'category': category_2 }) ObservationFactory.create_batch(5, **{ 'project': project, 'category': category_1, 'properties': {'text': 'yes'}} ) viewer = UserF.create() view = GroupingFactory(add_viewers=[viewer], **{'project': project}) RuleFactory(**{ 'grouping': view, 'category': category_1, 'constraints': {'text': 'yes'} }) serializer = GroupingSerializer(view, context={'user': admin}) self.assertEqual(len(serializer.get_data(view).get('features')), 5) self.assertEqual(serializer.get_num_contributions(view), 5)
def setUp(self): self.factory = APIRequestFactory() self.admin = UserF.create() self.creator = UserF.create() self.moderator = UserF.create() self.commenter = UserF.create() self.project = ProjectF( add_admins=[self.admin], add_contributors=[self.creator, self.commenter]) self.moderators = UserGroupF(add_users=[self.moderator], **{ 'project': self.project, 'can_moderate': True }) self.observation = ObservationFactory.create(**{ 'project': self.project, 'creator': self.creator, 'status': 'review' }) self.comment = CommentFactory.create( **{ 'commentto': self.observation, 'creator': self.commenter, 'review_status': 'open' })
def test_create_with_wrong_location(self): project = ProjectF() location = LocationFactory(**{ 'private': True, 'private_for_project': project }) data = { "properties": { "key_1": "value 1", "key_2": 12 }, "meta": { "category": self.category.id }, "location": { "id": location.id, "geometry": location.geometry.geojson } } serializer = ContributionSerializer(data=data, context={ 'user': self.contributor, 'project': self.project }) serializer.is_valid(raise_exception=True)
def test_update_where_clause(self): project = ProjectF.create() cat_1 = CategoryFactory.create(**{'project': project}) cat_2 = CategoryFactory.create(**{'project': project}) usergroup = UserGroupF.create(**{'project': project}) usergroup.filters = { cat_1.id: {}, cat_2.id: {} } usergroup.save() self.assertIn( UserGroup.objects.get(pk=usergroup.id).where_clause, [ '((category_id = %s)) OR ((category_id = %s))' % ( cat_2.id, cat_1.id ), '((category_id = %s)) OR ((category_id = %s))' % ( cat_1.id, cat_2.id ) ] ) usergroup.filters = {} usergroup.save() self.assertEqual( UserGroup.objects.get(pk=usergroup.id).where_clause, 'FALSE' )
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 })
def test_get_data_combined(self): user = UserF.create() project = ProjectF.create(add_admins=[user]) category_1 = CategoryFactory(**{'project': project}) category_2 = CategoryFactory(**{'project': project}) category_3 = CategoryFactory(**{'project': project}) view = GroupingFactory(**{'project': project}) for x in range(0, 5): ObservationFactory.create(**{ 'project': project, 'category': category_1} ) ObservationFactory.create(**{ 'project': project, 'category': category_2} ) ObservationFactory.create(**{ 'project': project, 'category': category_3} ) RuleFactory(**{ 'grouping': view, 'category': category_1 }) RuleFactory(**{ 'grouping': view, 'category': category_2 }) self.assertEqual(view.data(user).count(), 10) for observation in view.data(user): self.assertNotEqual( observation.category, category_3)
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])
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' )
def test_get_with_admin(self): view = RuleSettings.as_view() project = ProjectF.create() grouping = GroupingFactory.create(**{'project': project}) rule = RuleFactory.create(**{'grouping': grouping}) url = reverse( 'admin:rule_settings', kwargs={ 'project_id': project.id, 'grouping_id': grouping.id, 'rule_id': rule.id } ) request = APIRequestFactory().get(url) request.user = project.creator response = view( request, project_id=project.id, grouping_id=grouping.id, rule_id=rule.id).render() self.assertEqual(response.status_code, 200) self.assertNotContains( response, 'You are not member of the administrators group of this project ' 'and therefore not allowed to alter the settings of the project' )
def test_post_with_admin_no_rules(self): view = RuleSettings.as_view() project = ProjectF.create() category = CategoryFactory.create(**{'project': project}) grouping = GroupingFactory.create(**{'project': project}) rule = RuleFactory.create(**{'grouping': grouping}) data = { 'category': category.id } url = reverse( 'admin:rule_settings', kwargs={ 'project_id': project.id, 'grouping_id': grouping.id, 'rule_id': rule.id } ) request = APIRequestFactory().post(url, data) request.user = project.creator from django.contrib.messages.storage.fallback import FallbackStorage setattr(request, 'session', 'session') messages = FallbackStorage(request) setattr(request, '_messages', messages) response = view( request, project_id=project.id, grouping_id=grouping.id, rule_id=rule.id) self.assertEqual(response.status_code, 302)
def test_post_with_admin(self): view = RuleCreate.as_view() project = ProjectF.create() category = CategoryFactory.create(**{'project': project}) grouping = GroupingFactory.create(**{'project': project}) data = { 'category': category.id, 'rules': json.dumps({ 'text': 'blah', 'min_date': '2015-01-01', 'max_date': '2015-10-01' }) } url = reverse( 'admin:rule_create', kwargs={'project_id': project.id, 'grouping_id': grouping.id} ) request = APIRequestFactory().post(url, data) request.user = project.creator from django.contrib.messages.storage.fallback import FallbackStorage setattr(request, 'session', 'session') messages = FallbackStorage(request) setattr(request, '_messages', messages) response = view( request, project_id=project.id, grouping_id=grouping.id) self.assertEqual(response.status_code, 302) self.assertEqual(Rule.objects.count(), 1)
def test_post_with_admin(self): data = { 'name': 'Grouping', 'description': '' } view = GroupingSettings.as_view() project = ProjectF.create() grouping = GroupingFactory.create(**{'project': project}) url = reverse( 'admin:grouping_settings', kwargs={'project_id': project.id, 'grouping_id': grouping.id} ) request = APIRequestFactory().post(url, data) request.user = project.creator from django.contrib.messages.storage.fallback import FallbackStorage setattr(request, 'session', 'session') messages = FallbackStorage(request) setattr(request, '_messages', messages) response = view( request, project_id=project.id, grouping_id=grouping.id) self.assertEqual(response.status_code, 200)
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})
def test_contribute_with_wrong_project_location(self): project = ProjectF() location = LocationFactory(**{ 'private': True, 'private_for_project': project }) data = { "type": "Feature", "geometry": location.geometry.geojson, "location": { "id": location.id, "name": location.name, "description": location.description, "private": location.private }, "properties": { "key_1": "value 1", "key_2": 12 }, "meta": { "category": self.category.id, } } response = self._post(data, self.admin) self.assertEqual(response.status_code, 400)
def test_delete_with_admin(self): view = RuleDelete.as_view() project = ProjectF.create() 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 = project.creator from django.contrib.messages.storage.fallback import FallbackStorage setattr(request, 'session', 'session') messages = FallbackStorage(request) setattr(request, '_messages', messages) response = view( request, project_id=project.id, grouping_id=grouping.id, rule_id=rule.id) self.assertEqual(response.status_code, 302) self.assertEqual(Rule.objects.count(), 0)
def test_access_fields_with_admin(self): admin = UserF.create() project = ProjectF.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 setUp(self): self.admin = UserF.create() self.contributor = UserF.create() self.project = ProjectF.create( add_admins=[self.admin], add_contributors=[self.contributor] ) self.group = UserGroupF.create(**{'project': self.project})
def test_access_with_projct_non_member(self): contributor = UserF.create() project = ProjectF.create() CategoryFactory(**{'project': project, 'status': 'active'}) CategoryFactory(**{'project': project, 'status': 'inactive'}) Category.objects.get_list(contributor, project.id)
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)
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] )
def test_get_data_multiple_lookup_filter(self): user = UserF.create() project = ProjectF.create(add_admins=[user]) category_1 = CategoryFactory(**{'project': project}) lookup_field = MultipleLookupFieldFactory(**{ 'key': 'lookup', 'category': category_1 }) lookup_1 = MultipleLookupValueFactory(**{ 'name': 'Ms. Piggy', 'field': lookup_field }) lookup_2 = MultipleLookupValueFactory(**{ 'name': 'Kermit', 'field': lookup_field }) lookup_3 = MultipleLookupValueFactory(**{ 'name': 'Gonzo', 'field': lookup_field }) category_2 = CategoryFactory(**{'project': project}) lookup_field_2 = MultipleLookupFieldFactory(**{ 'key': 'bla', 'category': category_2 }) lookup_4 = MultipleLookupValueFactory(**{ 'name': 'Gonzo', 'field': lookup_field_2 }) for x in range(0, 5): ObservationFactory.create(**{ 'project': project, 'category': category_1, 'properties': {'lookup': [lookup_1.id, lookup_3.id]} }) ObservationFactory.create(**{ 'project': project, 'category': category_1, 'properties': {'lookup': [lookup_2.id, lookup_3.id]} }) ObservationFactory.create(**{ 'project': project, 'category': category_2, 'properties': {'bla': [lookup_4.id]} }) view = GroupingFactory(**{'project': project}) RuleFactory(**{ 'grouping': view, 'category': category_1, 'constraints': {'lookup': [lookup_1.id, lookup_2.id]} }) self.assertEqual(view.data(user).count(), 10)
def setUp(self): self.factory = RequestFactory() self.admin = UserF.create() self.contributor = UserF.create() self.non_member = UserF.create() self.project = ProjectF.create(add_admins=[self.admin], add_contributors=[self.contributor]) self.usergroup = UserGroupF(**{'project': self.project})
def test_delete_with_category_filter(self): project = ProjectF.create() category = CategoryFactory.create(**{"project": project}) category_2 = CategoryFactory.create(**{"project": project}) group = UserGroupF.create(**{"project": project, "filters": {category.id: {}, category_2.id: {}}}) category.delete() ref = UserGroup.objects.get(pk=group.id) self.assertEqual(ref.filters, {str(category_2.id): {}})
def test_get_context_data(self): view = GroupingList() project = ProjectF.create() url = reverse('admin:grouping_list', kwargs={'project_id': project.id}) request = APIRequestFactory().get(url) request.user = project.creator view.request = request context = view.get_context_data(project.id) self.assertEqual(context.get('project'), project)
def setUp(self): self.factory = RequestFactory() self.admin = UserF.create() self.contributor = UserF.create() self.non_member = UserF.create() self.project = ProjectF.create( add_admins=[self.admin], add_contributors=[self.contributor] ) self.usergroup = UserGroupF(**{'project': self.project})
def test_validate_location(self): project = ProjectF.create() serializer = ContributionSerializer(context={'user': project.creator}) serializer._errors = {} serializer.validate_location(project, 8271839172) self.assertIsNotNone(serializer._errors.get('location')) project = ProjectF.create() location = LocationFactory.create(**{'private': True}) serializer = ContributionSerializer(context={'user': project.creator}) serializer._errors = {} serializer.validate_location(project, location.id) self.assertIsNotNone(serializer._errors.get('location')) project = ProjectF.create() location = LocationFactory.create() serializer = ContributionSerializer(context={'user': project.creator}) serializer._errors = {} serializer.validate_location(project, location.id) self.assertEqual(serializer._errors, {})
def test_get_with_admin(self): project = ProjectF.create() view = GroupingCreate.as_view() url = reverse( 'admin:grouping_create', kwargs={'project_id': project.id} ) request = APIRequestFactory().get(url) request.user = project.creator response = view(request, project_id=project.id).render() self.assertEqual(response.status_code, 200)
def test_delete_with_field_filter(self): project = ProjectF.create() category = CategoryFactory.create(**{"project": project}) field = Field.create("name", "key", "description", False, category, "TextField") group = UserGroupF.create(**{"project": project, "filters": {category.id: {field.key: "blah"}}}) field.delete() ref = UserGroup.objects.get(pk=group.id) self.assertEqual(ref.filters, {str(category.id): {}})
def test_daily_digest(self): do_not_notify = UserF.create() moderator = UserF.create() contributor = UserF.create() UserF.create() project = ProjectF.create( add_admins=[moderator], add_contributors=[contributor] ) AdminsFactory.create(**{ 'project': project, 'user': do_not_notify, 'contact': False }) suspended = ObservationFactory.create( created_at=(datetime.utcnow() - timedelta(2)).replace(tzinfo=utc), project=project, creator=contributor, status='active' ) updated = ObservationFactory.create( project=project, creator=contributor ) approved = ObservationFactory.create( project=project, creator=contributor, status='pending' ) yesterday = datetime.utcnow().replace(tzinfo=utc) suspended.update( properties=None, status='pending', updator=UserF.create() ) updated.update(properties={'key': 'value'}, updator=UserF.create()) approved.update(properties=None, status='active', updator=moderator) for user in [moderator, contributor]: ObservationFactory.create( project=project, creator=user, status='pending' ) command = Command() command.send_daily_digest(yesterday) self.assertEquals(len(mail.outbox), 3)
def test_admin(self): user = UserF.create() project = ProjectF.create(add_admins=[user]) view = GroupingFactory.create(**{ 'project': project, 'isprivate': False }) self.assertTrue(view.can_view(user)) self.assertTrue(view.can_read(user)) self.assertTrue(view.can_moderate(user))
def test_create(self): project = ProjectF.create() Category.objects.create( name='Test', project=project, creator=project.creator, create_grouping=False ) views = project.groupings.all() self.assertEqual(len(views), 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))