Exemplo n.º 1
0
 def testMapRpxIdToExistingUser(self):
     """Tests RpxBackend.map_to_existing_user_by_email().
     
     This function should be able to map existing user to new RPX id's by
     matching email addresses from trusted providers.
     
     """
     user, auth_info = self.__create_normal_user__()
     
     testJson2 = '''
         {
           "profile": {
             "displayName": "brian",
             "preferredUsername": "******",
             "email": "*****@*****.**",
             "providerName": "Google",
             "identifier": "http:\/\/brian.anotheropenid.com\/"
           },
           "stat": "ok"
         }'''
         
     json = simplejson.loads(testJson2)
     
     auth_info = RpxAuthInfo(json)        
     
     self.assert_(auth_info)
     self.assert_(auth_info.get_status() == RpxAuthInfo.OK)       
     
     user2 = RpxBackend.map_to_existing_user_by_email(self.backend, auth_info)
     
     self.assert_(user == user2)
     
     RpxBackend.delete_user(self.backend, user)
Exemplo n.º 2
0
    def testCreateNewUserWithExistingUsername(self):

        user, auth_info = self.__create_normal_user__()
        
        testJson2 = '''
            {
              "profile": {
                "displayName": "brian",
                "preferredUsername": "******",
                "email": "*****@*****.**",
                "providerName": "Other",
                "identifier": "http:\/\/brian2.myopenid.com\/"
              },
              "stat": "ok"
            }'''

        json = simplejson.loads(testJson2)
        
        auth_info = RpxAuthInfo(json)

        user2 = RpxBackend.create_user(self.backend, auth_info)

        self.assert_(user2)
        self.assert_(user2.username == "brian")
        self.assert_(user2.email == "*****@*****.**")
        
        RpxBackend.delete_user(self.backend, user)
        RpxBackend.delete_user(self.backend, user2)
Exemplo n.º 3
0
 def testGetUserFromRpxId(self):
     """Tests RpxBackend.get_user_by_rpx_id()."""
     
     user, auth_info = self.__create_normal_user__()
     
     self.assert_(user == RpxBackend.get_user_by_rpx_id(self.backend, auth_info.get_rpx_id()))
     
     RpxBackend.delete_user(self.backend, user)
Exemplo n.º 4
0
 def testGetUserFromRpxIdWithInvalidModel(self):
     
     try:
         rpx_data = RpxData(key_name=RpxBackend.create_rpx_key(self.backend, "thisiskey"), user=None)
         self.assert_(False)
     except db.BadValueError:
         pass            
     
     self.assert_(RpxBackend.get_user_by_rpx_id(self.backend, "thisiskey") == None)
Exemplo n.º 5
0
 def testToString(self):
     """Tests that the __unicode__ function creates a string"""
     
     user = User(username="******")
     user.save()
     rpx_data = RpxData(user=user)
     rpx_data.save()
     rpx_string = str(rpx_data)
     self.assert_(rpx_string.__class__ == str)
     
     RpxBackend.delete_user(self.backend, user)
Exemplo n.º 6
0
 def testGetUserFromNoneRpxId(self):
     """Tests RpxBackend.get_user_by_rpx_id().
     
     Make sure it handles None and negative results.
     
     """
     try:
         RpxBackend.get_user_by_rpx_id(self.backend, None)
         self.assert_(False)
     except ValueError:
         pass
     
     self.assert_(RpxBackend.get_user_by_rpx_id(self.backend, "will not find") == None)
Exemplo n.º 7
0
 def testMapNoneRpxIdToExistingUser(self):
     """Tests error cases for RpxBackend.map_to_existing_user_by_email()."""
     
     try:
         RpxBackend.map_to_existing_user_by_email(self.backend, None)
         self.assert_(False)
     except ValueError:
         pass
     
     try:
         self.assert_(RpxBackend.map_to_existing_user_by_email(self.backend, "StringObject") == None)
         self.assert_(False)
     except ValueError:
         pass
Exemplo n.º 8
0
 def testCreateNewUser(self):
     """Tests RpxBackend.create_user().
     
     Creates a new normal user.
     
     """
     user, auth_info = self.__create_normal_user__()
     
     retreived_user = RpxBackend.get_user_by_rpx_id(self.backend, auth_info.get_rpx_id())
     
     self.assert_(retreived_user == user)        
     self.assert_(user.key() == retreived_user.key())
     
     RpxBackend.delete_user(self.backend, user)
Exemplo n.º 9
0
 def testDeleteNoneUser(self):
     """Tests RpxBackend.delete_user().
     
     Make sure that delete_user can handle None and that it only deletes Users!
     """
     try:
         RpxBackend.delete_user(self.backend, None)
         self.assert_(False)
     except ValueError:
         pass
     
     try:
         RpxBackend.delete_user(self.backend, "stringObject")
         self.assert_(False)
     except ValueError:
         pass
Exemplo n.º 10
0
 def __create_normal_user__(self):
     """Creates a "normal" user that used in numerous test cases below."""
     
     normalJson = '''
         {
           "profile": {
             "displayName": "brian",
             "preferredUsername": "******",
             "email": "*****@*****.**",
             "providerName": "Google",
             "identifier": "http:\/\/brian.myopenid.com\/"
           },
           "stat": "ok"
         }'''
         
     json = simplejson.loads(normalJson)
     
     auth_info = RpxAuthInfo(json)
     
     self.assert_(auth_info)
     self.assert_(auth_info.get_status() == RpxAuthInfo.OK)
     
     user = RpxBackend.create_user(self.backend, auth_info)
     
     self.assert_(user)
     self.assert_(user.username == "brian")
     self.assert_(user.email == "*****@*****.**")
     
     return user, auth_info
Exemplo n.º 11
0
def rpx_map_response(request):
    """Handle the login response from RPX used to map user with several id's"""
    
    logger = logging.getLogger('rpx')
    logger.setLevel(settings.RPX_LOG_LEVEL)
    
    token = request.POST.get('token')
    
    if not token:
        logger.error("rpx_login_response: No token was present") 
        return HttpResponseForbidden()
      
    backend = RpxBackend()
    backend.map_to_existing_user(token, request.user)
        
    return HttpResponseRedirect('.')
Exemplo n.º 12
0
 def testCreateRpxKey(self):
     """Tests create_rpx_key().
     
     create_rpx_key() should make sure that the key name starts with legal characters as defined by 
     http://code.google.com/appengine/docs/python/datastore/modelclass.html#Model
        
     """
     rpx_id = "abcd1234"
     rpx_key = generate_key_name(rpx_id)
     
     self.assert_(RpxBackend.create_rpx_key(self.backend, rpx_id) == rpx_key)
     
     try:
         RpxBackend.create_rpx_key(self.backend, None)
         self.assert_(False)
     except:
         pass
Exemplo n.º 13
0
 def setUp(self):
     self.backend = RpxBackend()
     # Need to hack get_current
     from django.conf import settings
     from django.contrib.sites.models import SiteManager, Site
     def get_current(self):
         return Site(domain = 'test.domain.com', name = 'test.site')
     if not hasattr(settings, 'SITE_ID'):
         SiteManager.get_current = get_current
Exemplo n.º 14
0
 def testDeleteUser(self):
     """Tests RpxBackend.delete_user().
     
     Tests that both the user and the associated RPX data entries are deleted.
     
     """
     user, auth_info = self.__create_normal_user__()
     
     user_key = user.key()
     
     number_of_users_entries = User.all().count()
     number_of_rpx_entries = RpxData.all().count()
     
     RpxBackend.delete_user(self.backend, user)
     
     self.assert_(User.all().count() == number_of_users_entries - 1)
     self.assert_(RpxData.all().count() == number_of_rpx_entries - 1)
     self.assert_(RpxBackend.get_user(self.backend, user_key) == None)
Exemplo n.º 15
0
    def testGetOrCreateUser(self):
        """Tests RpXBackend.get_or_create_user()."""
        
        try:
            RpxBackend.get_or_create_user(self.backend, None)
            self.assert_(False)
        except ValueError:
            pass
        
        user, auth_info = self.__create_normal_user__()
        
        self.assert_(user == RpxBackend.get_or_create_user(self.backend, auth_info))
        
        testJson2 = '''
            {
              "profile": {
                "displayName": "brian",
                "preferredUsername": "******",
                "email": "*****@*****.**",
                "providerName": "Google",
                "identifier": "http:\/\/brian.anotheropenid.com\/"
              },
              "stat": "ok"
            }'''
            
        json = simplejson.loads(testJson2)
        
        auth_info = RpxAuthInfo(json)       
        
        self.assert_(user == RpxBackend.get_or_create_user(self.backend, auth_info))
        
        RpxBackend.delete_user(self.backend, user)
        
        testJson2 = '''
            {
              "profile": {
                "displayName": "james",
                "preferredUsername": "******",
                "email": "*****@*****.**",
                "providerName": "Google",
                "identifier": "http:\/\/james.openid.com\/"
              },
              "stat": "ok"
            }'''
            
        json = simplejson.loads(testJson2)
        
        auth_info = RpxAuthInfo(json)
        
        new_user = RpxBackend.get_or_create_user(self.backend, auth_info)
        
        self.assert_(new_user)
        self.assert_(new_user.username == "james")

        self.backend.delete_user(new_user)
Exemplo n.º 16
0
 def testGetUserFromId(self):
     """Tests RpxBackend.get_user().
     
     Make sure it handles None and negative results.
     
     """
     
     try:
         RpxBackend.get_user(self.backend, None)
         self.assert_(False)
     except:
         pass
     
     self.assert_(RpxBackend.get_user(self.backend, "willnotfind") == None)
     
     user = User(username="******")
     user.save()
     rpx_data = RpxData(user=user)
     rpx_data.save()
     
     #Test that is can handle key from non User class and returns None
     self.assert_(RpxBackend.get_user(self.backend, rpx_data.key()) == None)
     
     RpxBackend.delete_user(self.backend, user)
Exemplo n.º 17
0
class TestRpxBackends(unittest.TestCase):
    """Test the functionality offered by backends.py"""
    
    def __create_normal_user__(self):
        """Creates a "normal" user that used in numerous test cases below."""
        
        normalJson = '''
            {
              "profile": {
                "displayName": "brian",
                "preferredUsername": "******",
                "email": "*****@*****.**",
                "providerName": "Google",
                "identifier": "http:\/\/brian.myopenid.com\/"
              },
              "stat": "ok"
            }'''
            
        json = simplejson.loads(normalJson)
        
        auth_info = RpxAuthInfo(json)
        
        self.assert_(auth_info)
        self.assert_(auth_info.get_status() == RpxAuthInfo.OK)
        
        user = RpxBackend.create_user(self.backend, auth_info)
        
        self.assert_(user)
        self.assert_(user.username == "brian")
        self.assert_(user.email == "*****@*****.**")
        
        return user, auth_info
    
    def setUp(self):
        self.backend = RpxBackend()
        # Need to hack get_current
        from django.conf import settings
        from django.contrib.sites.models import SiteManager, Site
        def get_current(self):
            return Site(domain = 'test.domain.com', name = 'test.site')
        if not hasattr(settings, 'SITE_ID'):
            SiteManager.get_current = get_current
        
    def tearDown(self):
        pass
    
    def testCreateRpxKey(self):
        """Tests create_rpx_key().
        
        create_rpx_key() should make sure that the key name starts with legal characters as defined by 
        http://code.google.com/appengine/docs/python/datastore/modelclass.html#Model
           
        """
        rpx_id = "abcd1234"
        rpx_key = generate_key_name(rpx_id)
        
        self.assert_(RpxBackend.create_rpx_key(self.backend, rpx_id) == rpx_key)
        
        try:
            RpxBackend.create_rpx_key(self.backend, None)
            self.assert_(False)
        except:
            pass
        
    def testCreateNewUser(self):
        """Tests RpxBackend.create_user().
        
        Creates a new normal user.
        
        """
        user, auth_info = self.__create_normal_user__()
        
        retreived_user = RpxBackend.get_user_by_rpx_id(self.backend, auth_info.get_rpx_id())
        
        self.assert_(retreived_user == user)        
        self.assert_(user.key() == retreived_user.key())
        
        RpxBackend.delete_user(self.backend, user)

    def testCreateNewUserWithExistingUsername(self):

        user, auth_info = self.__create_normal_user__()
        
        testJson2 = '''
            {
              "profile": {
                "displayName": "brian",
                "preferredUsername": "******",
                "email": "*****@*****.**",
                "providerName": "Other",
                "identifier": "http:\/\/brian2.myopenid.com\/"
              },
              "stat": "ok"
            }'''

        json = simplejson.loads(testJson2)
        
        auth_info = RpxAuthInfo(json)

        user2 = RpxBackend.create_user(self.backend, auth_info)

        self.assert_(user2)
        self.assert_(user2.username == "brian")
        self.assert_(user2.email == "*****@*****.**")
        
        RpxBackend.delete_user(self.backend, user)
        RpxBackend.delete_user(self.backend, user2)
        
    def testDeleteUser(self):
        """Tests RpxBackend.delete_user().
        
        Tests that both the user and the associated RPX data entries are deleted.
        
        """
        user, auth_info = self.__create_normal_user__()
        
        user_key = user.key()
        
        number_of_users_entries = User.all().count()
        number_of_rpx_entries = RpxData.all().count()
        
        RpxBackend.delete_user(self.backend, user)
        
        self.assert_(User.all().count() == number_of_users_entries - 1)
        self.assert_(RpxData.all().count() == number_of_rpx_entries - 1)
        self.assert_(RpxBackend.get_user(self.backend, user_key) == None)
        
    
    def testDeleteNoneUser(self):
        """Tests RpxBackend.delete_user().
        
        Make sure that delete_user can handle None and that it only deletes Users!
        """
        try:
            RpxBackend.delete_user(self.backend, None)
            self.assert_(False)
        except ValueError:
            pass
        
        try:
            RpxBackend.delete_user(self.backend, "stringObject")
            self.assert_(False)
        except ValueError:
            pass
        
    def testGetUserFromRpxId(self):
        """Tests RpxBackend.get_user_by_rpx_id()."""
        
        user, auth_info = self.__create_normal_user__()
        
        self.assert_(user == RpxBackend.get_user_by_rpx_id(self.backend, auth_info.get_rpx_id()))
        
        RpxBackend.delete_user(self.backend, user)
        
    def testGetUserFromNoneRpxId(self):
        """Tests RpxBackend.get_user_by_rpx_id().
        
        Make sure it handles None and negative results.
        
        """
        try:
            RpxBackend.get_user_by_rpx_id(self.backend, None)
            self.assert_(False)
        except ValueError:
            pass
        
        self.assert_(RpxBackend.get_user_by_rpx_id(self.backend, "will not find") == None)
        
    def testGetUserFromRpxIdWithInvalidModel(self):
        
        try:
            rpx_data = RpxData(key_name=RpxBackend.create_rpx_key(self.backend, "thisiskey"), user=None)
            self.assert_(False)
        except db.BadValueError:
            pass            
        
        self.assert_(RpxBackend.get_user_by_rpx_id(self.backend, "thisiskey") == None)
        
    def testGetUserFromId(self):
        """Tests RpxBackend.get_user().
        
        Make sure it handles None and negative results.
        
        """
        
        try:
            RpxBackend.get_user(self.backend, None)
            self.assert_(False)
        except:
            pass
        
        self.assert_(RpxBackend.get_user(self.backend, "willnotfind") == None)
        
        user = User(username="******")
        user.save()
        rpx_data = RpxData(user=user)
        rpx_data.save()
        
        #Test that is can handle key from non User class and returns None
        self.assert_(RpxBackend.get_user(self.backend, rpx_data.key()) == None)
        
        RpxBackend.delete_user(self.backend, user)
          
    def testMapRpxIdToExistingUser(self):
        """Tests RpxBackend.map_to_existing_user_by_email().
        
        This function should be able to map existing user to new RPX id's by
        matching email addresses from trusted providers.
        
        """
        user, auth_info = self.__create_normal_user__()
        
        testJson2 = '''
            {
              "profile": {
                "displayName": "brian",
                "preferredUsername": "******",
                "email": "*****@*****.**",
                "providerName": "Google",
                "identifier": "http:\/\/brian.anotheropenid.com\/"
              },
              "stat": "ok"
            }'''
            
        json = simplejson.loads(testJson2)
        
        auth_info = RpxAuthInfo(json)        
        
        self.assert_(auth_info)
        self.assert_(auth_info.get_status() == RpxAuthInfo.OK)       
        
        user2 = RpxBackend.map_to_existing_user_by_email(self.backend, auth_info)
        
        self.assert_(user == user2)
        
        RpxBackend.delete_user(self.backend, user)

    def testMapNoneRpxIdToExistingUser(self):
        """Tests error cases for RpxBackend.map_to_existing_user_by_email()."""
        
        try:
            RpxBackend.map_to_existing_user_by_email(self.backend, None)
            self.assert_(False)
        except ValueError:
            pass
        
        try:
            self.assert_(RpxBackend.map_to_existing_user_by_email(self.backend, "StringObject") == None)
            self.assert_(False)
        except ValueError:
            pass
        
        
    def testGetOrCreateUser(self):
        """Tests RpXBackend.get_or_create_user()."""
        
        try:
            RpxBackend.get_or_create_user(self.backend, None)
            self.assert_(False)
        except ValueError:
            pass
        
        user, auth_info = self.__create_normal_user__()
        
        self.assert_(user == RpxBackend.get_or_create_user(self.backend, auth_info))
        
        testJson2 = '''
            {
              "profile": {
                "displayName": "brian",
                "preferredUsername": "******",
                "email": "*****@*****.**",
                "providerName": "Google",
                "identifier": "http:\/\/brian.anotheropenid.com\/"
              },
              "stat": "ok"
            }'''
            
        json = simplejson.loads(testJson2)
        
        auth_info = RpxAuthInfo(json)       
        
        self.assert_(user == RpxBackend.get_or_create_user(self.backend, auth_info))
        
        RpxBackend.delete_user(self.backend, user)
        
        testJson2 = '''
            {
              "profile": {
                "displayName": "james",
                "preferredUsername": "******",
                "email": "*****@*****.**",
                "providerName": "Google",
                "identifier": "http:\/\/james.openid.com\/"
              },
              "stat": "ok"
            }'''
            
        json = simplejson.loads(testJson2)
        
        auth_info = RpxAuthInfo(json)
        
        new_user = RpxBackend.get_or_create_user(self.backend, auth_info)
        
        self.assert_(new_user)
        self.assert_(new_user.username == "james")

        self.backend.delete_user(new_user)