Example #1
0
async def test_handlers(app):
    """Test if all handlers are available on the Authenticator"""
    auth = NaasAuthenticator(db=app.db)
    handlers = auth.get_handlers(app)
    assert handlers[0][0] == "/login"
    assert handlers[1][0] == "/signup"
    assert handlers[2][0] == "/authorize"
    assert handlers[3][0] == "/authorize/([^/]*)"
    assert handlers[4][0] == "/delete/([^/]*)"
    assert handlers[5][0] == "/reset-password"
    assert handlers[6][0] == "/change-password"
    assert handlers[7][0] == "/change-password/([^/]+)"
Example #2
0
async def test_authentication(username, password, authorized, expected, tmpcwd,
                              app):
    """Test if authentication fails with a unexistent user"""
    auth = NaasAuthenticator(db=app.db)
    auth.create_user("johnsnow", "password")
    if authorized:
        UserInfo.change_authorization(app.db, "johnsnow")
    response = await auth.authenticate(app, {
        "username": username,
        "password": password
    })
    assert bool(response) == expected
Example #3
0
async def test_authentication_login_count(tmpcwd, app):
    auth = NaasAuthenticator(db=app.db)
    infos = {'username': '******', 'password': '******'}
    wrong_infos = {'username': '******', 'password': '******'}
    auth.get_or_create_user(infos['username'], infos['password'])
    UserInfo.change_authorization(app.db, 'johnsnow')

    assert not auth.login_attempts

    await auth.authenticate(app, wrong_infos)
    assert auth.login_attempts['johnsnow']['count'] == 1

    await auth.authenticate(app, wrong_infos)
    assert auth.login_attempts['johnsnow']['count'] == 2

    await auth.authenticate(app, infos)
    assert not auth.login_attempts.get('johnsnow')
Example #4
0
async def test_authentication_login_count(tmpcwd, app):
    auth = NaasAuthenticator(db=app.db)
    infos = {"username": "******", "password": "******"}
    wrong_infos = {"username": "******", "password": "******"}
    auth.create_user(infos["username"], infos["password"])
    UserInfo.change_authorization(app.db, "johnsnow")

    assert not auth.login_attempts

    await auth.authenticate(app, wrong_infos)
    assert auth.login_attempts["johnsnow"]["count"] == 1

    await auth.authenticate(app, wrong_infos)
    assert auth.login_attempts["johnsnow"]["count"] == 2

    await auth.authenticate(app, infos)
    assert not auth.login_attempts.get("johnsnow")
Example #5
0
async def test_list_users(tmpcwd, app):
    auth = NaasAuthenticator(db=app.db)
    auth.create_user("johnsnow", "password")
    auth.create_user("johnsnow2", "password2")

    res = auth.get_users()
    users = [item.as_dict() for item in res]
    assert len(users) == 2
    user = dict(users[0])
    assert type(user) == dict
Example #6
0
async def test_create_user(is_admin, expected_authorization, tmpcwd, app):
    """Test method create_user for new user and authorization"""
    auth = NaasAuthenticator(db=app.db)

    if is_admin:
        auth.admin_users = {"johnsnow"}

    auth.create_user("johnsnow", "password")
    if expected_authorization:
        UserInfo.change_authorization(app.db, "johnsnow")
    user_info = UserInfo.find(app.db, "johnsnow")
    assert user_info.username == "johnsnow"
    assert user_info.is_authorized == expected_authorization
    assert user_info.is_authorized == UserInfo.get_authorization(
        app.db, "johnsnow")

    UserInfo.change_authorization(app.db, "johnsnow")
    assert UserInfo.get_authorization(app.db,
                                      "johnsnow") != expected_authorization
    UserInfo.update_authorization(app.db, "johnsnow", expected_authorization)
    assert UserInfo.get_authorization(app.db,
                                      "johnsnow") == expected_authorization
Example #7
0
async def test_add_new_attempt_of_login(tmpcwd, app):
    auth = NaasAuthenticator(db=app.db)

    assert not auth.login_attempts
    auth.add_login_attempt('username')
    assert auth.login_attempts['username']['count'] == 1
    auth.add_login_attempt('username')
    assert auth.login_attempts['username']['count'] == 2
Example #8
0
async def test_add_new_attempt_of_login(tmpcwd, app):
    auth = NaasAuthenticator(db=app.db)

    assert not auth.login_attempts
    auth.add_login_attempt("username")
    assert auth.login_attempts["username"]["count"] == 1
    auth.add_login_attempt("username")
    assert auth.login_attempts["username"]["count"] == 2
Example #9
0
async def test_delete_user(tmpcwd, app):
    auth = NaasAuthenticator(db=app.db)
    auth.get_or_create_user('johnsnow', 'password')

    user = type('User', (), {'name': 'johnsnow'})
    auth.delete_user(user)

    user_info = UserInfo.find(app.db, 'johnsnow')
    assert not user_info
Example #10
0
async def test_delete_user(tmpcwd, app):
    auth = NaasAuthenticator(db=app.db)
    auth.create_user("johnsnow", "password")

    user = type("User", (), {"name": "johnsnow"})
    auth.delete_user(user)

    user_info = UserInfo.find(app.db, "johnsnow")
    assert not user_info
Example #11
0
async def test_authentication_with_exceed_atempts_of_login(tmpcwd, app):
    auth = NaasAuthenticator(db=app.db)
    auth.allowed_failed_logins = 3
    auth.secs_before_next_try = 10

    infos = {"username": "******", "password": "******"}
    auth.create_user(infos["username"], "password")
    UserInfo.change_authorization(app.db, "johnsnow")

    for i in range(3):
        response = await auth.authenticate(app, infos)
        assert not response

    infos["password"] = "******"
    response = await auth.authenticate(app, infos)
    assert not response
Example #12
0
async def test_authentication_with_exceed_atempts_of_login(tmpcwd, app):
    auth = NaasAuthenticator(db=app.db)
    auth.allowed_failed_logins = 3
    auth.secs_before_next_try = 10

    infos = {'username': '******', 'password': '******'}
    auth.get_or_create_user(infos['username'], 'password')
    UserInfo.change_authorization(app.db, 'johnsnow')

    for i in range(3):
        response = await auth.authenticate(app, infos)
        assert not response

    infos['password'] = '******'
    response = await auth.authenticate(app, infos)
    assert not response

    time.sleep(12)
    response = await auth.authenticate(app, infos)
    assert response
Example #13
0
async def test_create_user_bas_characters(tmpcwd, app):
    '''Test method get_or_create_user with bad characters on username'''
    auth = NaasAuthenticator(db=app.db)
    assert not auth.get_or_create_user('john snow', 'password')
    assert not auth.get_or_create_user('john,snow', 'password')
Example #14
0
async def test_create_user_bas_characters(tmpcwd, app):
    """Test method create_user with bad characters on username"""
    auth = NaasAuthenticator(db=app.db)
    assert not auth.create_user("john snow", "password")
    assert not auth.create_user("john,snow", "password")