Questions about User Management ticket

I have run into two issues with this ticket.

  1. There are 4 copies of the test file test_user_management.py in my tests directory. All with a slightly different extension (".py~beta release", “.py~beta release0”, “.py~HEAD”, and “.py~HEAD0”). They all look to be same. For now I have copied one and changed the extenstion to “.py”. Should we be using one of these files instead of another?

  2. In the users collection, the fields are _id, name, email, and pw. In db.py and in the test file the code is looking for a field “password”. Don’t these fields need to match?

@frankfitch

  1. You could use any of these files. Like you said, they look almost same.
  2. I reported this issue before. But for the sake of finishing this lab without further modification to other files, we use ‘password’.

I renamed one file to test_user_management.py and replaced password to pw ( since the collection contains pw name). I am not getting errors but it says

C:\new\Software\Anaconda3\mflix-python\mflix>pytest -m user_management
============================= test session starts =============================
platform win32 – Python 3.7.0, pytest-3.8.0, py-1.6.0, pluggy-0.7.1
rootdir: C:\new\Software\Anaconda3\mflix-python\mflix, inifile:
plugins: remotedata-0.3.0, openfiles-0.3.0, flask-0.10.0, doctestplus-0.1.3, arraydiff-0.2
collected 0 items

======================== no tests ran in 0.15 seconds =========================

@Rao_97297

Where did you put your renamed test file? From the output you posted, I saw you run test under mflix folder. Your tests folder should have the same level as mflix. And you should run pytest in that level. Otherwise, It is no way for pytest to find test files.

In my reply, I mean you should stick on password as key name. Because if you change it to pw. You will have to modify your test file and user.py file.

Q Yang

Both are the same file, just choose anyone and rename as “test_user_management.py”.

Hi frankfitch,

The issue has been fixed. You need to download handouts, unzip the file, and copy the tests from there to your current mflix directory:

  mflix-python$ rm -rf tests/
  mflix-python$ cp -R ~/Downloads/mflix-python/tests/ tests/

It will solve the issue.

Kanika

Hi @Kanika

Thank you very much. I’ll take a look at things tonight.

Hi,

the test file test_user_management.py still uses the property “password” instead of “pw” :

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}

found_user = get_user(test_user.get('email'))
assert found_user.get('name') == test_user.get('name')
assert found_user.get('email') == test_user.get('email')
assert found_user.get('password') == test_user.get('password')

the last line should be replaced by

assert found_user.get(‘pw’) == test_user.get(‘password’)

Bart

Hi Bart,

You need to re-import data. The field has been updated to password .
Please drop the database as whole or just users collection. Download the handout zip and re-import the data. Here is the command to restore a single collection:

mongorestore --nsInclude 'mflix.users' --gzip data

Or drop everything and restore back again

mongorestore --drop --gzip --uri <your-atlas-uri> data

I am sorry for your inconvenience.

Kanika

Hello Kanika,

I noticed your reply earlier. I have imported the database again, but I have the impression that a required unique index on users.email is now missing.

MongoDB Enterprise mflix-shard-0:PRIMARY> db.users.getIndexes()
[
{
“v” : 2,
“key” : {
“_id” : 1
},
“name” : “id”,
“ns” : “mflix.users”
}
]
MongoDB Enterprise mflix-shard-0:PRIMARY>

This causes a test on add_user to fail because of duplicate entries.
Could you please verify ?

Thanks

Bart

Hi Bart_22366,

Sorry for the confusion. I guess then you need to drop mflix database. Download handouts, remove existing data directory under mflix-python and copy the data directory from downloaded handouts.

And restore back using the same command you used before, just add --drop incase you din’t use it before:

mongorestore --drop --gzip --uri <your-atlas-uri> data

Kanika

Well, here’s what I did :

mongorestore --drop --gzip --uri mongodb+srv://m220student:m220password@… data

This dropped the database, didn’t it ?

Bart

Yes it did!

Now, is everything ok?

I don’t think so.
The add_user function requires a unique index on email.
This index was missing. I had to create it myself.
Can you verify if the index exists in your database ?

I will reload it once more.

Bart

Thank you nyamama for the reply.

  1. my tests directory is in the level as that of mflix. Based on new instructions, I recopied the tests directory from the new handouts.
  2. Regarding password issue: I decided to follow the instructions to restore db with the new data provided.
    i got duplicate email issue- after removing the new email record, I manually created the index.

I still have some other issue. needs to figure out.

C:\new\Software\Anaconda3\mflix-python>pytest -m user_management
============================= test session starts =============================
platform win32 – Python 3.7.0, pytest-3.8.0, py-1.6.0, pluggy-0.7.1
rootdir: C:\new\Software\Anaconda3\mflix-python, inifile:
plugins: remotedata-0.3.0, openfiles-0.3.0, flask-0.10.0, doctestplus-0.1.3, arraydiff-0.2
collected 39 items / 35 deselected

tests\test_user_management.py …F. [100%]

================================== FAILURES ===================================
_________________________________ test_login __________________________________

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}
    session_result = get_user_session(test_user.get('email'))

> assert session_result.get(‘user_id’) == test_user.get(‘email’)
E AttributeError: ‘NoneType’ object has no attribute 'get’

tests\test_user_management.py:45: AttributeError

Hi, Rao,

It looks like login_user works well, but get_user_session does not work. You could post your function here, maybe we can find something.

Q

Here it is

def get_user_session(email):
“”"
Given a user’s email, finds that user’s session in sessions.

In `sessions`, each user's email is stored in a field called "user_id".
"""
try:
    # TODO: User Management
    # Retrieve the session document corresponding with the user's email.
    return db.sessions.find_one({"email": email })
except Exception as e:
    return {"error": e}

Hmmm… Not sure what happened, the code that didn’t work yesterday , today it worked. looks strange. probably pycharm editor i am using is not automatically saved. Same is true with the next assignment too.

Hi,
Still cannot pass test.
-update db
mongorestore --drop --gzip --uri <your-atlas-uri> data

-add index
db.users.createIndex({“email” : 1},{unique: true})

and the error is

  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

any hint or shotcuts to pass the test ???

Hi Yuri,
I solved that issue by adding a check if that user already exists before doing the insert.