def test_client_authtoken_for(self): """ Test for retrieving authtoken for this user and client (only confidential clients) """ # scenario 1: for a client that has confidential=True auth_client = self.fixtures.auth_client crusoe = self.fixtures.crusoe result = auth_client.authtoken_for(crusoe) client_token = models.AuthToken(auth_client=auth_client, user=crusoe, scope='id', validity=0) result = auth_client.authtoken_for(user=crusoe) self.assertEqual(client_token, result) self.assertIsInstance(result, models.AuthToken) assert result.user == crusoe # scenario 2: for a client that has confidential=False varys = models.User(username='******', fullname='Lord Varys') house_lannisters = models.AuthClient( title='House of Lannisters', confidential=False, user=varys, website='houseoflannisters.westeros', ) varys_session = models.UserSession( user=varys, ipaddr='192.168.1.99', user_agent= '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(), ) lannisters_auth_token = models.AuthToken( auth_client=house_lannisters, user=varys, scope='throne', validity=0, user_session=varys_session, ) db.session.add_all( [varys, house_lannisters, lannisters_auth_token, varys_session]) db.session.commit() result = house_lannisters.authtoken_for(varys, user_session=varys_session) self.assertIsInstance(result, models.AuthToken) assert "Lord Varys" == result.user.fullname
def test_authtoken_refresh(self): """Test to verify creation of new token while retaining the refresh token.""" hagrid = models.User(username='******', fullname='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_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_authtoken_user(self): """ Test for checking AuthToken's user property """ crusoe = self.fixtures.crusoe auth_client = self.fixtures.auth_client user_session = models.UserSession(buid=buid(), user=crusoe) auth_token_with_user_session = models.AuthToken( user=crusoe, user_session=user_session ) self.assertIsInstance( auth_token_with_user_session.user_session.user, models.User ) self.assertEqual(auth_token_with_user_session.user_session.user, crusoe) auth_token_without_user_session = models.AuthToken( auth_client=auth_client, user=crusoe ) self.assertIsInstance(auth_token_without_user_session._user, models.User) self.assertEqual(auth_token_without_user_session._user, crusoe)
def test_authtoken_is_valid(self): """ Test for verifying if AuthToken's token is valid """ auth_client = self.fixtures.auth_client # scenario 1: when validity is unlimited (0) tomriddle = models.User(username='******', fullname='Tom Riddle') scope = ['id', 'email'] tomriddle_token = models.AuthToken( auth_client=auth_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='******', fullname='Draco Malfoy') draco_token = models.AuthToken(auth_client=auth_client, user=draco, scope=scope) with self.assertRaises(TypeError): draco_token.is_valid() # scenario 3: when validity is limited harry = models.User(username='******', fullname='Harry Potter') harry_token = models.AuthToken( auth_client=auth_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='******', fullname='Cedric Diggory') cedric_token = models.AuthToken( auth_client=auth_client, user=cedric, scope=scope, validity=1, created_at=utcnow() - timedelta(1), ) self.assertFalse(cedric_token.is_valid())
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 = ['teams', 'wars', 'alliances'] sansa = models.User(username='******', fullname='Sansa Stark') auth_client = self.fixtures.auth_client sansa_token = models.AuthToken(auth_client=auth_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_authtoken_init(self): """ Test for verifying creation of AuthToken instance note: Only one authtoken per user and client """ auth_client = self.fixtures.auth_client crusoe = self.fixtures.crusoe result = models.AuthToken( auth_client=auth_client, user=crusoe, scope='id', validity=0 ) self.assertIsInstance(result, models.AuthToken) self.assertEqual(result.user, crusoe) self.assertEqual(result.auth_client, auth_client)
def test_authtoken_algorithm(self): """ Test for checking AuthToken's algorithm property """ snape = models.User(username='******', fullname='Professor Severus Snape') valid_algorithm = 'hmac-sha-1' auth_token = models.AuthToken(user=snape) auth_token.algorithm = None self.assertIsNone(auth_token._algorithm) auth_token.algorithm = valid_algorithm self.assertEqual(auth_token._algorithm, valid_algorithm) self.assertEqual(auth_token.algorithm, valid_algorithm) with self.assertRaises(ValueError): auth_token.algorithm = "hmac-sha-2016"
def test_scopemixin__scope(self): """ Test to retrieve scope on an ScopeMixin inherited class instance via _scope method """ scope = 'id' bellatrix = models.User(username='******', fullname='Bellatrix Lestrange') auth_client = self.fixtures.auth_client bellatrix_token = models.AuthToken(auth_client=auth_client, user=bellatrix, scope=scope, validity=0) db.session.add_all([bellatrix, bellatrix_token]) db.session.commit() self.assertEqual(bellatrix_token._scope, scope)
def test_scopemixin_add_scope(self): """ Test for adding scope to a ScopeMixin inherited class instance """ scope1 = 'spells' scope2 = 'charms' neville = models.User(username='******', fullname='Neville Longbottom') auth_client = self.fixtures.auth_client neville_token = models.AuthToken(auth_client=auth_client, user=neville, validity=0, scope=scope1) db.session.add_all([neville, neville_token]) neville_token.add_scope(scope2) self.assertEqual(neville_token.scope, (scope2, scope1))
def test_authtoken_get(self): """ Test for retreiving a AuthToken instance given a token """ specialdachs = self.fixtures.specialdachs oakley = self.fixtures.oakley scope = ['id'] dachsadv = models.AuthClient( title="Dachshund Adventures", organization=specialdachs, confidential=True, website="http://dachsadv.com", ) auth_token = models.AuthToken(auth_client=dachsadv, user=oakley, scope=scope) token = auth_token.token db.session.add(dachsadv, auth_token) result = models.AuthToken.get(token) self.assertIsInstance(result, models.AuthToken) self.assertEqual(result.auth_client, dachsadv)
def test_authtoken_all(self): """ Test for retreiving all AuthToken instances for given users """ auth_client = self.fixtures.auth_client # scenario 1: When users passed are an instance of Query class hermione = models.User(username='******', fullname='Hermione Granger') herminone_token = models.AuthToken( auth_client=auth_client, user=hermione, scope=['id'] ) myrtle = models.User(username='******', fullname='Moaning Myrtle') myrtle_token = models.AuthToken( auth_client=auth_client, user=myrtle, scope=['id'] ) alastor = models.User(username='******', fullname='Alastor Moody') alastor_token = models.AuthToken( auth_client=auth_client, user=alastor, scope=['id'] ) greyback = models.User(username='******', fullname='Fenrir Greyback') greyback_token = models.AuthToken( auth_client=auth_client, user=greyback, scope=['id'] ) pottermania = models.Organization( name='pottermania', title='Pottermania', owner=hermione ) db.session.add_all( [ myrtle, myrtle_token, hermione, herminone_token, alastor, alastor_token, greyback, greyback_token, pottermania, ] ) db.session.commit() # scenario 1 result1 = models.AuthToken.all(pottermania.owner_users) self.assertIsInstance(result1, list) self.assertIsInstance(result1[0], models.AuthToken) self.assertCountEqual(result1, [herminone_token]) # Scenario 2: When users passed are not an instance of Query class lily = models.User(username='******', fullname='Lily Evans Potter') cho = models.User(username='******', fullname='Cho Chang') lily_token = models.AuthToken( auth_client=auth_client, user=lily, scope=['memories'] ) cho_token = models.AuthToken( auth_client=auth_client, user=cho, scope=['charms'] ) db.session.add_all([lily, lily_token, cho, cho_token]) db.session.commit() # scenario 2 and count == 1 result3 = models.AuthToken.all([lily]) self.assertIsInstance(result3, list) self.assertIsInstance(result3[0], models.AuthToken) self.assertCountEqual(result3, [lily_token]) # scenario 2 and count > 1 result4 = models.AuthToken.all([lily, cho]) self.assertIsInstance(result4, list) for each in result4: self.assertIsInstance(each, models.AuthToken) self.assertCountEqual(result4, [lily_token, cho_token]) # scenario 5: When user instances passed don't have any AuthToken against them oakley = self.fixtures.oakley piglet = self.fixtures.piglet users = [piglet, oakley] result5 = models.AuthToken.all(users) self.assertListEqual(result5, [])