Skip to content

Commit 169e045

Browse files
author
Aaron Suarez
authored
Support authorization header (#395)
* Add test for authorization header with upvote/downvote * Use API key from globals instead of request
1 parent 96233c6 commit 169e045

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

app/api/routes/resource_modification.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from os import environ
22

33
from algoliasearch.exceptions import AlgoliaException, AlgoliaUnreachableHostException
4-
from flask import redirect, request
4+
from flask import redirect, request, g
55
from sqlalchemy.exc import IntegrityError
66

77
from app import db, index, utils as utils
@@ -119,7 +119,7 @@ def update_votes(id, vote_direction):
119119
opposite_direction = 'downvotes' if vote_direction == 'upvotes' else 'upvotes'
120120
opposite_count = getattr(resource, opposite_direction)
121121

122-
api_key = request.headers.get('x-apikey')
122+
api_key = g.auth_key.apikey
123123
vote_info = VoteInformation.query.get(
124124
{'voter_apikey': api_key, 'resource_id': id}
125125
)

tests/unit/test_routes/test_resource_update.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
assert_correct_response
44
)
55

6+
from ..test_auth_jwt import GOOD_AUTH
7+
68

79
def test_update_votes(module_client, module_db, fake_auth_from_oc, fake_algolia_save):
810
client = module_client
@@ -252,3 +254,31 @@ def test_update_resource(
252254
follow_redirects=True
253255
)
254256
assert_correct_response(response, 404)
257+
258+
259+
def test_update_votes_authorization_header(
260+
module_client, module_db, fake_auth_from_oc, fake_algolia_save):
261+
client = module_client
262+
id = 1
263+
UPVOTE = 'upvote'
264+
DOWNVOTE = 'downvote'
265+
266+
data = client.get(f"api/v1/resources/{id}").json['resource']
267+
response = client.put(
268+
f"/api/v1/resources/{id}/upvote",
269+
follow_redirects=True,
270+
headers={'authorization': GOOD_AUTH})
271+
initial_upvotes = data.get(f"{UPVOTE}s")
272+
initial_downvotes = data.get(f"{DOWNVOTE}s")
273+
274+
assert (response.status_code == 200)
275+
assert (response.json['resource'].get(f"{UPVOTE}s") == initial_upvotes + 1)
276+
277+
response = client.put(
278+
f"/api/v1/resources/{id}/downvote",
279+
follow_redirects=True,
280+
headers={'authorization': GOOD_AUTH})
281+
282+
assert (response.status_code == 200)
283+
assert (response.json['resource'].get(f"{UPVOTE}s") == initial_upvotes)
284+
assert (response.json['resource'].get(f"{DOWNVOTE}s") == initial_downvotes + 1)

0 commit comments

Comments
 (0)