librelist archives

« back to archive

Testing a stream response causes trouble

Testing a stream response causes trouble

From:
Cornelius Kölbel
Date:
2015-02-04 @ 16:25
Hello,

I have a problem of a failing test case with the error "Popped wrong
request context".

The traceback looks like this:

======================================================================

FAIL: test_00_authenticate_admin_fail
(tests.test_api_roles.APISelfserviceTestCase)

----------------------------------------------------------------------

Traceback (most recent call last):

File
"/home/travis/build/privacyidea/privacyidea/tests/test_api_roles.py",
line 58, in test_00_authenticate_admin_fail

self.assertTrue(res.status_code == 401, res)

File
"/home/travis/virtualenv/python2.7.9/lib/python2.7/site-packages/flask/ctx.py",
line 386, in __exit__

self.auto_pop(exc_value)

File
"/home/travis/virtualenv/python2.7.9/lib/python2.7/site-packages/flask/ctx.py",
line 374, in auto_pop

self.pop(exc)

File
"/home/travis/virtualenv/python2.7.9/lib/python2.7/site-packages/flask/ctx.py",
line 357, in pop

% (rv, self)

AssertionError: Popped wrong request context. (<RequestContext
'http://localhost/audit/auditfile.csv' [GET] of privacyidea.app> instead
of <RequestContext 'http://localhost/auth' [POST] of privacyidea.app>)

Interesting enough, this only happens, when the test runs on
travis-ci.org. When I am running the tests locally, everything works fine.

It seems like the Request Context of the "GET
http://localhost/audit/auditfile.csv" is not handled correctly.
Since, it is not the test "test_00_authenticate_admin_fail" itself that
fails.

But when I remove the test, that tests the
"GET http://localhost/audit/auditfile.csv" request, everything works fine.

The /audit/auditfile.csv is supposed to return a file download, which is
streamed, as this might be a big file.
I assume, that the request of this test is not finished completely so it
mangles with the next tests.

This is the REST API of the streaming response:

@audit_blueprint.route('/<csvfile>', methods=['GET'])
def download_csv(csvfile=None):

    audit = getAudit(current_app.config)
    g.audit_object.log({'success': True})
    return Response(stream_with_context(audit.csv_generator(request.all_data)),
                    mimetype='text/csv',
                    headers={"Content-Disposition": ("attachment; "
                                                     "filename=%s" % csvfile)})

This is the way I am trying to test the download.



def test_01_download_audit(self):
    with self.app.test_request_context('/audit/auditfile.csv',
                                       method='GET',
                                       headers={'Authorization': self.at}):
        res = self.app.full_dispatch_request()
        self.assertTrue(res.status_code == 200, res)
        self.assertTrue(res.mimetype == "text/csv", res.mimetype)
        self.assertTrue(res.stream)

I am not really deep into this request popping thingy.
So any idea how I can cope with this problem is highly appreciated.

Thanks a lot and kind regards
Cornelius