Ticket Create/Update Comments: Can't validate my code

Hey everybody,
I’m kind of at the end of my rope.
I’m working on " Ticket: Create/Update Comments". All my unit tests are green and working, but if I try to validate it i get the error message “Create/Update Comments: Unable to update comment”
Any suggestions or hints what the problem could be?

1 Like

Please share the errors you see in console of your command line when you run status page.

Kanika

If you do not have any errors, the most likely cause is that you do not sort the comments as specified in the requirements.

when running tests about add/update and delete comments, no errors on console, but when clicking later on some other tests, i see such errors:

Cannot validate user token Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1MjIxNzI3NzMsIm5iZiI6MTUyMjE3Mjc3MywianRpIjoiYjFlYmI0ZDQtNjZlZS00MTY4LTg0MWQtZGNhODJkMThmN2NhIiwiZXhwIjoxNTIyMTczNjczLCJpZGVudGl0eSI6eyJlbWFpbCI6ImZvb2JhekBiYXIuY29tIiwibmFtZSI6ImZvbyBiYXIiLCJwYXNzd29yZCI6bnVsbCwicHJlZmVyZW5jZXMiOnsiZmF2b3JpdGVfY2FzdCI6Ik1lZyBSeWFuIiwicHJlZmVycmVkX2xhbmd1YWdlIjoiRW5nbGlzaCJ9fSwiZnJlc2giOmZhbHNlLCJ0eXBlIjoiYWNjZXNzIiwidXNlcl9jbGFpbXMiOnsidXNlciI6eyJlbWFpbCI6ImZvb2JhekBiYXIuY29tIiwibmFtZSI6ImZvbyBiYXIiLCJwYXNzd29yZCI6bnVsbCwicHJlZmVyZW5jZXMiOnsiZmF2b3JpdGVfY2FzdCI6Ik1lZyBSeWFuIiwicHJlZmVycmVkX2xhbmd1YWdlIjoiRW5nbGlzaCJ9fX19.q9z_tG7gEqaRMfrbTpj9Jz52vocqOBWgEpCd3KC6giI: error thrown - JWT signature does not match locally computed signature. JWT validity cannot be asserted and should not be trusted.

Servlet.service() for servlet [dispatcherServlet] in context with path threw exception [Request processing failed; nested exception is com.mongodb.MongoWriteException: E11000 duplicate key error collection: mflix.users index: email_1 dup key: { : “2epztbm45@gprdf.ij0” }] with root cause

com.mongodb.MongoWriteException: E11000 duplicate key error collection: mflix.users index: email_1 dup key: { : “2epztbm45@gprdf.ij0” }
at com.mongodb.client.internal.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:967) ~[mongodb-driver-sync-3.9.1.jar:na]
at com.mongodb.client.internal.MongoCollectionImpl.executeInsertOne(MongoCollectionImpl.java:494) ~[mongodb-driver-sync-3.9.1.jar:na]
at com.mongodb.client.internal.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:478) ~[mongodb-driver-sync-3.9.1.jar:na]
at com.mongodb.client.internal.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:472) ~[mongodb-driver-sync-3.9.1.jar:na]
at mflix.api.daos.UserDao.addUser(UserDao.java:62) ~[classes/:na]
at mflix.api.services.UserService.createUser(UserService.java:76) ~[classes/:na]
at mflix.api.services.UserService.createUser(UserService.java:48) ~[classes/:na]
at mflix.api.controllers.UserController.register(UserController.java:59) ~[classes/:na]

Between, UpdateCreateCommentTest and DeleteCommentTest pass all tests with green

there are no errors in my command line, if I run the status page or when i click on the validation for create/update comments.

Thank you! That was it! :slight_smile:

Where is the specification to the sort the comments? I cannot see something like this in the requirements for Chapter 2: User-Facing Backend Ticket: Create/Update Comments.

I’ve been debugging the mflix.api.services.MoviesService.updateMovieComment which is being called (except of other methods) by this ticket validation from client side, and I can clearly see that the provided commentId and email are not matching previously created comments. I mean it seems that test comment is created by one email and then the updateMovieComment method receives as parameter some other email. It actually testing the correct behaviour that a user cannot modify a comment of other user. Therefore my unit tests are green.

How is even possible, guys from MongoDb University, that once I’ve implement the XYZDao logic and unit tests are green, that the validation from MFlix application turns red. Are we, as students supposed to debug the code made by you, which is supposed to work as a charm? Seriously?

This is the second time I have to forcibly debug your code. You have some other bug related to User Preferences validation.

1 Like

@Marina_37816 please check your implementation of mflix.api.daos.UserDao#createUserSession This method is called actually twice by the backend code when the client side ticket validation executes. Although you should not create two user session entries for the same email and jwt, this method should always return true, unless some exception is thrown. This is my current implementation:

  public boolean createUserSession(String userId, String jwt) {
    log.info("Creating user session {} with jwt {}", userId, jwt);
    //TODO > Ticket: Handling Errors - implement a safeguard against
    // creating a session with the same jwt token.
    Session userSession = sessionsCollection.find(Filters.eq("user_id", userId)).first();
    if (userSession == null) {
      //TODO> Ticket: User Management - implement the method that allows session information to be
      // stored in it's designated collection.
      userSession = new Session();
      userSession.setUserId(userId);
      userSession.setJwt(jwt);
      sessionsCollection.insertOne(userSession);
    }
    return true;
  }

As you can see the safeguard for JWT is not yet implemented, will be in Ticket: Handling Errors.

In Chapter 2: User-Facing Backend - Ticket: Get Comments:

The comments should be returned in order from most recent to least recent using the date key.

1 Like

I had changed implementation of createUserSession a bit:

Bson filter = Filters.or(
Filters.eq(“user_id”, userId),
Filters.eq(“jwt”, jwt)
);
Session userSession = sessionsCollection.find(filter).first();

if (userSession == null) {

  //TODO> Ticket: User Management - implement the method that allows session information to be

  // stored in it's designated collection.

  userSession = new Session();

  userSession.setUserId(userId);

  userSession.setJwt(jwt);

  sessionsCollection.insertOne(userSession);

}

but all the same,
while mvn test -Dtest=UpdateCreateCommentTest,
mvn test -Dtest=UserTest,
mvn test -Dtest=DeleteCommentTest
do pass
I have in status page “Create/Update comments” and "“delete comments” turn red, can not get valisation code, while on console no any errors

and

I will try to implement what @steevej-1495 was suggesting, he’s actually right. But I have overlooked somehow this requirement. If that helps to turn validation codes to green I will let you know.

1 Like

@steevej-1495 Thank you, you were absolutely right. Such a tiny detail. Especially when all unit tests were green. I still think that if the ordering of joined comments is so important, for next phases, it should be covered by unit tests. So I finally managed to get green validation codes for Create/Update/Delete comments.

1 Like

@Marina_37816 the descending order (by date) of joined comments into the movie is crucial for getting green validation codes for create/update/delete comments. Let me know if you will struggle to implement that sort.

2 Likes

Yes, that indeed helped. I struggled whole day to get syntax for $lookup with let, pipeline, sort within right.

Same situation here (user different from that of the comment). I don’t see what this has to do with sorting the comments for a movie?!
The unit tests should have notified me if a previous part was not implemented correctly.
Any (more) pointers as to how to fix this? I am really stuck here…

Hi @Aad_17388,

Please let me know if this does not help:

Kanika

@Aad_17388 I agree with you that unit tests should completely cover the requirements given by the specific Ticket. Unfortunately this is not the case, maybe it’s the intention of the course creators. However in this particular case to be able to get the validation codes for Create/Update/Delete comments, you really have to get back to ticket Get Comments and update the MovieDao.getMovie() method. This method really needs to return a movie with ordered comments. For that you will need to leverage the power of $lookup, ‘let’ and ‘pipeline’. Please try to first figure it out by yourself, and if really not possible check my gist.

Ok, thanks. I will check it out…

@mbranicky @steevej-1495 Thank you for the comment. Without it, I completely lost what’s going on.