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 test_create_child_signal_transform_source(self, validate_address):
        parent_signal = SignalFactory.create()
        signal_count = Signal.objects.count()

        source, *_ = SourceFactory.create_batch(4)
        SourceFactory.create(
            name=settings.API_TRANSFORM_SOURCE_OF_CHILD_SIGNAL_TO)

        initial_data = copy.deepcopy(self.initial_data_base)
        initial_data['source'] = source.name
        initial_data['parent'] = parent_signal.pk

        with self.settings(FEATURE_FLAGS={
                'API_TRANSFORM_SOURCE_IF_A_SIGNAL_IS_A_CHILD': True
        }):
            response = self.client.post(self.list_endpoint,
                                        initial_data,
                                        format='json')

        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(Signal.objects.count(), signal_count + 1)

        response_data = response.json()
        self.assertNotEqual(response_data['source'], source.name)
        self.assertEqual(response_data['source'],
                         settings.API_TRANSFORM_SOURCE_OF_CHILD_SIGNAL_TO)

        signal = Signal.objects.get(pk=response_data['id'])
        self.assertNotEqual(signal.source, source.name)
        self.assertEqual(signal.source,
                         settings.API_TRANSFORM_SOURCE_OF_CHILD_SIGNAL_TO)
    def test_create_initial_child_signals_validate_source_online(self, validate_address):
        # Validating a valid source for child Signals causes a HTTP 500 in
        # SIA production, this testcase reproduces the problem.
        SourceFactory.create(name='online', description='online')

        with self.settings(FEATURE_FLAGS=self.prod_feature_flags_settings):
            parent_signal = SignalFactory.create()

            signal_count = Signal.objects.count()
            parent_signal_count = Signal.objects.filter(parent_id__isnull=True).count()
            child_signal_count = Signal.objects.filter(parent_id__isnull=False).count()

            self.assertEqual(signal_count, 1)
            self.assertEqual(parent_signal_count, 1)
            self.assertEqual(child_signal_count, 0)

            initial_data = []
            for i in range(2):
                data = copy.deepcopy(self.initial_data_base)
                data['parent'] = parent_signal.pk
                data['source'] = 'online'
                initial_data.append(data)

            response = self.client.post(self.list_endpoint, initial_data, format='json')

            self.assertEqual(response.status_code, status.HTTP_201_CREATED)
            self.assertEqual(Signal.objects.count(), signal_count + len(initial_data))
            self.assertEqual(Signal.objects.filter(parent_id__isnull=True).count(), parent_signal_count)
            self.assertEqual(Signal.objects.filter(parent_id__isnull=False).count(), len(initial_data))
    def test_signal_ids_cannot_be_skipped(self, validate_address):
        SourceFactory.create(name='online', description='online')

        with self.settings(FEATURE_FLAGS=self.prod_feature_flags_settings):
            parent_signal = SignalFactory.create()

            signal_count = Signal.objects.count()
            parent_signal_count = Signal.objects.filter(
                parent_id__isnull=True).count()
            child_signal_count = Signal.objects.filter(
                parent_id__isnull=False).count()

            self.assertEqual(signal_count, 1)
            self.assertEqual(parent_signal_count, 1)
            self.assertEqual(child_signal_count, 0)

            # bad data
            initial_data = []
            for _ in range(2):
                data = copy.deepcopy(self.initial_data_base)
                data['parent'] = parent_signal.pk
                data['source'] = 'online'
                data['category'] = {
                    'subcategory': data['category']['category_url']
                }
                initial_data.append(data)

            with self.assertRaises(IntegrityError):
                response = self.client.post(self.list_endpoint,
                                            initial_data,
                                            format='json')

            # good data
            initial_data = []
            for _ in range(2):
                data = copy.deepcopy(self.initial_data_base)
                data['parent'] = parent_signal.pk
                data['source'] = 'online'
                initial_data.append(data)

            response = self.client.post(self.list_endpoint,
                                        initial_data,
                                        format='json')
            response_json = response.json()

            self.assertEqual(response.status_code, status.HTTP_201_CREATED)
            self.assertEqual(Signal.objects.count(),
                             signal_count + len(initial_data))
            self.assertEqual(
                Signal.objects.filter(parent_id__isnull=True).count(),
                parent_signal_count)
            self.assertEqual(
                Signal.objects.filter(parent_id__isnull=False).count(),
                len(initial_data))

            # check that we did not skip signal ids
            ids = [entry['id'] for entry in response_json]
            self.assertEqual(ids[0] - parent_signal.id, 1)
            self.assertEqual(ids[1] - parent_signal.id, 2)
Esempio n. 5
0
 def handle(self, *args, **options):
     to_create = int(options['to_create'] or self.default_to_create)
     if not self.min_to_create <= to_create <= self.max_to_create:
         self.stderr.write(
             f'The to create option must be an integer from {self.min_to_create} to '
             f'{self.max_to_create}, {to_create} given')
     else:
         for _ in range(to_create):
             SourceFactory.create(name=slugify(fake.words()),
                                  description=fake.sentence(nb_words=10))
         self.stdout.write(f'Created {to_create} random Source(s)')
    def test_create_initial_signal_public_source(self, validate_address):
        public_source = SourceFactory.create(name='app',
                                             is_public=True,
                                             is_active=True)
        signal_count = Signal.objects.count()

        initial_data = copy.deepcopy(self.initial_data_base)
        initial_data['source'] = public_source.name
        response = self.client.post(self.list_endpoint,
                                    initial_data,
                                    format='json')

        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(Signal.objects.count(), signal_count)
    def test_create_with_source_with_is_public_property_set_to_true(
            self, validate_address):
        public_source = SourceFactory.create(is_public=True)

        create_initial_data = copy.deepcopy(self.create_initial_data)
        create_initial_data.update({'source': public_source.name})
        response = self.client.post(self.list_endpoint,
                                    create_initial_data,
                                    format='json')

        self.assertEqual(201, response.status_code)
        self.assertJsonSchema(self.create_schema, response.json())
        self.assertEqual(1, Signal.objects.count())

        signal = Signal.objects.last()
        self.assertEqual(signal.source, public_source.name)
    def test_create_with_source_with_is_public_property_set_to_false(
            self, validate_address):
        private_source = SourceFactory.create(is_public=False)

        create_initial_data = copy.deepcopy(self.create_initial_data)
        create_initial_data.update({'source': private_source.name})
        response = self.client.post(self.list_endpoint,
                                    create_initial_data,
                                    format='json')

        self.assertEqual(400, response.status_code)
        self.assertEqual(0, Signal.objects.count())

        response_json = response.json()
        self.assertEqual(len(response_json['source']), 1)
        self.assertEqual(response_json['source'][0],
                         'Invalid source given, value not known')