Exemple #1
0
def test_success(setup):
    # type: (SetupTest) -> None
    with setup.transaction():
        setup.add_user_to_group("*****@*****.**", "some-group")

    mock_ui = MagicMock()
    usecase = setup.usecase_factory.create_create_service_account_usecase("*****@*****.**", mock_ui)
    usecase.create_service_account(
        "*****@*****.**", "some-group", "machine-set", "description"
    )
    assert mock_ui.mock_calls == [
        call.created_service_account("*****@*****.**", "some-group")
    ]

    # Check the User and ServiceAccount that were created.
    user = User.get(setup.session, name="*****@*****.**")
    assert user is not None
    assert user.is_service_account
    assert user.enabled
    service = ServiceAccount.get(setup.session, name="*****@*****.**")
    assert service is not None
    assert service.machine_set == "machine-set"
    assert service.description == "description"

    # Check that the ServiceAccount is owned by the correct Group.
    group = Group.get(setup.session, name="some-group")
    assert group is not None
    linkage = GroupServiceAccount.get(setup.session, service_account_id=service.id)
    assert linkage is not None
    assert linkage.group_id == group.id
Exemple #2
0
def test_user_created_plugin_invocation(setup):
    # type: (SetupTest) -> None
    with setup.transaction():
        setup.add_user_to_group("*****@*****.**", "some-group")

    setup.plugins.add_plugin(ServiceAccountCreatedPlugin())

    ui = MagicMock()
    usecase = setup.usecase_factory.create_create_service_account_usecase(
        "*****@*****.**", ui)
    usecase.create_service_account("service", "some-group", "", "")
    assert ui.mock_calls == [
        call.created_service_account("*****@*****.**", "some-group")
    ]
Exemple #3
0
def test_admin_can_create(setup):
    # type: (SetupTest) -> None
    with setup.transaction():
        setup.create_group("some-group")
        setup.add_user_to_group("*****@*****.**", "admins")
        setup.grant_permission_to_group(USER_ADMIN, "", "admins")

    mock_ui = MagicMock()
    usecase = setup.usecase_factory.create_create_service_account_usecase("*****@*****.**", mock_ui)
    usecase.create_service_account("*****@*****.**", "some-group", "", "")
    assert mock_ui.mock_calls == [
        call.created_service_account("*****@*****.**", "some-group")
    ]

    service = ServiceAccount.get(setup.session, name="*****@*****.**")
    assert service is not None
    assert service.machine_set == ""
    assert service.description == ""
Exemple #4
0
def test_name_rejected_by_plugin(setup):
    # type: (SetupTest) -> None
    with setup.transaction():
        setup.add_user_to_group("*****@*****.**", "some-group")

    setup.plugins.add_plugin(ServiceAccountNameTestPlugin())

    mock_ui = MagicMock()
    usecase = setup.usecase_factory.create_create_service_account_usecase(
        "*****@*****.**", mock_ui)
    usecase.create_service_account("ser_vice", "some-group", "", "")
    assert mock_ui.mock_calls == [
        call.create_service_account_failed_invalid_name(
            "*****@*****.**", "some-group", "*****@*****.**")
    ]

    mock_ui.reset_mock()
    usecase.create_service_account("service", "some-group", "", "")
    assert mock_ui.mock_calls == [
        call.created_service_account("*****@*****.**", "some-group")
    ]