diff --git a/tests/test_friends.py b/tests/test_friends.py index 1314016..146bff2 100644 --- a/tests/test_friends.py +++ b/tests/test_friends.py @@ -1,9 +1,6 @@ from test_main import * -def test_add_friend(): - login_response = client.post("/login", data={"username": "testuser", "password": "testpassword"}) - token = login_response.json()["access_token"] - +def test_add_friend(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"] @@ -15,24 +12,19 @@ def test_add_friend(): data = response.json() assert "id" in data +def test_add_friend_myself(token): 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"] - + +def test_list_friends(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"] - +def test_get_friend(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 @@ -42,10 +34,10 @@ def test_get_friend(): assert "friend_user_id" in data assert "status" in data - # Test wrong format +def test_get_friend_wrong_format(token): response = client.get(f"/friend/randomIdThatDoesntExists", headers={"Authorization": f"Bearer {token}"}) assert response.status_code == 422 - # Test inexistant id +def test_get_friend_unknown_id(token): 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 fae3e24..2205353 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1,3 +1,4 @@ +import pytest import sys from pathlib import Path sys.path.append(str(Path(__file__).absolute().parent.parent)) @@ -29,7 +30,11 @@ db = client[config.MONGODB_DATABASE] # TestClient with the app imported from main client = TestClient(app) - +@pytest.fixture +def token(): + login_response = client.post("/login", data={"username": "testuser", "password": "testpassword"}) + token = login_response.json()["access_token"] + return token diff --git a/tests/test_pins.py b/tests/test_pins.py index 2286b57..2e96ec9 100644 --- a/tests/test_pins.py +++ b/tests/test_pins.py @@ -1,27 +1,18 @@ from test_main import * -def test_add_pin(): - login_response = client.post("/login", data={"username": "testuser", "password": "testpassword"}) - token = login_response.json()["access_token"] - +def test_add_pin(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"] - +def test_list_pins(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"] - +def test_get_pin(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"] @@ -33,18 +24,17 @@ def test_get_pin(): assert data["location"] == "Test location" assert data["files"] == ["Test file 1"] - # Test wrong format + +def test_get_pin_wrong_format(token): response = client.get(f"/pin/randomIdThatDoesntExists", headers={"Authorization": f"Bearer {token}"}) assert response.status_code == 422 - # Test inexistant id +def test_get_pin_unknown_id(token): 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"] + +def test_update_pin(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"] @@ -59,10 +49,11 @@ def test_update_pin(): assert data["location"] == "Updated location" assert data["files"] == ["Updated file 1"] - # Test wrong format + +def test_update_wrong_format(token): response = client.get(f"/pin/randomIdThatDoesntExists", headers={"Authorization": f"Bearer {token}"}) assert response.status_code == 422 - # Test inexistant id +def test_update_wrong_unknown_id(token): 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 index ed3f76c..eb79611 100644 --- a/tests/test_user.py +++ b/tests/test_user.py @@ -10,6 +10,8 @@ def test_register_user(): assert "token_type" in data assert "user_id" in data + +def test_register_same_user(): response = client.post("/register", json={"username": "testuser", "password": "testpassword"}) assert response.status_code == 409 # Conflict - Cannot create two times the same user @@ -21,13 +23,12 @@ def test_login_user(): assert "token_type" in data assert "user_id" in data + +def test_login_user_bad_password(): 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"] - +def test_search_users(token): response = client.get("/users?name=testuser", headers={"Authorization": f"Bearer {token}"}) assert response.status_code == 200 data = response.json()