from rest_framework.test import APITestCase, APIClient

from api.tests import factories


class LogoutApiTestCase(APITestCase):
    def test_get_sites_403(self):
        response = self.client.get('/api/sites/')
        self.assertEqual(403, response.status_code)


class LoginApiTestCase(APITestCase):
    def setUp(self):
        self.user = factories.UserFactory()
        self.client = APIClient()
        self.client.force_authenticate(user=self.user)

    def test_get_empty_sites(self):
        request = self.client.get('/api/sites/')
        self.assertEqual(0, len(request.data['results']))

    def test_retrieve_its_own_sites(self):
        site = factories.SiteFactory(user=self.user)
        not_my_site = factories.SiteFactory(user=factories.UserFactory())

        request = self.client.get('/api/sites/%s/' % not_my_site.id)
        self.assertEqual(404, request.status_code)

        request = self.client.get('/api/sites/')
        self.assertEqual(1, len(request.data['results']))
        self.assertEqual(site.name, request.data['results'][0]['name'])



@@ -49,16 +49,39 @@
 Client%0A%0A
+from api import models%0A
 from api
@@ -714,16 +714,242 @@
 f.user)%0A
+        request = self.client.get('/api/sites/')%0A        self.assertEqual(1, len(request.data%5B'results'%5D))%0A        self.assertEqual(site.name, request.data%5B'results'%5D%5B0%5D%5B'name'%5D)%0A%0A    def test_cant_retrieve_other_sites(self):%0A
         
@@ -1014,17 +1014,16 @@
 tory())%0A
-%0A
         
@@ -1135,16 +1135,173 @@
 _code)%0A%0A
+    def test_delete_its_own_sites(self):%0A        site = factories.SiteFactory(user=self.user)%0A        self.assertEqual(1, models.Site.objects.all().count())%0A
         
@@ -1314,35 +1314,38 @@
 t = self.client.
-g
+del
 et
+e
 ('/api/sites/')%0A
@@ -1333,33 +1333,46 @@
 ete('/api/sites/
-'
+%25s/' %25 site.id
 )%0A        self.a
@@ -1386,111 +1386,460 @@
 ual(
-1, len(request.data%5B'results'%5D))%0A        self.assertEqual(site.name, request.data%5B'results'%5D%5B0%5D%5B'name'%5D)%0A%0A
+204, request.status_code)%0A        self.assertEqual(0, models.Site.objects.all().count())%0A%0A    def test_cant_delete_other_site(self):%0A        not_my_site = factories.SiteFactory(user=factories.UserFactory())%0A        self.assertEqual(1, models.Site.objects.all().count())%0A        request = self.client.delete('/api/sites/%25s/' %25 not_my_site.id)%0A        self.assertEqual(404, request.status_code)%0A        self.assertEqual(1, models.Site.objects.all().count())
 %0A

test delete site