Beispiel #1
0
def test_update_user_role_request_invalid_data():
    request = UpdateUserRoleRequest(id=1, name=[])

    assert request.is_valid() is False
    message = 'expected string, got list([])'
    assert any([
        e.parameter == 'name' and e.message == message for e in request.errors
    ])
    assert len(request.errors) == 1
Beispiel #2
0
def test_update_user_role_request_without_id():
    request = UpdateUserRoleRequest(id=None)

    assert request.is_valid() is False
    assert any([
        e.parameter == 'id' and e.message == 'is required'
        for e in request.errors
    ])
    assert len(request.errors) == 1
Beispiel #3
0
def update_user_role(id):
    data = http_request.get_json()
    request = UpdateUserRoleRequest(id, data['name'])
    action = UpdateUserRoleAction(user_roles_repository)
    action.add_before_execution_hook(authorization_manager.authorize)
    response = action.execute(request)
    status = get_status(response)

    return json_response(response.value, UserRoleEncoder, status)
Beispiel #4
0
def test_update_user_role():
    user_role_id = 1
    repo = mock.Mock()
    request = UpdateUserRoleRequest(id=user_role_id, name='admin')
    action = UpdateUserRoleAction(repo=repo)
    response = action.execute(request)

    assert bool(response) is True
    assert repo.update.called
    assert response.value == repo.update.return_value
Beispiel #5
0
def test_update_user_role_handles_bad_request():
    repo = mock.Mock()
    request = UpdateUserRoleRequest(id=None, name=[])
    action = UpdateUserRoleAction(repo=repo)
    response = action.execute(request)

    assert bool(response) is False
    assert not repo.update.called
    assert response.value == {
        'type': ResponseFailure.PARAMETER_ERROR,
        'message': 'id: is required\nname: expected string, got list([])'
    }
Beispiel #6
0
def test_update_user_handle_generic_error():
    repo = mock.Mock()
    error_message = 'Just an error message'
    repo.update.side_effect = Exception(error_message)

    action = UpdateUserRoleAction(repo=repo)
    request = UpdateUserRoleRequest(id=2)
    response = action.execute(request)

    assert bool(response) is False
    assert repo.update.called
    assert response.value == {
        'type': ResponseFailure.SYSTEM_ERROR,
        'message': 'Exception: {}'.format(error_message)
    }
Beispiel #7
0
def test_update_user_role_with_hooks():
    user_role_id = 1
    repo = mock.Mock()
    request = UpdateUserRoleRequest(id=user_role_id, name='admin')
    action = UpdateUserRoleAction(repo=repo)

    before = mock.MagicMock()
    after = mock.MagicMock()

    action.add_before_execution_hook(before)
    action.add_after_execution_hook(after)

    response = action.execute(request)

    assert before.called
    assert after.called

    assert bool(response) is True
    assert repo.update.called
    assert response.value == repo.update.return_value
Beispiel #8
0
def test_update_user_role_request():
    request = UpdateUserRoleRequest(id=1, name='admin')

    assert request.id == 1
    assert request.name == 'admin'
    assert request.is_valid() is True
Beispiel #9
0
def test_update_user_role_request_without_data():
    with pytest.raises(TypeError):
        UpdateUserRoleRequest()