From 2349c4c93f9419d99ac61d5a745703c3a68fa086 Mon Sep 17 00:00:00 2001 From: Maxence Date: Mon, 27 Jan 2025 17:29:39 +0000 Subject: [PATCH] division test file into several files + add pytest to order test --- tests/requirements.txt | 3 +- tests/test_friends.py | 51 +++++++++++++++ tests/test_main.py | 144 ----------------------------------------- tests/test_pins.py | 68 +++++++++++++++++++ tests/test_user.py | 35 ++++++++++ 5 files changed, 156 insertions(+), 145 deletions(-) create mode 100644 tests/test_friends.py create mode 100644 tests/test_pins.py create mode 100644 tests/test_user.py diff --git a/tests/requirements.txt b/tests/requirements.txt index 3bcfd29..518ec9e 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -4,4 +4,5 @@ pymongo==4.10.1 uvicorn==0.32.0 joserfc==1.0.1 pytest==8.3.4 -pytest-cov==6.0.0 \ No newline at end of file +pytest-cov==6.0.0 +pytest-order==1.3.0 diff --git a/tests/test_friends.py b/tests/test_friends.py new file mode 100644 index 0000000..1314016 --- /dev/null +++ b/tests/test_friends.py @@ -0,0 +1,51 @@ +from test_main import * + +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"] + + 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 \ No newline at end of file diff --git a/tests/test_main.py b/tests/test_main.py index eb5f144..fae3e24 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -29,152 +29,8 @@ db = client[config.MONGODB_DATABASE] # TestClient with the app imported from main client = TestClient(app) -def test_register_user(): - response = client.post("/register", json={"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("/register", json={"username": "testuser", "password": "testpassword"}) - assert response.status_code == 409 # Conflict - Cannot create two times the same user -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", "location": "Test location", "files": ["Test file 1"]}, 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", "location": "Test location", "files": ["Test file 1"]}, 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" - assert data["location"] == "Test location" - assert data["files"] == ["Test file 1"] - - # 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_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", "location": "Test location", "files": ["Test file 1"]}, 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", "location": "Updated location", "files": ["Updated file 1"]}, 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" - assert data["location"] == "Updated location" - assert data["files"] == ["Updated file 1"] - - # 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"] - - 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 - data = response.json() - assert isinstance(data, list) - assert data[0]["username"] == "testuser" \ No newline at end of file diff --git a/tests/test_pins.py b/tests/test_pins.py new file mode 100644 index 0000000..2286b57 --- /dev/null +++ b/tests/test_pins.py @@ -0,0 +1,68 @@ +from test_main import * + +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", "location": "Test location", "files": ["Test file 1"]}, 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", "location": "Test location", "files": ["Test file 1"]}, 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" + assert data["location"] == "Test location" + assert data["files"] == ["Test file 1"] + + # 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_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", "location": "Test location", "files": ["Test file 1"]}, 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", "location": "Updated location", "files": ["Updated file 1"]}, 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" + assert data["location"] == "Updated location" + assert data["files"] == ["Updated file 1"] + + # 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 \ No newline at end of file diff --git a/tests/test_user.py b/tests/test_user.py new file mode 100644 index 0000000..ed3f76c --- /dev/null +++ b/tests/test_user.py @@ -0,0 +1,35 @@ +from test_main import * +import pytest + +@pytest.mark.order(1) +def test_register_user(): + response = client.post("/register", json={"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("/register", json={"username": "testuser", "password": "testpassword"}) + assert response.status_code == 409 # Conflict - Cannot create two times the same user + +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_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 + data = response.json() + assert isinstance(data, list) + assert data[0]["username"] == "testuser" \ No newline at end of file