File size: 5,378 Bytes
40a6098
dc66a7c
 
40a6098
 
dc66a7c
40a6098
 
c6a9f21
40a6098
 
0354eb3
40a6098
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc66a7c
40a6098
 
 
 
 
c6a9f21
dc66a7c
 
 
 
 
 
 
 
 
 
40a6098
 
 
 
dc66a7c
40a6098
c6a9f21
dc66a7c
 
 
 
 
 
 
 
 
 
40a6098
 
 
 
 
c6a9f21
40a6098
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a0e77d
40a6098
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a0e77d
40a6098
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f0e322
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
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": "test@gmail.com",
    "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": "test2@gmail.com",
    "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 == 400
        # 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")
        # 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)
        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 == 400

        # Create invitee user
        user_ref.document(invitee['id']).set({"deviceId": deviceId})
        # 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
        # Delete entity for next time test
        user_ref.document(inviter['id']).delete()
        user_ref.document(invitee['id']).delete()