Fix test.

This commit is contained in:
Paulo Gustavo Veiga 2024-01-21 22:27:53 -08:00
parent 6ca26ffd10
commit bbb6f362bd
3 changed files with 47 additions and 214 deletions

View File

@ -19,34 +19,44 @@
package com.wisemapping.test.rest;
import com.wisemapping.config.Application;
import com.wisemapping.config.common.CommonConfig;
import com.wisemapping.config.rest.RestAppConfig;
import com.wisemapping.rest.AdminController;
import com.wisemapping.rest.MindmapController;
import com.wisemapping.rest.UserController;
import com.wisemapping.rest.model.RestUser;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
import java.net.URI;
import java.util.Collection;
import static com.wisemapping.test.rest.RestHelper.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
@SpringBootTest(classes = {RestAppConfig.class, CommonConfig.class, MindmapController.class, AdminController.class, UserController.class}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class RestAccountControllerTest {
private static final String ADMIN_USER = "admin@wisemapping.org";
private static final String ADMIN_PASSWORD = "test";
@SpringBootTest(classes = Application.class)
public class RestAccountITCase {
@Autowired
private TestRestTemplate restTemplate;
@Test
public void deleteUser() { // Configure media types ...
final HttpHeaders requestHeaders = createHeaders(MediaType.APPLICATION_JSON);
final RestTemplate adminTemplate = createTemplate(ADMIN_CREDENTIALS);
final TestRestTemplate adminRestTemplate = this.restTemplate.withBasicAuth(ADMIN_USER, ADMIN_PASSWORD);
final RestUser dummyUser = createDummyUser();
createUser(requestHeaders, adminTemplate, dummyUser);
createUser(requestHeaders, adminRestTemplate, dummyUser);
// Delete user ...
final RestTemplate dummyTemplate = createTemplate(dummyUser.getEmail() + ":fooPassword");
final TestRestTemplate dummyTemplate = this.restTemplate.withBasicAuth(dummyUser.getEmail(), "fooPassword");
dummyTemplate.delete(BASE_REST_URL + "/account");
// Is the user there ?
@ -58,11 +68,13 @@ public class RestAccountITCase {
// }
}
public String createNewUser(final @NotNull MediaType mediaType) {
@Test
public void createNewUser() {
// Configure media types ...
final HttpHeaders requestHeaders = createHeaders(mediaType);
final RestTemplate templateRest = createTemplate(ADMIN_CREDENTIALS);
final HttpHeaders requestHeaders = createHeaders(MediaType.APPLICATION_JSON);
final TestRestTemplate templateRest = this.restTemplate.withBasicAuth(ADMIN_USER, ADMIN_PASSWORD);
// Fill user data ...
final RestUser restUser = createDummyUser();
@ -77,27 +89,25 @@ public class RestAccountITCase {
// Find by email and check ...
result = findUserByEmail(requestHeaders, templateRest, restUser.getEmail());
assertEquals(result.getBody().getEmail(), restUser.getEmail(), "Returned object object seems not be the same.");
return restUser.getEmail();
}
private ResponseEntity<RestUser> findUser(HttpHeaders requestHeaders, RestTemplate templateRest, URI location) {
HttpEntity<RestUser> findUserEntity = new HttpEntity<RestUser>(requestHeaders);
private ResponseEntity<RestUser> findUser(HttpHeaders requestHeaders, TestRestTemplate templateRest, URI location) {
HttpEntity<RestUser> findUserEntity = new HttpEntity<>(requestHeaders);
final String url = HOST_PORT + location;
return templateRest.exchange(url, HttpMethod.GET, findUserEntity, RestUser.class);
}
private ResponseEntity<RestUser> findUserByEmail(HttpHeaders requestHeaders, RestTemplate templateRest, final String email) {
HttpEntity<RestUser> findUserEntity = new HttpEntity<RestUser>(requestHeaders);
private ResponseEntity<RestUser> findUserByEmail(HttpHeaders requestHeaders, TestRestTemplate templateRest, final String email) {
HttpEntity<RestUser> findUserEntity = new HttpEntity<>(requestHeaders);
// Add extension only to avoid the fact that the last part is extracted ...
final String url = BASE_REST_URL + "/admin/users/email/{email}";
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, requestHeaders);
private URI createUser(@NotNull HttpHeaders requestHeaders, TestRestTemplate templateRest, RestUser restUser) {
final HttpEntity<RestUser> createUserEntity = new HttpEntity<>(restUser, requestHeaders);
return templateRest.postForLocation(BASE_REST_URL + "/admin/users", createUserEntity);
}

View File

@ -1,153 +0,0 @@
/*
* Copyright [2022] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.wisemapping.test.rest;
import com.wisemapping.rest.model.RestUser;
import org.jetbrains.annotations.NotNull;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
import org.testng.annotations.Test;
import java.net.URI;
import static com.wisemapping.test.rest.RestHelper.*;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;
@Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function")
public class RestAdminITCase {
String authorisation = "admin@wisemapping.org" + ":" + "test";
@Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function")
public void changePassword(final @NotNull MediaType mediaType) { // Configure media types ...
final HttpHeaders requestHeaders = createHeaders(mediaType);
final RestTemplate templateRest = createTemplate(authorisation);
// Fill user data ...
final RestUser restUser = createDummyUser();
// 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);
templateRest.put(BASE_REST_URL + "/admin/users/{id}/password", createUserEntity, result.getBody().getId());
}
@Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function")
public void deleteUser(final @NotNull MediaType mediaType) { // Configure media types ...
final HttpHeaders requestHeaders = createHeaders(mediaType);
final RestTemplate templateRest = createTemplate(authorisation);
final RestUser restUser = createDummyUser();
// 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) {
}
}
public RestUser createNewUserAndGetUser(final @NotNull MediaType mediaType) {
// Configure media types ...
final HttpHeaders requestHeaders = createHeaders(mediaType);
final RestTemplate templateRest = createTemplate(authorisation);
// Fill user data ...
final RestUser restUser = createDummyUser();
// Create a new user ...
final URI location = createUser(requestHeaders, templateRest, restUser);
// Check that the user has been created ...
ResponseEntity<RestUser> result = findUser(requestHeaders, templateRest, location);
assertEquals(result.getBody().getEmail(), restUser.getEmail(), "Returned object object seems not be the same.");
return result.getBody();
}
public String createNewUser(final @NotNull MediaType mediaType) {
// Fill user data ...
final RestUser restUser = createNewUserAndGetUser(mediaType);
// Find by email and check ...
// @todo: review find by email... It's failing with 406
// findUser(requestHeaders, templateRest, location);
// result = findUserByEmail(requestHeaders, templateRest, restUser.getEmail());
// assertEquals(result.getBody().getEmail(), restUser.getEmail(), "Returned object object seems not be the same.");
return restUser.getEmail();
}
@Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function")
public void createUser(final @NotNull MediaType mediaType) {
this.createNewUser(mediaType);
}
private ResponseEntity<RestUser> findUser(HttpHeaders requestHeaders, RestTemplate templateRest, URI location) {
HttpEntity<RestUser> findUserEntity = new HttpEntity<RestUser>(requestHeaders);
final String url = HOST_PORT + location;
return templateRest.exchange(url, HttpMethod.GET, findUserEntity, RestUser.class);
}
public ResponseEntity<RestUser> findUserByEmail(HttpHeaders requestHeaders, RestTemplate templateRest, final String email) {
HttpEntity<RestUser> findUserEntity = new HttpEntity<>(requestHeaders);
// Add extension only to avoid the fact that the last part is extracted ...
final String url = BASE_REST_URL + "/admin/users/email/{email}";
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 RestUser createDummyUser() {
final RestUser restUser = new RestUser();
final String username = "foo-to-delete" + System.nanoTime();
final String email = username + "@example.org";
restUser.setEmail(email);
restUser.setFirstname("foo first name");
restUser.setLastname("foo last name");
restUser.setPassword("admin");
return restUser;
}
}

View File

@ -1,15 +1,12 @@
package com.wisemapping.test.rest;
import org.jetbrains.annotations.NotNull;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.security.crypto.codec.Base64;
import org.springframework.web.client.RestTemplate;
import org.testng.annotations.DataProvider;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;
@ -17,8 +14,6 @@ public class RestHelper {
public static final String HOST_PORT = "http://localhost:8081";
public static final String BASE_REST_URL = HOST_PORT + "/api/restfull";
public static final String ADMIN_CREDENTIALS = "admin@wisemapping.org" + ":" + "test";
public static final String COLOR = "#000000";
static HttpHeaders createHeaders(@NotNull MediaType mediaType) {
List<MediaType> acceptableMediaTypes = new ArrayList<>();
@ -30,23 +25,4 @@ public class RestHelper {
return result;
}
static RestTemplate createTemplate(@NotNull final String authorisation) {
SimpleClientHttpRequestFactory s = new SimpleClientHttpRequestFactory() {
@Override
protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
super.prepareConnection(connection, httpMethod);
byte[] encodedAuthorisation = Base64.encode(authorisation.getBytes());
connection.setRequestProperty("Authorization", "Basic " + new String(encodedAuthorisation));
}
};
return new RestTemplate(s);
}
@DataProvider(name = "ContentType-Provider-Function")
static Object[][] contentTypes() {
return new Object[][]{{MediaType.APPLICATION_JSON}};
}
}