Пример #1
0
def test_extra_claims():
    policy = JWTAuthenticationPolicy('secret')
    token = policy.create_token(15, name=u'Jöhn', admin=True)
    request = Request.blank('/')
    request.authorization = ('JWT', token)
    jwt_claims = policy.get_claims(request)
    assert jwt_claims['name'] == u'Jöhn'
    assert jwt_claims['admin']
Пример #2
0
def test_expired_token():
    policy = JWTAuthenticationPolicy('secret', expiration=-1)
    request = Request.blank('/')
    request.authorization = ('JWT', policy.create_token(15))
    request.jwt_claims = policy.get_claims(request)
    assert policy.unauthenticated_userid(request) is None
    policy.leeway = 5
    request.jwt_claims = policy.get_claims(request)
    assert policy.unauthenticated_userid(request) == 15
Пример #3
0
def test_algorithm_unsupported():
    policy = JWTAuthenticationPolicy("secret", algorithm="SHA1")
    with pytest.raises(NotImplementedError):
        token = policy.create_token(15, name="Jöhn", admin=True)
Пример #4
0
def test_minimal_roundtrip():
    policy = JWTAuthenticationPolicy("secret")
    request = Request.blank("/")
    request.authorization = ("JWT", policy.create_token(15))
    request.jwt_claims = policy.get_claims(request)
    assert policy.unauthenticated_userid(request) == 15
Пример #5
0
def test_token_most_be_str():
    policy = JWTAuthenticationPolicy("secret")
    token = policy.create_token(15)
    assert isinstance(token, str)
Пример #6
0
def test_interface():
    verifyObject(IAuthenticationPolicy, JWTAuthenticationPolicy("secret"))
Пример #7
0
def test_default_json_encoder():
    policy = JWTAuthenticationPolicy("secret")
    assert isinstance(policy.json_encoder, PyramidJSONEncoderFactory)
    assert isinstance(policy.json_encoder(), JSONEncoder)
Пример #8
0
def test_dynamic_expired_token():
    policy = JWTAuthenticationPolicy("secret", expiration=-1)
    request = Request.blank("/")
    request.authorization = ("JWT", policy.create_token(15, expiration=5))
    request.jwt_claims = policy.get_claims(request)
    assert policy.unauthenticated_userid(request) == 15

    policy = JWTAuthenticationPolicy("secret")
    request.authorization = ("JWT", policy.create_token(15, expiration=-1))
    request.jwt_claims = policy.get_claims(request)
    assert policy.unauthenticated_userid(request) is None
    request.authorization = ("JWT", policy.create_token(15))
    request.jwt_claims = policy.get_claims(request)
    assert policy.unauthenticated_userid(request) == 15
Пример #9
0
def test_invalid_authorization_header():
    policy = JWTAuthenticationPolicy("secret")
    request = Request.blank("/")
    request.environ["HTTP_AUTHORIZATION"] = "token"
    request.jwt_claims = policy.get_claims(request)
    assert policy.unauthenticated_userid(request) is None
Пример #10
0
def test_wrong_auth_scheme():
    policy = JWTAuthenticationPolicy("secret")
    request = Request.blank("/")
    request.authorization = ("Other", policy.create_token(15))
    request.jwt_claims = policy.get_claims(request)
    assert policy.unauthenticated_userid(request) is None
Пример #11
0
def test_other_header():
    policy = JWTAuthenticationPolicy("secret", http_header="X-Token")
    request = Request.blank("/")
    request.headers["X-Token"] = policy.create_token(15)
    request.jwt_claims = policy.get_claims(request)
    assert policy.unauthenticated_userid(request) == 15
Пример #12
0
def test_other_header():
    policy = JWTAuthenticationPolicy('secret', http_header='X-Token')
    request = Request.blank('/')
    request.headers['X-Token'] = policy.create_token(15)
    request.jwt_claims = policy.get_claims(request)
    assert policy.unauthenticated_userid(request) == 15
Пример #13
0
def test_invalid_authorization_header():
    policy = JWTAuthenticationPolicy('secret')
    request = Request.blank('/')
    request.environ['HTTP_AUTHORIZATION'] = 'token'
    request.jwt_claims = policy.get_claims(request)
    assert policy.unauthenticated_userid(request) is None
Пример #14
0
def test_wrong_auth_scheme():
    policy = JWTAuthenticationPolicy('secret')
    request = Request.blank('/')
    request.authorization = ('Other', policy.create_token(15))
    request.jwt_claims = policy.get_claims(request)
    assert policy.unauthenticated_userid(request) is None
Пример #15
0
def test_minimal_roundtrip():
    policy = JWTAuthenticationPolicy('secret')
    request = Request.blank('/')
    request.authorization = ('JWT', policy.create_token(15))
    request.jwt_claims = policy.get_claims(request)
    assert policy.unauthenticated_userid(request) == 15
Пример #16
0
def test_token_most_be_str():
    policy = JWTAuthenticationPolicy('secret')
    token = policy.create_token(15)
    assert isinstance(token, str)