def test_subcommand_not_known(self, argv_mock): stdout = six.StringIO() command = DjangoCommand() argv_mock.return_value = ['manage.py', 'opbeat'] command.execute('foo', stdout=stdout) output = stdout.getvalue() assert 'No such command "foo"' in output
def test_middleware_not_set(self): stdout = six.StringIO() command = DjangoCommand() with self.settings(MIDDLEWARE_CLASSES=()): command.execute('check', stdout=stdout) output = stdout.getvalue() assert 'Opbeat APM middleware not set!' in output
def test_request_kwarg(self): handler = OpbeatHandler() logger = self.logger logger.handlers = [] logger.addHandler(handler) logger.error('This is a test error', extra={ 'request': WSGIRequest( environ={ 'wsgi.input': six.StringIO(), 'REQUEST_METHOD': 'POST', 'SERVER_NAME': 'testserver', 'SERVER_PORT': '80', 'CONTENT_TYPE': 'application/octet-stream', 'ACCEPT': 'application/json', }) }) self.assertEquals(len(self.opbeat.events), 1) event = self.opbeat.events.pop(0) self.assertTrue('http' in event) http = event['http'] self.assertEquals(http['method'], 'POST')
def test_subcommand_not_set(self, argv_mock): stdout = six.StringIO() command = DjangoCommand() argv_mock.return_value = ['manage.py', 'opbeat'] command.execute(stdout=stdout) output = stdout.getvalue() assert 'No command specified' in output
def test_middleware_not_first(self): stdout = six.StringIO() with self.settings(MIDDLEWARE_CLASSES=( 'foo', 'opbeat.contrib.django.middleware.OpbeatAPMMiddleware')): call_command('opbeat', 'check', stdout=stdout) output = stdout.getvalue() assert 'not at the first position' in output
def test_settings_missing(self): stdout = six.StringIO() with self.settings(OPBEAT={}): call_command('opbeat', 'check', stdout=stdout) output = stdout.getvalue() assert 'Configuration errors detected' in output assert 'ORGANIZATION_ID not set' in output assert 'APP_ID not set' in output assert 'SECRET_TOKEN not set' in output
def test_test_exception(self, urlopen_mock): stdout = six.StringIO() resp = mock.Mock(status=200, getheader=lambda h: 'http://example.com') urlopen_mock.return_value = resp with self.settings(MIDDLEWARE_CLASSES=( 'foo', 'opbeat.contrib.django.middleware.OpbeatAPMMiddleware')): call_command('opbeat', 'test', stdout=stdout, stderr=stdout) output = stdout.getvalue() assert 'http://example.com' in output
def test_http_error(self, mock_urlopen): url, status, message, body = ( 'http://localhost:9999', 418, "I'm a teapot", 'Nothing' ) transport = HTTPTransport(urlparse.urlparse(url)) mock_urlopen.side_effect = HTTPError( url, status, message, hdrs={}, fp=six.StringIO(body) ) with pytest.raises(TransportException) as exc_info: transport.send('x', {}) for val in (url, status, message, body): assert str(val) in str(exc_info.value)
def test_test_exception(self, urlopen_mock): stdout = six.StringIO() command = DjangoCommand() resp = six.moves.urllib.response.addinfo( mock.Mock(), headers={'Location': 'http://example.com'}) urlopen_mock.return_value = resp with self.settings(MIDDLEWARE_CLASSES=( 'foo', 'opbeat.contrib.django.middleware.OpbeatAPMMiddleware')): command.execute('test', stdout=stdout, stderr=stdout) output = stdout.getvalue() assert 'http://example.com' in output
def test_middleware_not_set(self): stdout = six.StringIO() with self.settings(MIDDLEWARE_CLASSES=()): call_command('opbeat', 'check', stdout=stdout) output = stdout.getvalue() assert 'Opbeat APM middleware not set!' in output
def test_subcommand_not_set(self, argv_mock): stdout = six.StringIO() argv_mock.return_value = ['manage.py', 'opbeat'] call_command('opbeat', stdout=stdout) output = stdout.getvalue() assert 'No command specified' in output