test_event_context_processors.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from django.test import TestCase
  2. from ..event_context_processors import (
  3. EVENT_CONTEXT_PROCESSORS,
  4. UserAgentContextProcessor,
  5. )
  6. class EventTagTestCase(TestCase):
  7. def make_fake_event_with_ua(self, ua_string: str):
  8. return {"request": {"headers": [("User-Agent", ua_string)]}}
  9. def test_user_agent_context_processors(self):
  10. """
  11. Smoke test user agent context processors
  12. No need to exhaustively test user-agents
  13. """
  14. ua_test_cases = [
  15. {
  16. "ua_string": "invalid string",
  17. "browser": {"name": "Other"},
  18. "os": {"name": "Other"},
  19. "device": {"family": "Other"},
  20. },
  21. {
  22. "ua_string": "Mozilla/5.0 (Android 9; Mobile; rv:68.0) Gecko/68.0 Firefox/68.0",
  23. "browser": {"name": "Firefox Mobile", "version": "68.0"},
  24. "os": {"name": "Android", "version": "9"},
  25. "device": {
  26. "brand": "Generic",
  27. "family": "Generic Smartphone",
  28. "model": "Smartphone",
  29. },
  30. },
  31. ]
  32. for ua_test_case in ua_test_cases:
  33. for Processor in EVENT_CONTEXT_PROCESSORS:
  34. processor = Processor()
  35. event = self.make_fake_event_with_ua(ua_test_case["ua_string"])
  36. context = processor.get_context(event)
  37. self.assertEqual(
  38. context, ua_test_case[processor.name], str(processor),
  39. )
  40. def test_user_agent_context_processor_no_ua(self):
  41. """ Missing UA header should return None """
  42. event = {
  43. "request": {
  44. "headers": [
  45. ["Accept-Encoding", "gzip"],
  46. ["Connection", "Keep-Alive"],
  47. ["Content-Length", "123"],
  48. ["Content-Type", "multipart/form-data; boundary=1M",],
  49. ["Host", "example.com"],
  50. ]
  51. }
  52. }
  53. self.assertFalse(UserAgentContextProcessor().get_context(event))
  54. self.assertFalse(UserAgentContextProcessor().get_context({}))
  55. self.assertFalse(UserAgentContextProcessor().get_context({"request": {}}))