conftest.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. from django.conf import settings
  2. import mock
  3. import sys
  4. import os
  5. import os.path
  6. sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
  7. def pytest_configure(config):
  8. import warnings
  9. warnings.filterwarnings('error', '', Warning, r'(sentry|raven)')
  10. if not settings.configured:
  11. os.environ['DJANGO_SETTINGS_MODULE'] = 'sentry.conf.server'
  12. os.environ['RECAPTCHA_TESTING'] = 'True'
  13. test_db = os.environ.get('DB', 'sqlite')
  14. if test_db == 'mysql':
  15. settings.DATABASES['default'].update({
  16. 'ENGINE': 'django.db.backends.mysql',
  17. 'NAME': 'sentry',
  18. 'USER': 'root',
  19. })
  20. elif test_db == 'postgres':
  21. settings.DATABASES['default'].update({
  22. 'ENGINE': 'django.db.backends.postgresql_psycopg2',
  23. 'USER': 'postgres',
  24. 'NAME': 'sentry',
  25. 'OPTIONS': {
  26. 'autocommit': True,
  27. }
  28. })
  29. elif test_db == 'sqlite':
  30. settings.DATABASES['default'].update({
  31. 'ENGINE': 'django.db.backends.sqlite3',
  32. 'NAME': ':memory:',
  33. })
  34. # http://djangosnippets.org/snippets/646/
  35. class InvalidVarException(object):
  36. def __mod__(self, missing):
  37. try:
  38. missing_str = unicode(missing)
  39. except:
  40. missing_str = 'Failed to create string representation'
  41. raise Exception('Unknown template variable %r %s' % (missing, missing_str))
  42. def __contains__(self, search):
  43. if search == '%s':
  44. return True
  45. return False
  46. settings.TEMPLATE_DEBUG = True
  47. # settings.TEMPLATE_STRING_IF_INVALID = InvalidVarException()
  48. # Disable static compiling in tests
  49. settings.STATIC_BUNDLES = {}
  50. # override a few things with our test specifics
  51. settings.INSTALLED_APPS = tuple(settings.INSTALLED_APPS) + (
  52. 'tests',
  53. )
  54. # Need a predictable key for tests that involve checking signatures
  55. settings.SENTRY_KEY = 'abc123'
  56. settings.SENTRY_PUBLIC = False
  57. # This speeds up the tests considerably, pbkdf2 is by design, slow.
  58. settings.PASSWORD_HASHERS = [
  59. 'django.contrib.auth.hashers.MD5PasswordHasher',
  60. ]
  61. # Replace real sudo middleware with our mock sudo middleware
  62. # to assert that the user is always in sudo mode
  63. middleware = list(settings.MIDDLEWARE_CLASSES)
  64. sudo = middleware.index('sentry.middleware.sudo.SudoMiddleware')
  65. middleware[sudo] = 'tests.middleware.SudoMiddleware'
  66. settings.MIDDLEWARE_CLASSES = tuple(middleware)
  67. # enable draft features
  68. settings.SENTRY_ENABLE_EXPLORE_CODE = True
  69. settings.SENTRY_ENABLE_EXPLORE_USERS = True
  70. settings.SENTRY_ENABLE_EMAIL_REPLIES = True
  71. settings.SENTRY_REDIS_OPTIONS = {'hosts': {0: {'db': 9}}}
  72. settings.SENTRY_ALLOW_ORIGIN = '*'
  73. settings.SENTRY_TSDB = 'sentry.tsdb.inmemory.InMemoryTSDB'
  74. settings.SENTRY_TSDB_OPTIONS = {}
  75. settings.RECAPTCHA_PUBLIC_KEY = 'a' * 40
  76. settings.RECAPTCHA_PRIVATE_KEY = 'b' * 40
  77. # django mail uses socket.getfqdn which doesn't play nice if our
  78. # networking isn't stable
  79. patcher = mock.patch('socket.getfqdn', return_value='localhost')
  80. patcher.start()
  81. from sentry.utils.runner import initialize_receivers
  82. initialize_receivers()
  83. from sentry.testutils.cases import flush_redis
  84. flush_redis()
  85. def pytest_runtest_teardown(item):
  86. from sentry.app import tsdb
  87. tsdb.flush()