tests.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from django.core.cache import cache
  2. from django.test import TestCase
  3. from django.urls import reverse
  4. from glitchtip.test_utils import generators # noqa: F401
  5. from glitchtip.test_utils.test_case import GlitchTipTestCaseMixin
  6. from .constants import SETUP_WIZARD_CACHE_EMPTY, SETUP_WIZARD_CACHE_KEY
  7. class WizardTestCase(GlitchTipTestCaseMixin, TestCase):
  8. def setUp(self):
  9. self.create_logged_in_user()
  10. self.url = reverse("api:setup_wizard")
  11. self.url_set_token = reverse("api:setup_wizard_set_token")
  12. def test_get_hash(self):
  13. res = self.client.get(self.url)
  14. self.assertEqual(res.status_code, 200)
  15. wizard_hash = res.json().get("hash")
  16. self.assertEqual(len(wizard_hash), 64)
  17. key = SETUP_WIZARD_CACHE_KEY + wizard_hash
  18. self.assertEqual(cache.get(key), SETUP_WIZARD_CACHE_EMPTY)
  19. def test_set_token(self):
  20. res = self.client.get(self.url)
  21. wizard_hash = res.json().get("hash")
  22. self.client.force_login(self.user)
  23. res = self.client.post(
  24. self.url_set_token, {"hash": wizard_hash}, content_type="application/json"
  25. )
  26. self.assertEqual(res.status_code, 200)
  27. key = SETUP_WIZARD_CACHE_KEY + wizard_hash
  28. self.assertTrue(cache.get(key)["apiKeys"])
  29. self.assertTrue(self.user.apitoken_set.exists())
  30. res = self.client.get(self.url + wizard_hash + "/")
  31. self.assertEqual(res.status_code, 200)