/* * Copyright 2016 ThoughtWorks, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package com.thoughtworks.go.config.update; import com.thoughtworks.go.config.BasicCruiseConfig; import com.thoughtworks.go.config.CaseInsensitiveString; import com.thoughtworks.go.domain.config.*; import com.thoughtworks.go.domain.scm.SCM; import com.thoughtworks.go.helper.GoConfigMother; import com.thoughtworks.go.server.domain.Username; import com.thoughtworks.go.server.service.GoConfigService; import com.thoughtworks.go.server.service.materials.PluggableScmService; import com.thoughtworks.go.server.service.result.LocalizedOperationResult; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; import static org.junit.Assert.*; import static org.mockito.Mockito.when; import static org.mockito.MockitoAnnotations.initMocks; import static org.hamcrest.core.Is.is; public class CreateSCMConfigCommandTest { private Username currentUser; private BasicCruiseConfig cruiseConfig; private SCM scm; @Mock private PluggableScmService pluggableScmService; @Mock private GoConfigService goConfigService; @Mock private LocalizedOperationResult result; @Before public void setup() throws Exception { initMocks(this); currentUser = new Username(new CaseInsensitiveString("user")); cruiseConfig = new GoConfigMother().defaultCruiseConfig(); scm = new SCM("id", new PluginConfiguration("non-existent-plugin-id", "1"), new Configuration(new ConfigurationProperty(new ConfigurationKey("key"),new ConfigurationValue("value")))); } @Test public void shouldAddANewScmToTheGivenConfig() throws Exception{ CreateSCMConfigCommand command = new CreateSCMConfigCommand(scm, pluggableScmService, result, currentUser, goConfigService); assertThat(cruiseConfig.getSCMs().contains(scm), is(false)); command.update(cruiseConfig); assertThat(cruiseConfig.getSCMs().contains(scm), is(true)); } @Test public void shouldNotContinueWithConfigSaveIfUserIsUnauthorized() { when(goConfigService.isUserAdmin(currentUser)).thenReturn(false); when(goConfigService.isGroupAdministrator(currentUser.getUsername())).thenReturn(false); CreateSCMConfigCommand command = new CreateSCMConfigCommand(scm, pluggableScmService, result, currentUser, goConfigService); assertThat(command.canContinue(cruiseConfig), is(false)); } @Test public void shouldContinueWithConfigSaveIfUserIsAdmin() { when(goConfigService.isUserAdmin(currentUser)).thenReturn(true); when(goConfigService.isGroupAdministrator(currentUser.getUsername())).thenReturn(false); CreateSCMConfigCommand command = new CreateSCMConfigCommand(scm, pluggableScmService, result, currentUser, goConfigService); assertThat(command.canContinue(cruiseConfig), is(true)); } @Test public void shouldContinueWithConfigSaveIfUserIsGroupAdmin() { when(goConfigService.isUserAdmin(currentUser)).thenReturn(false); when(goConfigService.isGroupAdministrator(currentUser.getUsername())).thenReturn(true); CreateSCMConfigCommand command = new CreateSCMConfigCommand(scm, pluggableScmService, result, currentUser, goConfigService); assertThat(command.canContinue(cruiseConfig), is(true)); } }