Skip to content

Commit

Permalink
Add aliases for interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
dcramer committed Jan 30, 2013
1 parent d6fdb4a commit ed2830c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
22 changes: 16 additions & 6 deletions src/sentry/coreapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@
MAX_CULPRIT_LENGTH = 200
MAX_MESSAGE_LENGTH = 1000

INTERFACE_ALIASES = {
'exception': 'sentry.interfaces.Exception',
'request': 'sentry.interfaces.Http',
'user': 'sentry.interfaces.User',
'stacktrace': 'sentry.interfaces.Stacktrace',
'template': 'sentry.interfaces.Template',
}

RESERVED_FIELDS = (
'project',
'event_id',
Expand Down Expand Up @@ -295,25 +303,27 @@ def validate_data(project, data, client=None):
if k in RESERVED_FIELDS:
continue

if '.' not in k:
logger.info('Ignoring unknown attribute %r passed by client %r',
if not data[k]:
logger.info('Ignoring empty interface %r passed by client %r',
k, client or '<unknown client>', extra={'request': env.request})
del data[k]
continue

if not data[k]:
logger.info('Ignoring empty interface %r passed by client %r',
import_path = INTERFACE_ALIASES.get(k, k)

if '.' not in import_path:
logger.info('Ignoring unknown attribute %r passed by client %r',
k, client or '<unknown client>', extra={'request': env.request})
del data[k]
continue

try:
interface = import_string(k)
interface = import_string(import_path)
except (ImportError, AttributeError), e:
raise InvalidInterface('%r is not a valid interface name: %s' % (k, e))

try:
data[k] = interface(**data[k]).serialize()
data[import_path] = interface(**data.pop(k)).serialize()
except Exception, e:
logger.error('Client %r passed an invalid value for interface %r',
client or '<unknown client>',
Expand Down
40 changes: 36 additions & 4 deletions tests/sentry/coreapi/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@

from sentry.models import Project
from sentry.exceptions import InvalidTimestamp, InvalidInterface, InvalidData
from sentry.coreapi import project_from_id, project_from_api_key_and_id, \
extract_auth_vars, project_from_auth_vars, APIUnauthorized, \
APIForbidden, process_data_timestamp, \
insert_data_to_database, validate_data
from sentry.coreapi import (project_from_id, project_from_api_key_and_id,
extract_auth_vars, project_from_auth_vars, APIUnauthorized, APIForbidden,
process_data_timestamp, insert_data_to_database, validate_data, INTERFACE_ALIASES)
from sentry.testutils import TestCase


Expand All @@ -24,6 +23,23 @@ def setUp(self):
self.pk = self.project.key_set.get_or_create(user=self.user)[0]


class InterfaceAliasesTest(BaseAPITest):
def test_http(self):
assert INTERFACE_ALIASES['request'] == 'sentry.interfaces.Http'

def test_user(self):
assert INTERFACE_ALIASES['user'] == 'sentry.interfaces.User'

def test_exception(self):
assert INTERFACE_ALIASES['exception'] == 'sentry.interfaces.Exception'

def test_stacktrace(self):
assert INTERFACE_ALIASES['stacktrace'] == 'sentry.interfaces.Stacktrace'

def test_template(self):
assert INTERFACE_ALIASES['template'] == 'sentry.interfaces.Template'


class ProjectFromIdTest(BaseAPITest):
def test_valid(self):
request = mock.Mock()
Expand Down Expand Up @@ -268,6 +284,22 @@ def test_invalid_interface_args(self):
'tests.manager.tests.DummyInterface': {'foo': 'bar'}
})

@mock.patch('sentry.coreapi.import_string')
def test_an_alias_maps_correctly(self, import_string):
alias, full_path = INTERFACE_ALIASES.items()[0]

result = validate_data(self.project, {
'project': self.project.id,
'message': 'foo',
alias: {'foo': 'bar'},
})
import_string.assert_called_once_with(full_path)
interface = import_string.return_value
interface.assert_called_once_with(foo='bar')
assert alias not in result
assert full_path in result
assert result[full_path] == interface.return_value.serialize.return_value

def test_log_level_as_string(self):
data = validate_data(self.project, {
'project': self.project.id,
Expand Down

0 comments on commit ed2830c

Please sign in to comment.