Search in sources :

Example 6 with DatabaseException

use of com.sanction.thunder.dao.DatabaseException in project thunder by RohanNagar.

the class UserResourceTest method testPostUserDatabaseDown.

@Test
public void testPostUserDatabaseDown() {
    when(usersDao.insert(any(PilotUser.class))).thenThrow(new DatabaseException(DatabaseError.DATABASE_DOWN));
    Response response = resource.postUser(key, user);
    assertEquals(Response.Status.SERVICE_UNAVAILABLE, response.getStatusInfo());
}
Also used : Response(javax.ws.rs.core.Response) PilotUser(com.sanction.thunder.models.PilotUser) DatabaseException(com.sanction.thunder.dao.DatabaseException) Test(org.junit.Test)

Example 7 with DatabaseException

use of com.sanction.thunder.dao.DatabaseException in project thunder by RohanNagar.

the class VerificationResourceTest method testVerifyEmailFindUserException.

@Test
public void testVerifyEmailFindUserException() {
    when(usersDao.findByEmail(anyString())).thenThrow(new DatabaseException(DatabaseError.DATABASE_DOWN));
    Response response = resource.verifyEmail("test@test.com", "verificationToken", ResponseType.JSON);
    assertEquals(response.getStatusInfo(), Response.Status.SERVICE_UNAVAILABLE);
}
Also used : Response(javax.ws.rs.core.Response) DatabaseException(com.sanction.thunder.dao.DatabaseException) Test(org.junit.Test)

Example 8 with DatabaseException

use of com.sanction.thunder.dao.DatabaseException in project thunder by RohanNagar.

the class VerificationResourceTest method testVerifyUserFindUserException.

@Test
public void testVerifyUserFindUserException() {
    when(usersDao.findByEmail(anyString())).thenThrow(new DatabaseException(DatabaseError.DATABASE_DOWN));
    Response response = resource.createVerificationEmail(key, "test@test.com", "password");
    assertEquals(response.getStatusInfo(), Response.Status.SERVICE_UNAVAILABLE);
}
Also used : Response(javax.ws.rs.core.Response) DatabaseException(com.sanction.thunder.dao.DatabaseException) Test(org.junit.Test)

Example 9 with DatabaseException

use of com.sanction.thunder.dao.DatabaseException in project thunder by RohanNagar.

the class UserResource method deleteUser.

/**
 * Deletes a PilotUser from the database.
 *
 * @param key The basic authentication key necessary to access the resource.
 * @param password The password of the user to delete. Used to verify authentication.
 * @param email The email of the user to delete.
 * @return The user that was deleted from the database.
 */
@DELETE
public Response deleteUser(@Auth Key key, @HeaderParam("password") String password, @QueryParam("email") String email) {
    deleteRequests.mark();
    if (email == null || email.isEmpty()) {
        LOG.warn("Attempted to delete a null user.");
        return Response.status(Response.Status.BAD_REQUEST).entity("Incorrect or missing email query parameter.").build();
    }
    if (password == null || password.isEmpty()) {
        LOG.warn("Attempted to delete user {} without a password.", email);
        return Response.status(Response.Status.BAD_REQUEST).entity("Incorrect or missing header credentials.").build();
    }
    LOG.info("Attempting to delete user {}.", email);
    PilotUser user;
    try {
        user = usersDao.findByEmail(email);
    } catch (DatabaseException e) {
        LOG.error("Error retrieving user {} in database. Caused by: {}", email, e.getErrorKind());
        return e.getErrorKind().buildResponse(email);
    }
    // Check that password is correct before deleting
    if (!user.getPassword().equals(password)) {
        LOG.error("The password for user {} was incorrect.", email);
        return Response.status(Response.Status.UNAUTHORIZED).entity("Unable to validate user with provided credentials.").build();
    }
    PilotUser result;
    try {
        result = usersDao.delete(email);
    } catch (DatabaseException e) {
        LOG.error("Error deleting user {} in database. Caused by: {}", email, e.getErrorKind());
        return e.getErrorKind().buildResponse(email);
    }
    LOG.info("Successfully deleted user {}.", email);
    return Response.ok(result).build();
}
Also used : PilotUser(com.sanction.thunder.models.PilotUser) DatabaseException(com.sanction.thunder.dao.DatabaseException) DELETE(javax.ws.rs.DELETE)

Example 10 with DatabaseException

use of com.sanction.thunder.dao.DatabaseException in project thunder by RohanNagar.

the class UserResource method postUser.

/**
 * Posts a new PilotUser to the database.
 *
 * @param key The basic authentication key necessary to access the resource.
 * @param user The user to post to the database.
 * @return The user that was created in the database.
 */
@POST
public Response postUser(@Auth Key key, PilotUser user) {
    postRequests.mark();
    if (user == null) {
        LOG.warn("Attempted to post a null user.");
        return Response.status(Response.Status.BAD_REQUEST).entity("Cannot post a null user.").build();
    }
    if (user.getEmail() == null) {
        LOG.warn("Attempted to post a user with a null Email object.");
        return Response.status(Response.Status.BAD_REQUEST).entity("Cannot post a user without an email address.").build();
    }
    LOG.info("Attempting to create new user {}.", user.getEmail().getAddress());
    if (!isValidEmail(user.getEmail().getAddress())) {
        LOG.error("The new user has an invalid email address: {}", user.getEmail());
        return Response.status(Response.Status.BAD_REQUEST).entity("Invalid email address format. Please try again.").build();
    }
    // Update the user to non-verified status
    PilotUser updatedUser = new PilotUser(new Email(user.getEmail().getAddress(), false, null), user.getPassword(), user.getFacebookAccessToken(), user.getTwitterAccessToken(), user.getTwitterAccessSecret());
    PilotUser result;
    try {
        result = usersDao.insert(updatedUser);
    } catch (DatabaseException e) {
        LOG.error("Error posting user {} to the database. Caused by {}", user.getEmail(), e.getErrorKind());
        return e.getErrorKind().buildResponse(user.getEmail().getAddress());
    }
    LOG.info("Successfully created new user {}.", user.getEmail());
    return Response.status(Response.Status.CREATED).entity(result).build();
}
Also used : Email(com.sanction.thunder.models.Email) PilotUser(com.sanction.thunder.models.PilotUser) DatabaseException(com.sanction.thunder.dao.DatabaseException) POST(javax.ws.rs.POST)

Aggregations

DatabaseException (com.sanction.thunder.dao.DatabaseException)25 Response (javax.ws.rs.core.Response)19 Test (org.junit.Test)19 PilotUser (com.sanction.thunder.models.PilotUser)10 Email (com.sanction.thunder.models.Email)3 GET (javax.ws.rs.GET)2 POST (javax.ws.rs.POST)2 URI (java.net.URI)1 StringJoiner (java.util.StringJoiner)1 DELETE (javax.ws.rs.DELETE)1 PUT (javax.ws.rs.PUT)1