def test_make_email_context_for_category_with_or_without_public_name(self): # Check categories with public names. main_cat_with_public_name = ParentCategoryFactory.create( parent=None, name='PRIVATE_MAIN_I', public_name='PUBLIC_MAIN_I') sub_cat_with_public_name = CategoryFactory.create( parent=main_cat_with_public_name, name='PRIVATE_SUB', public_name='PUBLIC_SUB_I') signal_with_public_name = SignalFactory.create( category_assignment__category=sub_cat_with_public_name) context_with = make_email_context(signal=signal_with_public_name) self.assertEqual(context_with['main_category_public_name'], 'PUBLIC_MAIN_I') self.assertEqual(context_with['sub_category_public_name'], 'PUBLIC_SUB_I') # Check categories without public names. main_cat_no_public_name = ParentCategoryFactory.create( parent=None, name='PRIVATE_MAIN_II', public_name=None) sub_cat_no_public_name = CategoryFactory.create( parent=main_cat_no_public_name, name='PRIVATE_SUB_II', public_name=None) signal_no_public_name = SignalFactory.create( category_assignment__category=sub_cat_no_public_name) context_without = make_email_context(signal=signal_no_public_name) self.assertEqual(context_without['main_category_public_name'], 'PRIVATE_MAIN_II') self.assertEqual(context_without['sub_category_public_name'], 'PRIVATE_SUB_II')
def test_only_main_category_filter(self): """ unit test to only filter items with the main category create 5 signals and only 3 under the main category 'trash' that should be returned """ parent_cat = ParentCategoryFactory.create(name='trash') child_category = CategoryFactory.create(parent=parent_cat, public_name='', is_public_accessible=True) child_category_2 = CategoryFactory.create(parent=parent_cat, public_name='', is_public_accessible=True) SignalFactoryValidLocation.create( category_assignment__category=child_category) SignalFactoryValidLocation.create( category_assignment__category=child_category) SignalFactoryValidLocation.create( category_assignment__category=child_category_2) parent_cat_2 = ParentCategoryFactory.create(name='animal') child_animal = CategoryFactory.create(parent=parent_cat_2, public_name='', is_public_accessible=True) SignalFactoryValidLocation.create( category_assignment__category=child_animal) SignalFactoryValidLocation.create( category_assignment__category=child_animal) response = self.client.get( f'{self.geography_endpoint}/?bbox=4.700000,52.200000,5.000000,52.500000&' f'maincategory_slug={parent_cat.slug}') data = response.json() self.assertEqual(3, len(data['features']))
def setUp(self): SourceFactory.create(name='online', is_active=True) SourceFactory.create(name='Telefoon – ASC', is_active=True) self.main_category = ParentCategoryFactory.create(name='main', slug='main') self.link_main_category = '/signals/v1/public/terms/categories/main' self.sub_category_1 = CategoryFactory.create(name='sub1', slug='sub1', parent=self.main_category) self.link_sub_category_1 = f'{self.link_main_category}/sub_categories/sub1' self.sub_category_2 = CategoryFactory.create(name='sub2', slug='sub2', parent=self.main_category) self.link_sub_category_2 = f'{self.link_main_category}/sub_categories/sub2' self.sia_read_write_user.user_permissions.add( Permission.objects.get(codename='sia_can_view_all_categories')) self.client.force_authenticate(user=self.sia_read_write_user) self.initial_data_base = dict( text='Mensen in het cafe maken erg veel herrie', location=dict(geometrie=dict( type='point', coordinates=[4.90022563, 52.36768424])), category=dict(category_url=self.link_sub_category_1), reporter=dict(email='*****@*****.**'), incident_date_start=timezone.now().strftime('%Y-%m-%dT%H:%M'), source='Telefoon – ASC', ) self.retrieve_signal_schema = self.load_json_schema( os.path.join(THIS_DIR, 'json_schema', 'get_signals_v1_private_signals_{pk}.json'))
def setUp(self): user_model = get_user_model() self.superuser, _ = user_model.objects.get_or_create( email='*****@*****.**', is_superuser=True, defaults={ 'first_name': 'John', 'last_name': 'Doe', 'is_staff': True }) self.department = DepartmentFactory.create() parent_category = ParentCategoryFactory.create() self.category_1 = CategoryFactory.create(parent=parent_category, departments=[self.department]) ServiceLevelObjectiveFactory.create(category=self.category_1, n_days=2, use_calendar_days=True) self.category_2 = CategoryFactory.create(parent=parent_category, departments=[self.department]) ServiceLevelObjectiveFactory.create(category=self.category_2, n_days=3, use_calendar_days=False) self.category_3 = CategoryFactory.create(parent=parent_category, departments=[self.department]) ServiceLevelObjectiveFactory.create(category=self.category_3, n_days=4, use_calendar_days=False)
def test_category_question_field_types(self): """ Create a question for every field type and check if they are returned correctly """ category = ParentCategoryFactory.create( name='Category for field_type testing', slug='question-field-type') for field_type in list(dict(Question.FIELD_TYPE_CHOICES).keys()): category.questions.clear() question = QuestionFactory.create(field_type=field_type) category.questions.add(question) category.refresh_from_db() response = self.client.get( f'/signals/v1/public/questions/?slug={category.slug}') self.assertEqual(response.status_code, 200) response_data = response.json() self.assertJsonSchema(self.retrieve_sub_category_question_schema, response_data) self.assertEqual(response_data['count'], 1) result = response_data['results'][0] self.assertEqual(result['key'], question.key) self.assertEqual(result['field_type'], question.field_type) self.assertEqual(result['meta'], question.meta) self.assertEqual(result['required'], question.required)
def test_only_category_filter(self): """ unit test to only filter items with the category create 3 signals and only 2 signals with the category 'plastic_trash" """ cat1 = ParentCategoryFactory.create(name='trash') plastic = CategoryFactory.create(parent=cat1, public_name='plastic_trash', is_public_accessible=True) compost = CategoryFactory.create(parent=cat1, public_name='compost_trash', is_public_accessible=True) SignalFactoryValidLocation.create( category_assignment__category=plastic) SignalFactoryValidLocation.create( category_assignment__category=plastic) SignalFactoryValidLocation.create( category_assignment__category=compost) response = self.client.get( f'{self.geography_endpoint}/?bbox=4.700000,52.200000,5.000000,52.500000&' f'category_slug={plastic.slug}') data = response.json() self.assertEqual(2, len(data['features']))
def test_public_name_isset(self): """ An empty public_name (None) must fallback to the name """ parent_category = ParentCategoryFactory.create() child_category = CategoryFactory.create(parent=parent_category, public_name='test', is_public_accessible=True) SignalFactoryValidLocation.create( category_assignment__category=child_category) response = self.client.get( f'{self.geography_endpoint}/?maincategory_slug={parent_category.slug}' f'&category_slug={child_category.slug}&bbox=4.700000,52.200000,5.000000,52.500000' ) self.assertEqual(200, response.status_code) self.assertEqual(1, int(response.headers['X-Total-Count'])) data = response.json() self.assertEqual(1, len(data['features'])) self.assertEqual(child_category.public_name, data['features'][0]['properties']['category']['name']) self.assertNotEqual( child_category.name, data['features'][0]['properties']['category']['name'])
def setUp(self): self.list_categories_schema = self.load_json_schema( os.path.join( THIS_DIR, 'json_schema', 'get_signals_v1_public_terms_categories.json' ) ) self.retrieve_category_schema = self.load_json_schema( os.path.join( THIS_DIR, 'json_schema', 'get_signals_v1_public_terms_categories_{slug}.json' ) ) self.retrieve_sub_category_schema = self.load_json_schema( os.path.join( THIS_DIR, 'json_schema', 'get_signals_v1_public_terms_categories_{slug}_sub_categories_{sub_slug}.json' ) ) self.parent_category = ParentCategoryFactory.create() CategoryFactory.create_batch(5, parent=self.parent_category) self.parent_category.refresh_from_db() super(TestCategoryTermsEndpoints, self).setUp()
def setUp(self): self.parent_category = ParentCategoryFactory.create( name='PARENT-CATEGORY') self.child_category = CategoryFactory.create( name='CHILD-CATEGORY', parent=self.parent_category) self.signal = SignalFactoryWithImage.create( text='BLAH BLAH BLAH', incident_date_start=timezone.now(), category_assignment__category=self.child_category, reporter__email='*****@*****.**', reporter__phone='0612345678') StatusFactory.create(_signal=self.signal, state=workflow.AFWACHTING, text='waiting') StatusFactory.create(_signal=self.signal, state=workflow.ON_HOLD, text='please hold') status = StatusFactory.create(_signal=self.signal, state=workflow.AFGEHANDELD, text='Consider it done') self.signal.status = status self.signal.save() self.user = SuperUserFactory.create()
def test_lon_lat_parameters(self): """ Return the GEOJson containing all signals in the child category """ parent_category = ParentCategoryFactory.create() child_category = CategoryFactory.create( parent=parent_category, public_name='Public category for testing', is_public_accessible=True) signal = SignalFactoryValidLocation.create( category_assignment__category=child_category) lon = signal.location.geometrie.x lat = signal.location.geometrie.y response = self.client.get( f'{self.geography_endpoint}/?maincategory_slug={parent_category.slug}' f'&category_slug={child_category.slug}&lon={lon}&lat={lat}') self.assertEqual(200, response.status_code) self.assertEqual(1, int(response.headers['X-Total-Count'])) data = response.json() self.assertEqual(1, len(data['features'])) for feature in data['features']: self.assertEqual(feature['properties']['category']['name'], child_category.public_name)
def test_get_geojson(self): """ Return the GEOJson containing all signals in the child category """ parent_category = ParentCategoryFactory.create() child_category = CategoryFactory.create( parent=parent_category, public_name='Public category for testing', is_public_accessible=True) now = timezone.now() for x in range(5): with (freeze_time(now - timedelta(days=x))): SignalFactoryValidLocation.create( category_assignment__category=child_category) response = self.client.get( f'{self.geography_endpoint}/?maincategory_slug={parent_category.slug}' f'&category_slug={child_category.slug}&bbox=4.700000,52.200000,5.000000,52.500000' ) self.assertEqual(200, response.status_code) self.assertEqual(5, int(response.headers['X-Total-Count'])) data = response.json() self.assertEqual(5, len(data['features'])) for feature in data['features']: self.assertEqual(feature['properties']['category']['name'], child_category.public_name)
def test_get_group_by(self): """ Return the GEOJson containing the first created_at in a BBOX grouped by a category """ parent_category = ParentCategoryFactory.create() child_category = CategoryFactory.create( parent=parent_category, public_name='Public category for testing', is_public_accessible=True) now = timezone.now() for x in range(5): with (freeze_time(now - timedelta(days=x))): # Should appear in the response SignalFactory.create( location__geometrie=Point(STADHUIS['lon'], STADHUIS['lat']), location__buurt_code=STADHUIS['buurt_code'], category_assignment__category=child_category) response = self.client.get( f'{self.geography_endpoint}/?maincategory_slug={parent_category.slug}' f'&category_slug={child_category.slug}&bbox=4.875759,52.360656,4.921221,52.37942' '&group_by=category') self.assertEqual(200, response.status_code) self.assertEqual(1, int(response.headers['X-Total-Count'])) data = response.json() self.assertEqual(1, len(data['features'])) self.assertEqual(data['features'][0]['properties']['category']['name'], child_category.public_name)
def handle(self, *args, **options): parent_slug = options.get('parent_slug', False) if parent_slug: parents_to_create = 1 parent_options = { 'slug': parent_slug, } else: parents_to_create = int(options['parents_to_create'] or self.default_to_create) parent_options = {} children_to_create = int(options['children_to_create'] or self.default_to_create) if not self.min_to_create <= parents_to_create <= self.max_to_create: self.stderr.write( f'The parents to create option must be an integer from {self.min_to_create} to ' f'{self.max_to_create}, {parents_to_create} given') return if not self.min_to_create <= children_to_create <= self.max_to_create: self.stderr.write( f'The children to create option must be an integer from {self.min_to_create} to ' f'{self.max_to_create}, {children_to_create} given') return parents = ParentCategoryFactory.create_batch(parents_to_create, **parent_options) for parent in parents: CategoryFactory.create_batch(children_to_create, parent=parent)
def test_no_category_filters(self): """ Get all the signals when no category is set """ cat1 = ParentCategoryFactory.create(name='trash') SignalFactoryValidLocation.create(category_assignment__category=cat1) cat2 = ParentCategoryFactory.create(name='animals') SignalFactoryValidLocation.create(category_assignment__category=cat2) SignalFactoryValidLocation.create(category_assignment__category=cat2) response = self.client.get( f'{self.geography_endpoint}/?bbox=4.700000,52.200000,5.000000,52.500000' ) data = response.json() self.assertEqual(3, len(data['features']))
def setUp(self): sia_category_write_permission = Permission.objects.get(codename='sia_category_write') self.sia_read_write_user.user_permissions.add(sia_category_write_permission) self.parent_category = ParentCategoryFactory.create() CategoryFactory.create_batch(5, parent=self.parent_category) self.parent_category.refresh_from_db() super().setUp()
def setUp(self): self.department_read = Permission.objects.get( codename='sia_department_read') self.department_write = Permission.objects.get( codename='sia_department_write') self.sia_read_write_user.user_permissions.add(self.department_read) self.sia_read_write_user.user_permissions.add(self.department_write) self.department = DepartmentFactory.create() self.maincategory = ParentCategoryFactory.create() self.subcategory = CategoryFactory.create(parent=self.maincategory)
def test_category_detail_questionnaire(self): questionnaire = QuestionnaireFactory.create() parent_category = ParentCategoryFactory.create( questionnaire=questionnaire) url = f'/signals/v1/public/terms/categories/{parent_category.slug}' response = self.client.get(url) self.assertEqual(response.status_code, 200) data = response.json() # JSONSchema validation self.assertJsonSchema(self.retrieve_category_schema, data) self.assertIsNotNone(data['_links']['sia:questionnaire'])
def setUp(self): self.retrieve_sub_category_question_schema = self.load_json_schema( os.path.join( THIS_DIR, 'json_schema', 'get_signals_v1_public_questions_categories_{slug}_sub_categories_{sub_slug}.json' ) ) question = QuestionFactory.create_batch(1) question2 = QuestionFactory.create_batch(1) self.parent_category = ParentCategoryFactory.create(questions=question2) CategoryFactory.create_batch(1, parent=self.parent_category, questions=question) self.parent_category.refresh_from_db() super().setUp()
def test_missing_bbox_and_lonlat(self): """ Validate that when neither bbox or lon/lat is filled in to return a non_field_error """ parent_category = ParentCategoryFactory.create() child_category = CategoryFactory.create(parent=parent_category, is_public_accessible=False) response = self.client.get( f'{self.geography_endpoint}/?maincategory_slug={parent_category.slug}' f'&category_slug={child_category.slug}') data = response.json() self.assertEqual(400, response.status_code) self.assertIn('non_field_errors', data.keys()) self.assertEqual(1, len(data['non_field_errors'])) self.assertEqual('Either bbox or lon/lat must be filled in', data['non_field_errors'][0])
def test_missing_lon(self): """ Validate that both lat /lon parameters have to be filled in if lons parameter is missing """ parent_category = ParentCategoryFactory.create() child_category = CategoryFactory.create(parent=parent_category, is_public_accessible=False) response = self.client.get( f'{self.geography_endpoint}/?maincategory_slug={parent_category.slug}' f'&category_slug={child_category.slug}&lat=5') data = response.json() self.assertEqual(400, response.status_code) self.assertIn('non_field_errors', data.keys()) self.assertEqual(1, len(data['non_field_errors'])) self.assertEqual('Either bbox or lon/lat must be filled in', data['non_field_errors'][0])
def test_filter_maincategory_and_category_slugs_not_a_unique_slug(self): parent_category = ParentCategoryFactory.create(name='not_unique') category = CategoryFactory.create(name='not_unique', parent=parent_category) SignalFactory.create(category_assignment__category=category) params = { 'maincategory_slug': [ parent_category.slug, ], 'category_slug': [ category.slug, ] } result_ids = self._request_filter_signals(params) self.assertEqual(1, len(result_ids))
def test_get_geojson_no_signals(self): """ Return an empty FeatureCollection because there are no signals at all """ parent_category = ParentCategoryFactory.create() child_category = CategoryFactory.create( parent=parent_category, public_name='Public category for testing', is_public_accessible=True) response = self.client.get( f'{self.geography_endpoint}/?maincategory_slug={parent_category.slug}' f'&category_slug={child_category.slug}&bbox=4.700000,52.200000,5.000000,52.500000' ) self.assertEqual(200, response.status_code) self.assertEqual(0, int(response.headers['X-Total-Count'])) data = response.json() self.assertIsNone(data['features'])
def test_child_category_public_name_and_is_public_accessible_true(self): """ Parent category has a public name and is_public_accessible set to False Child category has a public name and is_public_accessible set to True The is_public_accessible of the parent category should be True because there is at least one child category that has the is_public_accessible set to True. The is_public_accessible of the child category should be True in the response. """ parent_category = ParentCategoryFactory.create( public_name='Publieke naam hoofdcategorie', is_public_accessible=False) sub_category = CategoryFactory.create( parent=parent_category, public_name='Publieke naam kindcategorie', is_public_accessible=True) url = f'/signals/v1/public/terms/categories/{parent_category.slug}' response = self.client.get(url) self.assertEqual(response.status_code, 200) data = response.json() # JSONSchema validation self.assertJsonSchema(self.retrieve_category_schema, data) self.assertEqual(data['public_name'], parent_category.public_name) self.assertTrue(data['is_public_accessible']) self.assertEqual(data['sub_categories'][0]['public_name'], sub_category.public_name) self.assertTrue(data['sub_categories'][0]['is_public_accessible']) url = f'/signals/v1/public/terms/categories/{parent_category.slug}/sub_categories/{sub_category.slug}' response = self.client.get(url) self.assertEqual(response.status_code, 200) data = response.json() # JSONSchema validation self.assertJsonSchema(self.retrieve_sub_category_schema, data) self.assertEqual(data['public_name'], sub_category.public_name) self.assertTrue(data['is_public_accessible'])
def test_category_question_list(self): question = QuestionFactory.create_batch(1) question2 = QuestionFactory.create_batch(1) self.parent_category = ParentCategoryFactory.create( questions=question2) CategoryFactory.create_batch(1, parent=self.parent_category, questions=question) self.parent_category.refresh_from_db() endpoint_url = '/signals/v1/public/questions/' response = self.client.get(endpoint_url) self.assertEqual(response.status_code, 200) data = response.json() # JSONSchema validation self.assertJsonSchema(self.retrieve_sub_category_question_schema, data) self.assertEqual(data['count'], 2) # filter on main sub_category = self.parent_category.children.first() url = '{endp}?main_slug={slug}'.format(endp=endpoint_url, slug=sub_category.parent.slug) response = self.client.get(url) self.assertEqual(response.status_code, 200, msg=url) data = response.json() # JSONSchema validation self.assertJsonSchema(self.retrieve_sub_category_question_schema, data) self.assertEqual(data['count'], 1) # filter on main and sub sub_category = self.parent_category.children.first() url = '{endp}?main_slug={slug}&sub_slug={sub_slug}'.format( endp=endpoint_url, slug=sub_category.parent.slug, sub_slug=sub_category.slug) response = self.client.get(url) self.assertEqual(response.status_code, 200, msg=url) data = response.json() # JSONSchema validation self.assertJsonSchema(self.retrieve_sub_category_question_schema, data) self.assertEqual(data['count'], 2)
def test_command_with_parent_slug_children_to_create_parameter(self): parent_category = ParentCategoryFactory.create(slug='wonen') parent_db_cnt = Category.objects.filter(parent__isnull=True).count() child_db_cnt = Category.objects.filter(parent__isnull=False).count() out = StringIO() err = StringIO() call_command('dummy_categories', parent_slug=parent_category.slug, children_to_create=5, stdout=out, stderr=err) self.assertEqual( Category.objects.filter(parent__isnull=True).count(), parent_db_cnt) self.assertEqual( Category.objects.filter(parent__isnull=False).count(), child_db_cnt + 5)
def test_get_invalid_category_in_query_parameters(self): """ The query parameters category_slug should be a slug from a valid category that is public accessible """ parent_category = ParentCategoryFactory.create() child_category = CategoryFactory.create(parent=parent_category, is_public_accessible=False) # Now provide a BBOX, the only error should be the invalid category response = self.client.get( f'{self.geography_endpoint}/?maincategory_slug={parent_category.slug}' f'&category_slug={child_category.slug}&bbox=4.700000,52.200000,5.000000,52.500000' ) self.assertEqual(400, response.status_code) data = response.json() self.assertNotIn('bbox', data.keys()) self.assertNotIn('maincategory_slug', data.keys()) self.assertIn('category_slug', data.keys()) self.assertEqual(1, len(data['category_slug'])) self.assertEqual( f'Selecteer een geldige keuze. {child_category.slug} is geen beschikbare keuze.', data['category_slug'][0])
def test_create_and_validate_extra_properties_for_streetlights( self, validate_address): """ Disabled the additional check. This check prevents the creation of a "child" signal in the lantaarnpaal-straatverlichting category because there is no way to provide the streetlight SIG-4382 [BE] Extra check op formaat en inhoud van extra_properties bij de verlichting sub categorien (tbv koppeling Techview) To make sure we accept the valid data in the extra properties when a Signal is created for the category containing streetlights we are adding an additional check. """ parent_category = ParentCategoryFactory.create( slug='wegen-verkeer-straatmeubilair') link_main_category = f'/signals/v1/public/terms/categories/{parent_category.slug}' child_category = CategoryFactory.create( slug='lantaarnpaal-straatverlichting', parent=parent_category) link_sub_category = f'{link_main_category}/sub_categories/{child_category.slug}' extra_properties_none = None extra_properties_empty_list = [] extra_properties_not_on_map_simple = [ { 'id': 'extra_straatverlichting_nummer', 'label': 'Lichtpunt(en) op kaart', 'category_url': link_sub_category, 'answer': { 'type': 'not-on-map' }, }, ] extra_properties_not_on_map_complex = [ { 'id': 'extra_straatverlichting_nummer', 'label': 'Lichtpunt(en) op kaart', 'category_url': link_sub_category, 'answer': { 'id': '345345433', 'type': 'not-on-map', 'label': 'De container staat niet op de kaart - 345345433', }, }, ] extra_properties = [ { 'id': 'extra_straatverlichting_nummer', 'label': 'Lichtpunt(en) op kaart', 'category_url': link_sub_category, 'answer': { 'id': '115617', 'type': '4', 'description': 'Overig lichtpunt', 'isReported': False, 'label': 'Overig lichtpunt - 115617', }, }, ] create_initial_data = copy.deepcopy(self.create_initial_data) create_initial_data.update( {'category': { 'category_url': link_sub_category }}), for invalid_extra_properties in [ extra_properties_none, extra_properties_empty_list ]: create_initial_data.update( {'extra_properties': invalid_extra_properties}) response = self.client.post(self.list_endpoint, create_initial_data, format='json') self.assertEqual(400, response.status_code) self.assertEqual( response.json()['extra_properties'][0], 'Extra properties not valid for category "lantaarnpaal-straatverlichting"' ) self.assertEqual(0, Signal.objects.count()) signal_count = Signal.objects.count() for valid_extra_properties in [ extra_properties_not_on_map_simple, extra_properties_not_on_map_complex, extra_properties ]: create_initial_data.update( {'extra_properties': valid_extra_properties}) response = self.client.post(self.list_endpoint, create_initial_data, format='json') self.assertEqual(201, response.status_code) signal_count += 1 self.assertEqual(signal_count, Signal.objects.count())