Spaces:
Runtime error
Runtime error
File size: 10,221 Bytes
40a6098 dc66a7c 2c11b57 40a6098 dc66a7c 40a6098 c6a9f21 40a6098 0354eb3 40a6098 2c11b57 40a6098 2c11b57 40a6098 2c11b57 40a6098 2c11b57 40a6098 2c11b57 dc66a7c 40a6098 2c11b57 40a6098 2c11b57 dc66a7c 40a6098 2c11b57 40a6098 2c11b57 dc66a7c 40a6098 2c11b57 dc66a7c 40a6098 2c11b57 40a6098 2c11b57 c6a9f21 40a6098 2c11b57 40a6098 2c11b57 40a6098 2c11b57 40a6098 2c11b57 40a6098 2c11b57 40a6098 2c11b57 40a6098 2c11b57 40a6098 2c11b57 40a6098 2c11b57 40a6098 2c11b57 40a6098 2c11b57 40a6098 2c11b57 40a6098 2c11b57 40a6098 2c11b57 40a6098 2c11b57 40a6098 75817a1 40a6098 2c11b57 40a6098 2c11b57 40a6098 2c11b57 40a6098 2c11b57 40a6098 2c11b57 40a6098 f92965c 87f4be5 f92965c 87f4be5 f92965c 40a6098 2c11b57 40a6098 2c11b57 40a6098 2c11b57 40a6098 75817a1 87f4be5 75817a1 40a6098 2c11b57 f92965c 75817a1 f92965c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
import os
import pytest
import json
import cv2
import mmcv
import requests
from fastapi.testclient import TestClient
from app.main import app
from app import db
from app.constants import deviceId
from fastapi.routing import APIRoute
from app import db
from google.cloud.firestore_v1.base_query import FieldFilter
def endpoints():
endpoints = []
for route in app.routes:
if isinstance(route, APIRoute):
endpoints.append(route.path)
return endpoints
def read_qr_code(filename):
"""Read an image and read the QR code.
Args:
filename (string): Path to file
Returns:
qr (string): Value from QR code
"""
try:
img = cv2.imread(filename)
detect = cv2.QRCodeDetector()
value, points, straight_qrcode = detect.detectAndDecode(img)
return value
except:
return
@pytest.fixture
def client():
client = TestClient(app)
yield client
@pytest.fixture
def inviter():
url = (
"https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key="
+ os.environ.get("FIREBASE_API_KEY")
)
payload = json.dumps(
{"email": "[email protected]", "password": "testing", "returnSecureToken": True}
)
headers = {"Content-Type": "application/json"}
response = requests.request("POST", url, headers=headers, data=payload)
data = response.json()
inviter = {"id": data["localId"], "token": data["idToken"]}
yield inviter
@pytest.fixture()
def invitee():
url = (
"https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key="
+ os.environ.get("FIREBASE_API_KEY")
)
payload = json.dumps(
{"email": "[email protected]", "password": "testing2", "returnSecureToken": True}
)
headers = {"Content-Type": "application/json"}
response = requests.request("POST", url, headers=headers, data=payload)
data = response.json()
invitee = {"id": data["localId"], "token": data["idToken"]}
yield invitee
class TestFriendRequest:
@pytest.mark.skipif(
"/friend_request" not in endpoints(), reason="Route not defined"
)
def test_post_friend(self, client, inviter, invitee):
# Call the firebase database
friend_request_ref = db.collection("friend_request")
# Remove all the friend_request use for testing in the past
query = friend_request_ref.where(
filter=FieldFilter("inviter", "==", inviter["id"])
)
docs = query.stream()
for doc in docs:
doc.reference.delete()
# Delete the user for safety-check
user_ref = db.collection("user")
user_ref.document(inviter["id"]).delete()
# Send request with no token
payload = ""
headers = {
"Content-Type": "application/json",
}
response = client.request(
"POST", "friend_request", headers=headers, data=payload
)
assert response.status_code == 403
# Send request with false token
payload = ""
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer amksckmasckmafvqnwfniqoniofv",
}
response = client.request(
"POST", "friend_request", headers=headers, data=payload
)
assert response.status_code == 401
# Send request with unknown user
payload = ""
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + inviter["token"],
}
response = client.request(
"POST", "friend_request", headers=headers, data=payload
)
assert response.status_code == 403
# Create request and re-send
user_ref.document(inviter["id"]).set({"deviceId": deviceId})
payload = ""
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + inviter["token"],
}
response = client.request(
"POST", "friend_request", headers=headers, data=payload
)
assert response.status_code == 200
result = mmcv.imfrombytes(response.read())
# Check returned QR image
assert result.shape[2] == 3
# Write image for later read
mmcv.imwrite(result, "qrcode.jpg")
request_id = read_qr_code("qrcode.jpg")
## Send request again (accept friend request when no invitee)
payload = ""
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + inviter["token"],
}
response = client.request(
"PATCH", "friend_request/" + request_id, headers=headers, data=payload
)
assert response.status_code == 409
# Now test for the invitee aka the one that scan QR code
# Delete invitee user (if existed)
user_ref.document(invitee["id"]).delete()
# Test when the invitee is unknow user (no user entity in database)
payload = ""
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + invitee["token"],
}
response = client.request(
"PATCH", "friend_request/" + request_id, headers=headers, data=payload
)
assert response.status_code == 403
# Create invitee user
user_ref.document(invitee["id"]).set({"deviceId": deviceId})
# Send request
## Send request with false token
payload = ""
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + "xxxxxx",
}
response = client.request(
"PATCH", "friend_request/" + request_id, headers=headers, data=payload
)
assert response.status_code == 401
## Send request
request_id = read_qr_code("qrcode.jpg")
payload = ""
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + invitee["token"],
}
response = client.request(
"PATCH", "friend_request/" + request_id, headers=headers, data=payload
)
assert response.status_code == 200
## Send request again (false)
request_id = read_qr_code("qrcode.jpg")
payload = ""
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + invitee["token"],
}
response = client.request(
"PATCH", "friend_request/" + request_id, headers=headers, data=payload
)
assert response.status_code == 409
## Send request again (accept friend request from invitee)
request_id = read_qr_code("qrcode.jpg")
payload = ""
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + inviter["token"],
}
response = client.request(
"PATCH", "friend_request/" + request_id, headers=headers, data=payload
)
assert response.status_code == 200
# Delete entity for next time test
user_ref.document(inviter["id"]).delete()
user_ref.document(invitee["id"]).delete()
def test_delete_requestF(self, client, inviter, invitee):
# Call the firebase database
friend_request_ref = db.collection("friend_request")
# Remove all the friend_request use for testing in the past
query = friend_request_ref.where(
filter=FieldFilter("inviter", "==", inviter["id"])
)
docs = query.stream()
for doc in docs:
doc.reference.delete()
# Delete the user for safety-check
user_ref = db.collection("user")
user_ref.document(inviter["id"]).delete()
# Create request and re-send
user_ref.document(inviter["id"]).set({"deviceId": deviceId})
payload = ""
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + inviter["token"],
}
response = client.request(
"POST", "friend_request", headers=headers, data=payload
)
assert response.status_code == 200
result = mmcv.imfrombytes(response.read())
# Check returned QR image
assert result.shape[2] == 3
# Write image for later read
mmcv.imwrite(result, "qrcode.jpg")
request_id = read_qr_code("qrcode.jpg")
#Check with unknowed invitee
payload = ""
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + invitee["token"],
}
response = client.request(
"DELETE", "friend_request/" + request_id, headers=headers, data=payload
)
assert response.status_code == 403
#Delete invitee for safety check
user_ref.document(invitee["id"]).delete()
# Create invitee user
user_ref.document(invitee["id"]).set({"deviceId": deviceId})
# check DELETE route
payload = ""
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + inviter["token"],
}
response = client.request(
"DELETE", "friend_request/" + "xxxx", headers=headers, data=payload
)
assert response.status_code == 404
payload = ""
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + invitee["token"],
}
response = client.request(
"DELETE", "friend_request/" + request_id, headers=headers, data=payload
)
assert response.status_code == 403
#Correct Request
payload = ""
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + inviter["token"],
}
response = client.request(
"DELETE", "friend_request/" + request_id, headers=headers, data=payload
)
assert response.status_code == 200
# Delete entity for next time test
user_ref.document(inviter["id"]).delete()
user_ref.document(invitee["id"]).delete() |