dimanche 15 février 2015

Cron job fails accessing url with admin_required decorator

According to the documentation Cron jobs should be allowed to access admin protected views. However I get a 302 error if I have the @admin_required decorator on the GET method.


In app.yaml I have defined this:



- url: /generator
script: run.news.app
login: admin


the view:



class GeneratorView(MethodView):
@admin_required
def get(self):
return 'success', 200


urls.py



app.add_url_rule('/generator', 'generator', view_func=GeneratorView.as_view('generator'))


cron job:



cron:
- description: Scrape every 3 hours
url: /generator
schedule: every 3 hours synchronized


decorator:



def admin_required(func):
"""Requires App Engine admin credentials"""

@wraps(func)
def decorated_view(*args, **kwargs):
if users.get_current_user():
if not users.is_current_user_admin():
abort(401) # Unauthorized
return func(*args, **kwargs)
return redirect(users.create_login_url(request.url))

return decorated_view


the funny part is, when I remove the admin_required decorator, the url is still admin-only protected because of login: admin in app.yaml.


However my unit test fails the authorization check because of the missing decorator.



def test_generator_fails_as_normal_user(self):
self.setCurrentUser(u'john@example.com', u'123')
rv = self.client.get('/generator')
self.assertEqual(rv.status_code, 401)



AssertionError: 200 != 401



If I put the decorator back in, the unit test passes and cron job fails. Any suggestions?


Aucun commentaire:

Enregistrer un commentaire