You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.4 KiB
92 lines
2.4 KiB
from fastapi.testclient import TestClient
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
sys.path.append(str(Path(__file__).absolute().parent.parent))
|
|
|
|
# Contains all constants
|
|
import app.config as config
|
|
|
|
import pymongo
|
|
|
|
# Import the app
|
|
from app.main import app
|
|
|
|
# Database setup
|
|
client = pymongo.MongoClient(config.MONGODB_URL, username=config.MONGODB_USERNAME, password=config.MONGODB_PASSWORD)
|
|
|
|
if config.MONGODB_DATABASE in client.list_database_names():
|
|
try:
|
|
client.drop_database(config.MONGODB_DATABASE)
|
|
except Exception as e:
|
|
print(f"Error with database : {e}")
|
|
|
|
# Create empty database
|
|
db = client[config.MONGODB_DATABASE]
|
|
|
|
|
|
client = TestClient(app)
|
|
|
|
def test_number_one():
|
|
print("OK")
|
|
|
|
"""
|
|
def test_read_item():
|
|
response = client.get("/items/foo", headers={"X-Token": "coneofsilence"})
|
|
assert response.status_code == 200
|
|
assert response.json() == {
|
|
"id": "foo",
|
|
"title": "Foo",
|
|
"description": "There goes my hero",
|
|
}
|
|
|
|
|
|
def test_read_item_bad_token():
|
|
response = client.get("/items/foo", headers={"X-Token": "hailhydra"})
|
|
assert response.status_code == 400
|
|
assert response.json() == {"detail": "Invalid X-Token header"}
|
|
|
|
|
|
def test_read_nonexistent_item():
|
|
response = client.get("/items/baz", headers={"X-Token": "coneofsilence"})
|
|
assert response.status_code == 404
|
|
assert response.json() == {"detail": "Item not found"}
|
|
|
|
|
|
def test_create_item():
|
|
response = client.post(
|
|
"/items/",
|
|
headers={"X-Token": "coneofsilence"},
|
|
json={"id": "foobar", "title": "Foo Bar", "description": "The Foo Barters"},
|
|
)
|
|
assert response.status_code == 200
|
|
assert response.json() == {
|
|
"id": "foobar",
|
|
"title": "Foo Bar",
|
|
"description": "The Foo Barters",
|
|
}
|
|
|
|
|
|
def test_create_item_bad_token():
|
|
response = client.post(
|
|
"/items/",
|
|
headers={"X-Token": "hailhydra"},
|
|
json={"id": "bazz", "title": "Bazz", "description": "Drop the bazz"},
|
|
)
|
|
assert response.status_code == 400
|
|
assert response.json() == {"detail": "Invalid X-Token header"}
|
|
|
|
|
|
def test_create_existing_item():
|
|
response = client.post(
|
|
"/items/",
|
|
headers={"X-Token": "coneofsilence"},
|
|
json={
|
|
"id": "foo",
|
|
"title": "The Foo ID Stealers",
|
|
"description": "There goes my stealer",
|
|
},
|
|
)
|
|
assert response.status_code == 409
|
|
assert response.json() == {"detail": "Item already exists"}
|
|
""" |