File size: 6,056 Bytes
1fff71f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
"""
Pytest configuration and fixtures for webapp testing.
Feature: 012-profile-contact-ui
"""

import os
import sqlite3
import tempfile
from typing import Generator

import pytest
from flask import Flask
from flask.testing import FlaskClient

# Set test environment variables before importing app
os.environ["SECRET_KEY"] = "test-secret-key"
os.environ["BACKEND_API_URL"] = "http://test-api:8080/v1"
os.environ["HF_CLIENT_ID"] = "test-client-id"
os.environ["HF_CLIENT_SECRET"] = "test-client-secret"
os.environ["HF_REDIRECT_URI"] = "http://localhost:5000/callback"


@pytest.fixture
def app() -> Generator[Flask, None, None]:
    """Create and configure Flask app for testing."""
    from webapp.src.app import create_app

    # Create temporary database file
    db_fd, db_path = tempfile.mkstemp(suffix=".db")
    os.environ["DATABASE_PATH"] = db_path

    # Create app
    app = create_app()
    app.config["TESTING"] = True
    app.config["WTF_CSRF_ENABLED"] = False

    # Initialize database schema
    with open("webapp/migrations/001_create_tables.sql", "r") as f:
        schema = f.read()

    conn = sqlite3.connect(db_path)
    conn.executescript(schema)
    conn.close()

    yield app

    # Cleanup
    os.close(db_fd)
    os.unlink(db_path)


@pytest.fixture
def client(app: Flask) -> FlaskClient:
    """Create test client for making requests."""
    return app.test_client()


@pytest.fixture
def runner(app: Flask):
    """Create CLI test runner."""
    return app.test_cli_runner()


@pytest.fixture
def authenticated_client(client: FlaskClient, monkeypatch) -> FlaskClient:
    """Create authenticated test client with mocked OAuth."""
    # Mock OAuth flow
    def mock_authorize_redirect(*args, **kwargs):
        from flask import redirect

        return redirect("/callback?code=test-code")

    def mock_fetch_token(*args, **kwargs):
        return {"access_token": "test-token", "token_type": "Bearer"}

    def mock_fetch_userinfo(*args, **kwargs):
        return {
            "preferred_username": "testuser",
            "name": "Test User",
            "picture": "https://example.com/avatar.jpg",
        }

    # Import and patch auth service
    from webapp.src.services.auth_service import auth_service

    monkeypatch.setattr(auth_service.hf, "authorize_redirect", mock_authorize_redirect)
    monkeypatch.setattr(auth_service, "fetch_token", mock_fetch_token)
    monkeypatch.setattr(auth_service, "fetch_userinfo", mock_fetch_userinfo)

    # Mock backend API calls
    def mock_create_session(*args, **kwargs):
        return {
            "id": kwargs.get("session_id", "testuser_session"),
            "user_id": "testuser",
            "created_at": "2025-01-01T00:00:00Z",
        }

    from webapp.src.services import backend_client

    monkeypatch.setattr(
        backend_client.backend_client, "create_session", mock_create_session
    )

    # Perform login flow
    with client.session_transaction() as sess:
        sess["user_id"] = "testuser"
        sess["display_name"] = "Test User"
        sess["profile_picture_url"] = "https://example.com/avatar.jpg"
        sess["session_id"] = "testuser_session"
        sess["access_token"] = "test-token"

    return client


@pytest.fixture
def test_user_profile():
    """Create test user profile data."""
    return {
        "user_id": "testuser",
        "display_name": "Test User",
        "profile_picture_url": "https://example.com/avatar.jpg",
        "session_id": "testuser_session",
        "created_at": "2025-01-01T00:00:00Z",
    }


@pytest.fixture
def test_contact_session():
    """Create test contact session data."""
    return {
        "session_id": "testuser_12345678-1234-1234-1234-123456789abc",
        "user_id": "testuser",
        "display_name": "Alice Smith",
        "last_interaction": "2025-01-01T12:00:00Z",
        "created_at": "2025-01-01T10:00:00Z",
    }


@pytest.fixture
def mock_backend_api(monkeypatch):
    """Mock all backend API calls for testing."""
    from webapp.src.services import backend_client

    class MockBackendAPI:
        """Mock backend API client."""

        def __init__(self):
            self.sessions = {}
            self.messages = {}

        def create_session(self, session_id: str, user_id: str, is_reference: bool = False):
            self.sessions[session_id] = {
                "id": session_id,
                "user_id": user_id,
                "is_reference": is_reference,
                "created_at": "2025-01-01T00:00:00Z",
            }
            self.messages[session_id] = []
            return self.sessions[session_id]

        def get_session(self, session_id: str):
            if session_id not in self.sessions:
                raise backend_client.BackendAPIError(f"Session {session_id} not found")
            return {
                **self.sessions[session_id],
                "messages": self.messages.get(session_id, []),
            }

        def send_message(self, session_id: str, content: str, mode: str = "chat", sender: str = "user"):
            if session_id not in self.messages:
                self.messages[session_id] = []

            message = {
                "message_id": f"msg_{len(self.messages[session_id])}",
                "session_id": session_id,
                "mode": mode,
                "content": content,
                "sender": sender if mode == "chat" else None,
                "created_at": "2025-01-01T12:00:00Z",
            }
            self.messages[session_id].append(message)
            return message

        def list_sessions(self, user_id: str):
            return [s for s in self.sessions.values() if s["user_id"] == user_id]

        def get_messages(self, session_id: str, mode: str = None):
            messages = self.messages.get(session_id, [])
            if mode:
                messages = [m for m in messages if m["mode"] == mode]
            return messages

    mock_api = MockBackendAPI()
    monkeypatch.setattr(backend_client, "backend_client", mock_api)

    return mock_api