def test_fails_without_a_token(self):
        job_scope = JobScope(tokens=[None],
                             report_time=datetime.utcnow(),
                             report_type='entity',
                             report_variant=Entity.Page,
                             sweep_id='1')

        with SweepRunningFlag(job_scope.sweep_id), mock.patch.object(
                report_job_status_task, 'delay') as status_task, mock.patch(
                    'common.error_inspector.BugSnagContextData.notify'
                ) as bugsnag_notify, mock.patch(
                    'common.error_inspector.API_KEY', 'something'):
            collect_pages_from_business_task(job_scope, JobContext())

            assert bugsnag_notify.called
            actual_args, actual_kwargs = bugsnag_notify.call_args
            assert (
                str(actual_args[0]) ==
                'Job fb||||entity|P cannot proceed. No platform tokens provided.'
            ), 'Notify bugsnag correctly using correct Exception'
            assert {
                'severity': SEVERITY_ERROR,
                'job_scope': job_scope,
                'error_type': ErrorTypesReport.UNKNOWN,
            } == actual_kwargs, 'Notify bugsnag correctly'
            assert status_task.called

            status_task_args, _ = status_task.call_args
            assert (
                JobStatus.GenericError, job_scope
            ) == status_task_args, 'Must report status correctly on failure'
    def test_fails_with_wrong_report_variant(self):
        job_scope = JobScope(tokens=['blah'],
                             report_time=datetime.utcnow(),
                             report_type='entity',
                             report_variant=None,
                             sweep_id='1')

        with SweepRunningFlag(job_scope.sweep_id), mock.patch.object(
                report_job_status_task, 'delay') as status_task, mock.patch(
                    'common.error_inspector.BugSnagContextData.notify'
                ) as bugsnag_notify, mock.patch(
                    'common.error_inspector.API_KEY', 'something'):
            collect_pages_from_business_task(job_scope, JobContext())

            assert bugsnag_notify.called
            actual_args, actual_kwargs = bugsnag_notify.call_args
            assert (isinstance(actual_args[0], ValueError) and str(
                actual_args[0]) == 'Report level None specified is not: P'
                    ), 'Notify bugsnag correctly using correct Exception'
            assert {
                'severity': SEVERITY_ERROR,
                'job_scope': job_scope,
                'error_type': ErrorTypesReport.UNKNOWN,
            } == actual_kwargs, 'Notify bugsnag correctly'
            assert status_task.called
            parameters, _ = status_task.call_args
            assert (JobStatus.GenericError, job_scope
                    ) == parameters, 'Must report status correctly on failure'
Пример #3
0
    def test_page_import_dies_on_missing_token(self):

        job_scope = JobScope(
            sweep_id=self.sweep_id,
            entity_type=Entity.Scope,
            entity_id=self.scope_id,
            report_type=ReportType.import_pages,
            report_variant=Entity.Page,
            # Not passing tokens here
        )

        with mock.patch.object(
                report_job_status_task,
                'delay') as status_task, mock.patch.object(
                    PlatformTokenManager, 'get_best_token',
                    return_value=None) as get_best_token, mock.patch(
                        'common.error_inspector.BugSnagContextData.notify'
                    ) as bugsnag_notify, mock.patch(
                        'common.error_inspector.API_KEY', 'something'):

            # and code that falls back on PlatformTokenManager.get_best_token() gets nothing.

            import_pages_task(job_scope, None)
            assert bugsnag_notify.called
            actual_args, actual_kwargs = bugsnag_notify.call_args
            assert ' cannot proceed. No tokens provided.' in str(
                actual_args[0]
            ), 'Notify bugsnag correctly using correct Exception'
            assert {
                'severity': SEVERITY_ERROR,
                'job_scope': job_scope,
                'error_type': ErrorTypesReport.UNKNOWN,
            } == actual_kwargs, 'Notify bugsnag correctly'

        assert get_best_token.called

        # and must report failure status
        assert status_task.called
        aa, kk = status_task.call_args
        assert not kk
        assert aa == (JobStatus.GenericError, job_scope)
Пример #4
0
    def test_page_import(self):

        page_id = random.gen_string_id()

        pages = [dict(ad_account_id=page_id)]

        job_scope = JobScope(
            sweep_id=self.sweep_id,
            entity_type=Entity.Scope,
            entity_id=self.scope_id,
            report_type=ReportType.import_accounts,
            report_variant=Entity.Page,
            tokens=['token'],
        )

        with mock.patch.object(
                ConsoleApi, 'get_pages', return_value=pages
        ) as gp, mock.patch.object(
                PageEntity, 'upsert'
        ) as page_upsert, mock.patch.object(
                report_job_status_task, 'delay'
        ) as status_task, mock.patch(
                'oozer.entities.import_scope_entities_task._have_entity_access',
                return_value=True) as _have_entity_access_mock:
            import_pages_task(job_scope, None)

        assert gp.called

        assert status_task.called
        # it was called many times, but we care about the last time only
        aa, kk = status_task.call_args
        assert not kk
        assert aa == (JobStatus.Done, job_scope)

        assert page_upsert.call_count == 1

        page_upsert_args = (
            (self.scope_id, page_id),
            {
                'is_active': True,
                'updated_by_sweep_id': self.sweep_id,
                'is_accessible': True
            },
        )

        args1 = page_upsert.call_args_list[0]

        assert args1 == page_upsert_args
Пример #5
0
    def test_aa_import(self):

        active_ad_account_id = random.gen_string_id()
        inactive_ad_account_id = random.gen_string_id()

        accounts = [
            dict(ad_account_id=active_ad_account_id, active=True),
            dict(ad_account_id=inactive_ad_account_id, active=False),
        ]

        job_scope = JobScope(
            sweep_id=self.sweep_id,
            entity_type=Entity.Scope,
            entity_id=self.scope_id,
            report_type=ReportType.import_accounts,
            report_variant=Entity.AdAccount,
            tokens=['token'],
        )

        with mock.patch.object(
                ConsoleApi, 'get_accounts', return_value=accounts
        ) as gaa, mock.patch.object(
                AdAccountEntity, 'upsert'
        ) as aa_upsert, mock.patch.object(
                report_job_status_task, 'delay'
        ) as status_task, mock.patch(
                'oozer.entities.import_scope_entities_task._have_entity_access',
                return_value=True) as _have_entity_access_mock:

            import_ad_accounts_task(job_scope, None)

        assert gaa.called

        assert status_task.called
        # it was called many times, but we care about the last time only
        aa, kk = status_task.call_args
        assert not kk
        assert aa == (JobStatus.Done, job_scope)

        assert aa_upsert.call_count == 2

        active_account_upsert_args = (
            (self.scope_id, active_ad_account_id),
            {
                'is_active': True,
                'updated_by_sweep_id': self.sweep_id,
                'is_accessible': True
            },
        )

        inactive_account_upsert_args = (
            (self.scope_id, inactive_ad_account_id),
            {
                'is_active': False,
                'updated_by_sweep_id': self.sweep_id,
                'is_accessible': True
            },
        )

        args1, args2 = aa_upsert.call_args_list

        assert args1 == active_account_upsert_args
        assert args2 == inactive_account_upsert_args