Ticket User Management delete

Hi, i am having a problem with the deleteUser method.
The error that i receive is this:
UserTest.testDeleteUser:111 Should not find any sessions after deleting a user. deleteUser() method needs to remove the user sessions data! expected null, but was:mflix.api.models.Session@36cc9385

and the code used is this:

public boolean createUserSession(String userId, String jwt) {
    //TODO> Ticket: User Management - implement the method that allows session information to be
    // stored in it's designated collection.
	Document session = new Document("user_id",userId);
	session.append("jwt", jwt);
	if(getUserSession(userId)!= null && getUserSession(userId).getJwt()!=null && getUserSession(userId).getJwt().equals(jwt)) {
		return false;
	}else if (getUser(userId).getEmail()==null) {
		return false;
	}
	sessionsCollection.insertOne(session);
	return true;
    //TODO > Ticket: Handling Errors - implement a safeguard against
    // creating a session with the same jwt token.
}

public User getUser(String email) {
    User user = null;
    
    List<Bson> pipeline = new ArrayList<>();
    // match stage to find movie
    Bson match = Aggregates.match(Filters.eq("email", email));
    pipeline.add(match);
    user = usersCollection.aggregate(pipeline).first();
    //TODO> Ticket: User Management - implement the query that returns the first User object.
    return user;
}


public Session getUserSession(String userId) {
    //TODO> Ticket: User Management - implement the method that returns Sessions for a given
    // userId
	Session session = new Session();
    
	Bson filter = Filters.eq("user_id",userId);
    Document a  = sessionsCollection.find(filter).first();
    if(a!=null) {
    	session.setUserId((String) a.get("user_id"));
    	session.setJwt((String) a.get("jwt"));
    }
    return session;

}

public boolean deleteUserSessions(String userId) {
    //TODO> Ticket: User Management - implement the delete user sessions method
	
	Bson match = Filters.eq("user_id", userId); 
	if(getUserSession(userId).getUserId()!=null) {
		sessionsCollection.deleteMany(match);
		return true;
	}
    return false;
}


public boolean deleteUser(String email) {
    // remove user sessions
    //TODO> Ticket: User Management - implement the delete user method
    //TODO > Ticket: Handling Errors - make this method more robust by
    // handling potential exceptions.
	deleteUserSessions(email);
	Bson filterUser = Filters.eq("email",email);
	if(getUser(email).getEmail()!=null) {
		usersCollection.deleteMany(filterUser);
		return true;
	}
    return false;
}

any suggestions?
Thank you

Hello Oliviero Ferrari and welcome to mongodb community,

I think there is nothing wrong about your code.
The fail is caused by the unit test clean up. Without further ado, manually delete the session having email "gryffindor@hogwarts.edu"

Regards,

I am little bit skeptical about your getUserSession(). You will return a Session object even if there is no session for the user. If you look at the unit test testLogout() from UserTest.java, they expect a null when there is no session.

It is a bad practice to do 2 round trips to the database to perform an operation. For example, deleteUserSessions() first call getUserSession() but you should simply deleteMany() and verify the result of the delete.

2 Likes