Skip to content

Commit

Permalink
fix error reports
Browse files Browse the repository at this point in the history
  • Loading branch information
cpesch committed Oct 1, 2023
1 parent acc43df commit 49800e9
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,23 @@
public class RouteFeedback {
private static final Logger log = Logger.getLogger(RouteFeedback.class.getName());

private static final String ERROR_REPORT_URI = "error-report/";
private static final String UPDATE_CHECK_URI = "update-check/";
static final String USER_URI = V1 + "users/";
private static final String ERROR_REPORT_URI = V1 + "error-report/";
private static final String UPDATE_CHECK_URI = V1 + "update-check/";
private static final String USER_URI = V1 + "users/";

private final String rootUrl;
private final String apiUrl;
private final Credentials credentials;

public RouteFeedback(String rootUrl, String apiUrl, Credentials credentials) {
this.rootUrl = rootUrl;
public RouteFeedback(String apiUrl, Credentials credentials) {
this.apiUrl = apiUrl;
this.credentials = credentials;
}

public String addUser(String userName, String password, String firstName, String lastName, String email) throws IOException {
String userUrl = apiUrl + USER_URI;

log.info("Adding user " + userName + "," + firstName + "," + lastName + "," + email);
Post request = new Post(apiUrl + USER_URI);
Post request = new Post(userUrl);
request.setAccept(APPLICATION_JSON);
request.addString("username", userName);
request.addString("password", password);
Expand All @@ -82,11 +82,11 @@ public String addUser(String userName, String password, String firstName, String
request.addString("email", email);
String result = request.executeAsString();
if (request.isBadRequest())
throw new ForbiddenException("Cannot add user: " + result, apiUrl + USER_URI);
throw new ForbiddenException("Cannot add user: " + result, userUrl);
if (request.isForbidden())
throw new DuplicateNameException("User " + userName + " already exists", apiUrl + USER_URI);
throw new DuplicateNameException("User " + userName + " already exists", userUrl);
if (!request.isSuccessful())
throw new IOException("POST on " + (apiUrl + USER_URI) + " with payload " + userName + "," + firstName + "," + lastName + "," + email + " not successful: " + result);
throw new IOException("POST on " + userUrl + " with payload " + userName + "," + firstName + "," + lastName + "," + email + " not successful: " + result);
return request.getLocation();
}

Expand All @@ -101,24 +101,22 @@ void deleteUser(String userUrl) throws IOException {
throw new IOException("DELETE on " + userUrl + " not successful: " + result);
}

private String getErrorReportUrl() {
return rootUrl + ERROR_REPORT_URI;
}

public String sendErrorReport(String logOutput, String description, java.io.File file) throws IOException {
String errorReportUrl = apiUrl + ERROR_REPORT_URI;

log.fine("Sending error report with log \"" + logOutput + "\", description \"" + description + "\"" +
(file != null ? ", file " + file.getAbsolutePath() : ""));
Post request = new Post(getErrorReportUrl(), credentials);
Post request = new Post(errorReportUrl, credentials);
request.addString("log", logOutput);
request.addString("description", description);
if (file != null)
request.addFile("file", file);

String result = request.executeAsString();
if (request.isUnAuthorized())
throw new UnAuthorizedException("Cannot send error report " + (file != null ? ", file " + file.getAbsolutePath() : ""), getErrorReportUrl());
throw new UnAuthorizedException("Cannot send error report " + (file != null ? ", file " + file.getAbsolutePath() : ""), errorReportUrl);
if (!request.isSuccessful())
throw new IOException("POST on " + getErrorReportUrl() + " with log " + logOutput.length() + " characters" +
throw new IOException("POST on " + errorReportUrl + " with log " + logOutput.length() + " characters" +
", description \"" + description + "\", file " + file + " not successful: " + result);
return request.getLocation();
}
Expand All @@ -128,7 +126,7 @@ public String checkForUpdate(String routeConverterVersion, String routeConverter
String osName, String osVersion, String osArch,
long startTime) throws IOException {
log.fine("Checking for update for version " + routeConverterVersion);
Post request = new Post(rootUrl + UPDATE_CHECK_URI, credentials);
Post request = new Post(apiUrl + UPDATE_CHECK_URI, credentials);
request.addString("id", valueOf(startTime));
request.addString("javaBits", javaBits);
request.addString("javaVersion", javaVersion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,26 @@
import slash.navigation.rest.SimpleCredentials;
import slash.navigation.rest.exception.ForbiddenException;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import static java.lang.System.currentTimeMillis;
import static org.junit.Assert.assertTrue;
import static slash.navigation.feedback.domain.RouteFeedback.USER_URI;
import static slash.common.io.InputOutput.copyAndClose;

public class RouteFeedbackIT {
private static final String API = System.getProperty("api", "http://localhost:8000/");
public class RouteFeedbackIT extends RouteFeedbackServiceBase {
private static final String SUPER_USERNAME = "super";
private static final String PASSWORD = "test";
private static final String UMLAUTS = "\u00E4\u00F6\u00FC\u00DF\u00C4\u00D6\u00DC";
private static final String SPECIAL_CHARACTERS = "@!§$%&()=";
private static final String UMLAUTS = "\u00E4\u00F6\u00FC\u00DF\u00C4\u00D6\u00DC";

private RouteFeedback routeFeedback;
private String url;

@Before
public void setUp() {
routeFeedback = new RouteFeedback(null, API, null);
routeFeedback = new RouteFeedback(API, new SimpleCredentials(USERNAME, PASSWORD));
}

@After
Expand All @@ -53,8 +54,8 @@ public void tearDown() throws IOException {
}

private void deleteAsSuperuser(String url) throws IOException {
RouteFeedback superUser = new RouteFeedback(null, API, new SimpleCredentials(SUPER_USERNAME, PASSWORD));
superUser.deleteUser(url);
RouteFeedback superUser = new RouteFeedback(API, new SimpleCredentials(SUPER_USERNAME, PASSWORD));
superUser.deleteUser(API + "v1/" + url);
}

@Test
Expand All @@ -63,19 +64,21 @@ public void testCanAddUserWithUmlauts() throws IOException {
url = routeFeedback.addUser(userName, userName, "egal", "egal", "[email protected]");
}

@Test
public void testCanAddUserWithSpecialCharacters() throws IOException {
@Test(expected = ForbiddenException.class)
public void testCannotAddUserWithSpecialCharacters() throws IOException {
String userName = "Specials" + SPECIAL_CHARACTERS + currentTimeMillis();
url = routeFeedback.addUser(userName, userName, "egal", "egal", "[email protected]");
}

@Test
public void testSuperuserCanDeleteOtherUser() throws IOException {
String userName = "OtherUser" + currentTimeMillis();
String url = routeFeedback.addUser(userName, userName, "Other", "User", "[email protected]");
assertTrue(url.startsWith(API + USER_URI));
url = routeFeedback.addUser(userName, userName, "Other", "User", "[email protected]");
assertTrue(url.contains("users"));

deleteAsSuperuser(url);

url = null;
}

@Test(expected = ForbiddenException.class)
Expand Down Expand Up @@ -107,6 +110,35 @@ public void testCannotAddUserWithSameName() throws IOException {
public void testCannotDeleteOtherUser() throws IOException {
String userName = "TestUser" + currentTimeMillis();
url = routeFeedback.addUser(userName, userName, "Test", "User", "[email protected]");
routeFeedback.deleteUser(url);
routeFeedback.deleteUser(API + "v1/" + url);
}

@Test
public void testCanSendErrorReport() throws IOException {
InputStream input = getClass().getResourceAsStream("errorreporttest.txt");
File file = File.createTempFile("errorreport", ".txt");
file.deleteOnExit();
copyAndClose(input, new FileOutputStream(file));

String url = routeFeedback.sendErrorReport("log output", "description", file);
assertTrue(url.contains("error-report"));
}

@Test
public void testCanCheckForUpdateAnonymous() throws IOException {
RouteFeedback anonymous = new RouteFeedback(API, null);
String result = anonymous.checkForUpdate("1",
"2", 3, "4", "5", "6",
"7", "8", "9", 10);
assertTrue(result.contains("version"));
assertTrue(result.contains("feature"));
}

@Test
public void testCanCheckForUpdate() throws IOException {
String result = routeFeedback.checkForUpdate("2",
"3", 4, "5", "6", "7",
"8", "9", "10", 11);
assertTrue(result.contains("feature"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,14 @@
import slash.navigation.rest.SimpleCredentials;

public abstract class RouteFeedbackServiceBase {
protected static final String FEEDBACK = System.getProperty("feedback", "http://localhost:8000/feedback/");
protected static final String API = System.getProperty("api", "http://localhost:8000/");
protected static final String USERNAME = "test";
protected static final String PASSWORD = "test";
protected static final String UMLAUTS = "\u00E4\u00F6\u00FC\u00DF\u00C4\u00D6\u00DC";

protected RouteFeedback routeFeedback;

@Before
public void setUp() {
routeFeedback = new RouteFeedback(FEEDBACK, API, new SimpleCredentials(USERNAME, PASSWORD));
routeFeedback = new RouteFeedback(API, new SimpleCredentials(USERNAME, PASSWORD));
}
}
20 changes: 0 additions & 20 deletions feedback/src/test/resources/userstest.gpx

This file was deleted.

3 changes: 1 addition & 2 deletions rest/src/main/java/slash/navigation/rest/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ HttpRequestBase getMethod() {
}

private void setAuthentication(String userName, String password, URI uri) {
int port = uri.getScheme().equals("https") ? 443 : 80;
HttpHost httpHost = new HttpHost(uri.getHost(), port, uri.getScheme());
HttpHost httpHost = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
AuthScope authScope = new AuthScope(httpHost, "api", null);
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(authScope, new UsernamePasswordCredentials(userName, password));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,7 @@ private void enableActions() {

protected void initializeServices() {
System.setProperty("rest", parseVersionFromManifest().getVersion());
RouteFeedback routeFeedback = new RouteFeedback(System.getProperty("feedback", "https://www.routeconverter.com/feedback/"), getApiUrl(), RouteConverter.getInstance().getCredentials());
RouteFeedback routeFeedback = new RouteFeedback(getApiUrl(), RouteConverter.getInstance().getCredentials());
routeServiceOperator = new RouteServiceOperator(getFrame(), routeFeedback);
updateChecker = new UpdateChecker(routeFeedback);
DownloadManager downloadManager = new DownloadManager(new File(getApplicationDirectory(), getEditionId() + "-queue.xml"));
Expand Down

0 comments on commit 49800e9

Please sign in to comment.