import json
from restlib2.http import codes
from avocado.models import DataContext
from .base import AuthenticatedBaseTestCase


class ContextResourceTestCase(AuthenticatedBaseTestCase):
    def test_get_all(self):
        response = self.client.get('/api/contexts/',
            HTTP_ACCEPT='application/json')
        self.assertFalse(json.loads(response.content))

    def test_get_all_default(self):
        cxt = DataContext(template=True, default=True, json={})
        cxt.save()
        response = self.client.get('/api/contexts/',
            HTTP_ACCEPT='application/json')
        self.assertEqual(len(json.loads(response.content)), 1)

    def test_get(self):
        ctx = DataContext(user=self.user)
        ctx.save()
        response = self.client.get('/api/contexts/1/',
            HTTP_ACCEPT='application/json')
        self.assertEqual(response.status_code, codes.ok)
        self.assertTrue(response.content)
        self.assertLess(ctx.accessed,
                DataContext.objects.get(pk=ctx.pk).accessed)

        # Make sure that accessing a non-existent context returns a 404 error
        # indicating that it wasn't found.
        response = self.client.get('/api/contexts/999/',
            HTTP_ACCEPT='application/json')
        self.assertEqual(response.status_code, codes.not_found)



class ContextsRevisionsResourceTestCase(AuthenticatedBaseTestCase):
    def test_get(self):
        ctx = DataContext(user=self.user)
        ctx.save()

        response = self.client.get('/api/contexts/revisions/',
            HTTP_ACCEPT='application/json')
        self.assertEqual(response.status_code, codes.ok)
        self.assertEqual(len(json.loads(response.content)), 1)

@@ -1311,16 +1311,2460 @@
 found)%0A%0A
+    def test_put(self):%0A        # Add a context so we can try to update it later%0A        ctx = DataContext(user=self.user, name='Context 1')%0A        ctx.save()%0A        response = self.client.get('/api/contexts/1/',%0A            HTTP_ACCEPT='application/json')%0A        self.assertEqual(response.status_code, codes.ok)%0A        self.assertTrue(response.content)%0A%0A        # Attempt to update the name via a PUT request%0A        response = self.client.put('/api/contexts/1/',%0A            data=u'%7B%22name%22:%22New Name%22%7D', content_type='application/json')%0A        self.assertEqual(response.status_code, codes.no_content)%0A%0A        # Make sure our changes from the PUT request are persisted%0A        response = self.client.get('/api/contexts/1/',%0A            HTTP_ACCEPT='application/json')%0A        self.assertEqual(response.status_code, codes.ok)%0A        self.assertTrue(response.content)%0A        self.assertEqual(json.loads(response.content)%5B'name'%5D, 'New Name')%0A%0A        # Make a PUT request with invalid JSON and make sure we get an%0A        # unprocessable status code back.%0A        response = self.client.put('/api/contexts/1/',%0A            data=u'%7B%22json%22:%22%5D%5D%5D%22%7D', content_type='application/json')%0A        self.assertEqual(response.status_code, codes.unprocessable_entity)%0A%0A    def test_delete(self):%0A        ctx = DataContext(user=self.user, name='Context 1')%0A        ctx.save()%0A        ctx = DataContext(user=self.user, name='Context 2')%0A        ctx.save()%0A        ctx = DataContext(user=self.user, name='Context 3', session=True)%0A        ctx.save()%0A%0A        response = self.client.get('/api/contexts/',%0A            HTTP_ACCEPT='application/json')%0A        self.assertEqual(response.status_code, codes.ok)%0A        self.assertEqual(len(json.loads(response.content)), 3)%0A%0A        response = self.client.delete('/api/contexts/1/')%0A        self.assertEqual(response.status_code, codes.no_content)%0A%0A        response = self.client.get('/api/contexts/',%0A            HTTP_ACCEPT='application/json')%0A        self.assertEqual(response.status_code, codes.ok)%0A        self.assertEqual(len(json.loads(response.content)), 2)%0A%0A        response = self.client.delete('/api/contexts/3/')%0A        self.assertEqual(response.status_code, codes.bad_request)%0A%0A        response = self.client.get('/api/contexts/',%0A            HTTP_ACCEPT='application/json')%0A        self.assertEqual(response.status_code, codes.ok)%0A        self.assertEqual(len(json.loads(response.content)), 2)%0A
 %0A%0Aclass 

Add tests for PUT and DELETE requests on the context resource