Chapter 3 Ticket - User Management - User Login Test Fails

I’m really hung up on this section of the chapter. All tests pass except TestUserLogin which fails with the msg: Multiple failures or warnings in test:

  1. Expected: not null
    But was: null

  2. Expected: not null
    But was: null

Trying to track down the issue - user is NOT in the users collection and I don’t see where they would get added if the login routine doesn’t find them. Here is my implementation of LoginUserAsync:

        public async Task<UserResponse> LoginUserAsync(User user, CancellationToken cancellationToken = default)
                var storedUser = await GetUserAsync(user.Email, cancellationToken);
                if (storedUser == null)
                    return new UserResponse(false, "No user found. Please check the email address.");
                if (user.HashedPassword != null && user.HashedPassword != storedUser.HashedPassword)
                    return new UserResponse(false, "The hashed password provided is not valid");
                if (user.HashedPassword == null && !PasswordHashOMatic.Verify(user.Password, storedUser.HashedPassword))
                    return new UserResponse(false, "The password provided is not valid");

                // TODO Ticket: User Management
                // Locate the session object in the `sessions` collection by
                // matching the "user_id" field with the email passed to this function.
                // Then update the Session.UserId and Session.Jwt properties,
                // setting the former to the email and the latter to the
                // user.AuthToken that is passed in from the Controller.
                var sessionFilter = Builders<Session>.Filter.Eq(s => s.UserId, user.Email);
                BsonDocument bsonUserFilter = new BsonDocument();
                bsonUserFilter.Add(new BsonElement("email", user.Email));
                await _sessionsCollection.UpdateOneAsync(
                    Builders<Session>.Update.Set(s => s.UserId, user.Id).Set(s => s.Jwt, user.AuthToken),
                    new UpdateOptions() { IsUpsert = true },
                // If the session doesn't exist, allow MongoDB to create a
                // new one by passing the IsUpsert update option.
                //  await _sessionsCollection.UpdateOneAsync(
                //  new BsonDocument(...),
                //  Builders<Session>.Update.Set(...).Set(...),
                //  new UpdateOptions(...));
                storedUser.AuthToken = user.AuthToken;
                return new UserResponse(storedUser);
            catch (Exception ex)
                return new UserResponse(false, ex.Message);

Back at it this morning to try to debug this and I cnanot find the problem. Here is the code from the test:

        public async Task TestLogsInUser()
            var jwt = new JwtAuthentication
                SecurityKey = "ouNtF8Xds1jE55/d+iVZ99u0f2U6lQ+AHdiPFwjVW3o=",
                ValidAudience = "https://localhost:5000/",
                ValidIssuer = "https://localhost:5000/"

            var user = new User { Name = "Test User 2", Email = "", Password = "hdfn123?", AuthToken = jwt.SecurityKey };
            //var user = new User { Name = "Test User 2", Email = "", Password = "hdfn123?" };

                CancellationToken cancellationToken = new CancellationToken();
                var result = await _userRepository.LoginUserAsync(user, cancellationToken);
                Assert.AreEqual("No user found. Please check the email address.",
                var addUserResult = await _userRepository.AddUserAsync(user.Name, user.Email, user.Password);
                result = await _userRepository.LoginUserAsync(user);
                Assert.AreEqual("Test User 2", result.User.Name);
            catch (Exception e)
                await _userRepository.LogoutUserAsync(user.Email);
                await _userRepository.DeleteUserAsync(user.Email);

Holy crap. I went backward breaking user management validation before I finally tracked the problem. This is by far the most frustrating learning module I’ve ever experienced.

If I wasn’t required to get MongoDB certification, I would have been out of here in the previous module.

Horrible examples with zero help in the forums.

I solved the problem after six hours. Thanks for the help.

Hi @Chris_Scharf, welcome to the community :wave: .
We understand our courses are out of date and have issues, we are in fact working to update them and you can expect to see the new courses by November 15th. Please stay tuned on

If you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,