from django.test import TestCase
from django.conf import settings
from django.test.client import Client
from spotseeker_server.models import Spot, SpotExtendedInfo
import simplejson as json


class SpotAuthAllOK(TestCase):
    """ Tests that the all_ok auth module successfully allows any client access.
    """
    settings.SPOTSEEKER_AUTH_MODULE = 'spotseeker_server.auth.all_ok'

    def setUp(self):
        spot = Spot.objects.create(name="This is for testing the all ok auth module", capacity=10)
        extended = SpotExtendedInfo.objects.create(spot=spot, key="whiteboards", value=True)
        self.spot = spot
        self.url = "/api/v1/spot/%s" % self.spot.pk

    def test_get(self):
        c = Client()
        response = c.get(self.url)
        spot_dict = json.loads(response.content)
        returned_spot = Spot.objects.get(pk=spot_dict['id'])
        self.assertEquals(returned_spot, self.spot, "Returns the correct spot")

    def test_put(self):
        c = Client()

        response = c.get(self.url)
        etag = response["ETag"]

        spot_dict = json.loads(response.content)
        spot_dict['name'] = "Modifying all ok"

        # Tests the org specific SPOT_FORM if it's set in settings.py
        response = c.put(self.url, json.dumps(spot_dict), content_type="application/json", If_Match=etag)
        self.assertEquals(response.status_code, 200, "Accepts a valid json string")

        # Additionally test with the default spot form if there's an org specific one
        if settings.SPOTSEEKER_SPOT_FORM:
            with self.settings(SPOTSEEKER_SPOT_FORM='spotseeker_server.default_forms.spot.DefaultSpotForm'):
                # The previous PUT in the test changed the ETag!
                response = c.get(self.url)
                etag = response['ETag']
                response = c.put(self.url, json.dumps(spot_dict), content_type="application/json", If_Match=etag)
                self.assertEquals(response.status_code, 200, "Accepts a valid json string")

        updated_spot = Spot.objects.get(pk=self.spot.pk)
        self.assertEquals(updated_spot.name, "Modifying all ok", "a valid PUT changes the name")

@@ -9,20 +9,21 @@
 ngo.
-test
+utils
  import 
 Test
@@ -18,24 +18,24 @@
  import 
-TestCase
+unittest
 %0Afrom dj
@@ -143,26 +143,8 @@
 Spot
-, SpotExtendedInfo
 %0Aimp
@@ -188,16 +188,25 @@
 thAllOK(
+unittest.
 TestCase
@@ -366,16 +366,17 @@
 .all_ok'
+;
 %0A%0A    de
@@ -491,101 +491,9 @@
 y=10
-)%0A        extended = SpotExtendedInfo.objects.create(spot=spot, key=%22whiteboards%22, value=True
+ 
 )%0A  
@@ -1054,78 +1054,8 @@
 k%22%0A%0A
-        # Tests the org specific SPOT_FORM if it's set in settings.py%0A
     
@@ -1245,600 +1245,8 @@
 %22)%0A%0A
-        # Additionally test with the default spot form if there's an org specific one%0A        if settings.SPOTSEEKER_SPOT_FORM:%0A            with self.settings(SPOTSEEKER_SPOT_FORM='spotseeker_server.default_forms.spot.DefaultSpotForm'):%0A                # The previous PUT in the test changed the ETag!%0A                response = c.get(self.url)%0A                etag = response%5B'ETag'%5D%0A                response = c.put(self.url, json.dumps(spot_dict), content_type=%22application/json%22, If_Match=etag)%0A                self.assertEquals(response.status_code, 200, %22Accepts a valid json string%22)%0A%0A
     

Revert "Changed things so we now test against the DefaultSpotForm as well as any org specific spot form that might be set in the settings.py."