Exemplo n.º 1
0
    def test_ratio_aggregation(self):
        service_env = ServiceEnvironmentFactory(service__name='sample-service')
        vulnerability = VulnerabilityFactory(
            patch_deadline=datetime.date(2015, 1, 1)
        )
        for is_patched in [True, False]:
            for _ in range(3):
                dca = DataCenterAssetFactory(service_env=service_env)
                if is_patched:
                    ss = SecurityScanFactory(vulnerabilities=[])
                else:
                    ss = SecurityScanFactory(vulnerabilities=[vulnerability])
                dca.securityscan = ss
                ss.save()
                dca.save()
        graph = GraphFactory(
            aggregate_type=AggregateType.aggregate_ratio.id,
            params={
                'series': ['securityscan__is_patched', 'id'],
                'labels': 'service_env__service__name',
                'filters': {
                    'series__gt': 0,
                }
            }
        )

        qs = graph.build_queryset()
        self.assertEqual(qs.get(), {
            'series': 50,
            'service_env__service__name': 'sample-service'
        })
Exemplo n.º 2
0
    def test_filtering_queryset(self):
        DataCenterAssetFullFactory.create_batch(
            2,
            service_env__service__name='ServiceA',
        )
        DataCenterAssetFullFactory.create_batch(
            1,
            service_env__service__name='ServiceB',
        )
        DataCenterAssetFullFactory.create_batch(
            3,
            service_env__service__name='ServiceC',
        )
        ServiceEnvironmentFactory.create(service__name='ServiceD')

        graph = GraphFactory(
            aggregate_type=AggregateType.aggregate_count.id,
            params={
                'series': 'id',
                'labels': 'service_env__service__name',
            },
        )

        dca_qs = DataCenterAsset.objects.all()
        filtered_qs = graph.get_queryset_for_filter(
            dca_qs, {
                'service_env__service__name': 'ServiceA',
            })

        self.assertEqual(filtered_qs.count(), 2)
        self.assertEqual(
            list(
                filtered_qs.values_list('service_env__service__name',
                                        flat=True)), ['ServiceA', 'ServiceA'])
Exemplo n.º 3
0
    def test_key_sort_sorts_records_descending_when_minus_present(self):
        self.data_center_assets = DataCenterAssetFullFactory.create_batch(10)
        graph = GraphFactory(
            params=self._get_graph_params({'sort': '-barcode'})
        )

        qs = graph.build_queryset()

        self.assertTrue(qs.first()['barcode'] > qs.last()['barcode'])
Exemplo n.º 4
0
    def test_key_limit_limits_records_when_present(self):
        limit = 5
        self.data_center_assets = DataCenterAssetFullFactory.create_batch(
            2 * limit
        )
        graph = GraphFactory(params=self._get_graph_params({'limit': limit}))

        qs = graph.build_queryset()

        self.assertEqual(qs.count(), limit)
Exemplo n.º 5
0
 def test_endpoint_should_return_get_data_and_params(self):
     graph = GraphFactory(aggregate_type=AggregateType.aggregate_count.id,
                          params={
                              'series': 'id',
                              'labels': 'hostname',
                          })
     url = reverse('graph-detail', args=(graph.id, ))
     response = self.client.get(url)
     self.assertEqual(
         response.data, {
             'name': graph.name,
             'description': graph.description,
             'data': graph.get_data(),
             'params': graph.params
         })
Exemplo n.º 6
0
    def test_filtering_queryset_with_additional_filters(self):
        service_env_a = ServiceEnvironmentFactory(service__name='ServiceA')
        DataCenterAssetFullFactory.create_batch(2,
                                                service_env=service_env_a,
                                                scmstatuscheck=None)
        DataCenterAssetFullFactory.create_batch(
            3,
            service_env=service_env_a,
            scmstatuscheck__check_result=SCMCheckResult.scm_error)
        DataCenterAssetFullFactory.create_batch(4,
                                                service_env=service_env_a,
                                                scmstatuscheck__ok=True)
        DataCenterAssetFullFactory.create_batch(
            1,
            service_env__service__name='ServiceB',
        )
        DataCenterAssetFullFactory.create_batch(
            3,
            service_env__service__name='ServiceC',
        )
        ServiceEnvironmentFactory.create(service__name='ServiceD')

        graph = GraphFactory(
            aggregate_type=AggregateType.aggregate_sum_bool_negated_values.id,
            params={
                'series': 'id',
                'labels': 'service__name',
                'target': {
                    'model': 'DataCenterAsset',
                    'filter': 'service_env__service__name__in',
                    'value': 'service__name',
                    'additional_filters': {
                        'scmstatuscheck__ok': False
                    },
                }
            },
            model=ContentType.objects.get_for_model(ServiceEnvironment),
        )

        dca_qs = DataCenterAsset.objects.all()
        filtered_qs = graph.get_queryset_for_filter(
            dca_qs, {'service__name': 'ServiceA'})
        self.assertEqual(filtered_qs.count(), 3)
        self.assertEqual(
            list(
                filtered_qs.values_list('service_env__service__name',
                                        'scmstatuscheck__ok')),
            [('ServiceA', False)] * 3)
Exemplo n.º 7
0
 def test_count_aggregate_with_zeros(self):
     assets_num = 2
     DataCenterAssetFactory.create_batch(assets_num)
     graph = GraphFactory(
         aggregate_type=AggregateType.aggregate_count.id,
         params=self._get_graph_params({
             'aggregate_expression': 'scmstatuscheck',
             'filters': {},
             'labels': 'id',
             'series': 'id',
         })
     )
     qs = graph.build_queryset()
     self.assertEqual(qs.count(), assets_num)
     for item in qs.all():
         self.assertEqual(item['series'], 0)
Exemplo n.º 8
0
 def test_count_aggregate_sum_bool_values(self):
     assets_num = 2
     a, b = DataCenterAssetFactory.create_batch(assets_num)
     SCMStatusCheckFactory(
         base_object=a, check_result=SCMCheckResult.scm_ok.id
     )
     SCMStatusCheckFactory(
         base_object=b, check_result=SCMCheckResult.scm_error.id
     )
     graph = GraphFactory(
         aggregate_type=AggregateType.aggregate_sum_bool_values.id,
         params=self._get_graph_params({
             'filters': {},
             'labels': 'id',
             'series': 'scmstatuscheck__ok',
         })
     )
     qs = graph.build_queryset()
     self.assertTrue(qs.get(id=a.id)['series'] == 1)
     self.assertTrue(qs.get(id=b.id)['series'] == 0)
Exemplo n.º 9
0
    def test_label_works_when_month_grouping_on_foreign_key(self):
        self._genenrate_dca_with_scan(2, '2015-01-01')
        expected = self._genenrate_dca_with_scan(1, '2016-01-01')
        self._genenrate_dca_with_scan(3, '2017-01-01')

        graph = GraphFactory(
            aggregate_type=AggregateType.aggregate_count.id,
            params={
                'filters': {
                    'securityscan__last_scan_date__gte': '2016-01-01',
                    'securityscan__last_scan_date__lt': '2017-01-01',
                },
                'series': 'id',
                'labels': 'securityscan__last_scan_date|month',
            }
        )

        qs = graph.build_queryset()

        self.assertEqual(qs.get()['series'], len(expected))
        self.assertIn('month', qs.get())
Exemplo n.º 10
0
    def test_label_works_when_year_grouping(self):
        self.a_2016 = DataCenterAssetFactory.create_batch(
            2, delivery_date='2015-01-01',
        )
        expected = DataCenterAssetFactory.create_batch(
            1, delivery_date='2016-01-01',
        )
        self.a_2015 = DataCenterAssetFactory.create_batch(
            3, delivery_date='2017-01-01',
        )
        graph = GraphFactory(
            aggregate_type=AggregateType.aggregate_count.id,
            params=self._get_graph_params({
                'labels': 'delivery_date|year',
            })
        )

        qs = graph.build_queryset()

        self.assertEqual(qs.get()['series'], len(expected))
        self.assertIn('year', qs.get())
Exemplo n.º 11
0
    def test_duplicates_works_when_used_in_series_value(self):
        SecurityScanFactory(
            base_object=DataCenterAssetFactory().baseobject_ptr,
            vulnerabilities=[
                VulnerabilityFactory(
                    patch_deadline=datetime.datetime.strptime(
                        '2015-01-01', '%Y-%m-%d'
                    )
                ),
            ]
        )

        SecurityScanFactory(
            base_object=DataCenterAssetFactory().baseobject_ptr,
            vulnerabilities=[
                VulnerabilityFactory(
                    patch_deadline=datetime.datetime.strptime(
                        '2016-01-01', '%Y-%m-%d'
                    )
                ),
                VulnerabilityFactory(
                    patch_deadline=datetime.datetime.strptime(
                        '2016-02-02', '%Y-%m-%d'
                    )
                ),
                VulnerabilityFactory(
                    patch_deadline=datetime.datetime.strptime(
                        '2016-03-03', '%Y-%m-%d'
                    )
                ),
            ]
        )

        graph = GraphFactory(
            aggregate_type=AggregateType.aggregate_count.id,
            params={
                'filters': {
                    'patch_deadline__gte': '2010-01-01',
                    'securityscan__base_object__isnull': False,
                },
                'series': 'securityscan|distinct',
                'labels': 'patch_deadline|year',
            }
        )
        graph.model = ContentType.objects.get_for_model(Vulnerability)
        graph.save()

        qs = graph.build_queryset()

        self.assertEqual(qs.all()[0]['series'], 1)
        self.assertEqual(qs.all()[1]['series'], 1)
Exemplo n.º 12
0
    def test_get_data_for_choices_field_returns_names(self):
        test_data = {
            SCMCheckResult.scm_ok: 3,
            SCMCheckResult.check_failed: 2,
            SCMCheckResult.scm_error: 1
        }

        data_center_assets = DataCenterAssetFullFactory.create_batch(
            10, scmstatuscheck=None
        )
        scm_checks = []

        dca_number = 0
        for check_result in test_data:
            for i in range(test_data[check_result]):
                scm_checks.append(
                    SCMStatusCheckFactory(
                        check_result=check_result,
                        base_object=data_center_assets[dca_number]
                    )
                )
                dca_number += 1

        graph = GraphFactory(
            params=self._get_graph_params(
                {
                    "filters": {},
                    "labels": "scmstatuscheck__check_result",
                    "series": "id",
                    "sort": "series"
                }
            )
        )

        for check_result in test_data:
            encoded_params = encode_params({
                'pk': graph.pk,
                'filters': {'scmstatuscheck__check_result': check_result.id}
            })
            graph_filter = ByGraphFilter(
                None,
                {'graph-query': encoded_params},
                DataCenterAsset,
                DataCenterAdmin
            )
            qs = graph_filter.queryset(None, DataCenterAsset.objects.all())

            self.assertEqual(len(qs), test_data[check_result])

        encoded_params = encode_params({
            'pk': graph.pk,
            'filters': {'scmstatuscheck__check_result': None}
        })
        graph_filter = ByGraphFilter(
            None,
            {'graph-query': encoded_params},
            DataCenterAsset,
            DataCenterAdmin
        )
        qs = graph_filter.queryset(None, DataCenterAsset.objects.all())

        self.assertEqual(len(qs), len(data_center_assets) - dca_number)