def test_getuser(self): """ Test for retrieving username by prepending @ """ # scenario 1: with @ starting in name and extid crusoe = self.fixtures.crusoe service_twitter = 'twitter' oauth_token = environ.get('TWITTER_OAUTH_TOKEN') oauth_token_type = 'Bearer' # NOQA: S105 externalid = models.UserExternalId( service=service_twitter, user=crusoe, userid=crusoe.email.email, username=crusoe.username, oauth_token=oauth_token, oauth_token_type=oauth_token_type, ) db.session.add(externalid) db.session.commit() result1 = models.getuser('@crusoe') self.assertIsInstance(result1, models.User) self.assertEqual(result1, crusoe) # scenario 2: with @ in name and not extid d_email = '*****@*****.**' daenerys = models.User( username='******', fullname="Daenerys Targaryen", email=d_email ) daenerys_email = models.UserEmail(email=d_email, user=daenerys) db.session.add_all([daenerys, daenerys_email]) db.session.commit() result2 = models.getuser(d_email) self.assertIsInstance(result2, models.User) self.assertEqual(result2, daenerys) result3 = models.getuser('@daenerys') self.assertIsNone(result3) # scenario 3: with no @ starting in name, check by UserEmailClaim j_email = '*****@*****.**' jonsnow = models.User(username='******', fullname="Jon Snow") jonsnow_email_claimed = models.UserEmailClaim(email=j_email, user=jonsnow) db.session.add_all([jonsnow, jonsnow_email_claimed]) db.session.commit() result4 = models.getuser(j_email) self.assertIsInstance(result4, models.User) self.assertEqual(result4, jonsnow) # scenario 5: with no @ anywhere in name, fetch username arya = models.User(username='******', fullname="Arya Stark") db.session.add(arya) db.session.commit() result5 = models.getuser('arya') self.assertEqual(result5, arya) # scenario 6: with no starting with @ name and no UserEmailClaim or UserEmail cersei = models.User(username='******', fullname="Cersei Lannister") db.session.add(cersei) db.session.commit() result6 = models.getuser('*****@*****.**') self.assertIsNone(result6)
def test_UserExternalId(self): """ Test for creating an instance of UserExternalId """ crusoe = self.fixtures.crusoe service = u'google' oauth_token = u'196461869-pPh2cPTnlqGHcJBcyQ4CR407d1j5LY4OdbhNQuvX' oauth_token_type = u'Bearer' result = models.UserExternalId(service=service, user=crusoe, userid=crusoe.email.email, username=crusoe.email.email, oauth_token=oauth_token, oauth_token_type=oauth_token_type) self.assertIsInstance(result, models.UserExternalId) assert u'<UserExternalId {service}:{username} of {user}>'.format( service=service, username=crusoe.email.email, user=repr(crusoe)[1:-1]) in repr(result)
def test_userexternalid_get(self): """ Test for retrieving a UserExternalId instance given a serverice and userid or username """ service = 'twitter' # scenario 1: when neither userid nor username is passed with self.assertRaises(TypeError): models.UserExternalId.get(service) crusoe = self.fixtures.crusoe oauth_token = environ.get('TWITTER_OAUTH_TOKEN') oauth_token_type = 'Bearer' # NOQA: S105 externalid = models.UserExternalId( service=service, user=crusoe, userid=crusoe.email.email, username=crusoe.email.email, oauth_token=oauth_token, oauth_token_type=oauth_token_type, ) db.session.add(externalid) db.session.commit() # scenario 2: when userid is passed get_by_userid = models.UserExternalId.get(service=service, userid=crusoe.email.email) self.assertIsInstance(get_by_userid, models.UserExternalId) assert '<UserExternalId {service}:{username} of {user}>'.format( service=service, username=crusoe.email.email, user=repr(crusoe)[1:-1]) in repr(get_by_userid) # scenario 3: when username is passed get_by_username = models.UserExternalId.get( service=service, username=crusoe.email.email) self.assertIsInstance(get_by_username, models.UserExternalId) assert '<UserExternalId {service}:{username} of {user}>'.format( service=service, username=crusoe.email.email, user=repr(crusoe)[1:-1]) in repr(get_by_username)
def test_getextid(self): """ Test for retrieving user given service and userid """ crusoe = self.fixtures.crusoe email = crusoe.email.email service_facebook = u'facebook' externalid = models.UserExternalId( service=service_facebook, user=crusoe, userid=crusoe.email.email, username=crusoe.email.email, oauth_token=environ.get('FACEBOOK_OAUTH_TOKEN'), oauth_token_type=u'bearer') db.session.add(externalid) db.session.commit() result = models.getextid(service_facebook, userid=email) self.assertIsInstance(result, models.UserExternalId) assert u'<UserExternalId {service}:{username} of {user}>'.format( service=service_facebook, username=email, user=repr(crusoe)[1:-1]) in repr(result)