Search in sources :

Example 11 with PilotUser

use of com.sanction.thunder.models.PilotUser in project thunder by RohanNagar.

the class ThunderClientTest method testGetUser.

@Test
@SuppressWarnings("ConstantConditions")
public void testGetUser() throws IOException {
    PilotUser response = client.getUser("email", password).execute().body();
    assertEquals(user.getEmail(), response.getEmail());
}
Also used : PilotUser(com.sanction.thunder.models.PilotUser) Test(org.junit.Test)

Example 12 with PilotUser

use of com.sanction.thunder.models.PilotUser in project thunder by RohanNagar.

the class ThunderClientTest method testUpdateUser.

@Test
@SuppressWarnings("ConstantConditions")
public void testUpdateUser() throws IOException {
    PilotUser response = client.updateUser(user, "email", password).execute().body();
    assertEquals(user.getEmail(), response.getEmail());
}
Also used : PilotUser(com.sanction.thunder.models.PilotUser) Test(org.junit.Test)

Example 13 with PilotUser

use of com.sanction.thunder.models.PilotUser 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 14 with PilotUser

use of com.sanction.thunder.models.PilotUser 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)

Example 15 with PilotUser

use of com.sanction.thunder.models.PilotUser in project thunder by RohanNagar.

the class UserResource method updateUser.

/**
 * Updates a PilotUser in the database.
 *
 * @param key The basic authentication key necessary to access the resource.
 * @param password The password of the user to update. This should be the current password
 *                 before any updates are made. Used to verify the ability to edit the user.
 * @param existingEmail The existing email for the user. This can be {@code null} if the email
 *                     will stay the same. It must be present if the email is to be changed.
 * @param user The PilotUser object with updated properties.
 * @return The pilotUser that was updated in the database.
 */
@PUT
public Response updateUser(@Auth Key key, @HeaderParam("password") String password, @QueryParam("email") String existingEmail, PilotUser user) {
    updateRequests.mark();
    if (user == null) {
        LOG.warn("Attempted to update a null user.");
        return Response.status(Response.Status.BAD_REQUEST).entity("Cannot put a null user.").build();
    }
    if (user.getEmail() == null) {
        LOG.warn("Attempted to update user without an email object.");
        return Response.status(Response.Status.BAD_REQUEST).entity("Cannot post a user without an email address.").build();
    }
    // Get the current email address for the user
    String email = existingEmail != null ? existingEmail : user.getEmail().getAddress();
    LOG.info("Attempting to update existing user with email address {}.", email);
    if (!isValidEmail(user.getEmail().getAddress())) {
        LOG.error("The new email address is invalid: {}", user.getEmail());
        return Response.status(Response.Status.BAD_REQUEST).entity("Invalid email address format. Please try again.").build();
    }
    if (password == null || password.isEmpty()) {
        LOG.warn("Attempted to update user {} without a password.", email);
        return Response.status(Response.Status.BAD_REQUEST).entity("Incorrect or missing header credentials.").build();
    }
    PilotUser foundUser;
    try {
        foundUser = 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 the password is correct for the user to update
    if (!foundUser.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.update(existingEmail, user);
    } catch (DatabaseException e) {
        LOG.error("Error updating user {} in database. Caused by: {}", email, e.getErrorKind());
        return e.getErrorKind().buildResponse(email);
    }
    LOG.info("Successfully updated user {}.", email);
    return Response.ok(result).build();
}
Also used : PilotUser(com.sanction.thunder.models.PilotUser) DatabaseException(com.sanction.thunder.dao.DatabaseException) PUT(javax.ws.rs.PUT)

Aggregations

PilotUser (com.sanction.thunder.models.PilotUser)26 Test (org.junit.Test)20 Response (javax.ws.rs.core.Response)9 DatabaseException (com.sanction.thunder.dao.DatabaseException)6 Email (com.sanction.thunder.models.Email)4 Expected (com.amazonaws.services.dynamodbv2.document.Expected)3 Item (com.amazonaws.services.dynamodbv2.document.Item)3 DeleteItemSpec (com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec)2 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