From 26061d73667aca3c78c6adac538837c564a3bc53 Mon Sep 17 00:00:00 2001 From: Alix JEUDI--LEMOINE Date: Tue, 17 Dec 2024 11:08:19 +0100 Subject: [PATCH] :white_check_mark: Added a battery of tests for code coverage --- tests/test_main.py | 202 ++++++++++++++++++++++++++++++++------------- 1 file changed, 143 insertions(+), 59 deletions(-) diff --git a/tests/test_main.py b/tests/test_main.py index 7246019..17ba9e0 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1,20 +1,22 @@ -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 +# TestClient provided by FastAPI +from fastapi.testclient import TestClient +# PyMongo for the database import pymongo +# Contains all constants +import app.config as config # Import the app from app.main import app # Database setup client = pymongo.MongoClient(config.MONGODB_URL, username=config.MONGODB_USERNAME, password=config.MONGODB_PASSWORD) +# Drop test database if exists if config.MONGODB_DATABASE in client.list_database_names(): try: client.drop_database(config.MONGODB_DATABASE) @@ -24,69 +26,151 @@ if config.MONGODB_DATABASE in client.list_database_names(): # Create empty database db = client[config.MONGODB_DATABASE] - +# TestClient with the app imported from main client = TestClient(app) -def test_number_one(): - print("OK") - -""" -def test_read_item(): - response = client.get("/items/foo", headers={"X-Token": "coneofsilence"}) +def test_register_user(): + response = client.post("/register", json={"username": "testuser", "password": "testpassword"}) assert response.status_code == 200 - assert response.json() == { - "id": "foo", - "title": "Foo", - "description": "There goes my hero", - } + data = response.json() + assert "access_token" in data + assert "token_type" in data + assert "user_id" in data + response = client.post("/register", json={"username": "testuser", "password": "testpassword"}) + assert response.status_code == 409 # Conflict - Cannot create two times the same user -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_login_user(): + response = client.post("/login", data={"username": "testuser", "password": "testpassword"}) + assert response.status_code == 200 + data = response.json() + assert "access_token" in data + assert "token_type" in data + assert "user_id" in data + + response = client.post("/login", data={"username": "testuser", "password": "BADpassword"}) + assert response.status_code == 401 # Unauthorized, bad pwd + +def test_add_pin(): + login_response = client.post("/login", data={"username": "testuser", "password": "testpassword"}) + token = login_response.json()["access_token"] + + response = client.post("/pin/add", json={"title": "Test Pin", "description": "Test Description"}, headers={"Authorization": f"Bearer {token}"}) + assert response.status_code == 200 + data = response.json() + assert "id" in data + +def test_list_pins(): + login_response = client.post("/login", data={"username": "testuser", "password": "testpassword"}) + token = login_response.json()["access_token"] + + response = client.get("/pins", headers={"Authorization": f"Bearer {token}"}) + assert response.status_code == 200 + data = response.json() + assert isinstance(data, list) + +def test_get_pin(): + login_response = client.post("/login", data={"username": "testuser", "password": "testpassword"}) + token = login_response.json()["access_token"] + + add_pin_response = client.post("/pin/add", json={"title": "Test Pin", "description": "Test Description"}, headers={"Authorization": f"Bearer {token}"}) + pin_id = add_pin_response.json()["id"] + + response = client.get(f"/pin/{pin_id}", headers={"Authorization": f"Bearer {token}"}) + assert response.status_code == 200 + data = response.json() + assert data["title"] == "Test Pin" + assert data["description"] == "Test Description" + # Test wrong format + response = client.get(f"/pin/randomIdThatDoesntExists", headers={"Authorization": f"Bearer {token}"}) + assert response.status_code == 422 -def test_read_nonexistent_item(): - response = client.get("/items/baz", headers={"X-Token": "coneofsilence"}) + # Test inexistant id + response = client.get(f"/pin/123456789987654321abcdef", headers={"Authorization": f"Bearer {token}"}) assert response.status_code == 404 - assert response.json() == {"detail": "Item not found"} + + +def test_update_pin(): + login_response = client.post("/login", data={"username": "testuser", "password": "testpassword"}) + token = login_response.json()["access_token"] + + add_pin_response = client.post("/pin/add", json={"title": "Test Pin", "description": "Test Description"}, headers={"Authorization": f"Bearer {token}"}) + pin_id = add_pin_response.json()["id"] + + update_response = client.patch(f"/pin/{pin_id}", json={"title": "Updated Pin", "description": "Updated Description"}, headers={"Authorization": f"Bearer {token}"}) + assert update_response.status_code == 200 + + get_pin_response = client.get(f"/pin/{pin_id}", headers={"Authorization": f"Bearer {token}"}) + data = get_pin_response.json() + assert data["title"] == "Updated Pin" + assert data["description"] == "Updated Description" + + # Test wrong format + response = client.get(f"/pin/randomIdThatDoesntExists", headers={"Authorization": f"Bearer {token}"}) + assert response.status_code == 422 + + # Test inexistant id + response = client.get(f"/pin/123456789987654321abcdef", headers={"Authorization": f"Bearer {token}"}) + assert response.status_code == 404 + +def test_add_friend(): + login_response = client.post("/login", data={"username": "testuser", "password": "testpassword"}) + token = login_response.json()["access_token"] + + # Register another user that will be used as a friend + response = client.post("/register", json={"username": "testfriend", "password": "testpassword"}) + user_id = response.json()["user_id"] -def test_create_item(): - response = client.post( - "/items/", - headers={"X-Token": "coneofsilence"}, - json={"id": "foobar", "title": "Foo Bar", "description": "The Foo Barters"}, - ) + friend_data = {"friend_user_id": user_id} + + response = client.post("/friend/add", json=friend_data, headers={"Authorization": f"Bearer {token}"}) + assert response.status_code == 200 + data = response.json() + assert "id" in data + + friend_data = {"friend_user_id": str(db["users"].find_one({"username": "testuser"})["_id"])} + + response = client.post("/friend/add", json=friend_data, headers={"Authorization": f"Bearer {token}"}) + assert response.status_code == 409 # Cannot add yourself as a friend + +def test_list_friends(): + login_response = client.post("/login", data={"username": "testuser", "password": "testpassword"}) + token = login_response.json()["access_token"] + + response = client.get("/friends", headers={"Authorization": f"Bearer {token}"}) + assert response.status_code == 200 + data = response.json() + assert isinstance(data, dict) + +def test_get_friend(): + login_response = client.post("/login", data={"username": "testuser", "password": "testpassword"}) + token = login_response.json()["access_token"] + + friend_id = str(db["friends"].find_one({})["_id"]) + response = client.get(f"/friend/{friend_id}", headers={"Authorization": f"Bearer {token}"}) + assert response.status_code == 200 + data = response.json() + assert "id" in data + assert "user_id" in data + assert "friend_user_id" in data + assert "status" in data + + # Test wrong format + response = client.get(f"/friend/randomIdThatDoesntExists", headers={"Authorization": f"Bearer {token}"}) + assert response.status_code == 422 + + # Test inexistant id + response = client.get(f"/friend/123456789987654321abcdef", headers={"Authorization": f"Bearer {token}"}) + assert response.status_code == 404 + +def test_search_users(): + login_response = client.post("/login", data={"username": "testuser", "password": "testpassword"}) + token = login_response.json()["access_token"] + + response = client.get("/users?name=testuser", headers={"Authorization": f"Bearer {token}"}) 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"} -""" \ No newline at end of file + data = response.json() + assert isinstance(data, list) + assert data[0]["username"] == "testuser" \ No newline at end of file