def test_celery_error_task_should_be_set_as_failed_in_the_log(self): error_task.apply_async_on_commit() assert_equal(CeleryTaskLog.objects.count(), 1) assert_equal(CeleryTaskLog.objects.get().state, CeleryTaskLogState.FAILED) assert_equal(CeleryTaskRunLog.objects.count(), 1) assert_equal(CeleryTaskRunLog.objects.get().state, CeleryTaskRunLogState.FAILED) assert_is_not_none(CeleryTaskRunLog.objects.get().error_message)
def test_sms_template_should_be_immediately_send(self): responses.add(responses.POST, settings.ATS_URL, content_type='text/xml', body=self.ATS_SINGLE_SMS_REQUEST_RESPONSE_SENT.format(245), status=200) sms1 = send_template('+420777111222', slug='test', context={'variable': 'context works'}, pk=245) sms1 = OutputSMS.objects.get(pk=sms1.pk) assert_equal(sms1.state, ATS_STATES.OK) assert_true('context works' in sms1.content) assert_is_not_none(sms1.sent_at)
def test_sms_template_should_be_immediately_send(self): responses.add(responses.POST, settings.ATS_SMS_URL, content_type='text/xml', body=self.ATS_SINGLE_SMS_REQUEST_RESPONSE_SENT.format(245), status=200) sms1 = send_template('+420777111222', slug='test', context={'variable': 'context works'}, pk=245) sms1 = OutputSMS.objects.get(pk=sms1.pk) assert_equal(sms1.state, ATS_STATES.OK) assert_true('context works' in sms1.content) assert_is_not_none(sms1.sent_at)
def test_input_request_to_error_page_should_be_logged_in_sql_backend(self): with assert_raises(RuntimeError): self.get('/error/') assert_equal(SQLInputRequestLog.objects.count(), 1) sql_input_request_log = SQLInputRequestLog.objects.get() assert_equal_model_fields( sql_input_request_log, method='GET', path='/error/', time=(sql_input_request_log.stop - sql_input_request_log.start).total_seconds(), response_code=500, state=RequestLogState.ERROR, ) assert_is_not_none(sql_input_request_log.error_message)
def test_command_should_send_and_update_sms(self): responses.add(responses.POST, settings.ATS_URL, content_type='text/xml', body=self.ATS_SMS_REQUEST_RESPONSE_SENT.format(*self.ATS_TEST_UNIQ), status=200) sms1 = OutputSMSFactory(pk=self.ATS_TEST_UNIQ[0], **self.ATS_OUTPUT_SMS1) sms2 = OutputSMSFactory(pk=self.ATS_TEST_UNIQ[1], **self.ATS_OUTPUT_SMS2) SendCommand().handle() sms1 = OutputSMS.objects.get(pk=sms1.pk) sms2 = OutputSMS.objects.get(pk=sms2.pk) assert_equal(sms1.state, ATS_STATES.OK) assert_is_not_none(sms1.sent_at) assert_equal(sms2.state, ATS_STATES.LOCAL_UNKNOWN_ATS_STATE) assert_is_not_none(sms2.sent_at)
def test_input_request_to_error_page_should_be_logged_in_elasticsearch_backend(self): with capture_security_logs() as logged_data: with assert_raises(RuntimeError): self.get('/error/') elasticsearch_input_request_log = ElasticsearchInputRequestLog.get( id=logged_data.input_request[0].id ) assert_equal_model_fields( elasticsearch_input_request_log, method='GET', path='/error/', time=(elasticsearch_input_request_log.stop - elasticsearch_input_request_log.start).total_seconds(), response_code=500, state=RequestLogState.ERROR, ) assert_is_not_none(elasticsearch_input_request_log.error_message)
def test_command_should_send_and_update_sms(self): responses.add(responses.POST, settings.ATS_URL, content_type='text/xml', status=200, body=self.ATS_SMS_REQUEST_RESPONSE_SENT.format(prefix=settings.ATS_UNIQ_PREFIX, **self.ATS_TEST_UNIQ)) sms1 = OutputSMSFactory(pk=self.ATS_TEST_UNIQ['uniq1'], **self.ATS_OUTPUT_SMS1) sms2 = OutputSMSFactory(pk=self.ATS_TEST_UNIQ['uniq2'], **self.ATS_OUTPUT_SMS2) call_command('send_sms', stdout=StringIO(), stderr=StringIO()) sms1 = OutputSMS.objects.get(pk=sms1.pk) sms2 = OutputSMS.objects.get(pk=sms2.pk) assert_equal(sms1.state, ATS_STATES.OK) assert_is_not_none(sms1.sent_at) assert_equal(sms2.state, ATS_STATES.LOCAL_UNKNOWN_ATS_STATE) assert_is_not_none(sms2.sent_at)
def test_error_command_should_be_logged_in_sql_backend(self, user): with log_with_data(related_objects=[user]): with assert_raises(RuntimeError): test_call_command('test_error_command') assert_equal(SQLCommandLog.objects.count(), 1) sql_command_log = SQLCommandLog.objects.get() assert_equal_model_fields( sql_command_log, name='test_error_command', input='', is_executed_from_command_line=False, time=(sql_command_log.stop - sql_command_log.start).total_seconds(), state=CommandState.FAILED, output=None, ) assert_is_not_none(sql_command_log.error_message) assert_equal([rel_obj.object for rel_obj in sql_command_log.related_objects.all()], [user])
def test_command_should_be_logged_in_sql_backend(self, user): with log_with_data(related_objects=[user]): test_call_command('test_command', verbosity=0) assert_equal(SQLCommandLog.objects.count(), 1) sql_command_log = SQLCommandLog.objects.get() assert_equal_model_fields( sql_command_log, name='test_command', input='verbosity=0', is_executed_from_command_line=False, time=(sql_command_log.stop - sql_command_log.start).total_seconds(), state=CommandState.SUCCEEDED, error_message=None, ) assert_is_not_none(sql_command_log.output) assert_equal([rel_obj.object for rel_obj in sql_command_log.related_objects.all()], [user]) assert_equal(get_logs_related_with_object(LoggerName.COMMAND, user), [sql_command_log])
def test_command_should_send_and_update_sms(self): responses.add(responses.POST, settings.ATS_SMS_URL, content_type='text/xml', status=200, body=self.ATS_SMS_REQUEST_RESPONSE_SENT.format(prefix=settings.ATS_SMS_UNIQ_PREFIX, **self.ATS_TEST_UNIQ)) sms1 = OutputSMSFactory(pk=self.ATS_TEST_UNIQ['uniq1'], **self.ATS_OUTPUT_SMS1) sms2 = OutputSMSFactory(pk=self.ATS_TEST_UNIQ['uniq2'], **self.ATS_OUTPUT_SMS2) call_command('send_sms', stdout=StringIO(), stderr=StringIO()) sms1 = OutputSMS.objects.get(pk=sms1.pk) sms2 = OutputSMS.objects.get(pk=sms2.pk) assert_equal(sms1.state, ATS_STATES.OK) assert_is_not_none(sms1.sent_at) assert_equal(sms2.state, ATS_STATES.LOCAL_UNKNOWN_ATS_STATE) assert_is_not_none(sms2.sent_at)
def test_error_command_should_be_logged_in_elasticsearch_backend(self, user): with capture_security_logs() as logged_data: with log_with_data(related_objects=[user]): with assert_raises(RuntimeError): test_call_command('test_error_command') elasticsearch_command_log = ElasticsearchCommandLog.get( id=logged_data.command[0].id ) assert_equal_model_fields( elasticsearch_command_log, name='test_error_command', input='', is_executed_from_command_line=False, time=(elasticsearch_command_log.stop - elasticsearch_command_log.start).total_seconds(), state=CommandState.FAILED, output=None, ) assert_is_not_none(elasticsearch_command_log.error_message) assert_equal( [rel_obj for rel_obj in elasticsearch_command_log.related_objects], ['default|3|{}'.format(user.id)] )
def test_command_should_send_and_update_sms(self): responses.add(responses.POST, settings.ATS_URL, content_type='text/xml', body=self.ATS_SMS_REQUEST_RESPONSE_SENT.format( *self.ATS_TEST_UNIQ), status=200) sms1 = OutputSMSFactory(pk=self.ATS_TEST_UNIQ[0], **self.ATS_OUTPUT_SMS1) sms2 = OutputSMSFactory(pk=self.ATS_TEST_UNIQ[1], **self.ATS_OUTPUT_SMS2) SendCommand().handle() sms1 = OutputSMS.objects.get(pk=sms1.pk) sms2 = OutputSMS.objects.get(pk=sms2.pk) assert_equal(sms1.state, ATS_STATES.OK) assert_is_not_none(sms1.sent_at) assert_equal(sms2.state, ATS_STATES.LOCAL_UNKNOWN_ATS_STATE) assert_is_not_none(sms2.sent_at)
def test_command_should_be_logged_in_elasticsearch_backend(self, user): with capture_security_logs() as logged_data: with log_with_data(related_objects=[user]): test_call_command('test_command', verbosity=0) elasticsearch_command_log = ElasticsearchCommandLog.get( id=logged_data.command[0].id ) assert_equal_model_fields( elasticsearch_command_log, name='test_command', input='verbosity=0', is_executed_from_command_line=False, time=(elasticsearch_command_log.stop - elasticsearch_command_log.start).total_seconds(), state=CommandState.SUCCEEDED, error_message=None, ) assert_is_not_none(elasticsearch_command_log.output) assert_equal( [rel_obj for rel_obj in elasticsearch_command_log.related_objects], ['default|3|{}'.format(user.id)] ) ElasticsearchCommandLog._index.refresh() assert_equal(get_logs_related_with_object(LoggerName.COMMAND, user), [elasticsearch_command_log])