您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

47 行
1.8 KiB

  1. from rest_framework.test import APITestCase, APIClient
  2. from api import models
  3. from api.tests import factories
  4. class LogoutApiTestCase(APITestCase):
  5. def test_get_sites_403(self):
  6. response = self.client.get('/api/sites/')
  7. self.assertEqual(403, response.status_code)
  8. class LoginApiTestCase(APITestCase):
  9. def setUp(self):
  10. self.user = factories.UserFactory()
  11. self.client = APIClient()
  12. self.client.force_authenticate(user=self.user)
  13. def test_get_empty_sites(self):
  14. request = self.client.get('/api/sites/')
  15. self.assertEqual(0, len(request.data['results']))
  16. def test_retrieve_its_own_sites(self):
  17. site = factories.SiteFactory(user=self.user)
  18. request = self.client.get('/api/sites/')
  19. self.assertEqual(1, len(request.data['results']))
  20. self.assertEqual(site.name, request.data['results'][0]['name'])
  21. def test_cant_retrieve_other_sites(self):
  22. not_my_site = factories.SiteFactory(user=factories.UserFactory())
  23. request = self.client.get('/api/sites/%s/' % not_my_site.id)
  24. self.assertEqual(404, request.status_code)
  25. def test_delete_its_own_sites(self):
  26. site = factories.SiteFactory(user=self.user)
  27. self.assertEqual(1, models.Site.objects.all().count())
  28. request = self.client.delete('/api/sites/%s/' % site.id)
  29. self.assertEqual(204, request.status_code)
  30. self.assertEqual(0, models.Site.objects.all().count())
  31. def test_cant_delete_other_site(self):
  32. not_my_site = factories.SiteFactory(user=factories.UserFactory())
  33. self.assertEqual(1, models.Site.objects.all().count())
  34. request = self.client.delete('/api/sites/%s/' % not_my_site.id)
  35. self.assertEqual(404, request.status_code)
  36. self.assertEqual(1, models.Site.objects.all().count())