2018-01-05 21:27:03 +00:00
|
|
|
from auth.credentials import validate_credentials, CredentialKind
|
|
|
|
from auth.credential_consts import (ACCESS_TOKEN_USERNAME, OAUTH_TOKEN_USERNAME,
|
|
|
|
APP_SPECIFIC_TOKEN_USERNAME)
|
2017-10-27 18:05:53 +00:00
|
|
|
from auth.validateresult import AuthKind, ValidateResult
|
|
|
|
from data import model
|
|
|
|
|
|
|
|
from test.fixtures import *
|
|
|
|
|
|
|
|
def test_valid_user(app):
|
2017-10-27 18:11:19 +00:00
|
|
|
result, kind = validate_credentials('devtable', 'password')
|
|
|
|
assert kind == CredentialKind.user
|
2017-10-27 18:05:53 +00:00
|
|
|
assert result == ValidateResult(AuthKind.credentials, user=model.user.get_user('devtable'))
|
|
|
|
|
|
|
|
def test_valid_robot(app):
|
|
|
|
robot, password = model.user.create_robot('somerobot', model.user.get_user('devtable'))
|
2017-10-27 18:11:19 +00:00
|
|
|
result, kind = validate_credentials(robot.username, password)
|
|
|
|
assert kind == CredentialKind.robot
|
2017-10-27 18:05:53 +00:00
|
|
|
assert result == ValidateResult(AuthKind.credentials, robot=robot)
|
|
|
|
|
2017-12-08 22:05:59 +00:00
|
|
|
def test_valid_robot_for_disabled_user(app):
|
2017-12-12 22:51:54 +00:00
|
|
|
user = model.user.get_user('devtable')
|
2017-12-08 22:05:59 +00:00
|
|
|
user.enabled = False
|
|
|
|
user.save()
|
|
|
|
|
|
|
|
robot, password = model.user.create_robot('somerobot', user)
|
|
|
|
result, kind = validate_credentials(robot.username, password)
|
|
|
|
assert kind == CredentialKind.robot
|
|
|
|
|
|
|
|
err = 'This user has been disabled. Please contact your administrator.'
|
|
|
|
assert result == ValidateResult(AuthKind.credentials, error_message=err)
|
|
|
|
|
2017-10-27 18:05:53 +00:00
|
|
|
def test_valid_token(app):
|
|
|
|
access_token = model.token.create_delegate_token('devtable', 'simple', 'sometoken')
|
2017-10-27 18:11:19 +00:00
|
|
|
result, kind = validate_credentials(ACCESS_TOKEN_USERNAME, access_token.code)
|
|
|
|
assert kind == CredentialKind.token
|
2017-10-27 18:05:53 +00:00
|
|
|
assert result == ValidateResult(AuthKind.credentials, token=access_token)
|
|
|
|
|
|
|
|
def test_valid_oauth(app):
|
|
|
|
user = model.user.get_user('devtable')
|
|
|
|
oauth_token = list(model.oauth.list_access_tokens_for_user(user))[0]
|
2017-10-27 18:11:19 +00:00
|
|
|
result, kind = validate_credentials(OAUTH_TOKEN_USERNAME, oauth_token.access_token)
|
|
|
|
assert kind == CredentialKind.oauth_token
|
2017-10-27 18:05:53 +00:00
|
|
|
assert result == ValidateResult(AuthKind.oauth, oauthtoken=oauth_token)
|
|
|
|
|
|
|
|
def test_invalid_user(app):
|
2017-10-27 18:11:19 +00:00
|
|
|
result, kind = validate_credentials('devtable', 'somepassword')
|
|
|
|
assert kind == CredentialKind.user
|
2017-10-27 18:05:53 +00:00
|
|
|
assert result == ValidateResult(AuthKind.credentials,
|
|
|
|
error_message='Invalid Username or Password')
|
2017-12-08 22:05:59 +00:00
|
|
|
|
|
|
|
def test_valid_app_specific_token(app):
|
|
|
|
user = model.user.get_user('devtable')
|
|
|
|
app_specific_token = model.appspecifictoken.create_token(user, 'some token')
|
2017-12-12 22:51:54 +00:00
|
|
|
|
2017-12-08 22:05:59 +00:00
|
|
|
result, kind = validate_credentials(APP_SPECIFIC_TOKEN_USERNAME, app_specific_token.token_code)
|
|
|
|
assert kind == CredentialKind.app_specific_token
|
|
|
|
assert result == ValidateResult(AuthKind.credentials, appspecifictoken=app_specific_token)
|
|
|
|
|
|
|
|
def test_valid_app_specific_token_for_disabled_user(app):
|
|
|
|
user = model.user.get_user('devtable')
|
|
|
|
user.enabled = False
|
|
|
|
user.save()
|
|
|
|
|
|
|
|
app_specific_token = model.appspecifictoken.create_token(user, 'some token')
|
|
|
|
|
|
|
|
result, kind = validate_credentials(APP_SPECIFIC_TOKEN_USERNAME, app_specific_token.token_code)
|
|
|
|
assert kind == CredentialKind.app_specific_token
|
|
|
|
|
|
|
|
err = 'This user has been disabled. Please contact your administrator.'
|
|
|
|
assert result == ValidateResult(AuthKind.credentials, error_message=err)
|
|
|
|
|
|
|
|
def test_invalid_app_specific_token(app):
|
|
|
|
result, kind = validate_credentials(APP_SPECIFIC_TOKEN_USERNAME, 'somecode')
|
|
|
|
assert kind == CredentialKind.app_specific_token
|
|
|
|
assert result == ValidateResult(AuthKind.credentials, error_message='Invalid token')
|