m220P User Management Ticket

I am doing this ticket and the lecture leading up to it is TERRIBLE!!! No where. does it cover how to make your writes more durable…

# TODO: User Management
        # Insert a user with the "name", "email", and "password" fields.
        # TODO: Durable Writes
        # Use a more durable Write Concern for this operation.

Along with that, there is no mention of how to use the jwt token… I assume that it is the set field in the session collection.

There is no way to test your code without completing all of the functions (that I know of)… Having said that, I ran the test and the first two fail. I looked at the test and also the output from the failed test and it seems to be failing on the delete user portion. There seems to already be an entry with the test data in the system…

This is really confusing on how to troubleshoot.

def test_registration(client):
        delete_user('magicz@cats.com')
        # the password will be hashed at the api layer
        # NEVER
        # NEVER
        # NEVER store passwords in plaintext
    
        result = add_user(test_user.get('name'), test_user.get(
            'email'), test_user.get('password'))
    
>       assert result == {'success': True}
E       AssertionError: assert {'error': 'A ...eady exists.'} == {'success': True}
E         Left contains more items:
E         {'error': 'A user with the given email already exists.'}
E         Right contains more items:
E         {'success': True}
E         Use -v to get the full diff

tests/test_user_management.py:24: AssertionError

This is my output on the first test… How do I troubleshoot this?

Because there is so many functions that need to be updated to make the ticket work, how do I ask questions without a lengthy and complicated queston?

I will start by debugging my add_user(name, email, hashedpw)

It seems that if I am going to add this user, all I need to do is pass in the variables in the insert_one() method… The name for “name”, email for “email”, and hashedpw for “password”

Where I am confused is in the TODO it says to use a more durable write… This isn’t even talked about and while I can find write_concern in the Pymongo documentation, I can’t find any examples of how to use it.

UPDATE ____________________

I took another look at my methods and I was messing up on the delete_user method.

Updated it and now 3 of the four pass…

Struggling with the login_user method.

I am having trouble with the jwt tolken portion. I get the error

client = <FlaskClient <Flask 'mflix.factory'>>

    @pytest.mark.user_management
    def test_login(client):
        result = login_user(test_user.get('email'), test_user.get('jwt'))
>       assert result == {'success': True}
E       AssertionError: assert {'error': Ope...x.sessions]')} == {'success': True}
E         Left contains more items:
E         {'error': OperationFailure('user is not allowed to do action [bypassDocumentValidation] on [sample_mflix.sessions]')}
E         Right contains more items:
E         {'success': True}
E         Use -v to get the full diff

tests/test_user_management.py:43: AssertionError

I am further confused with the TODO line telling me to use and UPSERT for the jwt… When I do this I get the following error.

client = <FlaskClient <Flask 'mflix.factory'>>

    @pytest.mark.user_management
    def test_login(client):
        result = login_user(test_user.get('email'), test_user.get('jwt'))
>       assert result == {'success': True}
E       assert {'error': Typ...hod exists.")} == {'success': True}
E         Left contains more items:
E         {'error': TypeError("'Collection' object is not callable. If you meant to call the 'UPSERT' method on a 'Collection' object it is failing because no such method exists.")}
E         Right contains more items:
E         {'success': True}
E         Use -v to get the full diff

tests/test_user_management.py:43: AssertionError

Unsure on how to get the jwt token to update.
currently I use the

{"$set": {“jwt”: jwt}}

Unsure on where to go from here.

Hey @David_Thompson,

You are almost there. All you need to do is add upsert = True like this to your code:

db.sessions.update_one( {...}, upsert=True)

You can read more about this from the documentation. Also attaching the documentation section of example and usage of upsert.
If it’s still giving an error or something is not clear, the login_user method will be something like this:

def login_user(email, jwt):
    try:
        db.sessions.update_one(
            {"user_id": email}, {"$set": {"jwt": jwt}}, upsert=True)
        return {"success": True}
    except Exception as e:
        return {"error": e}

Let us know if there’s still any doubt around this. Feel free to reach out for anything else as well.

Regards,
Satyam

Ok… I have a question. On the original file, the login_user uses the db.session.insert_one() method.

There is no mention of changing it to db.sessions.update_one()… Is this just assumed?

Also… on the add_user()…Where can I go to get information on how to change the durability of the writes in Python… I have seen no reference to how to do this… Or is it on a later lesson?

# TODO: User Management
        # Insert a user with the "name", "email", and "password" fields.
        # TODO: Durable Writes
        # Use a more durable Write Concern for this operation.

------------UPDATE----------

Well the next module now deals with write concerns… Because of this being a new topic… I will move my above question to a new thread.

Hi @David_Thompson,

No, not assumed. As discussed in one of the lectures too, since we are inserting/updating, hence update_one() is being used. You can read more from the documentation.

This is discussed in the next lecture and this TODO is also infact of another upcoming lab. So don’t worry about this.

Let us know if any doubt still persists.

Regards,
Satyam

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.