test_config.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import pytest
  2. from django import forms
  3. from sentry.exceptions import PluginError
  4. from sentry.plugins.base.v2 import Plugin2
  5. from sentry.testutils.cases import TestCase
  6. class DummyForm(forms.Form):
  7. text = forms.CharField(help_text="text field")
  8. textarea = forms.CharField(widget=forms.Textarea, required=False)
  9. password = forms.CharField(label="A Password", widget=forms.PasswordInput)
  10. choice = forms.ChoiceField(choices=((1, "one"), (2, "two")))
  11. url = forms.URLField(assume_scheme="https")
  12. class DummyPlugin(Plugin2):
  13. project_conf_form = DummyForm()
  14. class ConfigTest(TestCase):
  15. def test_get_config(self):
  16. project = self.create_project()
  17. plugin = DummyPlugin()
  18. config = plugin.get_config(project=project)
  19. assert len(config) == 5
  20. assert config[0] == {
  21. "default": None,
  22. "help": "text field",
  23. "label": "Text",
  24. "name": "text",
  25. "placeholder": None,
  26. "required": True,
  27. "type": "text",
  28. }
  29. assert config[1] == {
  30. "default": None,
  31. "help": "",
  32. "label": "Textarea",
  33. "name": "textarea",
  34. "placeholder": None,
  35. "required": False,
  36. "type": "textarea",
  37. }
  38. assert config[2] == {
  39. "default": None,
  40. "help": "",
  41. "label": "A Password",
  42. "name": "password",
  43. "placeholder": None,
  44. "required": True,
  45. "type": "secret",
  46. }
  47. assert config[3] == {
  48. "default": None,
  49. "help": "",
  50. "label": "Choice",
  51. "name": "choice",
  52. "placeholder": None,
  53. "required": True,
  54. "type": "select",
  55. "choices": [(1, "one"), (2, "two")],
  56. }
  57. assert config[4] == {
  58. "default": None,
  59. "help": "",
  60. "label": "Url",
  61. "name": "url",
  62. "placeholder": None,
  63. "required": True,
  64. "type": "url",
  65. }
  66. def test_validate_url(self):
  67. project = self.create_project()
  68. plugin = DummyPlugin()
  69. with pytest.raises(PluginError):
  70. plugin.validate_config_field(project=project, name="url", value="foo")
  71. value = plugin.validate_config_field(
  72. project=project, name="url", value="https://example.com"
  73. )
  74. assert value == "https://example.com"
  75. value = plugin.validate_config_field(
  76. project=project, name="url", value="http://example.com"
  77. )
  78. assert value == "http://example.com"