def test_user_clients_with_team_access(self): """ Test to verify a list of clients with access to the user's organizations' teams. """ aro = models.User(username='******') jane = models.User(username='******') marcus = models.User(username='******') volturi = models.Organization(name='volturi', title='The Volturi') volturi.owners.users.append(aro) volterra = models.AuthClient( title='Volterra, Tuscany', organization=volturi, confidential=True, website='volterra.co.it', ) enforcers = models.AuthClient( title='Volturi\'s thugs', organization=volturi, confidential=True, website='volturi.co.it', ) volterra_auth_token = models.AuthToken(auth_client=volterra, user=aro, scope='teams', validity=0) volterra_auth_token enforcers_auth_token = models.AuthToken(auth_client=enforcers, user=marcus, scope='teams', validity=0) enforcers_auth_token self.assertCountEqual(aro.clients_with_team_access(), [volterra]) self.assertCountEqual(marcus.clients_with_team_access(), [enforcers]) self.assertEqual(jane.clients_with_team_access(), [])
def test_authtoken_is_valid(self): """ Test for verifying if AuthToken's token is valid """ client = self.fixtures.client # scenario 1: when validity is unlimited (0) tomriddle = models.User(username=u'voldemort', fullname=u'Tom Riddle') scope = [u'id', u'email'] tomriddle_token = models.AuthToken(client=client, user=tomriddle, scope=scope, validity=0) self.assertTrue(tomriddle_token.is_valid()) # scenario 2: when validity has not been given draco = models.User(username=u'draco', fullname=u'Draco Malfoy') draco_token = models.AuthToken(client=client, user=draco, scope=scope) with self.assertRaises(TypeError): draco_token.is_valid() # scenario 3: when validity is limited harry = models.User(username=u'harry', fullname=u'Harry Potter') harry_token = models.AuthToken(client=client, user=harry, scope=scope, validity=3600, created_at=utcnow()) self.assertTrue(harry_token.is_valid()) # scenario 4: when validity is limited *and* the token has expired cedric = models.User(username=u'cedric', fullname=u'Cedric Diggory') cedric_token = models.AuthToken(client=client, user=cedric, scope=scope, validity=1, created_at=utcnow() - timedelta(1)) self.assertFalse(cedric_token.is_valid())
def test_merge_users(self): """ Test to verify merger of user accounts and return new user """ # Scenario 1: if first user's created_at date younger than second user's created_at crusoe = self.fixtures.crusoe batdog = models.User(username=u"batdog", fullname=u"Batdog") db.session.add(batdog) db.session.commit() merged = models.merge_users(crusoe, batdog) self.assertEqual(merged, crusoe) self.assertIsInstance(merged, models.User) # because the logic is to merge into older account self.assertEqual(crusoe.status, 0) self.assertEqual(batdog.status, 2) # Scenario 1: if second user's created_at date older than first user 's created_at tyrion = models.User(username=u'tyrion', fullname=u'Tyrion Lannister') db.session.add(tyrion) db.session.commit() subramanian = models.User(username=u'subramanian', fullname=u'Tyrion subramanian') db.session.add(subramanian) db.session.commit() merged = models.merge_users(subramanian, tyrion) self.assertEqual(merged, tyrion) self.assertIsInstance(merged, models.User) # because the logic is to merge into older account self.assertEqual(tyrion.status, 0) self.assertEqual(subramanian.status, 2)
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_user_add_email(self): """ Test to add email address for a user """ # scenario 1: if primary flag is True and user has no existing email mr_whymper = models.User(username='******') whymper_email = '*****@*****.**' whymper_result = mr_whymper.add_email(whymper_email, primary=True) self.assertEqual(whymper_result.email, whymper_email) # # scenario 2: when primary flag is True but user has existing primary email crusoe = self.fixtures.crusoe crusoe_new_email = '*****@*****.**' crusoe_result = crusoe.add_email(email=crusoe_new_email, primary=True) self.assertEqual(crusoe_result.email, crusoe_new_email) # # scenario 3: when primary flag is True but user has existing email same as one passed crusoe_existing_email = '*****@*****.**' crusoe_result = crusoe.add_email(crusoe_existing_email, primary=True) self.assertEqual(crusoe_result.email, crusoe_existing_email) # scenario 4: when requested to adds an email with domain belonging to a team, add user to team gustav = models.User(username='******') gustav_email = '*****@*****.**' gustav_result = gustav.add_email(gustav_email) db.session.add(gustav) db.session.commit() self.assertEqual(gustav_result.email, gustav_email)
def test_user_password_is(self): """ Test to retrieve hashed password for a user """ # scenario 1: no password been set oldmajor = models.User(username='******') self.assertFalse(oldmajor.password_is('oinkoink')) # scenario 3: if password has been set dumbeldore = models.User('dumbeldore', fullname='Albus Dumberldore') dumbeldore_password = '******' dumbeldore.password = dumbeldore_password self.assertTrue(dumbeldore.password_is(dumbeldore_password))
def test_useremailclaim_all(self): """ Test for retrieving all UserEmailClaim instances given an email address """ gail = models.User(username='******', fullname='Gail Hawthorne') peeta = models.User(username='******', fullname='Peeta Mallark') email = '*****@*****.**' claim_by_gail = models.UserEmailClaim(email=email, user=gail) claim_by_peeta = models.UserEmailClaim(email=email, user=peeta) db.session.add(claim_by_gail) db.session.add(claim_by_peeta) db.session.commit() result = models.UserEmailClaim.all(email) self.assertCountEqual(set(result), {claim_by_gail, claim_by_peeta})
def test_user_all(self): """ Test for User's all method """ # scenario 1: when neither buids or usernames are passed with self.assertRaises(Exception): models.User.all() crusoe = self.fixtures.crusoe oakley = self.fixtures.oakley expected_result = [crusoe, oakley] # scenario 2: when both buids and usernames are passed lookup_by_both = models.User.all(buids=[crusoe.buid], usernames=[oakley.username]) self.assertIsInstance(lookup_by_both, list) self.assertCountEqual(lookup_by_both, expected_result) # scenario 3: when only buids are passed lookup_by_buids = models.User.all(buids=[crusoe.buid, oakley.buid]) self.assertIsInstance(lookup_by_buids, list) self.assertCountEqual(lookup_by_buids, expected_result) # scenario 4: when only usernames are passed lookup_by_usernames = models.User.all( usernames=[crusoe.username, oakley.username]) self.assertIsInstance(lookup_by_usernames, list) self.assertCountEqual(lookup_by_usernames, expected_result) # scenario 5: when defercols is set to True lookup_by_usernames_defercols = models.User.all( usernames=[crusoe.username, oakley.username], defercols=True) lookup_by_usernames_defercols self.assertIsInstance(lookup_by_usernames, list) self.assertCountEqual(lookup_by_usernames, expected_result) # scenario 6: when user.status is active hannibal = models.User(username='******') hannibal.status = models.USER_STATUS.ACTIVE db.session.add(hannibal) db.session.commit() lookup_by_buid_status = models.User.all(usernames=[hannibal.username]) self.assertIsInstance(lookup_by_buid_status, list) self.assertEqual(lookup_by_buid_status[0].status, hannibal.status) # scenario 7 : when user.status is USER_STATUS.MERGED jykll = models.User() hyde = models.User() db.session.add_all([jykll, hyde]) db.session.commit() merged_user = models.merge_users(jykll, hyde) merged_user db.session.commit() lookup_by_buid_merged = models.User.all(buids=[hyde.buid]) self.assertIsInstance(lookup_by_buid_merged, list) self.assertEqual(lookup_by_buid_merged[0].username, jykll.username)
def test_UserEmailClaim_all(self): """ Test for retrieving all UserEmailClaim instances given an email address """ gail = models.User(username=u'gail', fullname=u'Gail Hawthorne') peeta = models.User(username=u'peeta', fullname=u'Peeta Mallark') email = u'*****@*****.**' claim_by_gail = models.UserEmailClaim(email=email, user=gail) claim_by_peeta = models.UserEmailClaim(email=email, user=peeta) db.session.add(claim_by_gail) db.session.add(claim_by_peeta) db.session.commit() result = models.UserEmailClaim.all(email) self.assertIsInstance(result, list) self.assertItemsEqual(result, [claim_by_gail, claim_by_peeta])
def test_user_del_email(self): """ Test to delete email address for a user """ mr_jones = models.User(username='******') mr_jones_primary_email = models.UserEmail( email='*****@*****.**', user=mr_jones, primary=True) mr_jones_secondary_email = models.UserEmail( email='*****@*****.**', user=mr_jones) mr_jones_spare_email = models.UserEmail(email='*****@*****.**', user=mr_jones) db.session.add_all([ mr_jones, mr_jones_primary_email, mr_jones_secondary_email, mr_jones_spare_email, ]) db.session.commit() # scenario 1: when email requested to be deleted is primary primary_email = mr_jones_primary_email.email mr_jones.del_email(primary_email) db.session.commit() result1 = mr_jones.emails self.assertIsInstance(result1, list) self.assertCountEqual(result1, [mr_jones_secondary_email, mr_jones_spare_email]) self.assertTrue(mr_jones_secondary_email.primary) # scenario 2: when email requested to be delete is not primary spare_email = mr_jones_spare_email.email mr_jones.del_email(spare_email) db.session.commit() result2 = mr_jones.emails self.assertIsInstance(result2, list) self.assertCountEqual(result2, [mr_jones_secondary_email]) self.assertTrue(mr_jones_secondary_email.primary)
def test_ownermixin_owner(self): """Test to verify setting of owner property on any OwnerMixin inherited instance""" crusoe = self.fixtures.crusoe # scenario 1: check owner of existing UserEmail instance crusoe_email = self.fixtures.crusoe_email self.assertEqual(crusoe_email.owner, crusoe) # scenario 2: check owner when owner is org client = self.fixtures.client batdog = self.fixtures.batdog dachshunds = self.fixtures.dachshunds self.assertEqual(client.owner, batdog) # scenaio 3: check owner when new UserEmailClaim instance spock = models.User(username=u'spock') spock_email_claim = models.UserEmailClaim( email=u'*****@*****.**', user=spock) self.assertEqual(spock_email_claim.owner, spock) # scenario 4: set user as owner on some OwnerMixin inherited instance spock_phone = models.UserPhoneClaim(phone=u'+9112345678', user=spock) spock_phone.owner = spock self.assertEqual(spock_phone.owner, spock) # scenario 5: set organization as owner on some OwnerMixin inherited instance spock_phone.owner = batdog self.assertEqual(spock_phone.owner, batdog) # scenario 6: set Team as owner on some OwnerMixin inherited instance spock_phone.owner = dachshunds self.assertEqual(spock_phone.owner, dachshunds) # scenario 7: when owner not insance of User, organization or Team with self.assertRaises(ValueError): spock_phone.owner = client
def test_user_password(self): """ Test to set user password """ # Scenario 1: Set None as password castle = models.User(username='******', fullname='Rick Castle') castle.password = None self.assertIsNone(castle.pw_hash) # Scenario 2: Set valid password kate = models.User(username='******', fullname='Detective Kate Beckette') kate.password = '******' db.session.add(kate) db.session.commit() result = models.User.get(buid=kate.buid) self.assertEqual(len(result.pw_hash), 60) self.assertTrue(result.password_is('12thprecinct')) self.assertGreater(result.pw_expires_at, result.pw_set_at)
def test_authtoken_refresh(self): """Test to verify creation of new token while retaining the refresh token.""" hagrid = models.User(username=u'hagrid', fullname=u'Rubeus Hagrid') auth_token = models.AuthToken(user=hagrid, algorithm='hmac-sha-1') existing_token = auth_token.token existing_secret = auth_token.secret auth_token.refresh() self.assertNotEqual(existing_token, auth_token.token) self.assertNotEqual(existing_secret, auth_token.secret)
def test_useremailclaim_email(self): """ Test for verifying UserEmailClaim email property """ effie = models.User(username=u'effie', fullname=u'Miss. Effie Trinket') email = u'*****@*****.**' claim_by_effie = models.UserEmailClaim(email=email, user=effie) self.assertIsInstance(claim_by_effie.email, unicode) self.assertEqual(claim_by_effie.email, email)
def test_double_assigned_name(self): """ Names cannot be assigned to both a user and an organization simultaneously """ user = models.User(fullname="User") org = models.Organization(title="Organization") name = models.AccountName(name='double-assigned', user=user, organization=org) db.session.add_all([user, org, name]) with self.assertRaises(IntegrityError): db.session.commit()
def test_user_profileid(self): """ Test to verify profileid (same as username) if any """ crusoe = self.fixtures.crusoe # scenario 1: when username is given self.assertEqual(crusoe.profileid(), crusoe.username) # scenario 2: when username doesn't exist mollie = models.User(fullname='Mollie') self.assertEqual(len(mollie.profileid()), 22)
def test_make_email_primary(self): """ Test to make an email primary for a user """ mr_whymper = models.User(username='******') whymper_email = '*****@*****.**' whymper_result = mr_whymper.add_email(whymper_email) mr_whymper.primary_email = whymper_result self.assertEqual(whymper_result.email, whymper_email) self.assertTrue(whymper_result.primary)
def test_User(self): """ Test for creation of user object from User model """ user = models.User(username=u"lena", fullname=u"Lena Audrey Dachshund") db.session.add_all([user]) db.session.commit() lena = models.User.query.filter_by(username=u"lena").first() self.assertIsInstance(lena, models.User) self.assertEqual(user.username, u"lena") self.assertEqual(user.fullname, u"Lena Audrey Dachshund")
def test_UserSession_authenticate(self): """Test to verify authenticate method on UserSession""" chandler = models.User(username=u'chandler', fullname=u'Chandler Bing') chandler_buid = buid() chandler_session = models.UserSession(user=chandler, ipaddr='192.168.1.4', buid=chandler_buid, user_agent=u'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36', accessed_at=utcnow()) db.session.add(chandler) db.session.add(chandler_session) db.session.commit() result = models.UserSession.authenticate(chandler_buid) self.assertIsInstance(result, models.UserSession) self.assertEqual(result, chandler_session)
def test_user(self): """ Test for creation of user object from User model """ user = models.User(username='******', fullname="Lena Audrey Dachshund") db.session.add_all([user]) db.session.commit() lena = models.User.get(username='******') self.assertIsInstance(lena, models.User) self.assertEqual(user.username, 'lena') self.assertEqual(user.fullname, "Lena Audrey Dachshund")
def test_userclientpermissions_buid(self): """ Test for UserClientPermissions' buid """ beetee = models.User(username='******', fullname="Beetee") district3 = models.AuthClient(title='District 3') access_permissions = 'siteadmin' result = models.AuthClientUserPermissions( user=beetee, auth_client=district3, access_permissions=access_permissions ) self.assertEqual(result.buid, beetee.buid)
def test_userclientpermissions_pickername(self): """ Test for UserClientPermissions' pickername """ finnick = models.User(username='******', fullname="Finnick Odair") district4 = models.AuthClient(title="District 4") access_permissions = 'siteadmin' result = models.AuthClientUserPermissions( user=finnick, auth_client=district4, access_permissions=access_permissions ) self.assertEqual(result.pickername, finnick.pickername)
def test_scopemixin__scope_set(self): """Test to set scope with __scope_set on an AuthToken instance""" """Test to retrieve scope with __scope_get on an AuthToken instance """ scope = [u'teams', u'wars', u'alliances'] sansa = models.User(username=u'sansa', fullname=u'Sansa Stark') client = self.fixtures.client sansa_token = models.AuthToken(client=client, user=sansa, validity=0) sansa_token._scope_set(scope) db.session.add_all([sansa, sansa_token]) db.session.commit() self.assertEqual(sansa_token._scope_get(), tuple(sorted(scope)))
def test_user_is_profile_complete(self): """ Test to check if user profile is complete that is fullname, username and email are present """ crusoe = self.fixtures.crusoe self.assertTrue(crusoe.is_profile_complete()) lena = models.User() db.session.add(lena) db.session.commit() self.assertFalse(lena.is_profile_complete())
def test_userclientpermissions_migrate_user(self): """ Test for migrating users and transfering their client permissions """ # scenario 1: when *only* olduser has UserClientPermissions instance old_crusoe = self.fixtures.crusoe new_crusoe = models.User(username='******') models.AuthClientUserPermissions.migrate_user(old_crusoe, new_crusoe) for each in new_crusoe.client_permissions: self.assertIsInstance(each, models.AuthClientUserPermissions) self.assertEqual(new_crusoe.client_permissions[0].user, new_crusoe) # scenario 2: when *both* olduser and newuser have UserClientPermissions instances old_oakley = self.fixtures.oakley auth_client = self.fixtures.auth_client access_permissions_old_oakley = 'siteadmin' access_permissions_new_oakley = 'siteeditor' old_oakley_userclientperms = models.AuthClientUserPermissions( user=old_oakley, auth_client=auth_client, access_permissions=access_permissions_old_oakley, ) new_oakley = models.User(username='******') new_oakley_userclientperms = models.AuthClientUserPermissions( user=new_oakley, auth_client=auth_client, access_permissions=access_permissions_new_oakley, ) db.session.add(old_oakley_userclientperms) db.session.add(new_oakley_userclientperms) db.session.commit() models.AuthClientUserPermissions.migrate_user(old_oakley, new_oakley) result = new_oakley.client_permissions[0] for each in new_oakley.client_permissions: self.assertIsInstance(each, models.AuthClientUserPermissions) received_access_permissions = str(result.access_permissions) expected_access_permissions = " ".join( [access_permissions_old_oakley, access_permissions_new_oakley] ) self.assertEqual(expected_access_permissions, received_access_permissions)
def test_User_get(self): """ Test for User's get method """ # scenario 1: if both username and buid not passed with self.assertRaises(TypeError): models.User.get() crusoe = self.fixtures.crusoe piglet = self.fixtures.piglet # scenario 2: if buid is passed lookup_by_buid = models.User.get(buid=crusoe.buid) self.assertIsInstance(lookup_by_buid, models.client.User) self.assertEqual(lookup_by_buid.buid, crusoe.buid) # scenario 3: if username is passed lookup_by_username = models.User.get(username=u"crusoe") self.assertIsInstance(lookup_by_username, models.User) self.assertEqual(lookup_by_username.username, u"crusoe") # scenario 4: if defercols is set to True lookup_by_username = models.User.get(username=u"crusoe", defercols=True) self.assertIsInstance(lookup_by_username, models.User) self.assertEqual(lookup_by_username.username, u"crusoe") # scenario 5: when user.status is active lector = models.User() lector.status = models.USER_STATUS.ACTIVE db.session.add(lector) db.session.commit() lookup_by_buid_status = models.User.get(buid=lector.buid) self.assertIsInstance(lookup_by_buid_status, models.User) self.assertEqual(lookup_by_buid_status.status, lector.status) # scenario 6 : when user.status is USER_STATUS.MERGED piglet = self.fixtures.piglet piggy = models.User(username=u'piggy') db.session.add(piggy) db.session.commit() merged_user = models.merge_users(piglet, piggy) merged_user # NOQA db.session.commit() lookup_by_buid_merged = models.User.get(buid=piggy.buid) self.assertIsInstance(lookup_by_buid_merged, models.User) self.assertEqual(lookup_by_buid_merged.username, piglet.username)
def test_scopemixin__scope_get(self): """Test to retrieve scope with __scope_get on an AuthToken instance """ scope = ['teams', 'email', 'id'] khal = models.User(username='******', fullname='Khal Drogo') auth_client = self.fixtures.auth_client khal_token = models.AuthToken(auth_client=auth_client, user=khal, scope=scope, validity=0) db.session.add_all([khal, khal_token]) db.session.commit() self.assertEqual(khal_token._scope_get(), tuple(sorted(scope)))
def test_scopemixin_scope(self): """Test to retrieve scope on an ScopeMixin inherited class instance via scope method""" scope = 'tricks' ginny = models.User(username='******', fullname='Ginny Weasley') auth_client = self.fixtures.auth_client ginny_token = models.AuthToken(auth_client=auth_client, user=ginny, scope=scope, validity=0) db.session.add_all([ginny, ginny_token]) db.session.commit() self.assertEqual(ginny_token.scope, (scope, ))
def test_user_password_has_expired(self): """ Test to check if password for a user has expired """ alexis = models.User(username='******', fullname='Alexis Castle') alexis.password = '******' alexis.pw_expires_at = utcnow() + timedelta(0, 0, 1) db.session.add(alexis) db.session.commit() result = models.User.get(buid=alexis.buid) self.assertIsNotNone(result) self.assertTrue(alexis.password_has_expired())
def test_user_displayname(self): """ Test to verify any fullname or username or buid for displayname """ crusoe = self.fixtures.crusoe oakley = self.fixtures.oakley self.assertEqual(crusoe.displayname(), crusoe.fullname) self.assertEqual(oakley.displayname(), oakley.username) lena = models.User() db.session.add(lena) db.session.commit() self.assertEqual(lena.displayname(), lena.buid)