Spaces:
Paused
Paused
File size: 7,953 Bytes
ddc5bbd |
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 |
import os
from dotenv import dotenv_values
from fastapi import FastAPI
from pymongo import MongoClient
from main import requests
import uuid
import pytest
from dotenv import load_dotenv
import requests
import json
# Test Root endpoint
def test_root_pass():
response = requests.get("http://127.0.0.1:8080/")
assert response.status_code == 200
assert response.json() == {"message": "Welcome to InterpreTalk!"}
# POST /user/
# Test DB user record creation including response validation
def test_create_user_pass():
payload = {
"name": "Tester1",
"user_id": "testerID",
"email": "[email protected]"
}
response = requests.post("http://127.0.0.1:8080/user/", json=payload)
assert response.status_code == 201
'''Test User Endpoints'''
# GET /user/
# Test finding DB user record based on user ID
def test_find_user_pass():
response = requests.get("http://localhost:8080/user/ozpHhyum3sayTdxIKUAtF51uvWJ2") # existing user record
assert response.status_code == 200
assert response.json() == {
"user_id": "ozpHhyum3sayTdxIKUAtF51uvWJ2",
"name": "Benjamin",
"email": "[email protected]"
}
def test_find_user_fail():
response = requests.get(f"http://127.0.0.1:8080/users/fakeID") # non-existing user record
# check if response is inteded error code
assert response.status_code == 404
# PUT /user/{user_id}
# Updating DB user record based on user ID
def test_update_user_pass():
payload = {
"name": "TesterNewName"
}
response = requests.patch(f"http://127.0.0.1:8080/users/testerID", json=payload)
assert response.status_code == 202
assert response.json() == {
"name": "TesterNewName",
"user_id": "testerID",
"email": "[email protected]"
}
# Test with non-existing user ID
def test_update_user_fail():
payload = {
"name": "TesterNewName"
}
response = requests.patch(f"http://127.0.0.1:8080/users/falseID", json=payload)
assert response.status_code == 404
# DELETE /user/{user_id}
def test_delete_user_pass():
response = requests.delete(f"http://127.0.0.1:8080/users/testerID")
assert response.status_code == 200
def test_delete_user_fail():
response = requests.delete(f"http://127.0.0.1:8080/users/fakeID")
assert response.status_code == 404
# GET /user/find-name-id/{user_ud}
def test_find_name_id_pass():
response = requests.get("http://127.0.0.1:8080/user/find-name-id/ozpHhyum3sayTdxIKUAtF51uvWJ2")
assert response.status_code == 201
assert response.json == {
'name': "Benjamin"
}
def test_find_name_id_fail():
response = requests.get("http://127.0.0.1:8080/user/find-name-id/falseID")
assert response.status_code == 404
'''Test Call endpoints'''
# POST /call/create-call
# Test creating call record
def test_create_call_pass():
payload = {
"call_id": "test001",
"caller_id": "tester01",
"callee_id": "tester02",
"captions": [
{
"author_id": "tester01",
"author_username": "tester",
"original_text": "It is a test",
"translated_text": "Es un prueba",
}
]
}
response = requests.post("http://127.0.0.1:8080/call/create-call", json=payload)
assert response.status_code == 201
# GET /call/find-call
# Test finding DB call record based on call ID
def test_find_call_pass():
response = requests.get(f"http://127.0.0.1:8080/call/test001") # existing user record
assert response.status_code == 200
assert response.json() == {
"call_id": "test001",
"caller_id": "tester01",
"callee_id": "tester02",
"captions": [
{
"author_id": "tester01",
"author_username": "tester",
"original_text": "It is a test",
"translated_text": "Es un prueba",
}
]
}
def test_find_call_fail():
response = requests.get(f"http://127.0.0.1:8080/call/fakeID") # non-existing user record
# check if response is inteded error code
assert response.status_code == 404
# GET /call/find-user-call
# Test finding DB call record based on user ID
def test_find_user_call_pass():
response = requests.get(f"http://127.0.0.1:8080/call/find-user-calls/tester01") # existing user record
assert response.status_code == 200
assert response.json() == {
"call_id": "test001",
"caller_id": "tester01",
"callee_id": "tester02",
"captions": [
{
"author_id": "tester01",
"author_username": "tester",
"original_text": "It is a test",
"translated_text": "Es un prueba",
}
]
}
def test_find_user_call_fail():
response = requests.get(f"http://127.0.0.1:8080/calls/fakeID") # non-existing user record
# check if response is inteded error code
assert response.status_code == 404
# GET /call/get-captions
# Test finding DB call record based on user ID
def test_get_captions_pass():
response = requests.get(f"http://127.0.0.1:8080/call/find-user-calls/test001/tester01") # existing user record
assert response.status_code == 200
assert response.json() == {
"call_id": "test001",
"caller_id": "tester01",
"callee_id": "tester02",
"captions": [
{
"author_id": "tester01",
"author_username": "tester",
"original_text": "It is a test",
"translated_text": "Es un prueba",
}
]
}
def test_get_captions_fail():
response = requests.get(f"http://127.0.0.1:8080/call/find-user-calls/test001/tester00") # fake user record
# check if response is inteded error code
assert response.status_code == 404
def test_get_captions_fail():
response = requests.get(f"http://127.0.0.1:8080/call/find-user-calls/test000/tester01") # fake call record
# check if response is inteded error code
assert response.status_code == 404
# GET /call/update-call/{call_id}
# test updating call record based on id
def test_update_call_pass():
payload = {
"callee_id": "TesterNewName"
}
response = requests.patch(f"http://127.0.0.1:8080/call/update-call/tester02", json=payload)
assert response.status_code == 202
assert response.json() == {
"call_id": "test001",
"caller_id": "tester01",
"callee_id": "tester02",
"captions": [
{
"author_id": "tester01",
"author_username": "tester",
"original_text": "It is a test",
"translated_text": "Es un prueba",
}
]
}
# Test with non-existing user ID
def test_update_call_fail():
payload = {
"callee_id": "testName"
}
response = requests.patch(f"http://127.0.0.1:8080/users/falseID", json=payload)
assert response.status_code == 404
# GET /call/update-captions/{call_id}
# test updating caption record based on id
def test_update_caption_pass():
payload = {
"author_username": "testerNew"
}
response = requests.patch(f"http://127.0.0.1:8080/call/update-caption/tester01", json=payload)
assert response.status_code == 202
# Test with non-existing user ID
def test_update_call_fail():
payload = {
"callee_id": "testName"
}
response = requests.patch(f"http://127.0.0.1:8080/update-caption/falseID", json=payload)
assert response.status_code == 404
# DELETE /call/delete-call/{call_id}
def test_delete_user_pass():
response = requests.delete(f"http://127.0.0.1:8080//call/delete-call/test001")
assert response.status_code == 200
def test_delete_user_fail():
response = requests.delete(f"http://127.0.0.1:8080//call/delete-call/test009")
assert response.status_code == 404
|