Ejemplo n.º 1
0
    def test_root(self):

        principal = JwtPrincipal(dict(
            email='*****@*****.**',
            name='mohammad',
            family='sheikhian',
            id=1,
            sessionId=1,
            roles=['admin']
        ))
        self.login_token = principal.dump().decode("utf-8")

        call = dict(
            title='GET',
            description='',
            url='/',
            verb='GET',
            autodoc=f'{document_directory}/root_get_with_authorization.md'
        )
        with self.given(**call):
            then(response.status_code == 200)

        self.login_token = None
        call = dict(
            title='GET',
            description='',
            url='/',
            verb='GET',
            autodoc=f'{document_directory}/root_get_without_authorization.md'
        )
        with self.given(**call):
            then(response.status_code == 200)

        call = dict(
            title='POST',
            description='',
            url='/',
            verb='POST',
            form={
                'url': 'www.varzesh3.com'
            },
            autodoc=f'{document_directory}/root_post.md'
        )
        with self.given(**call):
            then(response.status_code == 200)

        call = dict(
            title='POST',
            description='',
            url='/',
            verb='POST',
            form={}
        )
        with self.given(**call):
            then(response.status_code == 400)
Ejemplo n.º 2
0
def test_principal():
    __configuration__ = '''
    jwt:
      secret: JWT-SECRET
      algorithm: HS256
      max_age: 86400  # 24 Hours
      refresh_token:
        secret: JWT-REFRESH-SECRET
        algorithm: HS256
        max_age: 2678400  # 30 Days
    '''
    configure(force=True)
    settings.merge(__configuration__)

    principal = JwtPrincipal(
        dict(email='*****@*****.**', id=1, sessionId=1, roles=['admin']))

    assert principal.email == '*****@*****.**'
    assert principal.id == 1
    assert principal.session_id == 1
    assert principal.roles == ['admin']
    assert principal.is_in_roles('admin') is True
    assert principal.is_in_roles('admin', 'god') is True

    encoded = principal.dump()

    principal = JwtPrincipal.load(encoded.decode())
    assert principal.email == '*****@*****.**'
    assert principal.id == 1
    assert principal.session_id == 1
    assert principal.roles == ['admin']
    assert principal.is_in_roles('admin') is True
    assert principal.is_in_roles('admin', 'god') is True

    principal = JwtPrincipal.load(encoded.decode(), force=True)
    assert principal.email == '*****@*****.**'
    assert principal.id == 1
    assert principal.session_id == 1
    assert principal.roles == ['admin']
    assert principal.is_in_roles('admin') is True
    assert principal.is_in_roles('admin', 'god') is True

    principal =\
        JwtPrincipal.load((b'Bearer %s' % encoded).decode(), force=True)
    assert principal.email == '*****@*****.**'
    assert principal.id == 1
    assert principal.session_id == 1
    assert principal.roles == ['admin']
    assert principal.is_in_roles('admin') is True
    assert principal.is_in_roles('admin', 'god') is True
Ejemplo n.º 3
0
    def test_principal(self):
        principal = JwtPrincipal(
            dict(email='*****@*****.**', id=1, sessionId=1, roles=['admin']))

        self.assertEqual(principal.email, '*****@*****.**')
        self.assertEqual(principal.id, 1)
        self.assertEqual(principal.session_id, 1)
        self.assertEqual(principal.roles, ['admin'])
        self.assertTrue(principal.is_in_roles('admin'))
        self.assertTrue(principal.is_in_roles('admin', 'god'))

        encoded = principal.dump()

        principal = JwtPrincipal.load(encoded.decode())
        self.assertEqual(principal.email, '*****@*****.**')
        self.assertEqual(principal.id, 1)
        self.assertEqual(principal.session_id, 1)
        self.assertEqual(principal.roles, ['admin'])
        self.assertTrue(principal.is_in_roles('admin'))
        self.assertTrue(principal.is_in_roles('admin', 'god'))

        principal = JwtPrincipal.load(encoded.decode(), force=True)
        self.assertEqual(principal.email, '*****@*****.**')
        self.assertEqual(principal.id, 1)
        self.assertEqual(principal.session_id, 1)
        self.assertEqual(principal.roles, ['admin'])
        self.assertTrue(principal.is_in_roles('admin'))
        self.assertTrue(principal.is_in_roles('admin', 'god'))

        principal = JwtPrincipal.load((b'Bearer %s' % encoded).decode(),
                                      force=True)
        self.assertEqual(principal.email, '*****@*****.**')
        self.assertEqual(principal.id, 1)
        self.assertEqual(principal.session_id, 1)
        self.assertEqual(principal.roles, ['admin'])
        self.assertTrue(principal.is_in_roles('admin'))
        self.assertTrue(principal.is_in_roles('admin', 'god'))