mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-17 02:31:14 +01:00
change password endpoint
This commit is contained in:
@@ -14,6 +14,7 @@ public class DispatcherMock {
|
||||
dispatcher.getProviderFactory().registerProvider(AlreadyExistsExceptionMapper.class);
|
||||
dispatcher.getProviderFactory().registerProvider(AuthorizationExceptionMapper.class);
|
||||
dispatcher.getProviderFactory().registerProvider(ConcurrentModificationExceptionMapper.class);
|
||||
dispatcher.getProviderFactory().registerProvider(ChangePasswordNotAllowedExceptionMapper.class);
|
||||
return dispatcher;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ public class MeResourceTest {
|
||||
|
||||
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
|
||||
assertTrue(response.getContentAsString().contains("\"name\":\"trillian\""));
|
||||
assertTrue(response.getContentAsString().contains("\"password\":\"__dummypassword__\""));
|
||||
assertTrue(response.getContentAsString().contains("\"password\":null"));
|
||||
assertTrue(response.getContentAsString().contains("\"self\":{\"href\":\"/v2/users/trillian\"}"));
|
||||
assertTrue(response.getContentAsString().contains("\"delete\":{\"href\":\"/v2/users/trillian\"}"));
|
||||
}
|
||||
|
||||
@@ -23,18 +23,9 @@ public class UserDtoToUserMapperTest {
|
||||
@Test
|
||||
public void shouldMapFields() {
|
||||
UserDto dto = createDefaultDto();
|
||||
User user = mapper.map(dto, "original password");
|
||||
User user = mapper.map(dto, "used password");
|
||||
assertEquals("abc" , user.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldEncodePassword() {
|
||||
when(passwordService.encryptPassword("unencrypted")).thenReturn("encrypted");
|
||||
|
||||
UserDto dto = createDefaultDto();
|
||||
dto.setPassword("unencrypted");
|
||||
User user = mapper.map(dto, "original password");
|
||||
assertEquals("encrypted" , user.getPassword());
|
||||
assertEquals("used password" , user.getPassword());
|
||||
}
|
||||
|
||||
@Before
|
||||
|
||||
@@ -22,6 +22,7 @@ import javax.servlet.http.HttpServletResponse;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.text.MessageFormat;
|
||||
|
||||
import static java.util.Collections.singletonList;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@@ -60,21 +61,23 @@ public class UserRootResourceTest {
|
||||
private UserToUserDtoMapperImpl userToDtoMapper;
|
||||
|
||||
private ArgumentCaptor<User> userCaptor = ArgumentCaptor.forClass(User.class);
|
||||
private User originalUser;
|
||||
|
||||
@Before
|
||||
public void prepareEnvironment() throws Exception {
|
||||
initMocks(this);
|
||||
User dummyUser = createDummyUser("Neo");
|
||||
originalUser = createDummyUser("Neo");
|
||||
when(userManager.create(userCaptor.capture())).thenAnswer(invocation -> invocation.getArguments()[0]);
|
||||
doNothing().when(userManager).modify(userCaptor.capture());
|
||||
doNothing().when(userManager).delete(userCaptor.capture());
|
||||
when(userManager.getDefaultType()).thenReturn("xml");
|
||||
|
||||
UserCollectionToDtoMapper userCollectionToDtoMapper = new UserCollectionToDtoMapper(userToDtoMapper, resourceLinks);
|
||||
UserCollectionResource userCollectionResource = new UserCollectionResource(userManager, dtoToUserMapper,
|
||||
userCollectionToDtoMapper, resourceLinks);
|
||||
UserResource userResource = new UserResource(dtoToUserMapper, userToDtoMapper, userManager);
|
||||
userCollectionToDtoMapper, resourceLinks, passwordService);
|
||||
UserResource userResource = new UserResource(dtoToUserMapper, userToDtoMapper, userManager, passwordService);
|
||||
UserRootResource userRootResource = new UserRootResource(MockProvider.of(userCollectionResource),
|
||||
MockProvider.of(userResource));
|
||||
MockProvider.of(userResource));
|
||||
|
||||
dispatcher = createDispatcher(userRootResource);
|
||||
}
|
||||
@@ -88,7 +91,7 @@ public class UserRootResourceTest {
|
||||
|
||||
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
|
||||
assertTrue(response.getContentAsString().contains("\"name\":\"Neo\""));
|
||||
assertTrue(response.getContentAsString().contains("\"password\":\"__dummypassword__\""));
|
||||
assertTrue(response.getContentAsString().contains("\"password\":null"));
|
||||
assertTrue(response.getContentAsString().contains("\"self\":{\"href\":\"/v2/users/Neo\"}"));
|
||||
assertTrue(response.getContentAsString().contains("\"delete\":{\"href\":\"/v2/users/Neo\"}"));
|
||||
}
|
||||
@@ -103,13 +106,49 @@ public class UserRootResourceTest {
|
||||
|
||||
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
|
||||
assertTrue(response.getContentAsString().contains("\"name\":\"Neo\""));
|
||||
assertTrue(response.getContentAsString().contains("\"password\":\"__dummypassword__\""));
|
||||
assertTrue(response.getContentAsString().contains("\"password\":null"));
|
||||
assertTrue(response.getContentAsString().contains("\"self\":{\"href\":\"/v2/users/Neo\"}"));
|
||||
assertFalse(response.getContentAsString().contains("\"delete\":{\"href\":\"/v2/users/Neo\"}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateNewUserWithEncryptedPassword() throws Exception {
|
||||
public void shouldEncryptPasswordBeforeChanging() throws Exception {
|
||||
String newPassword = "pwd123";
|
||||
String content = MessageFormat.format("'{'\"newPassword\": \"{0}\"'}'", newPassword);
|
||||
MockHttpRequest request = MockHttpRequest
|
||||
.put("/" + UserRootResource.USERS_PATH_V2 + "Neo/password")
|
||||
.contentType(VndMediaType.PASSWORD_CHANGE)
|
||||
.content(content.getBytes());
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
when(passwordService.encryptPassword(newPassword)).thenReturn("encrypted123");
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
assertEquals(HttpServletResponse.SC_NO_CONTENT, response.getStatus());
|
||||
verify(userManager).modify(any(User.class));
|
||||
User updatedUser = userCaptor.getValue();
|
||||
assertEquals("encrypted123", updatedUser.getPassword());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGet400OnChangePasswordOfUserWithNonDefaultType() throws Exception {
|
||||
originalUser.setType("not an xml type");
|
||||
String newPassword = "pwd123";
|
||||
String content = MessageFormat.format("'{'\"newPassword\": \"{0}\"'}'", newPassword);
|
||||
MockHttpRequest request = MockHttpRequest
|
||||
.put("/" + UserRootResource.USERS_PATH_V2 + "Neo/password")
|
||||
.contentType(VndMediaType.PASSWORD_CHANGE)
|
||||
.content(content.getBytes());
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
when(passwordService.encryptPassword(newPassword)).thenReturn("encrypted123");
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
assertEquals(HttpServletResponse.SC_BAD_REQUEST, response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldEncryptPasswordBeforeCreatingUser() throws Exception {
|
||||
URL url = Resources.getResource("sonia/scm/api/v2/user-test-create.json");
|
||||
byte[] userJson = Resources.toByteArray(url);
|
||||
|
||||
@@ -129,7 +168,7 @@ public class UserRootResourceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldUpdateChangedUserWithEncryptedPassword() throws Exception {
|
||||
public void shouldIgnoreGivenPasswordOnUpdatingUser() throws Exception {
|
||||
URL url = Resources.getResource("sonia/scm/api/v2/user-test-update.json");
|
||||
byte[] userJson = Resources.toByteArray(url);
|
||||
|
||||
@@ -138,14 +177,13 @@ public class UserRootResourceTest {
|
||||
.contentType(VndMediaType.USER)
|
||||
.content(userJson);
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
when(passwordService.encryptPassword("pwd123")).thenReturn("encrypted123");
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
assertEquals(HttpServletResponse.SC_NO_CONTENT, response.getStatus());
|
||||
verify(userManager).modify(any(User.class));
|
||||
User updatedUser = userCaptor.getValue();
|
||||
assertEquals("encrypted123", updatedUser.getPassword());
|
||||
assertEquals(originalUser.getPassword(), updatedUser.getPassword());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -153,7 +191,7 @@ public class UserRootResourceTest {
|
||||
MockHttpRequest request = MockHttpRequest
|
||||
.post("/" + UserRootResource.USERS_PATH_V2)
|
||||
.contentType(VndMediaType.USER)
|
||||
.content(new byte[] {});
|
||||
.content(new byte[]{});
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
when(passwordService.encryptPassword("pwd123")).thenReturn("encrypted123");
|
||||
|
||||
@@ -264,6 +302,7 @@ public class UserRootResourceTest {
|
||||
private User createDummyUser(String name) {
|
||||
User user = new User();
|
||||
user.setName(name);
|
||||
user.setType("xml");
|
||||
user.setPassword("redpill");
|
||||
user.setCreationDate(System.currentTimeMillis());
|
||||
when(userManager.get(name)).thenReturn(user);
|
||||
|
||||
@@ -8,12 +8,14 @@ import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import sonia.scm.api.rest.resources.UserResource;
|
||||
import org.mockito.Mock;
|
||||
import sonia.scm.user.User;
|
||||
import sonia.scm.user.UserManager;
|
||||
|
||||
import java.net.URI;
|
||||
import java.time.Instant;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -26,6 +28,9 @@ public class UserToUserDtoMapperTest {
|
||||
@SuppressWarnings("unused") // Is injected
|
||||
private final ResourceLinks resourceLinks = ResourceLinksMock.createMock(baseUri);
|
||||
|
||||
@Mock
|
||||
private UserManager userManager;
|
||||
|
||||
@InjectMocks
|
||||
private UserToUserDtoMapperImpl mapper;
|
||||
|
||||
@@ -37,6 +42,7 @@ public class UserToUserDtoMapperTest {
|
||||
@Before
|
||||
public void init() {
|
||||
initMocks(this);
|
||||
when(userManager.getDefaultType()).thenReturn("xml");
|
||||
expectedBaseUri = baseUri.resolve(UserRootResource.USERS_PATH_V2 + "/");
|
||||
subjectThreadState.bind();
|
||||
ThreadContext.bind(subject);
|
||||
@@ -53,11 +59,37 @@ public class UserToUserDtoMapperTest {
|
||||
when(subject.isPermitted("user:modify:abc")).thenReturn(true);
|
||||
|
||||
UserDto userDto = mapper.map(user);
|
||||
|
||||
assertEquals("expected self link", expectedBaseUri.resolve("abc").toString(), userDto.getLinks().getLinkBy("self").get().getHref());
|
||||
assertEquals("expected self link", expectedBaseUri.resolve("abc").toString(), userDto.getLinks().getLinkBy("self").get().getHref());
|
||||
assertEquals("expected update link", expectedBaseUri.resolve("abc").toString(), userDto.getLinks().getLinkBy("update").get().getHref());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetPasswordLinkOnlyForDefaultUserType() {
|
||||
User user = createDefaultUser();
|
||||
when(subject.isPermitted("user:modify:abc")).thenReturn(true);
|
||||
user.setType("xml");
|
||||
|
||||
UserDto userDto = mapper.map(user);
|
||||
|
||||
assertEquals("expected password link", expectedBaseUri.resolve("abc/password").toString(), userDto.getLinks().getLinkBy("password").get().getHref());
|
||||
user.setType("db");
|
||||
|
||||
userDto = mapper.map(user);
|
||||
|
||||
assertFalse("expected no password link", userDto.getLinks().getLinkBy("password").isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetEmptyPasswordProperty() {
|
||||
User user = createDefaultUser();
|
||||
user.setPassword("myHighSecurePassword");
|
||||
when(subject.isPermitted("user:modify:abc")).thenReturn(true);
|
||||
|
||||
UserDto userDto = mapper.map(user);
|
||||
|
||||
assertThat(userDto.getPassword()).isBlank();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMapLinks_forDelete() {
|
||||
User user = createDefaultUser();
|
||||
@@ -65,7 +97,7 @@ public class UserToUserDtoMapperTest {
|
||||
|
||||
UserDto userDto = mapper.map(user);
|
||||
|
||||
assertEquals("expected self link", expectedBaseUri.resolve("abc").toString(), userDto.getLinks().getLinkBy("self").get().getHref());
|
||||
assertEquals("expected self link", expectedBaseUri.resolve("abc").toString(), userDto.getLinks().getLinkBy("self").get().getHref());
|
||||
assertEquals("expected delete link", expectedBaseUri.resolve("abc").toString(), userDto.getLinks().getLinkBy("delete").get().getHref());
|
||||
}
|
||||
|
||||
@@ -97,16 +129,6 @@ public class UserToUserDtoMapperTest {
|
||||
assertEquals("abc", userDto.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRemovePassword() {
|
||||
User user = createDefaultUser();
|
||||
user.setPassword("password");
|
||||
|
||||
UserDto userDto = mapper.map(user);
|
||||
|
||||
assertEquals(UserResource.DUMMY_PASSWORT, userDto.getPassword());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMapTimes() {
|
||||
User user = createDefaultUser();
|
||||
|
||||
Reference in New Issue
Block a user