@Test
  public void shouldNotContinueWithConfigSaveIfObjectNotFound() {
    when(goConfigService.isUserAdmin(currentUser)).thenReturn(true);
    when(goConfigService.isGroupAdministrator(currentUser.getUsername())).thenReturn(false);

    SCM updatedScm =
        new SCM(
            "non-existent-id",
            new PluginConfiguration("plugin-id", "1"),
            new Configuration(
                new ConfigurationProperty(
                    new ConfigurationKey("key1"), new ConfigurationValue("value1"))));
    UpdateSCMConfigCommand command =
        new UpdateSCMConfigCommand(
            updatedScm,
            pluggableScmService,
            goConfigService,
            currentUser,
            result,
            "md5",
            entityHashingService);

    thrown.expect(NullPointerException.class);
    thrown.expectMessage("The pluggable scm material with id 'non-existent-id' is not found.");
    assertThat(command.canContinue(cruiseConfig), is(false));
  }
  @Test
  public void shouldNotContinueWithConfigSaveIfRequestIsNotFresh() {
    when(goConfigService.isUserAdmin(currentUser)).thenReturn(true);
    when(goConfigService.isGroupAdministrator(currentUser.getUsername())).thenReturn(false);

    SCM updatedScm =
        new SCM(
            "id",
            new PluginConfiguration("plugin-id", "1"),
            new Configuration(
                new ConfigurationProperty(
                    new ConfigurationKey("key1"), new ConfigurationValue("value1"))));
    updatedScm.setName("material");
    when(entityHashingService.md5ForEntity(cruiseConfig.getSCMs().find("id"), "material"))
        .thenReturn("another-md5");
    UpdateSCMConfigCommand command =
        new UpdateSCMConfigCommand(
            updatedScm,
            pluggableScmService,
            goConfigService,
            currentUser,
            result,
            "md5",
            entityHashingService);

    assertThat(command.canContinue(cruiseConfig), is(false));
    assertThat(result.toString(), containsString("STALE_RESOURCE_CONFIG"));
    assertThat(result.toString(), containsString(updatedScm.getName()));
  }
  @Test
  public void shouldNotContinueWithConfigSaveIfUserIsUnauthorized() {
    when(goConfigService.isUserAdmin(currentUser)).thenReturn(false);
    when(goConfigService.isGroupAdministrator(currentUser.getUsername())).thenReturn(false);

    SCM updatedScm =
        new SCM(
            "id",
            new PluginConfiguration("plugin-id", "1"),
            new Configuration(
                new ConfigurationProperty(
                    new ConfigurationKey("key1"), new ConfigurationValue("value1"))));
    UpdateSCMConfigCommand command =
        new UpdateSCMConfigCommand(
            updatedScm,
            pluggableScmService,
            goConfigService,
            currentUser,
            result,
            "md5",
            entityHashingService);

    assertThat(command.canContinue(cruiseConfig), is(false));
    assertThat(result.toString(), containsString("UNAUTHORIZED_TO_EDIT"));
  }
 private boolean isAuthorized() {
   if (!(goConfigService.isUserAdmin(username)
       || goConfigService.isGroupAdministrator(username.getUsername()))) {
     result.unauthorized(
         LocalizedMessage.string("UNAUTHORIZED_TO_EDIT"), HealthStateType.unauthorised());
     return false;
   }
   return true;
 }
  @Test
  public void shouldContinueWithConfigSaveIfUserIsAuthorized() {
    cruiseConfig.addTemplate(pipelineTemplateConfig);
    when(goConfigService.isUserAdmin(currentUser)).thenReturn(true);

    DeleteTemplateConfigCommand command =
        new DeleteTemplateConfigCommand(
            pipelineTemplateConfig, result, goConfigService, currentUser);

    assertThat(command.canContinue(cruiseConfig), is(true));
  }
  @Test
  public void shouldContinueWithConfigSaveIfUserIsGroupAdmin() {
    when(goConfigService.isUserAdmin(currentUser)).thenReturn(false);
    when(goConfigService.isGroupAdministrator(currentUser.getUsername())).thenReturn(true);

    CreatePackageRepositoryCommand command =
        new CreatePackageRepositoryCommand(
            goConfigService, packageRepositoryService, packageRepository, currentUser, result);

    assertThat(command.canContinue(cruiseConfig), is(true));
  }
  @Test
  public void shouldNotContinueIfTheUserDontHavePermissionsToOperateOnPackageRepositories()
      throws Exception {
    when(goConfigService.isUserAdmin(currentUser)).thenReturn(false);
    CreatePackageRepositoryCommand command =
        new CreatePackageRepositoryCommand(
            goConfigService, packageRepositoryService, packageRepository, currentUser, result);

    HttpLocalizedOperationResult expectedResult = new HttpLocalizedOperationResult();
    expectedResult.unauthorized(
        LocalizedMessage.string("UNAUTHORIZED_TO_EDIT"), HealthStateType.unauthorised());

    assertThat(command.canContinue(cruiseConfig), is(false));
    assertThat(result, is(expectedResult));
  }
  @Test
  public void shouldContinueWithConfigSaveIfUserIsGroupAdmin() {
    when(goConfigService.isUserAdmin(currentUser)).thenReturn(false);
    when(goConfigService.isGroupAdministrator(currentUser.getUsername())).thenReturn(true);
    when(entityHashingService.md5ForEntity(any(PackageRepository.class))).thenReturn("md5");

    UpdatePackageRepositoryCommand command =
        new UpdatePackageRepositoryCommand(
            goConfigService,
            packageRepositoryService,
            newPackageRepo,
            currentUser,
            "md5",
            entityHashingService,
            result,
            repoId);

    assertThat(command.canContinue(cruiseConfig), is(true));
  }
  @Test
  public void shouldNotContinueIfRepoIdIsChanged() {
    when(goConfigService.isUserAdmin(currentUser)).thenReturn(true);
    when(goConfigService.getPackageRepository(repoId)).thenReturn(oldPackageRepo);
    when(entityHashingService.md5ForEntity(oldPackageRepo)).thenReturn("md5");
    HttpLocalizedOperationResult expectResult = new HttpLocalizedOperationResult();
    expectResult.unprocessableEntity(
        LocalizedMessage.string("PACKAGE_REPOSITORY_ID_CHANGED", "repository"));

    UpdatePackageRepositoryCommand command =
        new UpdatePackageRepositoryCommand(
            goConfigService,
            packageRepositoryService,
            newPackageRepo,
            currentUser,
            "md5",
            entityHashingService,
            result,
            "old-repo-id");

    assertThat(command.canContinue(cruiseConfig), is(false));
    assertThat(result, is(expectResult));
  }
  @Test
  public void shouldNotContinueIfTheUserSubmittedStaleEtag() throws Exception {
    when(goConfigService.isUserAdmin(currentUser)).thenReturn(true);
    when(goConfigService.getPackageRepository(repoId)).thenReturn(oldPackageRepo);
    when(entityHashingService.md5ForEntity(oldPackageRepo)).thenReturn("foobar");
    HttpLocalizedOperationResult expectResult = new HttpLocalizedOperationResult();
    expectResult.stale(
        LocalizedMessage.string("STALE_RESOURCE_CONFIG", "Package Repository", repoId));

    UpdatePackageRepositoryCommand command =
        new UpdatePackageRepositoryCommand(
            goConfigService,
            packageRepositoryService,
            newPackageRepo,
            currentUser,
            "md5",
            entityHashingService,
            result,
            repoId);

    assertThat(command.canContinue(cruiseConfig), is(false));
    assertThat(result, is(expectResult));
  }
Beispiel #11
0
 public boolean isUserAdmin(Username username) {
   return goConfigService.isUserAdmin(username);
 }