mirror of
https://bitbucket.org/wisemapping/wisemapping-open-source.git
synced 2024-11-22 14:17:57 +01:00
Add tests for change password, remove user. All admin operations covered.
This commit is contained in:
parent
1aebcf48e3
commit
3f2deda442
@ -72,7 +72,7 @@ public class AdminController extends BaseController {
|
|||||||
response.setHeader("Location","/service/admin/users/" + user.getId());
|
response.setHeader("Location","/service/admin/users/" + user.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(method = RequestMethod.PUT, value = "admin/users/{id}/password", consumes = {"text/plain"}, produces = {"application/json", "text/html", "application/xml"})
|
@RequestMapping(method = RequestMethod.PUT, value = "admin/users/{id}/password", consumes = {"text/plain"})
|
||||||
@ResponseStatus(value = HttpStatus.NO_CONTENT)
|
@ResponseStatus(value = HttpStatus.NO_CONTENT)
|
||||||
public void changePassword(@RequestBody String password, @PathVariable long id) throws IOException, WiseMappingException {
|
public void changePassword(@RequestBody String password, @PathVariable long id) throws IOException, WiseMappingException {
|
||||||
if (password == null) {
|
if (password == null) {
|
||||||
|
@ -12,6 +12,8 @@ import org.testng.annotations.DataProvider;
|
|||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
import static org.testng.Assert.assertEquals;
|
import static org.testng.Assert.assertEquals;
|
||||||
|
import static org.testng.Assert.fail;
|
||||||
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
@ -28,12 +30,63 @@ public class RestAdminITCase {
|
|||||||
private static final String BASE_REST_URL = HOST_PORT + "service";
|
private static final String BASE_REST_URL = HOST_PORT + "service";
|
||||||
|
|
||||||
@Test(dataProvider = "ContentType-Provider-Function")
|
@Test(dataProvider = "ContentType-Provider-Function")
|
||||||
public void findUser(final @NotNull MediaType mediaType) { // Configure media types ...
|
public void changePassword(final @NotNull MediaType mediaType) { // Configure media types ...
|
||||||
final HttpHeaders requestHeaders = createHeaders(mediaType);
|
final HttpHeaders requestHeaders = createHeaders(mediaType);
|
||||||
final RestTemplate templateRest = createTemplate();
|
final RestTemplate templateRest = createTemplate();
|
||||||
|
|
||||||
HttpEntity<RestUser> findUserEntity = new HttpEntity<RestUser>(requestHeaders);
|
// Fill user data ...
|
||||||
final ResponseEntity<RestUser> result = templateRest.exchange(BASE_REST_URL + "/admin/users/2", HttpMethod.GET, findUserEntity, RestUser.class);
|
final RestUser restUser = new RestUser();
|
||||||
|
final String email = "foo-to-change" + System.nanoTime() + "@example.org";
|
||||||
|
restUser.setEmail(email);
|
||||||
|
restUser.setUsername("foo");
|
||||||
|
restUser.setFirstname("foo first name");
|
||||||
|
restUser.setLastname("foo last name");
|
||||||
|
restUser.setPassword("foo password");
|
||||||
|
|
||||||
|
// User has been created ...
|
||||||
|
final URI location = createUser(requestHeaders, templateRest, restUser);
|
||||||
|
|
||||||
|
// Check that the user has been created ...
|
||||||
|
ResponseEntity<RestUser> result = findUser(requestHeaders, templateRest, location);
|
||||||
|
|
||||||
|
// Change password ...
|
||||||
|
requestHeaders.setContentType(MediaType.TEXT_PLAIN);
|
||||||
|
HttpEntity<String> createUserEntity = new HttpEntity<String>("some-new-password", requestHeaders);
|
||||||
|
System.out.println("Changed password to:" + email);
|
||||||
|
templateRest.put(BASE_REST_URL + "/admin/users/{id}/password", createUserEntity, result.getBody().getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test(dataProvider = "ContentType-Provider-Function")
|
||||||
|
public void deleteUser(final @NotNull MediaType mediaType) { // Configure media types ...
|
||||||
|
final HttpHeaders requestHeaders = createHeaders(mediaType);
|
||||||
|
final RestTemplate templateRest = createTemplate();
|
||||||
|
|
||||||
|
// Fill user data ...
|
||||||
|
final RestUser restUser = new RestUser();
|
||||||
|
final String email = "foo-to-delete" + System.nanoTime() + "@example.org";
|
||||||
|
restUser.setEmail(email);
|
||||||
|
restUser.setUsername("foo");
|
||||||
|
restUser.setFirstname("foo first name");
|
||||||
|
restUser.setLastname("foo last name");
|
||||||
|
restUser.setPassword("foo password");
|
||||||
|
|
||||||
|
// User has been created ...
|
||||||
|
final URI location = createUser(requestHeaders, templateRest, restUser);
|
||||||
|
|
||||||
|
// Check that the user has been created ...
|
||||||
|
ResponseEntity<RestUser> result = findUser(requestHeaders, templateRest, location);
|
||||||
|
|
||||||
|
// Delete user ...
|
||||||
|
templateRest.delete(BASE_REST_URL + "/admin/users/{id}", result.getBody().getId());
|
||||||
|
|
||||||
|
// Is the user there ?
|
||||||
|
// Check that the user has been created ...
|
||||||
|
try {
|
||||||
|
findUser(requestHeaders, templateRest, location);
|
||||||
|
fail("User could not be deleted !");
|
||||||
|
} catch (Exception e) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(dataProvider = "ContentType-Provider-Function")
|
@Test(dataProvider = "ContentType-Provider-Function")
|
||||||
@ -41,29 +94,49 @@ public class RestAdminITCase {
|
|||||||
|
|
||||||
// Configure media types ...
|
// Configure media types ...
|
||||||
final HttpHeaders requestHeaders = createHeaders(mediaType);
|
final HttpHeaders requestHeaders = createHeaders(mediaType);
|
||||||
|
|
||||||
// Configure media ...
|
|
||||||
final RestTemplate templateRest = createTemplate();
|
final RestTemplate templateRest = createTemplate();
|
||||||
|
|
||||||
// Fill user data ...
|
// Fill user data ...
|
||||||
final RestUser restUser = new RestUser();
|
final RestUser restUser = new RestUser();
|
||||||
|
|
||||||
restUser.setEmail("foo" + System.nanoTime() + "@example.org");
|
final String email = "foo" + System.nanoTime() + "@example.org";
|
||||||
|
restUser.setEmail(email);
|
||||||
restUser.setUsername("foo");
|
restUser.setUsername("foo");
|
||||||
restUser.setFirstname("foo first name");
|
restUser.setFirstname("foo first name");
|
||||||
restUser.setLastname("foo last name");
|
restUser.setLastname("foo last name");
|
||||||
restUser.setPassword("foo password");
|
restUser.setPassword("foo password");
|
||||||
|
|
||||||
// Post request ...
|
// Create a new user ...
|
||||||
HttpEntity<RestUser> createUserEntity = new HttpEntity<RestUser>(restUser, requestHeaders);
|
final URI location = createUser(requestHeaders, templateRest, restUser);
|
||||||
URI location = templateRest.postForLocation(BASE_REST_URL + "/admin/users", createUserEntity);
|
|
||||||
System.out.println("location:" + location);
|
|
||||||
|
|
||||||
// Check that the user has been created ...
|
// Check that the user has been created ...
|
||||||
|
ResponseEntity<RestUser> result = findUser(requestHeaders, templateRest, location);
|
||||||
|
assertEquals(result.getBody(), restUser, "Returned object object seems not be the same.");
|
||||||
|
|
||||||
|
|
||||||
|
// Find by email and check ...
|
||||||
|
result = findUserByEmail(requestHeaders, templateRest, email);
|
||||||
|
assertEquals(result.getBody(), restUser, "Returned object object seems not be the same.");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private ResponseEntity<RestUser> findUser(HttpHeaders requestHeaders, RestTemplate templateRest, URI location) {
|
||||||
HttpEntity<RestUser> findUserEntity = new HttpEntity<RestUser>(requestHeaders);
|
HttpEntity<RestUser> findUserEntity = new HttpEntity<RestUser>(requestHeaders);
|
||||||
final String url = "http://localhost:8080" + location;
|
final String url = "http://localhost:8080" + location;
|
||||||
final ResponseEntity<RestUser> result = templateRest.exchange(url, HttpMethod.GET, findUserEntity, RestUser.class);
|
return templateRest.exchange(url, HttpMethod.GET, findUserEntity, RestUser.class);
|
||||||
assertEquals(result.getBody(), restUser, "Returned object object seems not be the same.");
|
}
|
||||||
|
|
||||||
|
private ResponseEntity<RestUser> findUserByEmail(HttpHeaders requestHeaders, RestTemplate templateRest, final String email) {
|
||||||
|
HttpEntity<RestUser> findUserEntity = new HttpEntity<RestUser>(requestHeaders);
|
||||||
|
|
||||||
|
// Add extension only to avoid the fact that the last part is extracted ...
|
||||||
|
final String url = "http://localhost:8080/service/admin/users/email/{email}.json";
|
||||||
|
return templateRest.exchange(url, HttpMethod.GET, findUserEntity, RestUser.class, email);
|
||||||
|
}
|
||||||
|
|
||||||
|
private URI createUser(HttpHeaders requestHeaders, RestTemplate templateRest, RestUser restUser) {
|
||||||
|
HttpEntity<RestUser> createUserEntity = new HttpEntity<RestUser>(restUser, requestHeaders);
|
||||||
|
return templateRest.postForLocation(BASE_REST_URL + "/admin/users", createUserEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
private HttpHeaders createHeaders(MediaType mediaType) {
|
private HttpHeaders createHeaders(MediaType mediaType) {
|
||||||
|
Loading…
Reference in New Issue
Block a user