mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 16:35:45 +01:00
Rename UserNewResource to UserV2Resource and add minimal test
This commit is contained in:
@@ -25,8 +25,8 @@ public abstract class User2UserDtoMapper {
|
|||||||
|
|
||||||
@AfterMapping
|
@AfterMapping
|
||||||
void appendLinks(@MappingTarget UserDto target, @Context UriInfo uriInfo) {
|
void appendLinks(@MappingTarget UserDto target, @Context UriInfo uriInfo) {
|
||||||
LinkBuilder userLinkBuilder = new LinkBuilder(uriInfo, UserNewResource.class, UserSubResource.class);
|
LinkBuilder userLinkBuilder = new LinkBuilder(uriInfo, UserV2Resource.class, UserSubResource.class);
|
||||||
LinkBuilder collectionLinkBuilder = new LinkBuilder(uriInfo, UserNewResource.class, UserCollectionResource.class);
|
LinkBuilder collectionLinkBuilder = new LinkBuilder(uriInfo, UserV2Resource.class, UserCollectionResource.class);
|
||||||
Map<String, Link> links = new HashMap<>();
|
Map<String, Link> links = new HashMap<>();
|
||||||
links.put("self", userLinkBuilder.method("getUserSubResource").parameters(target.getName()).method("get").parameters().create());
|
links.put("self", userLinkBuilder.method("getUserSubResource").parameters(target.getName()).method("get").parameters().create());
|
||||||
if (SecurityUtils.getSubject().hasRole(Role.ADMIN)) {
|
if (SecurityUtils.getSubject().hasRole(Role.ADMIN)) {
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ public class UserCollectionResource extends AbstractManagerResource<User, UserEx
|
|||||||
User user = dtoToUserMapper.userDtoToUser(userDto, "");
|
User user = dtoToUserMapper.userDtoToUser(userDto, "");
|
||||||
manager.create(user);
|
manager.create(user);
|
||||||
|
|
||||||
LinkBuilder builder = new LinkBuilder(uriInfo, UserNewResource.class, UserSubResource.class);
|
LinkBuilder builder = new LinkBuilder(uriInfo, UserV2Resource.class, UserSubResource.class);
|
||||||
return Response.created(builder.method("getUserSubResource").parameters(user.getName()).method("get").parameters().create().getHref()).build();
|
return Response.created(builder.method("getUserSubResource").parameters(user.getName()).method("get").parameters().create().getHref()).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,15 +6,15 @@ import com.google.inject.Singleton;
|
|||||||
import javax.ws.rs.Path;
|
import javax.ws.rs.Path;
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
@Path(UserNewResource.USERS_PATH_V2)
|
@Path(UserV2Resource.USERS_PATH_V2)
|
||||||
public class UserNewResource {
|
public class UserV2Resource {
|
||||||
|
|
||||||
public static final String USERS_PATH_V2 = "v2/users/";
|
public static final String USERS_PATH_V2 = "v2/users/";
|
||||||
private final UserCollectionResource userCollectionResource;
|
private final UserCollectionResource userCollectionResource;
|
||||||
private final UserSubResource userSubResource;
|
private final UserSubResource userSubResource;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public UserNewResource(UserCollectionResource userCollectionResource, UserSubResource userSubResource) {
|
public UserV2Resource(UserCollectionResource userCollectionResource, UserSubResource userSubResource) {
|
||||||
this.userCollectionResource = userCollectionResource;
|
this.userCollectionResource = userCollectionResource;
|
||||||
this.userSubResource = userSubResource;
|
this.userSubResource = userSubResource;
|
||||||
}
|
}
|
||||||
@@ -31,7 +31,7 @@ public class User2UserDtoMapperTest {
|
|||||||
@Before
|
@Before
|
||||||
public void init() throws URISyntaxException {
|
public void init() throws URISyntaxException {
|
||||||
baseUri = new URI("http://example.com/base/");
|
baseUri = new URI("http://example.com/base/");
|
||||||
expextedBaseUri = baseUri.resolve(UserNewResource.USERS_PATH_V2 + "/");
|
expextedBaseUri = baseUri.resolve(UserV2Resource.USERS_PATH_V2 + "/");
|
||||||
when(uriInfo.getBaseUri()).thenReturn(baseUri);
|
when(uriInfo.getBaseUri()).thenReturn(baseUri);
|
||||||
subjectThreadState.bind();
|
subjectThreadState.bind();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,84 @@
|
|||||||
|
package sonia.scm.api.v2.resources;
|
||||||
|
|
||||||
|
import com.github.sdorra.shiro.ShiroRule;
|
||||||
|
import com.github.sdorra.shiro.SubjectAware;
|
||||||
|
import org.jboss.resteasy.core.Dispatcher;
|
||||||
|
import org.jboss.resteasy.mock.MockDispatcherFactory;
|
||||||
|
import org.jboss.resteasy.mock.MockHttpRequest;
|
||||||
|
import org.jboss.resteasy.mock.MockHttpResponse;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Rule;
|
||||||
|
import org.junit.Test;
|
||||||
|
import sonia.scm.user.User;
|
||||||
|
import sonia.scm.user.UserManager;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
@SubjectAware(
|
||||||
|
username = "trillian",
|
||||||
|
password = "secret",
|
||||||
|
configuration = "classpath:sonia/scm/repository/shiro.ini"
|
||||||
|
)
|
||||||
|
public class UserV2ResourceTest {
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public ShiroRule shiro = new ShiroRule();
|
||||||
|
|
||||||
|
private Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void prepareEnvironment() {
|
||||||
|
UserManager userManager = mock(UserManager.class);
|
||||||
|
when(userManager.getAll()).thenReturn(Collections.singletonList(createDummyUser()));
|
||||||
|
|
||||||
|
UserDto2UserMapperImpl dtoToUserMapper = new UserDto2UserMapperImpl();
|
||||||
|
User2UserDtoMapperImpl userToDtoMapper = new User2UserDtoMapperImpl();
|
||||||
|
UserCollectionResource userCollectionResource = new UserCollectionResource(userManager, dtoToUserMapper, userToDtoMapper);
|
||||||
|
UserSubResource userSubResource = new UserSubResource(dtoToUserMapper, userToDtoMapper, userManager);
|
||||||
|
UserV2Resource userV2Resource = new UserV2Resource(userCollectionResource, userSubResource);
|
||||||
|
|
||||||
|
dispatcher.getRegistry().addSingletonResource(userV2Resource);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldCreateFullResponseForAdmin() throws URISyntaxException {
|
||||||
|
MockHttpRequest request = MockHttpRequest.get("/" + UserV2Resource.USERS_PATH_V2);
|
||||||
|
MockHttpResponse response = new MockHttpResponse();
|
||||||
|
|
||||||
|
dispatcher.invoke(request, response);
|
||||||
|
|
||||||
|
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
|
||||||
|
assertTrue(response.getContentAsString().contains("\"name\":\"Neo\""));
|
||||||
|
assertTrue(response.getContentAsString().contains("\"password\":\"__dummypassword__\""));
|
||||||
|
assertTrue(response.getContentAsString().contains("\"self\":{\"href\":\"/v2/users/Neo\"}"));
|
||||||
|
assertTrue(response.getContentAsString().contains("\"delete\":{\"href\":\"/v2/users/Neo\"}"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SubjectAware(username = "unpriv")
|
||||||
|
public void shouldCreateLimitedResponseForAdmin() throws URISyntaxException {
|
||||||
|
MockHttpRequest request = MockHttpRequest.get("/" + UserV2Resource.USERS_PATH_V2);
|
||||||
|
MockHttpResponse response = new MockHttpResponse();
|
||||||
|
|
||||||
|
dispatcher.invoke(request, response);
|
||||||
|
|
||||||
|
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
|
||||||
|
assertTrue(response.getContentAsString().contains("\"name\":\"Neo\""));
|
||||||
|
assertTrue(response.getContentAsString().contains("\"password\":\"__dummypassword__\""));
|
||||||
|
assertTrue(response.getContentAsString().contains("\"self\":{\"href\":\"/v2/users/Neo\"}"));
|
||||||
|
assertFalse(response.getContentAsString().contains("\"delete\":{\"href\":\"/v2/users/Neo\"}"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private User createDummyUser() {
|
||||||
|
User user = new User();
|
||||||
|
user.setName("Neo");
|
||||||
|
user.setPassword("redpill");
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user