From 17a893359717bf747aa048de70a2b45843626d87 Mon Sep 17 00:00:00 2001 From: GotthardG <51994228+GotthardG@users.noreply.github.com> Date: Fri, 31 Jan 2025 21:50:10 +0100 Subject: [PATCH] Clean up testfunctions.ipynb by removing redundant code Removed outdated and unused code snippets from testfunctions.ipynb to streamline the notebook. This improves readability and reduces clutter, ensuring the file contains only relevant and functional code samples. --- backend/tests/test_contact.py | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/backend/tests/test_contact.py b/backend/tests/test_contact.py index 6f0d511..b8df0a7 100644 --- a/backend/tests/test_contact.py +++ b/backend/tests/test_contact.py @@ -4,9 +4,21 @@ from backend.main import app client = TestClient(app) -def test_create_contact_success(): +def authenticate(client): response = client.post( - "/contact", + "/auth/login", json={"username": "testuser", "password": "testpassword"} + ) + assert response.status_code == 200 + return response.json()["access_token"] + + +def test_create_contact_success(): + token = authenticate(client) + headers = {"Authorization": f"Bearer {token}"} + + response = client.post( + "/protected/contacts", + headers=headers, json={ "pgroups": "p20001", "firstname": "John", @@ -15,7 +27,6 @@ def test_create_contact_success(): "phone_number": "+000000000", }, ) - # Assert success and verify response structure assert response.status_code == 201 json_response = response.json() assert json_response["firstname"] == "John" @@ -23,6 +34,22 @@ def test_create_contact_success(): assert json_response["email"] == "john.rambo@example.com" +def test_create_contact_unauthorized(): + # Omit Authorization header to simulate unauthorized access + response = client.post( + "/protected/contacts", + json={ + "pgroups": "p20001", + "firstname": "John", + "lastname": "Rambo", + "email": "john.rambo@example.com", + "phone_number": "+000000000", + }, + ) + assert response.status_code == 401 + assert response.json()["detail"] == "Not authenticated" + + def test_create_contact_already_exists(): # Ensure that the route fails gracefully if contact exists client.post(