def test_invalid_name(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("service@foo@bar", "some-group", "", "") assert mock_ui.mock_calls == [ call.create_service_account_failed_invalid_name( "service@foo@bar", "some-group", "service@foo@bar is not a valid service account name (does not match {})" .format(SERVICE_ACCOUNT_VALIDATION), ) ] # Test a service account name that's one character longer than MAX_NAME_LENGTH minus the length # of the default email domain minus 1 (for the @). mock_ui.reset_mock() long_name = "x" * (MAX_NAME_LENGTH - len(setup.settings.service_account_email_domain)) long_name += "@" + setup.settings.service_account_email_domain usecase.create_service_account(long_name, "some-group", "", "") assert mock_ui.mock_calls == [ call.create_service_account_failed_invalid_name( long_name, "some-group", "{} is longer than {} characters".format(long_name, MAX_NAME_LENGTH), ) ] mock_ui.reset_mock() usecase.create_service_account("*****@*****.**", "some-group", "", "") assert mock_ui.mock_calls == [ call.create_service_account_failed_invalid_name( "*****@*****.**", "some-group", "All service accounts must end in @{}".format( setup.settings.service_account_email_domain), ) ]
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") ]