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 __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 testApiEmptyProfile(self): """Tests error case where the 'profile' value is empty.""" testJson = ''' { "profile": "", "stat": "ok" }''' json = simplejson.loads(testJson) auth_info = RpxAuthInfo(json) self.assert_(auth_info) self.assert_(auth_info.get_status() == RpxAuthInfo.MISSING)
def testApiWrongProfile(self): """Tests error case where the 'profile' value is malformed.""" testJson = ''' { "profile": "hello", "stat": "ok" }''' json = simplejson.loads(testJson) auth_info = RpxAuthInfo(json) self.assert_(auth_info) self.assert_(auth_info.get_status() == RpxAuthInfo.MALFORMED)
def testApiEmpty(self): """Tests error case where the Json response is an empty string.""" testJson = "" #Test simplejson try: json = simplejson.loads(testJson) assert_(False) #simplejson should throw exception! except ValueError: pass #Test constructor auth_info = RpxAuthInfo({}) self.assert_(auth_info) self.assert_(auth_info.get_status() == RpxAuthInfo.MISSING)
def testApiNotOk(self): """Tests error case where the 'stat' value is not ok.""" testJson = ''' { "noProfile": { "displayName": "something" }, "stat": "nok" }''' json = simplejson.loads(testJson) auth_info = RpxAuthInfo(json) self.assert_(auth_info) self.assert_(auth_info.get_status() == RpxAuthInfo.ERROR)
def testApiMissingProfile(self): """Tests error case were the 'profile' entry is missing.""" testJson = ''' { "noProfile": { "displayName": "something" }, "stat": "ok" }''' json = simplejson.loads(testJson) auth_info = RpxAuthInfo(json) self.assert_(auth_info) self.assert_(auth_info.get_status() == RpxAuthInfo.MISSING)
def testApiValidJson(self): """Tests the RpxAuthInfo constructor. Should always be possible to create RpxAuthInfo objects. Status should be used by applications to decide if the response from RPX was valid. """ testJson = ''' { "profile": { "displayName": "brian", "preferredUsername": "******", "email": "*****@*****.**", "providerName": "Other", "identifier": "http:\/\/brian.myopenid.com\/" }, "stat": "ok" }''' json = simplejson.loads(testJson) auth_info = RpxAuthInfo(json) self.assert_(auth_info) self.assert_(auth_info.get_status() == RpxAuthInfo.OK) self.assertEquals(auth_info.get_user_name(), "brian") self.assertEquals(auth_info.get_rpx_id(), "http://brian.myopenid.com/") self.assertEquals(auth_info.get_email(), "*****@*****.**") self.assertEquals(auth_info.get_provider(), "Other")
def testApiValidJsonWithAltFields(self): """Tests that alternative fields for email and user name can be handled.""" testJson = ''' { "profile": { "displayName": "brian", "verifiedEmail": "*****@*****.**", "providerName": "Other", "identifier": "http:\/\/brian.myopenid.com\/" }, "stat": "ok" }''' json = simplejson.loads(testJson) auth_info = RpxAuthInfo(json) self.assert_(auth_info) self.assertEquals(auth_info.get_user_name(), "brian") self.assertEquals(auth_info.get_rpx_id(), "http://brian.myopenid.com/") self.assertEquals(auth_info.get_email(), "*****@*****.**") self.assertEquals(auth_info.get_provider(), "Other")