Ticket User Management : status page fails for User Management

Hi,

I have modified db.py for ticket User Management. I have used the latest test code.
The tests

pytest -m user_management

passes with

However, the status page now shows an error for the User Management entry :

User Management: invalid response to register

The output of the run.py command is

  • Restarting with stat
  • Debugger is active!
  • Debugger PIN: 277-721-900
  • Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
    127.0.0.1 - - [26/Oct/2018 15:00:52] “GET /status HTTP/1.1” 200 -
    127.0.0.1 - - [26/Oct/2018 15:00:52] “GET /service-worker.js HTTP/1.1” 200 -
    127.0.0.1 - - [26/Oct/2018 15:00:52] “GET /favicon.ico HTTP/1.1” 200 -
    127.0.0.1 - - [26/Oct/2018 15:00:53] “GET /api/v1/movies/ HTTP/1.1” 200 -
    127.0.0.1 - - [26/Oct/2018 15:00:54] “GET /api/v1/movies/countries?countries=Australia HTTP/1.1” 200 -
    127.0.0.1 - - [26/Oct/2018 15:00:54] “GET /api/v1/movies/search?cast=Griffin%20Gluck HTTP/1.1” 200 -
    127.0.0.1 - - [26/Oct/2018 15:00:55] “GET /api/v1/movies/search?cast=Morgan%20Freeman&page=0 HTTP/1.1” 200 -
    127.0.0.1 - - [26/Oct/2018 15:00:55] “GET /api/v1/movies/facet-search?cast=Denzel%20Washington&page=0 HTTP/1.1” 200 -
    127.0.0.1 - - [26/Oct/2018 15:00:55] “OPTIONS /api/v1/user/register HTTP/1.1” 200 -
    127.0.0.1 - - [26/Oct/2018 15:00:55] “OPTIONS /api/v1/user/register HTTP/1.1” 200 -
    127.0.0.1 - - [26/Oct/2018 15:00:56] “GET /api/v1/movies/id/573a13a7f29313caabd1aa1f HTTP/1.1” 200 -
    127.0.0.1 - - [26/Oct/2018 15:00:56] “OPTIONS /api/v1/user/register HTTP/1.1” 200 -
    127.0.0.1 - - [26/Oct/2018 15:00:56] “OPTIONS /api/v1/user/register HTTP/1.1” 200 -
    127.0.0.1 - - [26/Oct/2018 15:00:56] “OPTIONS /api/v1/user/make-admin HTTP/1.1” 200 -
    127.0.0.1 - - [26/Oct/2018 15:00:56] “GET /api/v1/movies/id/573a1390f29313caabcd4132 HTTP/1.1” 200 -
    127.0.0.1 - - [26/Oct/2018 15:00:57] “GET /api/v1/movies/config-options HTTP/1.1” 200 -
    127.0.0.1 - - [26/Oct/2018 15:00:57] “GET /api/v1/movies/id/foobar HTTP/1.1” 500 -
    Traceback (most recent call last):
    File “C:\Users\bartv\Anaconda3\lib\site-packages\flask\app.py”, line 1997, in call
    return self.wsgi_app(environ, start_response)
    File “C:\Users\bartv\Anaconda3\lib\site-packages\flask\app.py”, line 1985, in wsgi_app
    response = self.handle_exception(e)
    File “C:\Users\bartv\Anaconda3\lib\site-packages\flask_cors\extension.py”, line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
    File “C:\Users\bartv\Anaconda3\lib\site-packages\flask\app.py”, line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
    File “C:\Users\bartv\Anaconda3\lib\site-packages\flask_compat.py”, line 33, in reraise
    raise value
    File “C:\Users\bartv\Anaconda3\lib\site-packages\flask\app.py”, line 1982, in wsgi_app
    response = self.full_dispatch_request()
    File “C:\Users\bartv\Anaconda3\lib\site-packages\flask\app.py”, line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
    File “C:\Users\bartv\Anaconda3\lib\site-packages\flask_cors\extension.py”, line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
    File “C:\Users\bartv\Anaconda3\lib\site-packages\flask\app.py”, line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
    File “C:\Users\bartv\Anaconda3\lib\site-packages\flask_compat.py”, line 33, in reraise
    raise value
    File “C:\Users\bartv\Anaconda3\lib\site-packages\flask\app.py”, line 1612, in full_dispatch_request
    rv = self.dispatch_request()
    File “C:\Users\bartv\Anaconda3\lib\site-packages\flask\app.py”, line 1598, in dispatch_request
    return self.view_functionsrule.endpoint
    File “C:\Users\bartv\Documents\Workspace\MongoDB\m220\mflix-python\mflix\api\movies.py”, line 83, in api_get_movie_by_id
    movie = get_movie(id)
    File “C:\Users\bartv\Documents\Workspace\MongoDB\m220\mflix-python\mflix\db.py”, line 264, in get_movie
    “_id”: ObjectId(id)
    File “C:\Users\bartv\Anaconda3\lib\site-packages\bson\objectid.py”, line 125, in init
    self.__validate(oid)
    File “C:\Users\bartv\Anaconda3\lib\site-packages\bson\objectid.py”, line 221, in __validate
    _raise_invalid_id(oid)
    File “C:\Users\bartv\Anaconda3\lib\site-packages\bson\objectid.py”, line 67, in _raise_invalid_id
    " or a 24-character hex string" % oid)
    bson.errors.InvalidId: ‘foobar’ is not a valid ObjectId, it must be a 12-byte input or a 24-character hex string
    127.0.0.1 - - [26/Oct/2018 15:00:57] “GET /api/v1/movies/search?text=shawshank HTTP/1.1” 200 -
    127.0.0.1 - - [26/Oct/2018 15:00:58] “GET /api/v1/movies/search?cast=Morgan%20Freeman&page=1 HTTP/1.1” 200 -
    127.0.0.1 - - [26/Oct/2018 15:00:59] “GET /api/v1/movies/facet-search?cast=Morgan%20Freeman&page=2 HTTP/1.1” 200 -
    127.0.0.1 - - [26/Oct/2018 15:00:59] “POST /api/v1/user/register HTTP/1.1” 201 -
    127.0.0.1 - - [26/Oct/2018 15:01:00] “POST /api/v1/user/register HTTP/1.1” 201 -
    127.0.0.1 - - [26/Oct/2018 15:01:01] “POST /api/v1/user/register HTTP/1.1” 201 -
    127.0.0.1 - - [26/Oct/2018 15:01:02] “POST /api/v1/user/register HTTP/1.1” 201 -
    127.0.0.1 - - [26/Oct/2018 15:01:03] “POST /api/v1/user/make-admin HTTP/1.1” 201 -
    127.0.0.1 - - [26/Oct/2018 15:01:03] “GET /api/v1/movies/config-options HTTP/1.1” 200 -
    127.0.0.1 - - [26/Oct/2018 15:01:04] “GET /api/v1/movies/search?genre=Reality-TV HTTP/1.1” 200 -
    127.0.0.1 - - [26/Oct/2018 15:01:05] “GET /api/v1/movies/search?genre=Action&page=0 HTTP/1.1” 200 -
    127.0.0.1 - - [26/Oct/2018 15:01:05] “OPTIONS /api/v1/movies/comment HTTP/1.1” 200 -
    127.0.0.1 - - [26/Oct/2018 15:01:05] “OPTIONS /api/v1/movies/comment HTTP/1.1” 200 -
    127.0.0.1 - - [26/Oct/2018 15:01:05] “OPTIONS /api/v1/user/comment-report HTTP/1.1” 200 -
    127.0.0.1 - - [26/Oct/2018 15:01:05] “GET /api/v1/movies/config-options HTTP/1.1” 200 -
    127.0.0.1 - - [26/Oct/2018 15:01:06] “GET /api/v1/movies/search?genre=Action&page=5 HTTP/1.1” 200 -
    127.0.0.1 - - [26/Oct/2018 15:01:06] “POST /api/v1/movies/comment HTTP/1.1” 200 -
    127.0.0.1 - - [26/Oct/2018 15:01:07] “POST /api/v1/movies/comment HTTP/1.1” 200 -
    127.0.0.1 - - [26/Oct/2018 15:01:07] “GET /api/v1/user/comment-report HTTP/1.1” 422 -
    127.0.0.1 - - [26/Oct/2018 15:01:07] “GET /api/v1/movies/search?text=Heist&page=0 HTTP/1.1” 200 -
    127.0.0.1 - - [26/Oct/2018 15:01:22] “GET /api/v1/user/comment-report HTTP/1.1” 200 -
    127.0.0.1 - - [26/Oct/2018 15:01:23] “GET /api/v1/movies/search?text=Heist&page=7 HTTP/1.1” 200 -

It’s not clear if this is caused by an error on my side, or by an error in the test code. To me, it seems like the User Management tests are correct (the /api/v1/user/register lines), and the error shown above is from another test.

So, what to do now ?
Thanks for your help.

Bart

This is indeed caused by a different error. Looking at the output, the error is caused by an invalid ObjectId trying to be passed through the API to the db layer.

This is the focus of another lab later in the course. For now, replace the pinpoint StopIteration exception in get_movie with Exception. Does that make this go away?

Thanks for the report on this and I look forward to your response.

Nathan

Hello Nathan.

For the other ticket Questions about User Management I had to import a new version of the database. After that. The page loads.

Do you want me to perform the test you suggested on the previous version ?

Regards,

Bart

If it’s working now, all good. Glad the new dataset fixed the issue!

I am facing a very similar issue. ON accessing the status page, i get the same error :

127.0.0.1 - - [27/Oct/2018 18:12:26] “GET /status HTTP/1.1” 200 -
127.0.0.1 - - [27/Oct/2018 18:12:26] “GET /api/v1/movies/ HTTP/1.1” 200 -
127.0.0.1 - - [27/Oct/2018 18:12:26] “GET /api/v1/movies/countries?countries=Australia HTTP/1.1” 200 -
127.0.0.1 - - [27/Oct/2018 18:12:27] “GET /api/v1/movies/search?cast=Griffin%20Gluck HTTP/1.1” 200 -
127.0.0.1 - - [27/Oct/2018 18:12:27] “GET /api/v1/movies/search?cast=Morgan%20Freeman&page=0 HTTP/1.1” 200 -
127.0.0.1 - - [27/Oct/2018 18:12:27] “GET /api/v1/movies/facet-search?cast=Denzel%20Washington&page=0 HTTP/1.1” 200 -
127.0.0.1 - - [27/Oct/2018 18:12:27] “POST /api/v1/user/register HTTP/1.1” 201 -
127.0.0.1 - - [27/Oct/2018 18:12:28] “POST /api/v1/user/register HTTP/1.1” 201 -
127.0.0.1 - - [27/Oct/2018 18:12:28] “GET /api/v1/movies/id/573a13a7f29313caabd1aa1f HTTP/1.1” 200 -
127.0.0.1 - - [27/Oct/2018 18:12:28] “POST /api/v1/user/register HTTP/1.1” 201 -
127.0.0.1 - - [27/Oct/2018 18:12:29] “POST /api/v1/user/register HTTP/1.1” 201 -
127.0.0.1 - - [27/Oct/2018 18:12:29] “POST /api/v1/user/make-admin HTTP/1.1” 201 -
127.0.0.1 - - [27/Oct/2018 18:12:29] “GET /api/v1/movies/id/573a1390f29313caabcd4132 HTTP/1.1” 200 -
127.0.0.1 - - [27/Oct/2018 18:12:29] “GET /api/v1/movies/config-options HTTP/1.1” 400 -
127.0.0.1 - - [27/Oct/2018 18:12:29] “GET /api/v1/movies/id/foobar HTTP/1.1” 500 -
Traceback (most recent call last):
File “C:\ProgramData\Anaconda3\lib\site-packages\flask\app.py”, line 1997, in call
return self.wsgi_app(environ, start_response)
File “C:\ProgramData\Anaconda3\lib\site-packages\flask\app.py”, line 1985, in wsgi_app
response = self.handle_exception(e)
File “C:\ProgramData\Anaconda3\lib\site-packages\flask_cors\extension.py”, line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File “C:\ProgramData\Anaconda3\lib\site-packages\flask\app.py”, line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File “C:\ProgramData\Anaconda3\lib\site-packages\flask_compat.py”, line 33, in reraise
raise value
File “C:\ProgramData\Anaconda3\lib\site-packages\flask\app.py”, line 1982, in wsgi_app
response = self.full_dispatch_request()
File “C:\ProgramData\Anaconda3\lib\site-packages\flask\app.py”, line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File “C:\ProgramData\Anaconda3\lib\site-packages\flask_cors\extension.py”, line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File “C:\ProgramData\Anaconda3\lib\site-packages\flask\app.py”, line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File “C:\ProgramData\Anaconda3\lib\site-packages\flask_compat.py”, line 33, in reraise
raise value
File “C:\ProgramData\Anaconda3\lib\site-packages\flask\app.py”, line 1612, in full_dispatch_request
rv = self.dispatch_request()
File “C:\ProgramData\Anaconda3\lib\site-packages\flask\app.py”, line 1598, in dispatch_request
return self.view_functionsrule.endpoint
File “c:\mflix-python\mflix\api\movies.py”, line 83, in api_get_movie_by_id
movie = get_movie(id)
File “c:\mflix-python\mflix\db.py”, line 264, in get_movie
“_id”: ObjectId(id)
File “C:\ProgramData\Anaconda3\lib\site-packages\bson\objectid.py”, line 125, in init
self.__validate(oid)
File “C:\ProgramData\Anaconda3\lib\site-packages\bson\objectid.py”, line 221, in __validate
_raise_invalid_id(oid)
File “C:\ProgramData\Anaconda3\lib\site-packages\bson\objectid.py”, line 67, in _raise_invalid_id
" or a 24-character hex string" % oid)
bson.errors.InvalidId: ‘foobar’ is not a valid ObjectId, it must be a 12-byte input or a 24-character hex string
127.0.0.1 - - [27/Oct/2018 18:12:29] “GET /service-worker.js HTTP/1.1” 200 -
127.0.0.1 - - [27/Oct/2018 18:12:29] “GET /favicon.ico HTTP/1.1” 200 -
127.0.0.1 - - [27/Oct/2018 18:12:29] “GET /api/v1/movies/search?text=shawshank HTTP/1.1” 200 -
127.0.0.1 - - [27/Oct/2018 18:12:29] “GET /api/v1/movies/search?cast=Morgan%20Freeman&page=1 HTTP/1.1” 200 -
127.0.0.1 - - [27/Oct/2018 18:12:30] “GET /api/v1/movies/facet-search?cast=Morgan%20Freeman&page=2 HTTP/1.1” 200 -
127.0.0.1 - - [27/Oct/2018 18:12:30] “POST /api/v1/user/register HTTP/1.1” 400 -
127.0.0.1 - - [27/Oct/2018 18:12:30] “PUT /api/v1/user/update-preferences HTTP/1.1” 201 -
127.0.0.1 - - [27/Oct/2018 18:12:30] “POST /api/v1/movies/comment HTTP/1.1” 200 -
127.0.0.1 - - [27/Oct/2018 18:12:30] “POST /api/v1/movies/comment HTTP/1.1” 200 -
127.0.0.1 - - [27/Oct/2018 18:12:30] “GET /api/v1/user/comment-report HTTP/1.1” 422 -
127.0.0.1 - - [27/Oct/2018 18:12:30] “GET /api/v1/movies/config-options HTTP/1.1” 400 -
127.0.0.1 - - [27/Oct/2018 18:12:31] “GET /api/v1/movies/search?genre=Reality-TV HTTP/1.1” 200 -
127.0.0.1 - - [27/Oct/2018 18:12:31] “GET /api/v1/movies/search?genre=Action&page=0 HTTP/1.1” 200 -
127.0.0.1 - - [27/Oct/2018 18:12:31] “POST /api/v1/user/logout HTTP/1.1” 201 -
127.0.0.1 - - [27/Oct/2018 18:12:31] “POST /api/v1/user/login HTTP/1.1” 201 -
127.0.0.1 - - [27/Oct/2018 18:12:31] “GET /api/v1/movies/config-options HTTP/1.1” 400 -
127.0.0.1 - - [27/Oct/2018 18:12:40] “GET /api/v1/user/comment-report HTTP/1.1” 200 -
127.0.0.1 - - [27/Oct/2018 18:12:40] “GET /api/v1/movies/search?genre=Action&page=5 HTTP/1.1” 200 -
127.0.0.1 - - [27/Oct/2018 18:12:41] “POST /api/v1/user/login HTTP/1.1” 201 -
127.0.0.1 - - [27/Oct/2018 18:12:42] “GET /api/v1/movies/search?text=Heist&page=0 HTTP/1.1” 200 -

However, the page load is stuck on the following step :

127.0.0.1 - - [27/Oct/2018 18:12:42] “GET /api/v1/movies/search?text=Heist&page=0 HTTP/1.1” 200 -

It is not until I kill the server that I see the following error :

User Management: invalid response to delete

@nathan.leniz - In the fix you suggested to @Bart_22366, should I write the exception block like the following?

except Exception as e:

Yes, this is correct, except Exception as e: