Skip to content

Commit 835790c

Browse files
author
gdgate
authored
Merge pull request #1079 from cipous/1077_delete_user_from_project
1077 Remove user from a project Reviewed-by: Libor Ryšavý https://github.com/liry
2 parents 7ba193a + bd6abe6 commit 835790c

4 files changed

Lines changed: 158 additions & 4 deletions

File tree

gooddata-java/src/main/java/com/gooddata/sdk/service/project/ProjectService.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,4 +552,38 @@ private void doPostProjectUsersUpdate(final Project project, final User... users
552552
throw new GoodDataException("Unable to update users in project", e);
553553
}
554554
}
555+
556+
/**
557+
* Removes given account from a project without checking if really account is in project.
558+
* <p>
559+
* You can:
560+
* <ul>
561+
* <li>Remove yourself from a project (leave the project). You cannot leave the project if you are the only admin in the project.</li>
562+
* <li>Remove another user from a project. You need to have the <code>canSuspendUser</code> permission in this project.</li>
563+
* </ul>
564+
* </p>
565+
* @param account account to be removed
566+
* @param project project from user will be removed
567+
* @throws com.gooddata.sdk.common.GoodDataException when account can't be removed
568+
*/
569+
public void removeUserFromProject(final Project project, final Account account) {
570+
notNull(project, "project");
571+
notNull(project.getId(), "project.id");
572+
notNull(account, "account");
573+
notNull(account.getId(), "account.id");
574+
575+
try {
576+
restTemplate.delete(getUserUri(project, account));
577+
} catch (GoodDataRestException e) {
578+
if (HttpStatus.FORBIDDEN.value() == e.getStatusCode()) {
579+
throw new GoodDataException("You cannot leave the project " + project.getId() + " if you are the only admin in it. You can make another user an admin in this project, and then re-issue the call.", e);
580+
} else if (HttpStatus.METHOD_NOT_ALLOWED.value() == e.getStatusCode()) {
581+
throw new GoodDataException("You either misspelled your user ID or tried to remove another user but did not have the canSuspendUser permission in this project. Check your ID in the request and your permissions in the project " + project.getId() + ", then re-issue the call.", e);
582+
} else {
583+
throw e;
584+
}
585+
} catch (RestClientException e) {
586+
throw new GoodDataException("Unable to remove account " + account.getUri() + " from project " + project.getUri(), e);
587+
}
588+
}
555589
}

gooddata-java/src/test/java/com/gooddata/sdk/service/project/ProjectServiceAT.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,35 @@
88
import com.gooddata.sdk.common.collections.CustomPageRequest;
99
import com.gooddata.sdk.common.collections.Page;
1010
import com.gooddata.sdk.common.collections.PageBrowser;
11-
import com.gooddata.sdk.common.collections.PageRequest;
1211
import com.gooddata.sdk.model.account.Account;
13-
import com.gooddata.sdk.model.project.*;
12+
import com.gooddata.sdk.model.project.CreatedInvitations;
13+
import com.gooddata.sdk.model.project.Invitation;
14+
import com.gooddata.sdk.model.project.Project;
15+
import com.gooddata.sdk.model.project.ProjectValidationResults;
16+
import com.gooddata.sdk.model.project.Role;
17+
import com.gooddata.sdk.model.project.User;
1418
import com.gooddata.sdk.service.AbstractGoodDataAT;
1519
import com.gooddata.sdk.service.account.AccountService;
20+
1621
import org.apache.commons.lang3.RandomStringUtils;
1722
import org.hamcrest.core.IsIterableContaining;
1823
import org.testng.annotations.AfterClass;
1924
import org.testng.annotations.BeforeClass;
2025
import org.testng.annotations.Test;
2126

22-
import java.util.*;
27+
import java.util.ArrayList;
28+
import java.util.List;
29+
import java.util.Set;
30+
import java.util.UUID;
2331

2432
import static com.gooddata.sdk.model.project.ProjectEnvironment.TESTING;
2533
import static org.hamcrest.MatcherAssert.assertThat;
26-
import static org.hamcrest.Matchers.*;
34+
import static org.hamcrest.Matchers.empty;
35+
import static org.hamcrest.Matchers.hasSize;
36+
import static org.hamcrest.Matchers.is;
37+
import static org.hamcrest.Matchers.not;
38+
import static org.hamcrest.Matchers.notNullValue;
39+
import static org.testng.Assert.fail;
2740

2841
/**
2942
* Project acceptance tests.
@@ -150,6 +163,19 @@ public void getUserInProject() {
150163
assertThat(user, notNullValue());
151164
}
152165

166+
@Test(groups = {"project", "isolated_domain"}, dependsOnMethods = "disableUserInProject")
167+
public void removeAccountFromProject() {
168+
final User user = gd.getProjectService().getUser(project, account1);
169+
gd.getProjectService().removeUserFromProject(project, account1);
170+
171+
try {
172+
gd.getProjectService().getUser(project, account1);
173+
assertThat("Exception should be thrown.", false);
174+
} catch (UserInProjectNotFoundException e) {
175+
//expected
176+
}
177+
}
178+
153179
@AfterClass(groups = "isolated_domain")
154180
public void tearDownIsolatedDomainGroup() {
155181
try {

gooddata-java/src/test/java/com/gooddata/sdk/service/project/ProjectServiceIT.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,4 +429,26 @@ public void getUserInProjectNotFound() {
429429

430430
gd.getProjectService().getUser(enabled, account);
431431
}
432+
433+
@Test
434+
public void removeUserFromProject() {
435+
onRequest()
436+
.havingMethodEqualTo("DELETE")
437+
.havingPathEqualTo(ProjectService.PROJECT_USER_TEMPLATE.expand("PROJECT_ID", "ID").toString())
438+
.respond()
439+
.withStatus(200);
440+
441+
gd.getProjectService().removeUserFromProject(enabled, account);
442+
}
443+
444+
@Test(expectedExceptions = GoodDataException.class)
445+
public void removeUserFromProjectNotFound() {
446+
onRequest()
447+
.havingMethodEqualTo("DELETE")
448+
.havingPathEqualTo(ProjectService.PROJECT_USER_TEMPLATE.expand("PROJECT_ID", "ID").toString())
449+
.respond()
450+
.withStatus(404);
451+
452+
gd.getProjectService().removeUserFromProject(enabled, account);
453+
}
432454
}

gooddata-java/src/test/java/com/gooddata/sdk/service/project/ProjectServiceTest.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@
1818
import com.gooddata.sdk.service.account.AccountService;
1919
import org.mockito.Mock;
2020
import org.mockito.MockitoAnnotations;
21+
import org.springframework.http.HttpStatus;
2122
import org.springframework.web.client.RestClientException;
2223
import org.springframework.web.client.RestTemplate;
2324
import org.testng.annotations.BeforeMethod;
2425
import org.testng.annotations.Test;
2526

27+
import java.net.MalformedURLException;
2628
import java.net.URI;
29+
import java.net.URISyntaxException;
30+
import java.net.URL;
2731
import java.util.Collection;
2832

2933
import static com.gooddata.sdk.service.project.ProjectService.LIST_PROJECTS_TEMPLATE;
@@ -32,6 +36,7 @@
3236
import static org.hamcrest.CoreMatchers.is;
3337
import static org.hamcrest.MatcherAssert.assertThat;
3438
import static org.hamcrest.Matchers.hasSize;
39+
import static org.mockito.Mockito.doNothing;
3540
import static org.mockito.Mockito.doReturn;
3641
import static org.mockito.Mockito.doThrow;
3742
import static org.mockito.Mockito.mock;
@@ -203,4 +208,71 @@ public void testGetRoleByUri() {
203208
assertThat(roleByUri, is(role));
204209
}
205210

211+
@Test(expectedExceptions = IllegalArgumentException.class)
212+
public void removeUserFromProjectInvalidProject(){
213+
service.removeUserFromProject(project, null);
214+
}
215+
216+
@Test(expectedExceptions = IllegalArgumentException.class)
217+
public void removeUserFromProjectInvalidAccount(){
218+
service.removeUserFromProject(null, account);
219+
}
220+
221+
@Test(expectedExceptions = GoodDataException.class)
222+
public void removeUserProjectFail() throws URISyntaxException {
223+
when(project.getId()).thenReturn("1");
224+
when(account.getId()).thenReturn("1");
225+
doThrow(GoodDataRestException.class).when(restTemplate).delete(new URI("/gdc/projects/1/users/1"));
226+
service.removeUserFromProject(project, account);
227+
}
228+
229+
@Test(expectedExceptions = GoodDataException.class,
230+
expectedExceptionsMessageRegExp= "You cannot leave the project 1 if you are the " +
231+
"only admin in it. You can make another user an admin in this project, and then re-issue the call.")
232+
public void removeUserProjectFailForbidden() throws URISyntaxException {
233+
when(project.getId()).thenReturn("1");
234+
when(account.getId()).thenReturn("1");
235+
final GoodDataRestException goodDataRestException = new GoodDataRestException(
236+
HttpStatus.FORBIDDEN.value(),
237+
"r1",
238+
"not allowd",
239+
"component",
240+
"errorClass");
241+
doThrow(goodDataRestException).when(restTemplate).delete(new URI("/gdc/projects/1/users/1"));
242+
service.removeUserFromProject(project, account);
243+
}
244+
245+
@Test(expectedExceptions = GoodDataException.class,
246+
expectedExceptionsMessageRegExp= "You either misspelled your user ID or tried to remove another user but " +
247+
"did not have the canSuspendUser permission in this project. Check your ID in the request and your " +
248+
"permissions in the project 1, then re-issue the call.")
249+
public void removeUserProjectFailNotAllowed() throws URISyntaxException {
250+
when(project.getId()).thenReturn("1");
251+
when(account.getId()).thenReturn("1");
252+
final GoodDataRestException goodDataRestException = new GoodDataRestException(
253+
HttpStatus.METHOD_NOT_ALLOWED.value() ,
254+
"r1",
255+
"not allowd",
256+
"component",
257+
"errorClass");
258+
doThrow(goodDataRestException).when(restTemplate).delete(new URI("/gdc/projects/1/users/1"));
259+
service.removeUserFromProject(project, account);
260+
}
261+
262+
@Test(expectedExceptions = GoodDataException.class)
263+
public void removeUserProjectFailRestletClientException() throws URISyntaxException {
264+
when(project.getId()).thenReturn("1");
265+
when(account.getId()).thenReturn("1");
266+
doThrow(RestClientException.class).when(restTemplate).delete(new URI("/gdc/projects/1/users/1"));
267+
service.removeUserFromProject(project, account);
268+
}
269+
270+
@Test
271+
public void removeUserProject() throws URISyntaxException {
272+
when(project.getId()).thenReturn("1");
273+
when(account.getId()).thenReturn("1");
274+
doNothing().when(restTemplate).delete(new URI("/gdc/projects/1/users/1"));
275+
service.removeUserFromProject(project, account);
276+
}
277+
206278
}

0 commit comments

Comments
 (0)