Spaces:
Runtime error
Runtime error
Commit
·
2c11b57
1
Parent(s):
36753c2
change reponse 400 -> 403 when no profile found
Browse files- app/dependencies.py +11 -6
- tests/test_friend_request.py +88 -64
app/dependencies.py
CHANGED
@@ -1,9 +1,12 @@
|
|
1 |
from fastapi import Depends, HTTPException, status
|
2 |
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
3 |
-
from firebase_admin import
|
4 |
-
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
7 |
security = HTTPBearer()
|
8 |
|
9 |
|
@@ -11,10 +14,12 @@ def get_current_user(
|
|
11 |
credentials: HTTPAuthorizationCredentials = Depends(security),
|
12 |
):
|
13 |
try:
|
14 |
-
payload =
|
15 |
user_doc_ref = db.collection("user").document(payload["sub"]).get()
|
16 |
if not user_doc_ref.exists:
|
17 |
-
raise HTTPException(
|
|
|
|
|
18 |
except ExpiredIdTokenError as e:
|
19 |
logger.warning(e)
|
20 |
raise HTTPException(
|
|
|
1 |
from fastapi import Depends, HTTPException, status
|
2 |
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
3 |
+
from firebase_admin.auth import (
|
4 |
+
ExpiredIdTokenError,
|
5 |
+
InvalidIdTokenError,
|
6 |
+
verify_id_token,
|
7 |
+
)
|
8 |
+
from . import db, logger
|
9 |
+
|
10 |
security = HTTPBearer()
|
11 |
|
12 |
|
|
|
14 |
credentials: HTTPAuthorizationCredentials = Depends(security),
|
15 |
):
|
16 |
try:
|
17 |
+
payload = verify_id_token(credentials.credentials)
|
18 |
user_doc_ref = db.collection("user").document(payload["sub"]).get()
|
19 |
if not user_doc_ref.exists:
|
20 |
+
raise HTTPException(
|
21 |
+
status_code=status.HTTP_403_FORBIDDEN, detail="User profile not exist"
|
22 |
+
)
|
23 |
except ExpiredIdTokenError as e:
|
24 |
logger.warning(e)
|
25 |
raise HTTPException(
|
tests/test_friend_request.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import os
|
2 |
import pytest
|
3 |
-
import json
|
4 |
import cv2
|
5 |
import mmcv
|
6 |
import requests
|
@@ -11,18 +11,22 @@ from app.constants import deviceId
|
|
11 |
from fastapi.routing import APIRoute
|
12 |
from app import db
|
13 |
from google.cloud.firestore_v1.base_query import FieldFilter
|
|
|
|
|
14 |
def endpoints():
|
15 |
endpoints = []
|
16 |
for route in app.routes:
|
17 |
if isinstance(route, APIRoute):
|
18 |
endpoints.append(route.path)
|
19 |
return endpoints
|
|
|
|
|
20 |
def read_qr_code(filename):
|
21 |
"""Read an image and read the QR code.
|
22 |
-
|
23 |
Args:
|
24 |
filename (string): Path to file
|
25 |
-
|
26 |
Returns:
|
27 |
qr (string): Value from QR code
|
28 |
"""
|
@@ -33,87 +37,104 @@ def read_qr_code(filename):
|
|
33 |
return value
|
34 |
except:
|
35 |
return
|
|
|
|
|
36 |
@pytest.fixture
|
37 |
def client():
|
38 |
client = TestClient(app)
|
39 |
yield client
|
|
|
|
|
40 |
@pytest.fixture
|
41 |
def inviter():
|
42 |
-
url =
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
}
|
52 |
response = requests.request("POST", url, headers=headers, data=payload)
|
53 |
data = response.json()
|
54 |
-
inviter = {"id": data[
|
55 |
yield inviter
|
56 |
-
|
|
|
57 |
@pytest.fixture()
|
58 |
def invitee():
|
59 |
-
url =
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
}
|
69 |
response = requests.request("POST", url, headers=headers, data=payload)
|
70 |
data = response.json()
|
71 |
-
invitee = {"id": data[
|
72 |
yield invitee
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
|
|
75 |
def test_post_friend(self, client, inviter, invitee):
|
76 |
# Call the firebase database
|
77 |
-
friend_request_ref = db.collection(
|
78 |
# Remove all the friend_request use for testing in the past
|
79 |
-
query = friend_request_ref.where(
|
|
|
|
|
80 |
docs = query.stream()
|
81 |
for doc in docs:
|
82 |
doc.reference.delete()
|
83 |
-
# Delete the user for safety-check
|
84 |
user_ref = db.collection("user")
|
85 |
-
user_ref.document(inviter[
|
86 |
# Send request with no token
|
87 |
-
payload =
|
88 |
headers = {
|
89 |
-
|
90 |
}
|
91 |
-
response = client.request(
|
|
|
|
|
92 |
assert response.status_code == 403
|
93 |
# Send request with false token
|
94 |
-
payload =
|
95 |
headers = {
|
96 |
-
|
97 |
-
|
98 |
}
|
99 |
-
response = client.request(
|
|
|
|
|
100 |
assert response.status_code == 401
|
101 |
# Send request with unknown user
|
102 |
-
payload =
|
103 |
headers = {
|
104 |
-
|
105 |
-
|
106 |
}
|
107 |
-
response = client.request(
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
|
|
|
|
112 |
headers = {
|
113 |
-
|
114 |
-
|
115 |
}
|
116 |
-
response = client.request(
|
|
|
|
|
117 |
assert response.status_code == 200
|
118 |
result = mmcv.imfrombytes(response.read())
|
119 |
# Check returned QR image
|
@@ -122,29 +143,32 @@ class TestFriendRequest():
|
|
122 |
mmcv.imwrite(result, "qrcode.jpg")
|
123 |
# Now test for the invitee aka the one that scan QR code
|
124 |
# Delete invitee user (if existed)
|
125 |
-
user_ref.document(invitee[
|
126 |
# Test when the invitee is unknow user (no user entity in database)
|
127 |
request_id = read_qr_code("qrcode.jpg")
|
128 |
-
payload =
|
129 |
headers = {
|
130 |
-
|
131 |
-
|
132 |
}
|
133 |
-
response = client.request(
|
134 |
-
|
|
|
|
|
135 |
|
136 |
# Create invitee user
|
137 |
-
user_ref.document(invitee[
|
138 |
# Send request
|
139 |
request_id = read_qr_code("qrcode.jpg")
|
140 |
-
payload =
|
141 |
headers = {
|
142 |
-
|
143 |
-
|
144 |
}
|
145 |
-
response = client.request(
|
|
|
|
|
146 |
assert response.status_code == 200
|
147 |
# Delete entity for next time test
|
148 |
-
user_ref.document(inviter[
|
149 |
-
user_ref.document(invitee[
|
150 |
-
|
|
|
1 |
import os
|
2 |
import pytest
|
3 |
+
import json
|
4 |
import cv2
|
5 |
import mmcv
|
6 |
import requests
|
|
|
11 |
from fastapi.routing import APIRoute
|
12 |
from app import db
|
13 |
from google.cloud.firestore_v1.base_query import FieldFilter
|
14 |
+
|
15 |
+
|
16 |
def endpoints():
|
17 |
endpoints = []
|
18 |
for route in app.routes:
|
19 |
if isinstance(route, APIRoute):
|
20 |
endpoints.append(route.path)
|
21 |
return endpoints
|
22 |
+
|
23 |
+
|
24 |
def read_qr_code(filename):
|
25 |
"""Read an image and read the QR code.
|
26 |
+
|
27 |
Args:
|
28 |
filename (string): Path to file
|
29 |
+
|
30 |
Returns:
|
31 |
qr (string): Value from QR code
|
32 |
"""
|
|
|
37 |
return value
|
38 |
except:
|
39 |
return
|
40 |
+
|
41 |
+
|
42 |
@pytest.fixture
|
43 |
def client():
|
44 |
client = TestClient(app)
|
45 |
yield client
|
46 |
+
|
47 |
+
|
48 |
@pytest.fixture
|
49 |
def inviter():
|
50 |
+
url = (
|
51 |
+
"https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key="
|
52 |
+
+ os.environ.get("FIREBASE_API_KEY")
|
53 |
+
)
|
54 |
+
|
55 |
+
payload = json.dumps(
|
56 |
+
{"email": "[email protected]", "password": "testing", "returnSecureToken": True}
|
57 |
+
)
|
58 |
+
headers = {"Content-Type": "application/json"}
|
|
|
59 |
response = requests.request("POST", url, headers=headers, data=payload)
|
60 |
data = response.json()
|
61 |
+
inviter = {"id": data["localId"], "token": data["idToken"]}
|
62 |
yield inviter
|
63 |
+
|
64 |
+
|
65 |
@pytest.fixture()
|
66 |
def invitee():
|
67 |
+
url = (
|
68 |
+
"https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key="
|
69 |
+
+ os.environ.get("FIREBASE_API_KEY")
|
70 |
+
)
|
71 |
+
|
72 |
+
payload = json.dumps(
|
73 |
+
{"email": "[email protected]", "password": "testing2", "returnSecureToken": True}
|
74 |
+
)
|
75 |
+
headers = {"Content-Type": "application/json"}
|
|
|
76 |
response = requests.request("POST", url, headers=headers, data=payload)
|
77 |
data = response.json()
|
78 |
+
invitee = {"id": data["localId"], "token": data["idToken"]}
|
79 |
yield invitee
|
80 |
+
|
81 |
+
|
82 |
+
class TestFriendRequest:
|
83 |
+
@pytest.mark.skipif(
|
84 |
+
"/friend_request" not in endpoints(), reason="Route not defined"
|
85 |
+
)
|
86 |
def test_post_friend(self, client, inviter, invitee):
|
87 |
# Call the firebase database
|
88 |
+
friend_request_ref = db.collection("friend_request")
|
89 |
# Remove all the friend_request use for testing in the past
|
90 |
+
query = friend_request_ref.where(
|
91 |
+
filter=FieldFilter("inviter", "==", inviter["id"])
|
92 |
+
)
|
93 |
docs = query.stream()
|
94 |
for doc in docs:
|
95 |
doc.reference.delete()
|
96 |
+
# Delete the user for safety-check
|
97 |
user_ref = db.collection("user")
|
98 |
+
user_ref.document(inviter["id"]).delete()
|
99 |
# Send request with no token
|
100 |
+
payload = ""
|
101 |
headers = {
|
102 |
+
"Content-Type": "application/json",
|
103 |
}
|
104 |
+
response = client.request(
|
105 |
+
"POST", "friend_request", headers=headers, data=payload
|
106 |
+
)
|
107 |
assert response.status_code == 403
|
108 |
# Send request with false token
|
109 |
+
payload = ""
|
110 |
headers = {
|
111 |
+
"Content-Type": "application/json",
|
112 |
+
"Authorization": "Bearer amksckmasckmafvqnwfniqoniofv",
|
113 |
}
|
114 |
+
response = client.request(
|
115 |
+
"POST", "friend_request", headers=headers, data=payload
|
116 |
+
)
|
117 |
assert response.status_code == 401
|
118 |
# Send request with unknown user
|
119 |
+
payload = ""
|
120 |
headers = {
|
121 |
+
"Content-Type": "application/json",
|
122 |
+
"Authorization": "Bearer " + inviter["token"],
|
123 |
}
|
124 |
+
response = client.request(
|
125 |
+
"POST", "friend_request", headers=headers, data=payload
|
126 |
+
)
|
127 |
+
assert response.status_code == 403
|
128 |
+
# Create request and re-send
|
129 |
+
user_ref.document(inviter["id"]).set({"deviceId": deviceId})
|
130 |
+
payload = ""
|
131 |
headers = {
|
132 |
+
"Content-Type": "application/json",
|
133 |
+
"Authorization": "Bearer " + inviter["token"],
|
134 |
}
|
135 |
+
response = client.request(
|
136 |
+
"POST", "friend_request", headers=headers, data=payload
|
137 |
+
)
|
138 |
assert response.status_code == 200
|
139 |
result = mmcv.imfrombytes(response.read())
|
140 |
# Check returned QR image
|
|
|
143 |
mmcv.imwrite(result, "qrcode.jpg")
|
144 |
# Now test for the invitee aka the one that scan QR code
|
145 |
# Delete invitee user (if existed)
|
146 |
+
user_ref.document(invitee["id"]).delete()
|
147 |
# Test when the invitee is unknow user (no user entity in database)
|
148 |
request_id = read_qr_code("qrcode.jpg")
|
149 |
+
payload = ""
|
150 |
headers = {
|
151 |
+
"Content-Type": "application/json",
|
152 |
+
"Authorization": "Bearer " + invitee["token"],
|
153 |
}
|
154 |
+
response = client.request(
|
155 |
+
"PATCH", "friend_request/" + request_id, headers=headers, data=payload
|
156 |
+
)
|
157 |
+
assert response.status_code == 403
|
158 |
|
159 |
# Create invitee user
|
160 |
+
user_ref.document(invitee["id"]).set({"deviceId": deviceId})
|
161 |
# Send request
|
162 |
request_id = read_qr_code("qrcode.jpg")
|
163 |
+
payload = ""
|
164 |
headers = {
|
165 |
+
"Content-Type": "application/json",
|
166 |
+
"Authorization": "Bearer " + invitee["token"],
|
167 |
}
|
168 |
+
response = client.request(
|
169 |
+
"PATCH", "friend_request/" + request_id, headers=headers, data=payload
|
170 |
+
)
|
171 |
assert response.status_code == 200
|
172 |
# Delete entity for next time test
|
173 |
+
user_ref.document(inviter["id"]).delete()
|
174 |
+
user_ref.document(invitee["id"]).delete()
|
|