Skip to content

Commit

Permalink
Fixed header setting code for ratelimits
Browse files Browse the repository at this point in the history
  • Loading branch information
romanchyla committed Sep 25, 2018
1 parent 37cb173 commit ee4a5ed
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
19 changes: 12 additions & 7 deletions adsws/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,20 @@ def set_adsws_uid_header(valid, oauth):
If the user is authenticated, inject the header "X-adsws-uid" into
the incoming request header
"""
h = Headers(request.headers.items())

if current_user.is_authenticated():
h = Headers(request.headers.items())
h.add_header("X-Adsws-Uid", current_user.id)
if current_user.ratelimit_level is not None:
h.add_header(
"X-Adsws-Ratelimit-Level",
current_user.ratelimit_level
)
request.headers = h

if valid:
level = oauth.client.ratelimit
if level is None:
level = 1.0
h.add_header("X-Adsws-Ratelimit-Level", level)
else:
h.add_header("X-Adsws-Ratelimit-Level", 0.0)

request.headers = h
return valid, oauth

@app.teardown_request
Expand Down
10 changes: 7 additions & 3 deletions adsws/tests/test_discoverer.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,15 @@ def test_adsws_user_ratelimit_header(self):
remote services
"""
r = self.open('GET', '/test_webservice/ECHO_HEADERS')
self.assertNotIn('X-Adsws-Ratelimit-Level', r.json)
self.assertIn('X-Adsws-Ratelimit-Level', r.json)

user_manipulator.update(self.user, ratelimit_level=10)

ocl = db.session.query(OAuthClient).filter_by(user_id=self.user.id).first()
ocl.ratelimit = 10.56
db.session.commit()

r = self.open('GET', '/test_webservice/ECHO_HEADERS')
self.assertEqual('10', r.json['X-Adsws-Ratelimit-Level'])
self.assertEqual('10.56', r.json['X-Adsws-Ratelimit-Level'])

def test_adsws_proxy_retains_headers_from_service(self):
"""
Expand Down

0 comments on commit ee4a5ed

Please sign in to comment.