-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_services.py
More file actions
338 lines (274 loc) · 10.6 KB
/
test_services.py
File metadata and controls
338 lines (274 loc) · 10.6 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
from unittest.mock import MagicMock, patch
import pytest
from app.modules.auth.schemas import UserCreate
from app.modules.auth.service import (
create_user,
get_or_create_oauth_user,
hash_password,
verify_password,
)
from app.modules.events.schemas import EventLink, EventOut, LinkType
from app.modules.events.service import generate_json_schema_for_event
from app.modules.fields.schemas import FieldOut, FieldType
from app.modules.tags.schemas import TagOut
from app.shared.service import assert_db_empty
@pytest.fixture
def sample_event_out():
"""Sample EventOut for schema generation testing"""
return EventOut(
id=1,
name="user_signup",
description="User completes registration",
links=[
EventLink(
type=LinkType.figma,
url="https://figma.com/signup",
label="Signup Design",
)
],
tags=[
TagOut(
id="authentication",
description="Auth related events",
created_at="2024-01-01T00:00:00Z",
updated_at="2024-01-01T00:00:00Z",
),
TagOut(
id="onboarding",
description="User onboarding flow",
created_at="2024-01-01T00:00:00Z",
updated_at="2024-01-01T00:00:00Z",
),
],
fields=[
FieldOut(
id=1,
name="user_id",
description="Unique user identifier",
field_type=FieldType.string,
created_at="2024-01-01T00:00:00Z",
updated_at="2024-01-01T00:00:00Z",
),
FieldOut(
id=2,
name="signup_method",
description="How user signed up",
field_type=FieldType.string,
created_at="2024-01-01T00:00:00Z",
updated_at="2024-01-01T00:00:00Z",
),
],
created_at="2024-01-01T00:00:00Z",
updated_at="2024-01-01T00:00:00Z",
)
# Test events service functions
def test_generate_json_schema_basic(sample_event_out):
"""Test basic JSON schema generation"""
schema = generate_json_schema_for_event(sample_event_out)
assert isinstance(schema, dict)
assert schema["type"] == "object"
assert "properties" in schema
properties = schema["properties"]
assert "user_id" in properties
assert "signup_method" in properties
# Check field types are correctly mapped
assert properties["user_id"]["type"] == "string"
assert properties["signup_method"]["type"] == "string"
def test_generate_json_schema_with_descriptions(sample_event_out):
"""Test schema generation with descriptions enabled"""
schema = generate_json_schema_for_event(sample_event_out, include_descriptions=True)
properties = schema["properties"]
assert "description" in properties["user_id"]
assert properties["user_id"]["description"] == "Unique user identifier"
def test_generate_json_schema_without_descriptions(sample_event_out):
"""Test schema generation with descriptions disabled"""
schema = generate_json_schema_for_event(
sample_event_out, include_descriptions=False
)
properties = schema["properties"]
assert "description" not in properties["user_id"]
def test_generate_json_schema_additional_properties_true(sample_event_out):
"""Test schema generation with additional properties allowed"""
schema = generate_json_schema_for_event(
sample_event_out, additional_properties=True
)
assert schema["additionalProperties"] is True
def test_generate_json_schema_additional_properties_false(sample_event_out):
"""Test schema generation with additional properties disabled"""
schema = generate_json_schema_for_event(
sample_event_out, additional_properties=False
)
assert schema["additionalProperties"] is False
def test_generate_json_schema_different_field_types():
"""Test schema generation with different field types"""
event = EventOut(
id=1,
name="test_event",
description="Test event",
links=[],
tags=[],
fields=[
FieldOut(
id=1,
name="string_field",
field_type=FieldType.string,
created_at="2024-01-01T00:00:00Z",
updated_at="2024-01-01T00:00:00Z",
),
FieldOut(
id=2,
name="number_field",
field_type=FieldType.number,
created_at="2024-01-01T00:00:00Z",
updated_at="2024-01-01T00:00:00Z",
),
FieldOut(
id=3,
name="boolean_field",
field_type=FieldType.boolean,
created_at="2024-01-01T00:00:00Z",
updated_at="2024-01-01T00:00:00Z",
),
FieldOut(
id=4,
name="array_field",
field_type=FieldType.array,
created_at="2024-01-01T00:00:00Z",
updated_at="2024-01-01T00:00:00Z",
),
FieldOut(
id=5,
name="object_field",
field_type=FieldType.object,
created_at="2024-01-01T00:00:00Z",
updated_at="2024-01-01T00:00:00Z",
),
FieldOut(
id=6,
name="integer_field",
field_type=FieldType.integer,
created_at="2024-01-01T00:00:00Z",
updated_at="2024-01-01T00:00:00Z",
),
],
created_at="2024-01-01T00:00:00Z",
updated_at="2024-01-01T00:00:00Z",
)
schema = generate_json_schema_for_event(event)
properties = schema["properties"]
assert properties["string_field"]["type"] == "string"
assert properties["number_field"]["type"] == "number"
assert properties["boolean_field"]["type"] == "boolean"
assert properties["array_field"]["type"] == "array"
assert properties["object_field"]["type"] == "object"
assert properties["integer_field"]["type"] == "integer"
# Test auth service functions
def test_hash_password():
"""Test password hashing"""
password = "test_password_123"
hashed = hash_password(password)
assert isinstance(hashed, str)
assert hashed != password # Should be different from original
assert len(hashed) > 0
def test_verify_password():
"""Test password verification"""
password = "test_password_123"
hashed = hash_password(password)
# Correct password should verify
assert verify_password(password, hashed) is True
# Wrong password should fail
assert verify_password("wrong_password", hashed) is False
def test_verify_password_with_invalid_hash():
"""Test password verification with invalid hash"""
# Test with a completely invalid hash that will trigger an exception
try:
result = verify_password("any_password", "invalid_hash")
assert result is False
except Exception:
# If it raises an exception, that's also acceptable behavior
# for an invalid hash, just verify it doesn't crash the app
pass
@patch("app.modules.auth.crud.create_user")
def test_create_user_success(mock_create_user, override_get_db):
"""Test successful user creation"""
db = next(override_get_db())
# Mock successful user creation
mock_user = MagicMock()
mock_user.email = "test@example.com"
mock_create_user.return_value = mock_user
user_data = UserCreate(email="test@example.com", password="password123")
result = create_user(db, user_data)
assert result.email == "test@example.com"
mock_create_user.assert_called_once()
@patch("app.modules.auth.crud.create_user")
def test_create_user_duplicate_email(mock_create_user, override_get_db):
"""Test user creation with duplicate email"""
from fastapi import HTTPException
from sqlalchemy.exc import IntegrityError
db = next(override_get_db())
# Mock IntegrityError for duplicate email
mock_create_user.side_effect = IntegrityError("duplicate", None, None)
user_data = UserCreate(email="duplicate@example.com", password="password123")
with pytest.raises(HTTPException) as exc_info:
create_user(db, user_data)
assert exc_info.value.status_code == 400
assert "already exists" in exc_info.value.detail
@patch("app.modules.auth.crud.get_or_create_oauth_user")
def test_get_or_create_oauth_user(mock_get_or_create, override_get_db):
"""Test OAuth user creation/retrieval"""
db = next(override_get_db())
mock_user = MagicMock()
mock_user.email = "oauth@example.com"
mock_user.oauth_provider = "github"
mock_get_or_create.return_value = mock_user
result = get_or_create_oauth_user(db, email="oauth@example.com", provider="github")
assert result.email == "oauth@example.com"
assert result.oauth_provider == "github"
mock_get_or_create.assert_called_once_with(
db, email="oauth@example.com", provider="github"
)
# Test shared service functions
def test_assert_db_empty_with_empty_db(override_get_db):
"""Test assert_db_empty with empty database"""
db = next(override_get_db())
# Should not raise exception when database is empty
# (This test uses the actual database state from conftest.py)
try:
assert_db_empty(db)
except Exception:
# If it fails, database isn't empty, which is fine for this test
pass
def test_assert_db_empty_with_events(auth_client, override_get_db):
"""Test assert_db_empty with events in database"""
from fastapi import HTTPException
# Create an event first
auth_client.post(
"/v1/events/",
json={
"name": "test_event_for_assert_db_empty",
"description": "Test event",
"tags": [],
"fields": [],
},
)
db = next(override_get_db())
with pytest.raises(HTTPException) as exc_info:
assert_db_empty(db)
assert exc_info.value.status_code == 405 # Correct status code from source
assert "empty database" in exc_info.value.detail
def test_generate_json_schema_event_with_no_fields():
"""Test schema generation for event with no fields"""
event = EventOut(
id=1,
name="simple_event",
description="Event with no fields",
links=[],
tags=[],
fields=[], # No fields
created_at="2024-01-01T00:00:00Z",
updated_at="2024-01-01T00:00:00Z",
)
schema = generate_json_schema_for_event(event)
assert schema["type"] == "object"
assert schema["properties"] == {} # Empty properties
assert "additionalProperties" in schema