コード例 #1
0
def test_authenticate():
    users = Mock()
    users.get_by_name.return_value = User(id=0, password=Password("pwd"))
    users.get.return_value = User(id=0, name="linus")

    ldap = Mock()
    ldap.login.return_value = None
    ldap.get.return_value = None

    roles = Mock()
    roles.get_all_by_user.return_value = [
        Role(type=RoleType.READER, group=Group(id="group", name="group"))
    ]

    exp = JWTUser(
        id=0,
        impersonator=0,
        type="users",
        groups=[JWTGroup(id="group", name="group", role=RoleType.READER)],
    )

    service = LoginService(
        user_repo=users,
        bot_repo=Mock(),
        group_repo=Mock(),
        role_repo=roles,
        ldap=ldap,
        event_bus=Mock(),
    )
    assert exp == service.authenticate("dupond", "pwd")

    users.get_by_name.assert_called_once_with("dupond")
    roles.get_all_by_user.assert_called_once_with(0)
コード例 #2
0
def test_authentication_ldap_user():
    users = Mock()
    users.get_by_name.return_value = None

    roles = Mock()
    roles.get_all_by_user.return_value = []

    ldap = Mock()
    user = UserLdap(id=10, name="ExtUser")
    ldap.login.return_value = user
    ldap.get.return_value = user

    exp = JWTUser(
        id=10,
        impersonator=10,
        type="users_ldap",
    )

    service = LoginService(
        user_repo=users,
        bot_repo=Mock(),
        group_repo=Mock(),
        role_repo=roles,
        ldap=ldap,
        event_bus=Mock(),
    )
    assert exp == service.authenticate("dupond", "pwd")
    ldap.get.assert_called_once_with(10)
コード例 #3
0
def test_authentication_wrong_user():
    users = Mock()
    users.get_by_name.return_value = None

    ldap = Mock()
    ldap.login.return_value = None
    ldap.get_by_name.return_value = None

    service = LoginService(
        user_repo=users,
        bot_repo=Mock(),
        group_repo=Mock(),
        role_repo=Mock(),
        ldap=ldap,
        event_bus=Mock(),
    )
    assert not service.authenticate("dupond", "pwd")
    users.get_by_name.assert_called_once_with("dupond")