backend.tests.api.test_app

 1import pytest
 2from flask import json
 3
 4
 5# ============================================================================
 6# Test General Application Endpoints
 7# ============================================================================
 8
 9def test_healthcheck(client):
10    """
11    Endpoint healthcheck test to check service status.
12
13    Verifies that the application is running and responding to the primary monitoring endpoint.
14    Expects a status of 200 and a service ID in JSON format.
15    """
16    # Wyślij żądanie GET do endpointu monitorowania
17    response = client.get('/health')
18
19    # Sprawdź kod statusu
20    assert response.status_code == 200
21
22    # Sprawdź poprawność zwróconej struktury JSON
23    expected_response = {"status": "ok", "service": "camera-verification-api"}
24    assert response.get_json() == expected_response
25
26
27# ============================================================================
28# Test HTTP Error Handling
29# ============================================================================
30
31def test_404_not_found(client):
32    """
33    Tests handling requests to non-existent resources (Error 404).
34
35    Checks whether the application correctly returns a 404 error code for unknown paths.
36    It also verifies whether the response content is appropriate (HTML for browsers or JSON for APIs),
37    depending on the global error handling configuration.
38    """
39
40    # Próba dostępu do losowej, nieistniejącej ścieżki
41    response = client.get('/non/existent/route/12345')
42
43    # Weryfikacja kodu błędu
44    assert response.status_code == 404
45
46    # Analiza typu zawartości odpowiedzi (Content-Type)
47    if response.content_type == "text/html; charset=utf-8":
48        # Jeśli aplikacja zwraca szablon HTML (np. templates/404.html)
49        assert b"404" in response.data or b"Not Found" in response.data
50    elif response.is_json:
51        # Jeśli aplikacja zwraca błąd w formacie JSON
52        data = response.get_json()
53        assert "error" in data or "message" in data
54
55
56def test_method_not_allowed(client):
57    """
58    Test behavior using a disallowed HTTP method (Error 405).
59
60    Verifies that Flask routing correctly blocks requests (e.g., POST)
61    to endpoints defined only for another method (e.g., GET).
62    """
63
64    # Próba wysłania POST na endpoint tylko do odczytu
65    response = client.post('/api/workers/1', json={})
66
67    # Flask powinien automatycznie zwrócić 405 Method Not Allowed
68    assert response.status_code == 405
def test_healthcheck(client):
10def test_healthcheck(client):
11    """
12    Endpoint healthcheck test to check service status.
13
14    Verifies that the application is running and responding to the primary monitoring endpoint.
15    Expects a status of 200 and a service ID in JSON format.
16    """
17    # Wyślij żądanie GET do endpointu monitorowania
18    response = client.get('/health')
19
20    # Sprawdź kod statusu
21    assert response.status_code == 200
22
23    # Sprawdź poprawność zwróconej struktury JSON
24    expected_response = {"status": "ok", "service": "camera-verification-api"}
25    assert response.get_json() == expected_response

Endpoint healthcheck test to check service status.

Verifies that the application is running and responding to the primary monitoring endpoint. Expects a status of 200 and a service ID in JSON format.

def test_404_not_found(client):
32def test_404_not_found(client):
33    """
34    Tests handling requests to non-existent resources (Error 404).
35
36    Checks whether the application correctly returns a 404 error code for unknown paths.
37    It also verifies whether the response content is appropriate (HTML for browsers or JSON for APIs),
38    depending on the global error handling configuration.
39    """
40
41    # Próba dostępu do losowej, nieistniejącej ścieżki
42    response = client.get('/non/existent/route/12345')
43
44    # Weryfikacja kodu błędu
45    assert response.status_code == 404
46
47    # Analiza typu zawartości odpowiedzi (Content-Type)
48    if response.content_type == "text/html; charset=utf-8":
49        # Jeśli aplikacja zwraca szablon HTML (np. templates/404.html)
50        assert b"404" in response.data or b"Not Found" in response.data
51    elif response.is_json:
52        # Jeśli aplikacja zwraca błąd w formacie JSON
53        data = response.get_json()
54        assert "error" in data or "message" in data

Tests handling requests to non-existent resources (Error 404).

Checks whether the application correctly returns a 404 error code for unknown paths. It also verifies whether the response content is appropriate (HTML for browsers or JSON for APIs), depending on the global error handling configuration.

def test_method_not_allowed(client):
57def test_method_not_allowed(client):
58    """
59    Test behavior using a disallowed HTTP method (Error 405).
60
61    Verifies that Flask routing correctly blocks requests (e.g., POST)
62    to endpoints defined only for another method (e.g., GET).
63    """
64
65    # Próba wysłania POST na endpoint tylko do odczytu
66    response = client.post('/api/workers/1', json={})
67
68    # Flask powinien automatycznie zwrócić 405 Method Not Allowed
69    assert response.status_code == 405

Test behavior using a disallowed HTTP method (Error 405).

Verifies that Flask routing correctly blocks requests (e.g., POST) to endpoints defined only for another method (e.g., GET).