NMPhap commited on
Commit
c6a9f21
1 Parent(s): 72be3a5

<Test>: add test video route

Browse files
Files changed (3) hide show
  1. .coverage +0 -0
  2. tests/test_friend_request.py +4 -16
  3. tests/test_video.py +88 -0
.coverage CHANGED
Binary files a/.coverage and b/.coverage differ
 
tests/test_friend_request.py CHANGED
@@ -6,11 +6,10 @@ import mmcv
6
  import firebase_admin
7
  import requests
8
  from fastapi.testclient import TestClient
9
- from firebase_admin import credentials
10
  from app.main import app
 
11
  from app.constants import deviceId
12
  from fastapi.routing import APIRoute
13
- from firebase_admin import firestore
14
  from google.cloud.firestore_v1.base_query import FieldFilter
15
  def endpoints():
16
  endpoints = []
@@ -40,7 +39,7 @@ def client():
40
  yield client
41
  @pytest.fixture
42
  def inviter():
43
- url = "https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=AIzaSyArSoK9Wx9Hpe1R9ZywuLEIMVjCtHjO8Os"
44
 
45
  payload = json.dumps({
46
  "email": "test@gmail.com",
@@ -54,20 +53,10 @@ def inviter():
54
  data = response.json()
55
  inviter = {"id": data['localId'], "token": data["idToken"]}
56
  yield inviter
57
- @pytest.fixture()
58
- def firebase_app():
59
- if "test" in firebase_admin._apps:
60
- firebase_app = firebase_admin.get_app('test')
61
- yield firebase_app
62
- else:
63
- firebase_app = firebase_admin.initialize_app( credential= credentials.Certificate(
64
- json.loads(os.environ.get("FIREBASE_CREDENTIALS"))
65
- ),name='test')
66
- yield firebase_app
67
 
68
  @pytest.fixture()
69
  def invitee():
70
- url = "https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=AIzaSyArSoK9Wx9Hpe1R9ZywuLEIMVjCtHjO8Os"
71
 
72
  payload = json.dumps({
73
  "email": "test2@gmail.com",
@@ -83,9 +72,8 @@ def invitee():
83
  yield invitee
84
  class TestFriendRequest():
85
  @pytest.mark.skipif("/friend_request" not in endpoints(),reason="Route not defined")
86
- def test_post_friend(self, firebase_app, client, inviter, invitee):
87
  # Call the firebase database
88
- db = firestore.client(app=firebase_app)
89
  friend_request_ref = db.collection('friend_request')
90
  # Remove all the friend_request use for testing in the past
91
  query = friend_request_ref.where(filter=FieldFilter("inviter", "==", inviter['id']))
 
6
  import firebase_admin
7
  import requests
8
  from fastapi.testclient import TestClient
 
9
  from app.main import app
10
+ from app import db
11
  from app.constants import deviceId
12
  from fastapi.routing import APIRoute
 
13
  from google.cloud.firestore_v1.base_query import FieldFilter
14
  def endpoints():
15
  endpoints = []
 
39
  yield client
40
  @pytest.fixture
41
  def inviter():
42
+ url = "https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=" + os.environ.get("FIREBASE_API_KEY")
43
 
44
  payload = json.dumps({
45
  "email": "test@gmail.com",
 
53
  data = response.json()
54
  inviter = {"id": data['localId'], "token": data["idToken"]}
55
  yield inviter
 
 
 
 
 
 
 
 
 
 
56
 
57
  @pytest.fixture()
58
  def invitee():
59
+ url = "https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=" + os.environ.get("FIREBASE_API_KEY")
60
 
61
  payload = json.dumps({
62
  "email": "test2@gmail.com",
 
72
  yield invitee
73
  class TestFriendRequest():
74
  @pytest.mark.skipif("/friend_request" not in endpoints(),reason="Route not defined")
75
+ def test_post_friend(self, client, inviter, invitee):
76
  # Call the firebase database
 
77
  friend_request_ref = db.collection('friend_request')
78
  # Remove all the friend_request use for testing in the past
79
  query = friend_request_ref.where(filter=FieldFilter("inviter", "==", inviter['id']))
tests/test_video.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi.testclient import TestClient
2
+ from fastapi.routing import APIRoute
3
+ from app.main import app
4
+ from app import db
5
+ import os
6
+ import pytest
7
+ import requests
8
+ import json
9
+ from google.cloud.firestore_v1.base_query import FieldFilter
10
+
11
+ def endpoints():
12
+ endpoints = []
13
+ for route in app.routes:
14
+ if isinstance(route, APIRoute):
15
+ endpoints.append(route.path)
16
+ return endpoints
17
+ @pytest.fixture
18
+ def client():
19
+ client = TestClient(app, "http://0.0.0.0:3000")
20
+ yield client
21
+ @pytest.fixture
22
+ def user():
23
+ url = "https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=" + os.environ.get("FIREBASE_API_KEY")
24
+ payload = json.dumps({
25
+ "email": "test@gmail.com",
26
+ "password": "testing",
27
+ "returnSecureToken": True
28
+ })
29
+ headers = {
30
+ 'Content-Type': 'application/json'
31
+ }
32
+ response = requests.request("POST", url, headers=headers, data=payload)
33
+ data = response.json()
34
+ user = {"id": data['localId'], "token": data["idToken"]}
35
+ yield user
36
+ class TestVideoAPI:
37
+ @pytest.mark.skipif("/video" not in endpoints(),reason="Route not defined")
38
+ def test_video_API(self, user, client):
39
+ # Test when sent file is not a video
40
+ payload = {}
41
+ files=[
42
+ ('file',('demo.jpg',open('demo.jpg','rb'),'application/octet-stream'))
43
+ ]
44
+ headers = {
45
+ 'Content-Type': 'application/json',
46
+ }
47
+ response = client.request("POST", 'video', headers=headers, data=payload, files=files)
48
+ assert response.status_code == 400
49
+ assert response.text == "File must be video"
50
+ # Test when no token is pass to route
51
+ payload = {}
52
+ files=[
53
+ ('file',('demo.mp4',open('demo.mp4','rb'),'application/octet-stream'))
54
+ ]
55
+ headers = {
56
+ 'Content-Type': 'application/json',
57
+ }
58
+ response = client.request("POST", 'video', headers=headers, data=payload, files=files)
59
+ assert response.status_code == 400
60
+ assert response.text == "User not found"
61
+ # Test when all requirements have been fulfilled
62
+ payload = {}
63
+ files=[
64
+ ('file',('demo.mp4',open('demo.mp4','rb'),'application/octet-stream'))
65
+ ]
66
+ headers = {
67
+ 'Content-Type': 'application/json',
68
+ 'Authorization': "Bearer " + user['token']
69
+ }
70
+ response = client.request("POST", 'video', headers=headers, data=payload, files=files)
71
+ assert response.status_code == 200
72
+ artifactName = response.text
73
+ docs = db.collection("artifacts").where(filter = FieldFilter("name", '==', artifactName))
74
+ index = 0
75
+ for doc in docs:
76
+ # For each document in docs. Verify name and status of the artifact
77
+ index += 1
78
+ data = doc.get().to_dict()
79
+ assert data['name'] == artifactName
80
+ assert data['status'] == 'pending'
81
+ assert index == 1
82
+
83
+
84
+
85
+
86
+
87
+
88
+