def test_given_valid_auth_header_when_calling_login_then_token_is_returned():
    # given
    mock_user_collection = Mock()
    mock_whitelisted_collection = Mock()
    mock_user_collection.find_one.return_value = {
        # hashed version of 'test' with salt
        'password':
        "******",
        'name': 'dummy_name',
        '_id': 'some_id'
    }

    auth_service = AuthService(mock_user_collection,
                               mock_whitelisted_collection, mock_private_key,
                               jwt)

    # when
    token = auth_service.login({"username": '******', 'password': '******'})

    # then
    data = jwt.decode(token, mock_public_key, algorithm='RS256')
    assert 'some_id' == data['public_id']
    assert data['exp'] is not None

    # hashed version, no salt for 'test'
    print data['key'] == "xzwXZ2CoOI8Z/2QH"
    mock_user_collection.find_one.assert_called_once_with({'name': 'test'})
def test_given_not_registered_user__when_calling_login_then_UnrecognisedUserException_is_returned(
):
    # given
    mock_user_collection = Mock()
    mock_whitelisted_collection = Mock()
    mock_user_collection.find_one.return_value = False
    auth_service = AuthService(mock_user_collection,
                               mock_whitelisted_collection, mock_private_key,
                               jwt)

    # then
    with raises(UnrecognisedUserException):
        auth_service.login({'username': '******', "password": '******'})
def test_given_whitelisted_and_not_registerd_before_user_when_calling_register_then_user_is_registerd(
):
    # given
    mock_user_collection = Mock()
    mock_user_collection.find_one.return_value = False

    mock_whitelisted_collection = Mock()
    mock_whitelisted_collection.find_one.return_value = True

    auth_service = AuthService(mock_user_collection,
                               mock_whitelisted_collection, mock_private_key,
                               jwt)

    # when
    status = auth_service.register("test", "password")

    # then
    assert status is True
def test_given_invalid_header_when_calling_login_then_MissingAuthHeaderException_is_returned(
):
    # given
    mock_user_collection = Mock()
    mock_whitelisted_collection = Mock()
    auth_service = AuthService(mock_user_collection,
                               mock_whitelisted_collection, mock_private_key,
                               jwt)

    # then
    with raises(MissingAuthHeaderException):
        auth_service.login({"password": '******'})

    with raises(MissingAuthHeaderException):
        auth_service.login({"username": '******'})

    with raises(MissingAuthHeaderException):
        auth_service.login(None)
def test_given_invalid_password_when_calling_login_then_WrongPasswordException_is_returned(
):
    # given
    mock_user_collection = Mock()
    mock_whitelisted_collection = Mock()
    mock_user_collection.find_one.return_value = {
        # hashed version of 'test' with salt
        'password':
        "******",
        '_id': 'some_id'
    }
    auth_service = AuthService(mock_user_collection,
                               mock_whitelisted_collection, mock_private_key,
                               jwt)

    # then
    with raises(WrongPasswordException):
        auth_service.login({'username': '******', "password": '******'})
示例#6
0
import cherrypy
from auth_service import AuthService
from user_service import UserService
from subaccount_service import SubaccountService
from category_service import CategoryService
from currency_service import CurrencyService
from transaction_service import TransactionService
from csv_service import CSVService
from config import WITH_AUTHENTICATION, WITHOUT_AUTHENTICATION, CHERRYPY_CONFIG_DEFAULT


if __name__ == '__main__':
    cherrypy.config.update(CHERRYPY_CONFIG_DEFAULT)

    cherrypy.tree.mount(AuthService(), '/api/auth', WITHOUT_AUTHENTICATION)
    cherrypy.tree.mount(UserService(), '/api/user', WITH_AUTHENTICATION)
    cherrypy.tree.mount(SubaccountService(), '/api/subaccounts', WITH_AUTHENTICATION)
    cherrypy.tree.mount(CategoryService(), '/api/categories', WITH_AUTHENTICATION)
    cherrypy.tree.mount(TransactionService(), '/api/transactions', WITH_AUTHENTICATION)
    cherrypy.tree.mount(CurrencyService(), '/api/currency', WITH_AUTHENTICATION)
    cherrypy.tree.mount(CSVService(), '/api/csv', WITH_AUTHENTICATION)

    cherrypy.engine.start()
    cherrypy.engine.block()