def setup_models(self): self.project = ProjectFactory.create(current_questionnaire='a1') self.spatial_unit = SpatialUnitFactory(project=self.project) content_type = ContentType.objects.get( app_label='party', model='tenurerelationship') schema = Schema.objects.create( content_type=content_type, selectors=(self.project.organization.id, self.project.id, 'a1')) attr_type = AttributeType.objects.get(name='text') Attribute.objects.create( schema=schema, name='r_name', long_name='Relationship field', attr_type=attr_type, index=0, required=False, omit=False ) content_type = ContentType.objects.get( app_label='party', model='party') schema = Schema.objects.create( content_type=content_type, selectors=(self.project.organization.id, self.project.id, 'a1')) attr_type = AttributeType.objects.get(name='text') Attribute.objects.create( schema=schema, name='p_name', long_name='Party field', attr_type=attr_type, index=0, required=False, omit=False )
def setup_models(self): self.project = ProjectFactory.create() self.relationship = TenureRelationshipFactory.create( project=self.project) self.attached = ResourceFactory.create( project=self.project, content_object=self.relationship) self.unattached = ResourceFactory.create(project=self.project)
def setup_models(self): self.user = UserFactory.create() assign_policies(self.user) self.prj = ProjectFactory.create(slug='test-project', access='public') self.party1 = PartyFactory.create(project=self.prj, name='Landowner') self.su2 = SpatialUnitFactory.create(project=self.prj, type='PA')
def test_login_redirect_from_project_dashboard_to_org_dashboard(self): user = UserFactory.create() assign_user_policies(user, *[]) project = ProjectFactory.create() view = org_views.ProjectDashboard.as_view() request = HttpRequest() request.META['HTTP_REFERER'] = '/account/login/' setattr(request, 'user', user) setattr(request, 'method', 'GET') setattr(request, 'session', 'session') self.messages = FallbackStorage(request) setattr(request, '_messages', self.messages) kwargs = { 'organization': project.organization.slug, 'project': project.slug } def get_full_path(): return '/organizations/{}/projects/{}/'.format( project.organization.slug, project.slug ) setattr(request, 'get_full_path', get_full_path) exp_redirect = reverse('organization:dashboard', kwargs={ 'slug': project.organization.slug}) response = view(request, **kwargs) assert response.status_code == 302 assert exp_redirect == response['location']
def _test_serialize(self, https=False): form = self._get_form('xls-form') self.url = '/collect/' user = UserFactory.create() request = APIRequestFactory().get(self.url) force_authenticate(request, user=user) if https: request.META['SERVER_PROTOCOL'] = 'HTTPS/1.1' project = ProjectFactory.create() questionnaire = QuestionnaireSerializer( data={'xls_form': form}, context={'project': project} ) assert questionnaire.is_valid(raise_exception=True) is True questionnaire.save() form = Questionnaire.objects.get(filename__contains='xls-form') serializer = serializers.XFormListSerializer( form, context={'request': request}) url_refix = 'https' if https else 'http' assert serializer.data['formID'] == questionnaire.data['id_string'] assert serializer.data['name'] == questionnaire.data['title'] assert serializer.data['version'] == questionnaire.data['version'] assert (serializer.data['downloadUrl'] == url_refix + '://testserver' + reverse('form-download', args=[form.id])) assert serializer.data['hash'] == questionnaire.data['md5_hash']
def test_login_redirect_to_original_referer(self): user = UserFactory.create() project = ProjectFactory.create() view = LocationsAdd.as_view() request = HttpRequest() referer = '/organizations/{}/projects/{}'.format( project.organization.slug, project.slug ) request.META['HTTP_REFERER'] = referer setattr(request, 'user', user) setattr(request, 'method', 'GET') setattr(request, 'session', 'session') self.messages = FallbackStorage(request) setattr(request, '_messages', self.messages) kwargs = { 'organization': project.organization.slug, 'project': project.slug } response = view(request, **kwargs) assert response.status_code == 302 assert referer == response['location']
def test_get_values(self): project = ProjectFactory.create(current_questionnaire="123abc") exporter = Exporter(project) content_type = ContentType.objects.get(app_label="party", model="tenurerelationship") schema = Schema.objects.create( content_type=content_type, selectors=(project.organization.id, project.id, "123abc") ) text_type = AttributeType.objects.get(name="text") select_m_type = AttributeType.objects.get(name="select_multiple") attr = Attribute.objects.create( schema=schema, name="key", long_name="Test field", attr_type=text_type, index=0, required=False, omit=False ) attr2 = Attribute.objects.create( schema=schema, name="key_2", long_name="Test select multiple field", attr_type=select_m_type, index=1, choices=["choice_1", "choice_2", "choice_3"], choice_labels=["Choice 1", "Choice 2", "Choice 3"], required=False, omit=False, ) ttype = TenureRelationshipType.objects.get(id="LH") item = TenureRelationshipFactory.create( project=project, tenure_type=ttype, attributes={"key": "text", "key_2": ["choice_1", "choice_2"]} ) model_attrs = ("id", "party_id", "spatial_unit_id", "tenure_type.label") schema_attrs = [attr, attr2] values = exporter.get_values(item, model_attrs, schema_attrs) assert values == [item.id, item.party_id, item.spatial_unit_id, "Leasehold", "text", "choice_1, choice_2"]
def test_update_questionnaire_attribute_schema(self): file = self.get_file( '/questionnaires/tests/files/xls-form-attrs.xlsx', 'rb') form = self.storage.save('xls-forms/xls-form.xlsx', file) project = ProjectFactory.create(name='TestProject') q1 = models.Questionnaire.objects.create_from_form( xls_form=form, project=project ) q2 = models.Questionnaire.objects.create_from_form( xls_form=form, project=project ) assert 12 == Schema.objects.all().count() content_type = ContentType.objects.get( app_label='party', model='party') assert q1.id != q2.id s1 = Schema.objects.get( content_type=content_type, selectors=( project.organization.pk, project.pk, q1.id) ) assert s1 is not None s2 = Schema.objects.get( content_type=content_type, selectors=( project.organization.pk, project.pk, project.current_questionnaire) ) assert s2 is not None assert s1 != s2
def setup_models(self): self.prj = ProjectFactory.create() self.party = PartyFactory.create(project=self.prj) self.resource = ResourceFactory.create(content_object=self.party, project=self.prj) self.user = UserFactory.create() assign_policies(self.user)
def test_get_schema_attrs(self): project = ProjectFactory.create(current_questionnaire="123abc") content_type = ContentType.objects.get(app_label="spatial", model="spatialunit") schema = Schema.objects.create( content_type=content_type, selectors=(project.organization.id, project.id, "123abc") ) text_type = AttributeType.objects.get(name="text") select_m_type = AttributeType.objects.get(name="select_multiple") Attribute.objects.create( schema=schema, name="key", long_name="Test field", attr_type=text_type, index=0, required=False, omit=False ) Attribute.objects.create( schema=schema, name="key_2", long_name="Test field", attr_type=text_type, index=1, required=False, omit=True ) Attribute.objects.create( schema=schema, name="key_3", long_name="Test select multiple field", attr_type=select_m_type, index=2, choices=["choice_1", "choice_2", "choice_3"], choice_labels=["Choice 1", "Choice 2", "Choice 3"], required=False, omit=False, ) exporter = Exporter(project) attrs = exporter.get_schema_attrs(content_type) assert len(attrs) == 2
def setup_models(self): self.user = UserFactory.create() self.project = ProjectFactory.create() self.resource = ResourceFactory.create(content_object=self.project, project=self.project, archived=True) assign_permissions(self.user)
def setup_models(self): self.user = UserFactory.create() assign_policies(self.user) self.org = OrganizationFactory.create() self.prj = ProjectFactory.create( organization=self.org, add_users=[self.user]) self.party = PartyFactory.create(name="Test Party", project=self.prj)
def setUp(self): self.project = ProjectFactory.create(current_questionnaire='abc1') content_type = ContentType.objects.get( app_label='party', model='party') schema1 = Schema.objects.create( content_type=content_type, selectors=( self.project.organization.id, self.project.id, 'abc1')) schema2 = Schema.objects.create( content_type=content_type, selectors=( self.project.organization.id, self.project.id, 'abc1', 'GR')) create_attribute_type('text', 'Text', 'CharField', validator_type='str') create_attribute_type('boolean', 'boolean', 'BooleanField', validator_type='bool') Attribute.objects.create( schema=schema1, name='fname', long_name='Test field', attr_type=AttributeType.objects.get(name='text'), index=0 ) Attribute.objects.create( schema=schema2, name='homeowner', long_name='Homeowner', attr_type=AttributeType.objects.get(name='boolean'), index=1 )
def test_get_schema_attrs(self): project = ProjectFactory.create(current_questionnaire='123abc') content_type = ContentType.objects.get(app_label='spatial', model='spatialunit') schema = Schema.objects.create( content_type=content_type, selectors=(project.organization.id, project.id, '123abc', )) text_type = AttributeType.objects.get(name='text') select_m_type = AttributeType.objects.get(name='select_multiple') Attribute.objects.create( schema=schema, name='key', long_name='Test field', attr_type=text_type, index=0, required=False, omit=False ) Attribute.objects.create( schema=schema, name='key_2', long_name='Test field', attr_type=text_type, index=1, required=False, omit=True ) Attribute.objects.create( schema=schema, name='key_3', long_name='Test select multiple field', attr_type=select_m_type, index=2, choices=['choice_1', 'choice_2', 'choice_3'], choice_labels=['Choice 1', 'Choice 2', 'Choice 3'], required=False, omit=False ) exporter = Exporter(project) attrs = exporter.get_schema_attrs(content_type) assert len(attrs['DEFAULT']) == 2
def setUp(self): super().setUp() # Create a floating resource self.user = UserFactory.build( full_name='John Lennon', username='******' ) self.last_updated = datetime.now() self.resource = ResourceFactory.build( name='Resource Name', file='https://example.com/file.txt', original_file='original_file.jpg', mime_type='image/png', contributor=self.user, last_updated=self.last_updated, ) # Attach it to a project project = ProjectFactory.create() ContentObject.objects.create( resource=self.resource, content_type=ContentType.objects.get_for_model(project), object_id=project.id, )
def setup_models(self): self.project = ProjectFactory.create() self.resources = ResourceFactory.create_batch( 2, content_object=self.project, project=self.project) self.denied = ResourceFactory.create(content_object=self.project, project=self.project) ResourceFactory.create() self.user = UserFactory.create() assign_policies(self.user, add_clauses=[ { 'effect': 'deny', 'object': ['resource/*/*/' + self.denied.id], 'action': ['resource.*'], }, { 'effect': 'deny', 'object': ['resource/*/*/*'], 'action': ['resource.unarchive'], }, ]) self.storage = self.get_storage() self.file = self.get_file( '/resources/tests/files/image.jpg', 'rb') self.file_name = self.storage.save('resources/image.jpg', self.file)
def test_create_location_with_attributes(self): data = { 'geometry': '{"type": "Polygon","coordinates": [[[-0.1418137550354' '004,51.55240622205599],[-0.14117002487182617,51.55167' '905819532],[-0.1411914825439453,51.55181915488898],[-' '0.1411271095275879,51.55254631651022],[-0.14181375503' '54004,51.55240622205599]]]}', 'type': 'CB', 'spatialunit::default::fname': 'test' } project = ProjectFactory.create() content_type = ContentType.objects.get( app_label='spatial', model='spatialunit') schema = Schema.objects.create( content_type=content_type, selectors=(project.organization.id, project.id, )) attr_type = AttributeType.objects.get(name='text') Attribute.objects.create( schema=schema, name='fname', long_name='Test field', attr_type=attr_type, index=0, required=False, omit=False ) form = forms.LocationForm(project=project, data=data) form.is_valid() form.save() assert SpatialUnit.objects.filter(project=project).count() == 1 unit = SpatialUnit.objects.filter(project=project).first() assert unit.attributes.get('fname') == 'test'
def setup_models(self): clauses = { 'clause': [ { 'effect': 'allow', 'object': ['project/*/*', 'spatial/*/*/*', 'spatial_rel/*/*/*', 'party/*/*/*', 'party_rel/*/*/*', 'tenure_rel/*/*/*'], 'action': ['project.*', 'project.*.*', 'spatial.*', 'spatial_rel.*', 'party.*', 'party_rel.*', 'tenure_rel.*'] } ] } policy = Policy.objects.create( name='basic-test', body=json.dumps(clauses)) self.user = UserFactory.create() self.user.assign_policies(policy) self.prj = ProjectFactory.create(slug='test-project', access='public') self.SR = spatial_factories.SpatialRelationshipFactory self.PR = party_factories.PartyRelationshipFactory self.TR = party_factories.TenureRelationshipFactory
def test_create_party_with_attributes(self): data = {"name": "Cadasta", "type": "IN", "attributes::fname": "test"} project = ProjectFactory.create() content_type = ContentType.objects.get(app_label="party", model="party") schema = Schema.objects.create(content_type=content_type, selectors=(project.organization.id, project.id)) Attribute.objects.create( schema=schema, name="fname", long_name="Test field", attr_type=AttributeType.objects.get(name="text"), index=0, ) form = forms.PartyForm( project_id=project.id, data=data, schema_selectors=( {"name": "organization", "value": project.organization, "selector": project.organization.id}, {"name": "project", "value": project, "selector": project.id}, ), ) form.is_valid() form.save() assert Party.objects.filter(project=project).count() == 1 party = Party.objects.filter(project=project).first() assert party.attributes.get("fname") == "test"
def test_party_attribute_schema(self): project = ProjectFactory.create(name='TestProject') QuestionnaireFactory.create(project=project) content_type = ContentType.objects.get( app_label='party', model='party') create_attrs_schema( project=project, dict=individual_party_xform_group, content_type=content_type, errors=[]) party = PartyFactory.create( name='TestParty', project=project, type='IN', attributes={ 'homeowner': 'yes', 'dob': '1980-01-01' } ) assert 1 == Schema.objects.all().count() schema = Schema.objects.get(content_type=content_type) assert schema is not None assert schema.selectors == [ project.organization.pk, project.pk, project.current_questionnaire, party.type] assert 'homeowner' in party.attributes.attributes assert 'dob' in party.attributes.attributes assert 'gender' in party.attributes.attributes assert 3 == Attribute.objects.filter(schema=schema).count() assert party.attributes['homeowner'] == 'yes' assert party.attributes['dob'] == '1980-01-01'
def setup_models(self): self.prj = ProjectFactory.create() self.su = SpatialUnitFactory.create(project=self.prj) self.resource = ResourceFactory.create(content_object=self.su, project=self.prj) self.user = UserFactory.create() assign_policies(self.user)
def setup_models(self): self.project = ProjectFactory.create() self.org_slug = self.project.organization.slug self.resource = ResourceFactory.create(project=self.project) self.location = SpatialUnitFactory.create(project=self.project) self.party = PartyFactory.create(project=self.project) self.tenurerel = TenureRelationshipFactory.create(project=self.project) self.project_attachment = ContentObject.objects.create( resource_id=self.resource.id, content_object=self.project, ) self.location_attachment = ContentObject.objects.create( resource_id=self.resource.id, content_object=self.location, ) self.party_attachment = ContentObject.objects.create( resource_id=self.resource.id, content_object=self.party, ) self.tenurerel_attachment = ContentObject.objects.create( resource_id=self.resource.id, content_object=self.tenurerel, ) self.user = UserFactory.create() assign_permissions(self.user)
def test_invalid_deserialize_json(self): data = { 'id_string': 'yx8sqx6488wbc4yysnkrbnfq', 'questions': [{ 'name': "start", 'label': 'Label', 'type': "ST", 'required': False, 'constraint': None }, { 'name': "end", 'label': 'Label', 'type': "EN", }] } project = ProjectFactory.create() serializer = serializers.QuestionnaireSerializer( data=data, context={'project': project} ) assert serializer.is_valid() is False assert serializer.errors == {'title': ['This field is required.']} with pytest.raises(ValidationError): assert serializer.is_valid(raise_exception=True)
def test_spatial_unit_attribute_schema(self): project = ProjectFactory.create(name='TestProject') QuestionnaireFactory.create(project=project) content_type = ContentType.objects.get( app_label='spatial', model='spatialunit') create_attrs_schema( project=project, dict=location_xform_group, content_type=content_type, errors=[]) spatial_unit = SpatialUnitFactory.create( project=project, attributes={ 'quality': 'polygon_high', 'infrastructure': ['water', 'food'] } ) assert 1 == Schema.objects.all().count() schema = Schema.objects.get(content_type=content_type) assert schema is not None assert schema.selectors == [ project.organization.pk, project.pk, project.current_questionnaire] assert 'quality' in spatial_unit.attributes.attributes assert 'polygon_high' == spatial_unit.attributes['quality'] assert 'infrastructure' in spatial_unit.attributes.attributes assert 'water' in spatial_unit.attributes['infrastructure'] # notes field is omitted in xform assert 'notes' not in spatial_unit.attributes.attributes
def test_deserialize_json(self): data = { 'title': 'yx8sqx6488wbc4yysnkrbnfq', 'id_string': 'yx8sqx6488wbc4yysnkrbnfq', 'questions': [{ 'name': "start", 'label': 'Label', 'type': "ST", 'required': False, 'constraint': None }, { 'name': "end", 'label': 'Label', 'type': "EN", }] } project = ProjectFactory.create() serializer = serializers.QuestionnaireSerializer( data=data, context={'project': project} ) serializer.is_valid(raise_exception=True) serializer.save() assert Questionnaire.objects.count() == 1 questionnaire = Questionnaire.objects.first() assert project.current_questionnaire == questionnaire.id assert questionnaire.questions.count() == 2
def test_deserialize(self): form = self.get_form('xls-form') project = ProjectFactory.create() serializer = serializers.QuestionnaireSerializer( data={'xls_form': form}, context={'project': project} ) assert serializer.is_valid(raise_exception=True) is True serializer.save() assert Questionnaire.objects.count() == 1 questionnaire = Questionnaire.objects.first() assert questionnaire.id_string == 'question_types' assert questionnaire.filename == 'xls-form' assert questionnaire.title == 'Question types' assert serializer.data['id'] == questionnaire.id assert serializer.data['filename'] == questionnaire.filename assert serializer.data['title'] == questionnaire.title assert serializer.data['id_string'] == questionnaire.id_string assert serializer.data['xls_form'] == questionnaire.xls_form.url assert serializer.data['version'] == questionnaire.version assert len(serializer.data['questions']) == 1
def setup_models(self, access='public'): self.user = UserFactory.create() assign_policies(self.user) self.prj = ProjectFactory.create(slug='test-project', access='public') self.su1 = SpatialUnitFactory.create(project=self.prj, type='AP') self.su2 = SpatialUnitFactory.create(project=self.prj, type='BU')
def setup_models(self): self.user = UserFactory.create() self.org = OrganizationFactory.create() self.prj = ProjectFactory.create(organization=self.org) OrganizationRole.objects.create( organization=self.org, user=self.user, admin=True)
def test_save_new_party_with_attributes(self): project = ProjectFactory.create() questionnaire = QuestionnaireFactory.create(project=project) spatial_unit = SpatialUnitFactory.create(project=project) content_type = ContentType.objects.get( app_label='party', model='tenurerelationship') schema = Schema.objects.create( content_type=content_type, selectors=( project.organization.id, project.id, questionnaire.id, )) attr_type = AttributeType.objects.get(name='text') Attribute.objects.create( schema=schema, name='r_name', long_name='Relationship field', attr_type=attr_type, index=0, required=False, omit=False ) content_type = ContentType.objects.get( app_label='party', model='party') schema_gr = Schema.objects.create( content_type=content_type, selectors=( project.organization.id, project.id, questionnaire.id, 'GR')) Attribute.objects.create( schema=schema_gr, name='p_gr_name', long_name='Party GR field', attr_type=attr_type, index=0, required=True, omit=False ) form = forms.TenureRelationshipForm( project=project, spatial_unit=spatial_unit, data={'new_entity': 'on', 'id': '', 'name': 'The Beatles', 'party::gr::p_gr_name': 'Party Group Name', 'party_type': 'GR', 'tenure_type': 'CU', 'tenurerelationship::default::r_name': 'Rel Name'}) assert form.is_valid() form.save() assert Party.objects.count() == 1 party = Party.objects.first() assert party.name == 'The Beatles' assert party.type == 'GR' assert party.attributes.get('p_gr_name') == 'Party Group Name' assert TenureRelationship.objects.count() == 1 rel = TenureRelationship.objects.first() assert rel.party == party assert rel.spatial_unit == spatial_unit assert rel.tenure_type_id == 'CU' assert rel.attributes.get('r_name') == 'Rel Name'
def test_create_party(self): data = {"name": "Cadasta", "type": "IN"} project = ProjectFactory.create() form = forms.PartyForm(project_id=project.id, data=data, schema_selectors=()) form.is_valid() form.save() assert Party.objects.filter(project=project).count() == 1
def setup_models(self): clause = { 'clause': [ { 'effect': 'allow', 'object': ['project/*/*'], 'action': ['questionnaire.*'] } ] } policy = Policy.objects.create( name='test-policy', body=json.dumps(clause)) self.user = UserFactory.create() self.user.assign_policies(policy) self.org = OrganizationFactory.create() self.prj = ProjectFactory.create(organization=self.org)
def test_invalid_custom_location_type(self): project = ProjectFactory.create() questionnaire = q_factories.QuestionnaireFactory.create( project=project) question = q_factories.QuestionFactory.create( type='S1', name='location_type', questionnaire=questionnaire) q_factories.QuestionOptionFactory.create(question=question, name='FU', label='FU Label') data = {'type': 'PA'} serializer = serializers.SpatialUnitSerializer( data=data, context={'project': project}) assert serializer.is_valid() is False assert ("'PA' is not a valid choice for field 'type'." in serializer.errors['type'])
def test_write_relationships(self): dst_dir = os.path.join(settings.MEDIA_ROOT, 'temp/rels') if not os.path.exists(dst_dir): os.makedirs(dst_dir) filename = os.path.join(dst_dir, 'releationships.csv') project = ProjectFactory.create() exporter = ShapeExporter(project) exporter.write_relationships(filename) with open(filename) as csvfile: csvreader = csv.reader(csvfile) for i, row in enumerate(csvreader): if i == 0: assert row == ['id', 'party_id', 'spatial_unit_id', 'tenure_type.label'] else: assert False, "Too many rows in CSV."
def test_update_from_form(self): file = self.get_file('/questionnaires/tests/files/xls-form.xlsx', 'rb') form = self.storage.save('xls-forms/xls-form.xlsx', file.read()) file.close() project = ProjectFactory.create() m1 = models.Questionnaire.objects.create_from_form(xls_form=form, project=project) model = models.Questionnaire.objects.create_from_form(xls_form=form, project=project) assert model.id_string == 'question_types' assert model.filename == 'xls-form' assert model.title == 'Question types' assert m1.id != model.id assert project.current_questionnaire == model.id
def test_tenure_relationship_schema(self): project = ProjectFactory.create(name='TestProject') QuestionnaireFactory.create(project=project) content_type = ContentType.objects.get( app_label='party', model='tenurerelationship') create_attrs_schema( project=project, dict=tenure_relationship_xform_group, content_type=content_type, errors=[]) tr = TenureRelationshipFactory.create( project=project, attributes={ 'notes': 'Some additional textual info'} ) assert 1 == Schema.objects.all().count() schema = Schema.objects.get(content_type=content_type) assert schema is not None assert schema.selectors == [ project.organization.pk, project.pk, project.current_questionnaire] assert 'notes' in tr.attributes.attributes
def setup_models(self): self.project = ProjectFactory.create(slug='test-project') self.location = SpatialUnitFactory.create( project=self.project, geometry='SRID=4326;POINT(0 0)', ) self.party1 = PartyFactory.create(project=self.project) self.party2 = PartyFactory.create(project=self.project) self.tenure_rel = TenureRelationshipFactory.create( project=self.project, spatial_unit=self.location, party=self.party1, ) self.resource = ResourceFactory.create(project=self.project) self.query_format = ('{{"query": {{"bool": {{"should": [' '{{"multi_match": {{"query": "{q}"}}}}]}}}},' ' "from": {f}, "size": {s}}}')
def setup_models(self): self.project = ProjectFactory.create() questionnaire = q_factories.QuestionnaireFactory.create( project=self.project) content_type = ContentType.objects.get(app_label='spatial', model='spatialunit') schema = Schema.objects.create(content_type=content_type, selectors=(self.project.organization.id, self.project.id, questionnaire.id)) attr_type = AttributeType.objects.get(name='text') Attribute.objects.create(schema=schema, name='fname', long_name='Test field', attr_type=attr_type, index=0, required=False, omit=False)
def setup_models(self): self.project = ProjectFactory.create() questionnaire = QuestionnaireFactory.create(project=self.project) self.spatial_unit = SpatialUnitFactory(project=self.project) content_type = ContentType.objects.get(app_label='party', model='tenurerelationship') schema = Schema.objects.create(content_type=content_type, selectors=(self.project.organization.id, self.project.id, questionnaire.id)) attr_type = AttributeType.objects.get(name='text') Attribute.objects.create(schema=schema, name='r_name', long_name='Relationship field', attr_type=attr_type, index=0, required=False, omit=False) content_type = ContentType.objects.get(app_label='party', model='party') schema = Schema.objects.create(content_type=content_type, selectors=(self.project.organization.id, self.project.id, questionnaire.id)) attr_type = AttributeType.objects.get(name='text') Attribute.objects.create(schema=schema, name='p_name', long_name='Party field', attr_type=attr_type, index=0, required=False, omit=False) schema = Schema.objects.create(content_type=content_type, selectors=(self.project.organization.id, self.project.id, questionnaire.id, 'GR')) Attribute.objects.create(schema=schema, name='p_gr_name', long_name='Party Group field', attr_type=attr_type, index=0, required=False, omit=False)
def setup_models(self): self.project = ProjectFactory.create() content_type = ContentType.objects.get(app_label='spatial', model='spatialunit') schema = Schema.objects.create(content_type=content_type, selectors=( self.project.organization.id, self.project.id, )) attr_type = AttributeType.objects.get(name='text') Attribute.objects.create(schema=schema, name='fname', long_name='Test field', attr_type=attr_type, index=0, required=False, omit=False) Attribute.objects.create( schema=schema, name='fname_2', long_name='Test field 2', attr_type=AttributeType.objects.get(name='select_one'), choices=['-', 'one', 'two', 'three'], choice_labels=['None', 'Choice 1', 'Choice 2', 'Choice 3'], index=1, required=False, omit=False) Attribute.objects.create( schema=schema, name='fname_3', long_name='Test field 3', attr_type=AttributeType.objects.get(name='select_multiple'), choices=['-', 'one', 'two', 'three'], choice_labels=['None', 'Choice 1', 'Choice 2', 'Choice 3'], index=2, required=False, omit=False) self.location = SpatialUnitFactory.create(project=self.project, attributes={ 'fname': 'test', 'fname_2': 'two', 'fname_3': ['one', 'three'] })
def setup_models(self): self.org = OrganizationFactory.create() extent = ('SRID=4326;' 'POLYGON ((-5.1031494140625000 8.1299292850467957, ' '-5.0482177734375000 7.6837733211111425, ' '-4.6746826171875000 7.8252894725496338, ' '-4.8641967773437491 8.2278005261522775, ' '-5.1031494140625000 8.1299292850467957))') ProjectFactory.create(organization=self.org, extent=extent) ProjectFactory.create(organization=self.org, extent=extent) ProjectFactory.create( name='Private Project', access='private', organization=self.org, extent=extent)
def test_create_location(self): data = { 'geometry': '{"type": "Polygon","coordinates": [[[-0.1418137550354' '004,51.55240622205599],[-0.14117002487182617,51.55167' '905819532],[-0.1411914825439453,51.55181915488898],[-' '0.1411271095275879,51.55254631651022],[-0.14181375503' '54004,51.55240622205599]]]}', 'type': 'CB' } project = ProjectFactory.create() form = forms.LocationForm(project_id=project.id, data=data, schema_selectors=()) form.is_valid() form.save() assert SpatialUnit.objects.filter(project=project).count() == 1
def test_save(self): prj = ProjectFactory.create() prj_res = ResourceFactory.create(project=prj, content_object=prj) res = ResourceFactory.create(project=prj) data = { res.id: 'on' } form = AddResourceFromLibraryForm(data=data, project_id=prj.id, content_object=prj) assert form.is_valid() is True form.save() assert prj.resources.count() == 2 assert res in prj.resources assert prj_res in prj.resources
def setup_models(self): self.project = ProjectFactory.create() content_type = ContentType.objects.get(app_label='spatial', model='spatialunit') schema = Schema.objects.create(content_type=content_type, selectors=( self.project.organization.id, self.project.id, )) attr_type = AttributeType.objects.get(name='text') Attribute.objects.create(schema=schema, name='fname', long_name='Test field', attr_type=attr_type, index=0, required=False, omit=False) self.location = SpatialUnitFactory.create(project=self.project, attributes={'fname': 'test'})
def setup_models(self): self.project = ProjectFactory.create() content_type = ContentType.objects.get( app_label='party', model='tenurerelationship') schema = Schema.objects.create( content_type=content_type, selectors=(self.project.organization.id, self.project.id, )) attr_type = AttributeType.objects.get(name='text') Attribute.objects.create( schema=schema, name='fname', long_name='Test field', attr_type=attr_type, index=0, required=False, omit=False ) self.relationship = TenureRelationshipFactory.create( project=self.project, attributes={'fname': 'test'}, tenure_type=TenureRelationshipType.objects.get(id='WR') )
def test_save_exisiting_party(self): project = ProjectFactory.create() party = PartyFactory.create(project=project) spatial_unit = SpatialUnitFactory.create(project=project) form = forms.TenureRelationshipForm(project=project, spatial_unit=spatial_unit, data={ 'new_entity': '', 'id': party.id, 'tenure_type': 'CU' }) form.is_valid() form.save() assert TenureRelationship.objects.count() == 1 rel = TenureRelationship.objects.first() assert rel.party == party assert rel.spatial_unit == spatial_unit assert rel.tenure_type_id == 'CU'
def setUp(self): super().setUp() self.project = ProjectFactory.create() QuestionnaireFactory.create(project=self.project) ATTR_TYPE_IDS = get_attr_type_ids() content_type = ContentType.objects.get(app_label='spatial', model='spatialunit') create_attrs_schema( project=self.project, question_group_dict=attr_schemas.location_xform_group, content_type=content_type, attr_type_ids=ATTR_TYPE_IDS, ) content_type = ContentType.objects.get(app_label='party', model='party') create_attrs_schema( project=self.project, question_group_dict=attr_schemas.default_party_xform_group, content_type=content_type, attr_type_ids=ATTR_TYPE_IDS, ) create_attrs_schema( project=self.project, question_group_dict=attr_schemas.individual_party_xform_group, content_type=content_type, attr_type_ids=ATTR_TYPE_IDS, ) create_attrs_schema( project=self.project, question_group_dict=attr_schemas.group_party_attributes, content_type=content_type, attr_type_ids=ATTR_TYPE_IDS, ) content_type = ContentType.objects.get(app_label='party', model='tenurerelationship') create_attrs_schema( project=self.project, question_group_dict=attr_schemas.tenure_relationship_xform_group, content_type=content_type, attr_type_ids=ATTR_TYPE_IDS, )
def test_invalid_custom_tenure_type(self): project = ProjectFactory.create() questionnaire = q_factories.QuestionnaireFactory.create( project=project) question = q_factories.QuestionFactory.create( type='S1', name='tenure_type', questionnaire=questionnaire) q_factories.QuestionOptionFactory.create(question=question, name='FU', label='FU Label') su = SpatialUnitFactory.create(project=project) party = PartyFactory.create(project=project) data = {'tenure_type': 'FH', 'spatial_unit': su.id, 'party': party.id} serializer = serializers.TenureRelationshipSerializer( data=data, context={'project': project}) assert serializer.is_valid() is False assert ("'FH' is not a valid choice for field 'tenure_type'." in serializer.errors['tenure_type'])
def setUp(self): super().setUp() # Create a floating resource self.user = UserFactory.create(full_name='John Lennon', username='******') self.resource = ResourceFactory.create( name='Resource Name', file='https://example.com/file.txt', original_file='file.txt', mime_type='text/plain', contributor=self.user) # Attach it to a project project = ProjectFactory.create() ContentObject.objects.create( resource=self.resource, content_type=ContentType.objects.get_for_model(project), object_id=project.id, )
def test_spatial_relationship_schema(self): project = ProjectFactory.create(name='TestProject') QuestionnaireFactory.create(project=project) content_type = ContentType.objects.get(app_label='spatial', model='spatialrelationship') create_attrs_schema(project=project, content_type=content_type, errors=[], question_group_dict=party_relationship_xform_group, attr_type_ids=get_attr_type_ids()) sur = SpatialRelationshipFactory.create( project=project, attributes={'notes': 'Some additional textual info'}) assert 1 == Schema.objects.all().count() schema = Schema.objects.get(content_type=content_type) assert schema is not None assert schema.selectors == [ project.organization.pk, project.pk, project.current_questionnaire ] assert 'notes' in sur.attributes.attributes
def test_filter_archived_with_unarchiver(self): prj = ProjectFactory.create() ResourceFactory.create_from_kwargs([ {'content_object': prj, 'project': prj, 'archived': True}, {'content_object': prj, 'project': prj, 'archived': True}, {'content_object': prj, 'project': prj, 'archived': False}, ]) unarchiver = UserFactory.create() policy = Policy.objects.create( name='allow', body=json.dumps(clauses)) unarchiver.assign_policies(policy) response = self.request( user=unarchiver, url_kwargs={'organization': prj.organization.slug, 'project': prj.slug}, get_data={'archived': True}) assert response.status_code == 200 assert len(response.content) == 2
def test_project_resource(self): file = self.get_file('/resources/tests/files/image.jpg', 'rb') file_name = self.storage.save('resources/image.jpg', file) project = ProjectFactory.create() user = UserFactory.create() resource = Resource.objects.create(name='Re', file=file_name, content_object=project, contributor=user, project=project, mime_type='image/jpeg') assert resource.name == 'Re' assert resource.content_objects.count() == 1 assert resource.mime_type == 'image/jpeg' content_object = resource.content_objects.first() assert content_object.object_id == project.id assert content_object.content_type == ContentType.objects.get( app_label='organization', model='project')
def test_required_attribute(self): project = ProjectFactory.create(name='TestProject') QuestionnaireFactory.create(project=project) content_type = ContentType.objects.get(app_label='party', model='party') create_attrs_schema(project=project, dict=individual_party_xform_group, content_type=content_type, errors=[]) # with pytest.raises(ValidationError): # PartyFactory.create( # project=project, # attributes={} # ) # should raise validation error as dob value is required? PartyFactory.create(project=project, attributes={}) schema = Schema.objects.get(content_type=content_type) attr = Attribute.objects.get(schema=schema, name='dob') assert attr.required
def test_invalid_attributes(self): project = ProjectFactory.create(name='Test Project') content_type = ContentType.objects.get(app_label='spatial', model='spatialunit') schema = Schema.objects.create(content_type=content_type, selectors=( project.organization.id, project.id, )) Attribute.objects.create( schema=schema, name='notes', long_name='Notes', attr_type=AttributeType.objects.get(name='text'), index=0) Attribute.objects.create( schema=schema, name='age', long_name='Homeowner Age', attr_type=AttributeType.objects.get(name='integer'), index=1, required=True, default=0) data = { 'properties': { 'type': 'AP', 'project': project, 'attributes': { 'notes': 'Blah', 'age': 'Ten' } } } serializer = serializers.SpatialUnitSerializer( data=data, context={'project': project}) assert serializer.is_valid() is False assert serializer.errors['attributes']
def test_sanitise_string(self): project = ProjectFactory.create(name='Test Project') su = SpatialUnitFactory.create(project=project) party = PartyFactory.create(project=project) content_type = ContentType.objects.get(app_label='party', model='tenurerelationship') schema = Schema.objects.create(content_type=content_type, selectors=( project.organization.id, project.id, )) Attribute.objects.create( schema=schema, name='notes', long_name='Notes', attr_type=AttributeType.objects.get(name='text'), index=0) Attribute.objects.create( schema=schema, name='age', long_name='Homeowner Age', attr_type=AttributeType.objects.get(name='integer'), index=1, required=True, default=0) data = { 'spatial_unit': su.id, 'party': party.id, 'tenure_type': 'WR', 'attributes': { 'notes': '<Blah>', 'age': 2 } } serializer = serializers.TenureRelationshipSerializer( data=data, context={'project': project}) assert serializer.is_valid() is False assert SANITIZE_ERROR in serializer.errors['attributes']
def test_create_location_with_attributes(self): data = { 'geometry': '{"type": "Polygon","coordinates": [[[-0.1418137550354' '004,51.55240622205599],[-0.14117002487182617,51.55167' '905819532],[-0.1411914825439453,51.55181915488898],[-' '0.1411271095275879,51.55254631651022],[-0.14181375503' '54004,51.55240622205599]]]}', 'type': 'CB', 'attributes::fname': 'test' } project = ProjectFactory.create() content_type = ContentType.objects.get( app_label='spatial', model='spatialunit') schema = Schema.objects.create( content_type=content_type, selectors=(project.organization.id, project.id, )) attr_type = AttributeType.objects.get(name='text') Attribute.objects.create( schema=schema, name='fname', long_name='Test field', attr_type=attr_type, index=0, required=False, omit=False ) form = forms.LocationForm(project_id=project.id, data=data, schema_selectors=( {'name': 'organization', 'value': project.organization, 'selector': project.organization.id}, {'name': 'project', 'value': project, 'selector': project.id} )) form.is_valid() form.save() assert SpatialUnit.objects.filter(project=project).count() == 1 unit = SpatialUnit.objects.filter(project=project).first() assert unit.attributes.get('fname') == 'test'
def test_sanitize_string(self): file = self.get_file('/resources/tests/files/image.jpg', 'rb') file_name = self.storage.save('image.jpg', file.read()) file.close() project = ProjectFactory.create() user = UserFactory.create() data = { 'name': 'New resource', 'description': '<Description>', 'file': file_name, 'original_file': 'image.png' } serializer = ResourceSerializer(data=data, context={ 'content_object': project, 'contributor': user, 'project_id': project.id }) assert serializer.is_valid() is False assert SANITIZE_ERROR in serializer.errors['description']
def setup_models(self): self.project = ProjectFactory.create() self.resources = ResourceFactory.create_batch( 2, content_object=self.project, project=self.project) self.denied = ResourceFactory.create(content_object=self.project, project=self.project) ResourceFactory.create() self.user = UserFactory.create() assign_permissions(self.user, [ { 'effect': 'deny', 'object': ['resource/*/*/' + self.denied.id], 'action': ['resource.*'], }, { 'effect': 'deny', 'object': ['resource/*/*/*'], 'action': ['resource.unarchive'], }, ])
def test_get_schema_attrs(self): project = ProjectFactory.create(current_questionnaire='123abc') content_type = ContentType.objects.get(app_label='spatial', model='spatialunit') schema = Schema.objects.create(content_type=content_type, selectors=( project.organization.id, project.id, '123abc', )) text_type = AttributeType.objects.get(name='text') select_m_type = AttributeType.objects.get(name='select_multiple') Attribute.objects.create(schema=schema, name='key', long_name='Test field', attr_type=text_type, index=0, required=False, omit=False) Attribute.objects.create(schema=schema, name='key_2', long_name='Test field', attr_type=text_type, index=1, required=False, omit=True) Attribute.objects.create( schema=schema, name='key_3', long_name='Test select multiple field', attr_type=select_m_type, index=2, choices=['choice_1', 'choice_2', 'choice_3'], choice_labels=['Choice 1', 'Choice 2', 'Choice 3'], required=False, omit=False) exporter = Exporter(project) attrs = exporter.get_schema_attrs(content_type) assert len(attrs['DEFAULT']) == 2
def test_sanitize_strings(self): project = ProjectFactory.create(name='Test Project') content_type = ContentType.objects.get(app_label='party', model='party') schema = Schema.objects.create(content_type=content_type, selectors=( project.organization.id, project.id, )) Attribute.objects.create( schema=schema, name='notes', long_name='Notes', attr_type=AttributeType.objects.get(name='text'), index=0) Attribute.objects.create( schema=schema, name='age', long_name='Homeowner Age', attr_type=AttributeType.objects.get(name='integer'), index=1, required=True, default=0) party_data = { 'name': '<Tea Party>', 'type': 'IN', 'attributes': { 'notes': '<Blah>', 'age': 2 } } serializer = serializers.PartySerializer(data=party_data, context={'project': project}) with pytest.raises(ValidationError) as e: serializer.is_valid(raise_exception=True) assert e.value.detail['name'] assert e.value.detail['attributes']
def test_field_sanitation(self): data = { 'name': '<Cadasta>', 'type': 'IN', 'party::in::fname': '<FName>', 'party::in::age': 37 } project = ProjectFactory.create() content_type = ContentType.objects.get( app_label='party', model='party') schema = Schema.objects.create( content_type=content_type, selectors=(project.organization.id, project.id, )) Attribute.objects.create( schema=schema, name='fname', long_name='Test field', attr_type=AttributeType.objects.get(name='text'), index=0 ) Attribute.objects.create( schema=schema, name='homeowner', long_name='Homeowner', attr_type=AttributeType.objects.get(name='boolean'), index=1 ) Attribute.objects.create( schema=schema, name='age', long_name='Homeowner Age', attr_type=AttributeType.objects.get(name='integer'), index=2, required=True, default=0 ) form = forms.PartyForm(project=project, data=data) assert form.is_valid() is False assert form.errors.get('name') is not None assert form.errors.get('party::in::fname') is not None