Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

post_internal fails when using URL_PREFIX or API_VERSION #810

Closed
daviskirk opened this issue Jan 16, 2016 · 10 comments
Closed

post_internal fails when using URL_PREFIX or API_VERSION #810

daviskirk opened this issue Jan 16, 2016 · 10 comments
Labels

Comments

@daviskirk
Copy link

When setting URL_PREFIX option to a non-null value, the post_internal fails with a TypeError.
I first tried this on my own API but rechecked it in eve-demo which yields the error and traceback:

Traceback (most recent call last):
  File "run.py", line 35, in <module>
    a =  post_internal('people', {'firstname': 'Test', 'lastname': 'Name'})
  File "env/lib/python3.5/site-packages/eve/methods/post.py", line 247, in post_internal
    ids = app.data.insert(resource, documents)
  File "env/lib/python3.5/site-packages/eve/io/mongo/mongo.py", line 386, in insert
    datasource, _, _, _ = self._datasource_ex(resource)
  File "env/lib/python3.5/site-packages/eve/io/base.py", line 434, in _datasource_ex
    auth_field, request_auth_value = auth_field_and_value(resource)
  File "env/lib/python3.5/site-packages/eve/auth.py", line 258, in auth_field_and_value
    if '|resource' in request.endpoint:
TypeError: argument of type 'NoneType' is not iterable

To reproduce, add

URL_PREFIX = 'test_api'

to settings.py and add

with app.test_request_context():
    post_internal('people', {'firstname': 'Test', 'lastname': 'Name'})

after app = Eve() in run.py.

It seems that flask.request is set to None in this case but up until now I can't figure out why.

@nicolaiarocci
Copy link
Member

Hello, thanks for reporting this.

  1. With which Eve version is this happening?
  2. Which auth are you using, if any?

I tried reproducing by adding the following test:

def test_post_internal_with_url_prefix_or_api_version(self):
    self.app.config['URL_PREFIX'] = 'test_api'

    with self.app.test_request_context():
        post_internal(self.known_resource, {'firstname': 'Test', 'lastname': 'Name'})                               

Test however passes with BasicAuth and also with no auth at all.

@daviskirk
Copy link
Author

Hi,
Thanks for the quick answer!
I originally tested it with a stable Eve version 0.6.1. However, I just pulled the master branch from github (0.6.2.dev0) and the result/error stays the same.
I've also tested python versions is 3.5 and 3.4. On clean python environments.
pip freeze shows:

Cerberus==0.9.2
Eve==0.6.1
Events==0.2.1
Flask==0.10.1
Flask-PyMongo==0.3.1
itsdangerous==0.24
Jinja2==2.8
MarkupSafe==0.23
pymongo==2.9.1
simplejson==3.8.1
Werkzeug==0.10.4
wheel==0.26.0

.. which should cover at least some of the tests since they use tox, correct?
In my case I was running BasicAuth but the eve-demo version I posted had no auth at all and the results were identical.

Just for the sake of completeness I also cloned the eve master branch and inserted your test and ran it manually and it passes for me too (python 3.4 and 3.5). Could it have something to do with the way the database testing envorinment is set up?

I forked the eve-demo repository to make clearer what is being done to trigger the error. The diff is here:
pyeve/eve-demo@master...daviskirk:bug-dev

daviskirk pushed a commit to daviskirk/eve that referenced this issue Jan 17, 2016
- tests sub-issue of pyeve#810
- After initializing the eve app, changing the url prefix should result
  in the resulting api urls changing accordingly.
daviskirk pushed a commit to daviskirk/eve that referenced this issue Jan 17, 2016
- tests sub-issue of pyeve#810
- After initializing the eve app, changing the url prefix should result
  in the resulting api urls changing accordingly.
daviskirk pushed a commit to daviskirk/eve that referenced this issue Jan 17, 2016
- tests sub-issue of pyeve#810
- After initializing the eve app, changing the url prefix should result
  in the resulting api urls changing accordingly.
daviskirk pushed a commit to daviskirk/eve that referenced this issue Jan 17, 2016
- tests sub-issue of pyeve#810
- After initializing the eve app, changing the url prefix should result
  in the resulting api urls changing accordingly.
@daviskirk
Copy link
Author

Just another update:
I just found out that setting the app configuration (as in your test) after the database initialization changes the result. In particular, setting

app.config['URL_PREFIX'] = 'api'

after the app is initialized does not actually set the url prefix. To clarify:

  1. Setting URL_PREFIX in settings.py result in the api being reached at localhost:5000/test_api and the post_internal function results in the described error.

  2. Setting the url prefix using

    app = Eve()
    app.config['URL_PREFIX'] = 'api'

    does not result in an error using post_internal, but the api is still reachable at localhost:5000 and not localhost:5000/test_api. I added a pull request to make this clearer. Feel free to just ignore it since it is more a clarification of this issue than an actual contribution.

Hope this helps. Sorry for the patchwork bugreport.

@sybrenstuvel
Copy link
Contributor

I can confirm that the original report is still an issue on Eve 0.6.4. Is there any indication as to when this is going to be fixed?

@oiwn
Copy link

oiwn commented Sep 3, 2016

Yes same for me Eve (0.6.4)

Anyone could suggest workaround?

@oiwn
Copy link

oiwn commented Sep 3, 2016

Ok i got it. app.test_request_context() inherits from http://werkzeug.pocoo.org/docs/0.11/test/#werkzeug.test.EnvironBuilder

as you can see there is path argument which is '/', i tried to set it to my API_PREFIX

from settings import API_PREFIX
with api.test_request_context(path=API_PREFIX):
    api_response, _, _, return_code = post_internal(
        collection, data, skip_validation=False)
    # .....
    # check return code it should be 201

And it works fine on first look. I tried it on my testing environment, will try for the whole system.

@daviskirk @sybrenstuvel

@sybrenstuvel
Copy link
Contributor

@istinspring thanks for the workaround, we've been using it on Blender Cloud for a while now and it works fine.

@nicolaiarocci what is the status of this issue? Is it going to be fixed, and if so, when can we expect the fix to be released?

@nicolaiarocci
Copy link
Member

Hi folks, sorry for taking so long. Working on it.

@nicolaiarocci
Copy link
Member

Eve 0.7.4 is out with a fix for this. Enjoy!

@oiwn
Copy link

oiwn commented May 24, 2017

@nicolaiarocci thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants