Exemplo n.º 1
0
 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_authtoken_user(self):
        """
        Test for checking AuthToken's user property
        """
        crusoe = self.fixtures.crusoe
        client = self.fixtures.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(client=client, user=crusoe)
        self.assertIsInstance(auth_token_without_user_session._user, models.User)
        self.assertEqual(auth_token_without_user_session._user, crusoe)
Exemplo n.º 4
0
 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_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
Exemplo n.º 6
0
 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_AuthToken_init(self):
     """
     Test for verifying creation of AuthToken instance
     note: Only one authtoken per user and client
     """
     client = self.fixtures.client
     crusoe = self.fixtures.crusoe
     result = models.AuthToken(client=client, user=crusoe, scope=u'id', validity=0)
     self.assertIsInstance(result, models.AuthToken)
     self.assertEqual(result.user, crusoe)
     self.assertEqual(result.client, client)
Exemplo n.º 8
0
 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)))
Exemplo n.º 9
0
 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_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_AuthToken_get(self):
     """
     Test for retreiving a AuthToken instance given a token
     """
     specialdachs = self.fixtures.specialdachs
     oakley = self.fixtures.oakley
     scope = [u'id']
     dachsadv = models.Client(title=u"Dachshund Adventures", org=specialdachs, confidential=True, website=u"http://dachsadv.com")
     auth_token = models.AuthToken(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.client, dachsadv)
Exemplo n.º 12
0
 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)
Exemplo n.º 13
0
 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))
Exemplo n.º 14
0
    def test_AuthToken_all(self):
        """
        Test for retreiving all AuthToken instances for given users
        """
        client = self.fixtures.client

        # scenario 1: When users passed are an instance of Query class
        hermione = models.User(username=u'herminone',
                               fullname=u'Hermione Granger')
        herminone_token = models.AuthToken(client=client,
                                           user=hermione,
                                           scope=[u'id'])
        myrtle = models.User(username=u'myrtle', fullname=u'Moaning Myrtle')
        myrtle_token = models.AuthToken(client=client,
                                        user=myrtle,
                                        scope=[u'id'])
        alastor = models.User(username=u'alastor', fullname=u'Alastor Moody')
        alastor_token = models.AuthToken(client=client,
                                         user=alastor,
                                         scope=[u'id'])
        greyback = models.User(username=u'greyback',
                               fullname=u'Fenrir Greyback')
        greyback_token = models.AuthToken(client=client,
                                          user=greyback,
                                          scope=[u'id'])
        pottermania = models.Organization(name=u'Batdog', title=u'Batdog')
        pottermania.owners.users.append(hermione)
        pottermania_members = [hermione, alastor, greyback, myrtle]
        for member in pottermania_members:
            pottermania.members.users.append(member)
        db.session.add_all([
            myrtle, myrtle_token, hermione, herminone_token, alastor,
            alastor_token, greyback, greyback_token, pottermania
        ])
        db.session.commit()

        # scenario 1 and count == 1
        result1 = models.AuthToken.all(pottermania.owners.users)
        self.assertIsInstance(result1, list)
        self.assertIsInstance(result1[0], models.AuthToken)
        self.assertItemsEqual(result1, [herminone_token])

        # scenario 1 and count > 1
        result2 = models.AuthToken.all(pottermania.members.users)
        self.assertIsInstance(result2, list)
        for each in result2:
            self.assertIsInstance(each, models.AuthToken)
        self.assertItemsEqual(
            result2,
            [herminone_token, alastor_token, greyback_token, myrtle_token])

        # Scenario 2: When users passed are not an instance of Query class
        lily = models.User(username=u'lily', fullname=u'Lily Evans Potter')
        cho = models.User(username=u'cho', fullname=u'Cho Chang')
        lily_token = models.AuthToken(client=client,
                                      user=lily,
                                      scope=[u'memories'])
        cho_token = models.AuthToken(client=client,
                                     user=cho,
                                     scope=[u'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.assertItemsEqual(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.assertItemsEqual(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, [])