tests.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import requests_mock
  2. from django.test import TestCase
  3. from django.urls import reverse
  4. from model_bakery import baker
  5. class SettingsTestCase(TestCase):
  6. def setUp(self):
  7. self.url = reverse("api:get_settings")
  8. def test_settings(self):
  9. with self.assertNumQueries(1):
  10. res = self.client.get(self.url) # Check that no auth is necessary
  11. self.assertEqual(res.status_code, 200)
  12. def test_settings_oidc(self):
  13. social_app = baker.make(
  14. "socialaccount.socialapp",
  15. provider="openid_connect",
  16. provider_id="my-openid",
  17. settings={"server_url": "https://example.com"},
  18. )
  19. for provider in [
  20. "gitlab",
  21. "microsoft",
  22. "github",
  23. "google",
  24. "nextcloud",
  25. "digitalocean",
  26. ]:
  27. baker.make(
  28. "socialaccount.socialapp",
  29. provider=provider,
  30. )
  31. with requests_mock.Mocker() as m:
  32. m.get(
  33. "https://example.com/.well-known/openid-configuration",
  34. json={"authorization_endpoint": ""},
  35. )
  36. res = self.client.get(self.url)
  37. self.assertContains(res, social_app.name)
  38. class APIRootTestCase(TestCase):
  39. def setUp(self):
  40. self.url = reverse("api:api_root")
  41. def test_anon(self):
  42. self.assertContains(self.client.get(self.url), "version")
  43. def test_user(self):
  44. user = baker.make("users.user")
  45. self.client.force_login(user)
  46. res = self.client.get(self.url)
  47. self.assertContains(res, user.email)
  48. def test_token(self):
  49. user = baker.make("users.user")
  50. auth_token = baker.make("api_tokens.APIToken", user=user)
  51. headers = {"Authorization": f"Bearer {auth_token.token}"}
  52. res = self.client.get(self.url, headers=headers)
  53. self.assertContains(res, auth_token.token)
  54. self.assertContains(res, user.email)