You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.3 KiB

6 years ago
  1. import json
  2. from hc.api.models import Channel
  3. from hc.test import BaseTestCase
  4. class ChannelModelTestCase(BaseTestCase):
  5. def test_webhook_spec_handles_plain_single_address(self):
  6. c = Channel(kind="webhook")
  7. c.value = "http://example.org"
  8. self.assertEqual(
  9. c.down_webhook_spec,
  10. {"method": "GET", "url": "http://example.org", "body": "", "headers": {}},
  11. )
  12. self.assertEqual(
  13. c.up_webhook_spec, {"method": "GET", "url": "", "body": "", "headers": {}}
  14. )
  15. def test_webhook_spec_handles_plain_pair(self):
  16. c = Channel(kind="webhook")
  17. c.value = "http://example.org\nhttp://example.com/up/"
  18. self.assertEqual(
  19. c.down_webhook_spec,
  20. {"method": "GET", "url": "http://example.org", "body": "", "headers": {}},
  21. )
  22. self.assertEqual(
  23. c.up_webhook_spec,
  24. {
  25. "method": "GET",
  26. "url": "http://example.com/up/",
  27. "body": "",
  28. "headers": {},
  29. },
  30. )
  31. def test_webhook_spec_handles_plain_post(self):
  32. c = Channel(kind="webhook")
  33. c.value = "http://example.org\n\nhello world"
  34. self.assertEqual(
  35. c.down_webhook_spec,
  36. {
  37. "method": "POST",
  38. "url": "http://example.org",
  39. "body": "hello world",
  40. "headers": {},
  41. },
  42. )
  43. self.assertEqual(
  44. c.up_webhook_spec,
  45. {"method": "POST", "url": "", "body": "hello world", "headers": {}},
  46. )
  47. def test_webhook_spec_handles_legacy_get(self):
  48. c = Channel(kind="webhook")
  49. c.value = json.dumps(
  50. {
  51. "url_down": "http://example.org",
  52. "url_up": "http://example.org/up/",
  53. "headers": {"X-Name": "value"},
  54. "post_data": "",
  55. }
  56. )
  57. self.assertEqual(
  58. c.down_webhook_spec,
  59. {
  60. "method": "GET",
  61. "url": "http://example.org",
  62. "body": "",
  63. "headers": {"X-Name": "value"},
  64. },
  65. )
  66. self.assertEqual(
  67. c.up_webhook_spec,
  68. {
  69. "method": "GET",
  70. "url": "http://example.org/up/",
  71. "body": "",
  72. "headers": {"X-Name": "value"},
  73. },
  74. )
  75. def test_webhook_spec_handles_legacy_post(self):
  76. c = Channel(kind="webhook")
  77. c.value = json.dumps(
  78. {
  79. "url_down": "http://example.org",
  80. "url_up": "http://example.org/up/",
  81. "headers": {"X-Name": "value"},
  82. "post_data": "hello world",
  83. }
  84. )
  85. self.assertEqual(
  86. c.down_webhook_spec,
  87. {
  88. "method": "POST",
  89. "url": "http://example.org",
  90. "body": "hello world",
  91. "headers": {"X-Name": "value"},
  92. },
  93. )
  94. self.assertEqual(
  95. c.up_webhook_spec,
  96. {
  97. "method": "POST",
  98. "url": "http://example.org/up/",
  99. "body": "hello world",
  100. "headers": {"X-Name": "value"},
  101. },
  102. )
  103. def test_webhook_spec_handles_mixed(self):
  104. c = Channel(kind="webhook")
  105. c.value = json.dumps(
  106. {
  107. "method_down": "GET",
  108. "url_down": "http://example.org",
  109. "body_down": "",
  110. "headers_down": {"X-Status": "X"},
  111. "method_up": "POST",
  112. "url_up": "http://example.org/up/",
  113. "body_up": "hello world",
  114. "headers_up": {"X-Status": "OK"},
  115. }
  116. )
  117. self.assertEqual(
  118. c.down_webhook_spec,
  119. {
  120. "method": "GET",
  121. "url": "http://example.org",
  122. "body": "",
  123. "headers": {"X-Status": "X"},
  124. },
  125. )
  126. self.assertEqual(
  127. c.up_webhook_spec,
  128. {
  129. "method": "POST",
  130. "url": "http://example.org/up/",
  131. "body": "hello world",
  132. "headers": {"X-Status": "OK"},
  133. },
  134. )