@Test
  public void fail_if_not_logged_in() {
    expectedException.expect(UnauthorizedException.class);
    userSession.anonymous();

    newRequest(USER_LOGIN, permissionTemplate.getUuid(), DEFAULT_PERMISSION);
  }
  @Test
  public void fail_if_user_does_not_exist() {
    expectedException.expect(NotFoundException.class);
    expectedException.expectMessage("User with login 'unknown-login' is not found");

    newRequest("unknown-login", permissionTemplate.getUuid(), DEFAULT_PERMISSION);
  }
  @Test
  public void fail_if_insufficient_privileges() {
    expectedException.expect(ForbiddenException.class);
    userSession.setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);

    newRequest(USER_LOGIN, permissionTemplate.getUuid(), DEFAULT_PERMISSION);
  }
  @Test
  public void remove_user_from_template() {
    newRequest(USER_LOGIN, permissionTemplate.getUuid(), DEFAULT_PERMISSION);

    assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), DEFAULT_PERMISSION))
        .isEmpty();
  }
  @Test
  public void keep_other_users_when_one_user_removed() {
    UserDto newUser = insertUser(newUserDto().setLogin("new-login"));
    addUserToTemplate(newUser, permissionTemplate, DEFAULT_PERMISSION);
    commit();

    newRequest(USER_LOGIN, permissionTemplate.getUuid(), DEFAULT_PERMISSION);

    assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), DEFAULT_PERMISSION))
        .containsExactly("new-login");
  }
  @Test
  public void keep_user_permission_not_removed() {
    addUserToTemplate(user, permissionTemplate, ISSUE_ADMIN);
    commit();

    newRequest(USER_LOGIN, permissionTemplate.getUuid(), DEFAULT_PERMISSION);

    assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), DEFAULT_PERMISSION))
        .isEmpty();
    assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), ISSUE_ADMIN))
        .containsExactly(user.getLogin());
  }
  @Test
  public void fail_if_permission_missing() {
    expectedException.expect(IllegalArgumentException.class);

    newRequest(USER_LOGIN, permissionTemplate.getUuid(), null);
  }
  @Test
  public void fail_if_user_missing() {
    expectedException.expect(IllegalArgumentException.class);

    newRequest(null, permissionTemplate.getUuid(), DEFAULT_PERMISSION);
  }
  @Test
  public void fail_if_not_a_project_permission() {
    expectedException.expect(BadRequestException.class);

    newRequest(USER_LOGIN, permissionTemplate.getUuid(), GlobalPermissions.PREVIEW_EXECUTION);
  }