commit dd53338a516dd18d165afaa7d559f6e7fe335150
Author: Georgios Gousios <gousiosg@gmail.com>
Date:   Tue Apr 5 14:16:55 2011 +0300

    Make tests fail gracefully with assertions.
    
    Also correct spelling of the word shibboleth

diff --git a/api/fixtures/api_test_data.json b/api/fixtures/api_test_data.json
index 1c6079e..f92b5e3 100644
--- a/api/fixtures/api_test_data.json
+++ b/api/fixtures/api_test_data.json
@@ -358,5 +358,4 @@
             "machines" : [1003,1004]
         }
     }
-
 ] 
diff --git a/api/middleware.py b/api/middleware.py
index 0c2ca31..a2c41f9 100644
--- a/api/middleware.py
+++ b/api/middleware.py
@@ -1,6 +1,5 @@
 from django.conf import settings
 from django.http import HttpResponse, HttpResponseRedirect
-from synnefo.api.errors import Unauthorized
 from synnefo.db.models import SynnefoUser
 
 class SynnefoAuthMiddleware(object):
@@ -15,7 +14,7 @@ class SynnefoAuthMiddleware(object):
             #Retrieve user from DB or other caching mechanism
             user = SynnefoUser.objects.filter(auth_token = request.META[self.auth_token])
             if user is None :
-                return HttpResponseRedirect(content='Athentication Required')
+                return HttpResponseRedirect(settings.SIBBOLLETH_HOST)
             request.user = user
             return
 
@@ -28,9 +27,13 @@ class SynnefoAuthMiddleware(object):
             # mechanism
             user = SynnefoUser.objects.filter(username = request.META[self.auth_user])
 
-            return HttpResponseRedirect(content= settings.SIBBOLLETH_HOST)
+            return HttpResponseRedirect(settings.SIBBOLLETH_HOST)
 
-        return HttpResponseRedirect(content='Athentication Required')
+        return HttpResponseRedirect(settings.SIBBOLLETH_HOST)
+
+    def process_response(self, request, response):
+        response['Vary'] = self.auth_key
+        return response
 
 #class HttpResponseAuthenticationRequired(HttpResponse):
 #    status_code = 401
\ No newline at end of file
diff --git a/api/tests_auth.py b/api/tests_auth.py
index 16cccf5..398e124 100644
--- a/api/tests_auth.py
+++ b/api/tests_auth.py
@@ -9,38 +9,50 @@
 from django.test import TestCase
 from django.test.client import Client
 
+from synnefo.logic.shibboleth import Tokens
+from synnefo.db.models import SynnefoUser
+
 class AuthTestCase(TestCase):
-    fixtures = ['auth_test_data']
-    apibase = '/api/v1.0'
+    fixtures = ['api_test_data']
+    apibase = '/api/v1.1'
 
     def setUp(self):
         self.client = Client()
 
-    def register_sibbolleth_user(self):
-        """ test registration of sibboleth user upon new incoming request
+    def test_auth_shibboleth(self):
+        """ test redirect to shibboleth page
         """
-        response = self.client.get( self.apibase + '/servers', {},
-                                   **{'X-givenName':'notme',
-                                      'X-sn':'0xdeadbabe'})
-        
+        response = self.client.get(self.apibase + '/servers')
+        self.assertEquals(response.status_code, 302)
 
-    def test_auth_sibbolleth(self):
-        """ test whether the authentication mechanism sets the correct headers
+    def test_register_shibboleth_user(self):
+        """ test registration of sibboleth user upon new incoming request
         """
+        response = self.client.get(self.apibase + '/servers', {},
+                                   **{Tokens.SIB_GIVEN_NAME: 'Jimmy',
+                                      Tokens.SIB_EDU_PERSON_PRINCIPAL_NAME: 'jh@gmail.com',
+                                      Tokens.SIB_DISPLAY_NAME: 'Jimmy Hendrix'})
+
+        user = None
+        try:
+            user = SynnefoUser.objects.get(uniq = "jh@gmail.com")
+        except SynnefoUser.DoesNotExist:
+            self.assertNotEqual(user, None)
+        self.assertNotEqual(user, None)
 
     def test_auth_headers(self):
         """ test whether the authentication mechanism sets the correct headers
         """
         #Check with non-existing user
-        response = self.client.get( self.apibase + '/servers', {},
-                                   **{'X-Auth-User':'notme',
-                                      'X-Auth-Key':'0xdeadbabe'})
+        response = self.client.get(self.apibase + '/servers', {},
+                                   **{'X-Auth-User': 'notme',
+                                      'X-Auth-Key': '0xdeadbabe'})
         self.assertEquals(response.status_code, 401)
 
         #Check with existing user
-        response = self.client.get( self.apibase + '/', {},
-                                   **{'X-Auth-User':'testuser',
-                                      'X-Auth-Key':'testuserpasswd'})
+        response = self.client.get(self.apibase + '/', {},
+                                   **{'X-Auth-User': 'testuser',
+                                      'X-Auth-Key': 'testuserpasswd'})
         self.assertEquals(response.status_code, 204)
         self.assertNotEqual(response['X-Auth-Token'], None)
         self.assertEquals(response['X-Server-Management-Url'], '')
@@ -49,6 +61,6 @@ class AuthTestCase(TestCase):
 
         #Check access now that we do have an auth token
         token = response['X-Auth-Token']
-        response = self.client.get (self.apibase + '/servers/detail', {},
+        response = self.client.get(self.apibase + '/servers/detail', {},
                                    **{'X-Auth-Token': token})
         self.assertEquals(response.status_code, 200)
diff --git a/db/fixtures/initial_data.json b/db/fixtures/initial_data.json
index b1084a7..240dc6d 100644
--- a/db/fixtures/initial_data.json
+++ b/db/fixtures/initial_data.json
@@ -1,2 +1,15 @@
 [
+    {
+        "model": "db.SynnefoUser",
+        "pk": 1,
+        "fields": {
+            "name": "testdbuser",
+            "realname" :"test db user",
+            "uniq" :"test@synnefo.gr",
+            "credit": 10,
+            "auth_token": "46e427d657b20defe352804f0eb6f8a2",
+            "type": "STUDENT",
+            "created": "2011-02-06"
+   	    }
+    }
 ] 
diff --git a/db/models.py b/db/models.py
index 9004126..326f946 100644
--- a/db/models.py
+++ b/db/models.py
@@ -2,7 +2,6 @@
 
 from django.conf import settings
 from django.db import models
-from django.contrib.auth.models import User
 
 import datetime
 
diff --git a/logic/shibboleth.py b/logic/shibboleth.py
new file mode 100644
index 0000000..154a833
--- /dev/null
+++ b/logic/shibboleth.py
@@ -0,0 +1,41 @@
+#
+# Business Logic for working with sibbolleth users
+#
+# Copyright 2010 Greek Research and Technology Network
+#
+
+from synnefo.logic import users
+
+class Tokens:
+    SIB_GIVEN_NAME = "givenName"
+    SIB_SN = "sn"
+    SIB_CN = "cn"
+    SIB_DISPLAY_NAME = "displayName"
+    SIB_EDU_PERSON_PRINCIPAL_NAME = "eduPersonPrincipalName"
+    SIB_EDU_PERSON_AFFILIATION = "eduPersonAffiliation"
+    SIB_SCHAC_HOME_ORGANISATION = "schacHomeOrganization"
+    SIB_SCHAC_PERSONAL_UNIQUE_CODE = "schacPersonalUniqueCode"
+    SIB_GR_EDU_PERSON_UNDERGRADUATE_BRANCH = "grEduPersonUndergraduateBranch"
+
+class NoUniqueToken(object):
+    pass
+
+def register_shibboleth_user(tokens):
+    """Registers a sibbolleth user using the input hash as a source for data.
+       The token requirements are described in this document
+       http://aai.grnet.gr/policy
+    """
+
+    realname = tokens[Tokens.SIB_GIVEN_NAME] | tokens[Tokens.SIB_GIVEN_NAME]
+    is_student = tokens[Tokens.SIB_SCHAC_PERSONAL_UNIQUE_CODE] | \
+                 tokens[Tokens.SIB_GR_EDU_PERSON_UNDERGRADUATE_BRANCH]
+
+    unq = tokens[Tokens.SIB_EDU_PERSON_PRINCIPAL_NAME]
+
+    if unq is None:
+        raise NoUniqueToken
+
+    if is_student:
+        users.register_student(realname, '' ,unq)
+    else :
+        users.register_professor(realname, '' ,unq)
diff --git a/logic/sibbolleth.py b/logic/sibbolleth.py
deleted file mode 100644
index 00ce6c1..0000000
--- a/logic/sibbolleth.py
+++ /dev/null
@@ -1,42 +0,0 @@
-#
-# Business Logic for working with sibbolleth users
-#
-# Copyright 2010 Greek Research and Technology Network
-#
-
-from synnefo.logic import users
-
-class Tokens:
-    SIB_GIVEN_NAME = "givenName"
-    SIB_SN = "sn"
-    SIB_CN = "cn"
-    SIB_DISPLAY_NAME = "displayName"
-    SIB_EDU_PERSON_PRINCIPAL_NAME = "eduPersonPrincipalName"
-    SIB_EDU_PERSON_AFFILIATION = "eduPersonAffiliation"
-    SIB_SCHAC_HOME_ORGANISATION = "schacHomeOrganization"
-    SIB_SCHAC_PERSONAL_UNIQUE_CODE = "schacPersonalUniqueCode"
-    SIB_GR_EDU_PERSON_UNDERGRADUATE_BRANCH = "grEduPersonUndergraduateBranch"
-
-class NoUniqueToken(object):
-    pass
-
-
-def register_sibbolleth_user(tokens):
-    """Registers a sibbolleth user using the input hash as a source for data.
-       The token requirements are described in this document
-       http://aai.grnet.gr/policy
-    """
-    
-    realname = tokens[Tokens.SIB_GIVEN_NAME] | tokens[Tokens.SIB_GIVEN_NAME]
-    is_student = tokens[Tokens.SIB_SCHAC_PERSONAL_UNIQUE_CODE] | \
-                 tokens[Tokens.SIB_GR_EDU_PERSON_UNDERGRADUATE_BRANCH]
-
-    unq = tokens[Tokens.SIB_EDU_PERSON_PRINCIPAL_NAME]
-
-    if unq is None:
-        raise NoUniqueToken
-
-    if is_student:
-        users.register_student(realname, '' ,unq)
-    else :
-        users.register_professor(realname, '' ,unq)
diff --git a/logic/tests.py b/logic/tests.py
index e4d8ec1..1942cca 100644
--- a/logic/tests.py
+++ b/logic/tests.py
@@ -10,7 +10,6 @@ from synnefo.db.models import *
 from synnefo.logic import credits
 from synnefo.logic import users
 from django.test import TestCase
-from django.core.exceptions import ObjectDoesNotExist
 
 import hashlib
 
@@ -116,7 +115,7 @@ class AuthTestCase(TestCase):
         """
         user = self._register_user()
         self.assertNotEquals(user, None)
-        
+
         #Check hash generation
         md5 = hashlib.md5()
         md5.update(user.uniq)
@@ -129,9 +128,5 @@ class AuthTestCase(TestCase):
         """
         user = self._register_user()
         users.delete_user(user)
-        user1 = None
 
-        try:
-            user1 = SynnefoUser.objects.get(name = "jpage")
-        except ObjectDoesNotExist:
-            self.assertEquals(user1, None)
+        self.assertRaises(SynnefoUser.DoesNotExist, SynnefoUser.objects.get, name = "jpage")
diff --git a/settings.py.dist b/settings.py.dist
index 129577c..dd031b8 100644
--- a/settings.py.dist
+++ b/settings.py.dist
@@ -97,7 +97,8 @@ MIDDLEWARE_CLASSES = (
     'django.middleware.locale.LocaleMiddleware',
     'django.middleware.common.CommonMiddleware',
     'synnefo.middleware.StripURLMiddleware',
-    'django.contrib.auth.middleware.AuthenticationMiddleware',
+    #'django.contrib.auth.middleware.AuthenticationMiddleware',
+    #'synnefo.api.middleware.SynnefoAuthMiddleware',
     'django.contrib.messages.middleware.MessageMiddleware',
 )
 
@@ -153,4 +154,3 @@ GANETI_ZMQ_PUBLISHER = "tcp://62.217.120.67:5801"
 API_ROOT_URL = 'http://127.0.0.1:8000/api/'
 
 SIBBOLLETH_HOST = "http://wayf.grnet.gr/"
-
