def test_create_scheduled_query_pass(self, mock_login, mock_post):
        """
        Test ._create_scheduled_query() passes
        """
        # simulate login
        mock_login.return_value = self.account_id

        # Create the response
        mock_response = Mock(spec=requests.Response)
        mock_response.status_code = 201
        mock_response.json.return_value = {
            'scheduled_query': {
                'id': '00000000-0000-03a2-0000-000000000000'
            }
        }
        mock_response.ok = True

        mock_post.return_value = mock_response

        session = requests.session()

        alert = AnomalyAlert(self.username, self.password, session)

        query = 'where(status=404) calculate(COUNT)'
        scope_count = 1
        scope_unit = 'day'
        change = '+15'

        # Call .create
        response = alert._create_scheduled_query(
            query=query,
            change=change,
            scope_count=scope_count,
            scope_unit=scope_unit,
        )

        self.assertDictEqual(
            response,
            {
                'scheduled_query': {
                    'id': '00000000-0000-03a2-0000-000000000000'
                }
            }
        )
    def test_delete_passes(self, mock_login, mock_delete, mock_list_tags):
        """
        Test .delete() works
        """
        mock_login.return_value = self.account_id

        mock_response = Mock(spec=requests.Response)
        mock_response.status_code = 200
        mock_response.ok = True

        mock_delete.return_value = mock_response

        mock_list_tags.return_value = [
            {
                'id': '19dede15-118b-467f-bfe9-e9c771d7cc2c',
                'scheduled_query_id': '00000000-0000-469c-0000-000000000000'
            }
        ]

        alert = AnomalyAlert(self.username, self.password)

        alert.delete('19dede15-118b-467f-bfe9-e9c771d7cc2c')
        mock_delete.assert_has_calls(
            [
                call(
                    url='https://logentries.com/rest/{account_id}/api/tags/{tag}'.format(
                        account_id=self.account_id,
                        tag='19dede15-118b-467f-bfe9-e9c771d7cc2c'
                    )
                ),
                call(
                    url='https://logentries.com/rest/{account_id}/api/scheduled_queries/{query_id}'.format(
                        account_id=self.account_id,
                        query_id='00000000-0000-469c-0000-000000000000'
                    )
                ),
            ]
        )
    def test_delete_doesnt_exist(self, mock_login, mock_delete, mock_list_tags):
        """
        Test .delete() where key doesn't exist
        """
        mock_login.return_value = self.account_id

        mock_response = Mock(spec=requests.Response)
        mock_response.status_code = 200
        mock_response.ok = True

        mock_delete.return_value = mock_response

        mock_list_tags.return_value = [
            {
                'id': 'not-a-match',
                'scheduled_query_id': '00000000-0000-469c-0000-000000000000'
            }
        ]

        alert = AnomalyAlert(self.username, self.password)

        alert.delete('19dede15-118b-467f-bfe9-e9c771d7cc2c')

        self.assertFalse(mock_delete.called)
    def test_create_success(self, mock_login, mock_scheduled_query, mock_post):
        """
        Test .create() passes
        """
        # simulate login
        mock_login.return_value = self.account_id

        mock_scheduled_query.return_value = {
            'scheduled_query': {
                'id': '00000000-0000-03a2-0000-000000000000'
            }
        }

        # Create the response
        mock_response = Mock(spec=requests.Response)
        mock_response.status_code = 201
        mock_response.text = "Created"
        mock_response.ok = True

        mock_post.return_value = mock_response

        session = requests.session()

        alert = AnomalyAlert(self.username, self.password, session)

        name = 'Too few 404s'
        query = 'where(status=404) calculate(COUNT)'
        logs = [
            '5d481b23-9c4d-4250-bfe8-be389a227f0b',
        ]
        trigger_config = AlertTriggerConfig(
            timeframe_value=7,
            timeframe_period='day',
        )

        slack_url = 'https://hooks.slack.com/services'
        alert_config = SlackAlertConfig(slack_url)

        alert_reports = [
            AlertReportConfig(
                report_count=4,
                report_period='day',
                alert_config=alert_config,
            )
        ]

        # Call .create
        alert.create(
            name=name,
            query=query,
            scope_count=1,
            scope_unit='day',
            increase_positive=False,
            percentage_change=15,
            trigger_config=trigger_config,
            logs=logs,
            alert_reports=alert_reports
        )

        headers = alert.default_headers.copy()
        headers.update({
            'Content-Type': 'application/json;charset=utf-8',
            'Accept': 'application/json, text/plain, */*',
            'Referer': 'https://logentries.com/app/{account_id}'.format(account_id=alert.account_id),
            'X-CSRFToken': alert._get_csrf_token(),
        })

        data = {
            'tag': {
                'actions': [
                    {
                        'enabled': True,
                        'min_report_count': 4,
                        'min_report_period': 'Day',
                        'targets': [{
                            'type': 'slack',
                            'params_set': {
                                'url': slack_url
                            }
                        }],
                        'type': 'Alert',
                    },
                ],
                'name': name[:30],
                'scheduled_query_id': '00000000-0000-03a2-0000-000000000000',
                'sources': [
                    {'id': '5d481b23-9c4d-4250-bfe8-be389a227f0b'}
                ],
                'sub_type': 'AnomalyAlert',
                'type': 'AlertNotify',
                'timeframe_period': 'Day',
                'timeframe_value': 7
            }
        }

        mock_post.assert_called_once_with(
            alert.session,
            url='https://logentries.com/rest/{account_id}/api/tags'.format(
                account_id=alert.account_id
            ),
            headers=headers,
            data=json.dumps(data, sort_keys=True)
        )

        mock_scheduled_query.assert_called_once_with(
            query=query,
            change='-15',
            scope_unit='day',
            scope_count=1,
        )