Skip to content

Commit 83d62be

Browse files
committed
refactor(fdc): Use named app fixture and dc_client fixture in test_data_connect.py
Aligned integration/test_data_connect.py with test_functions.py and test_db.py by using a named app fixture ('integration-dataconnect'), overriding default_app with pass, and injecting dc_client fixture into test methods.
1 parent 91f3475 commit 83d62be

1 file changed

Lines changed: 37 additions & 47 deletions

File tree

integration/test_data_connect.py

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,21 @@ def integration_conf(request):
2929

3030
return conftest.integration_conf(request)
3131

32-
@pytest.fixture(scope='module', autouse=True)
33-
def default_app(request):
32+
@pytest.fixture(scope='module')
33+
def app(request):
3434
cred, project_id = integration_conf(request)
35-
try:
36-
firebase_admin.delete_app(firebase_admin.get_app())
37-
except ValueError:
38-
pass
3935
return firebase_admin.initialize_app(
40-
cred, options={'projectId': project_id})
36+
cred, options={'projectId': project_id}, name='integration-dataconnect')
37+
38+
@pytest.fixture(scope='module', autouse=True)
39+
def default_app():
40+
# Overwrites the default_app fixture in conftest.py.
41+
# This test suite should not use the default app. Use the app fixture instead.
42+
pass
43+
44+
@pytest.fixture
45+
def dc_client(app):
46+
return dataconnect.client(CONNECTOR_CONFIG, app=app)
4147

4248

4349
CONNECTOR_CONFIG = dataconnect.ConnectorConfig(
@@ -147,6 +153,10 @@ def default_app(request):
147153
@pytest.fixture(autouse=True)
148154
def setup_and_cleanup_database():
149155
"""Initializes database via seed.sh before each test and wipes it via cleanup.sh afterwards."""
156+
if not os.environ.get('DATA_CONNECT_EMULATOR_HOST'):
157+
yield
158+
return
159+
150160
script_dir = os.path.dirname(__file__)
151161
seed_script = os.path.join(script_dir, 'emulators', 'seed.sh')
152162
cleanup_script = os.path.join(script_dir, 'emulators', 'cleanup.sh')
@@ -176,39 +186,34 @@ def setup_and_cleanup_database():
176186
class TestExecuteGraphql:
177187
"""Integration tests for execute_graphql method."""
178188

179-
def test_execute_graphql_query(self):
189+
def test_execute_graphql_query(self, dc_client):
180190
"""Tests executing a query via execute_graphql."""
181-
dc_client = dataconnect.client(CONNECTOR_CONFIG)
182191
resp = dc_client.execute_graphql(QUERY_LIST_USERS)
183192
assert sorted(resp.data['users'], key=lambda user: user['id']) == sorted(
184193
INITIAL_STATE['users'], key=lambda user: user['id']
185194
)
186195

187-
def test_execute_graphql_query_with_variables(self):
196+
def test_execute_graphql_query_with_variables(self, dc_client):
188197
"""Tests query execution with variables."""
189-
dc_client = dataconnect.client(CONNECTOR_CONFIG)
190198
user_id = INITIAL_STATE['users'][0]['id']
191199
options = dataconnect.GraphqlOptions(variables={'id': {'id': user_id}})
192200
resp = dc_client.execute_graphql(QUERY_GET_USER_BY_ID, options=options)
193201
assert resp.data['user'] == INITIAL_STATE['users'][0]
194202

195-
def test_execute_graphql_operation_name_multiple_queries(self):
203+
def test_execute_graphql_operation_name_multiple_queries(self, dc_client):
196204
"""Tests operation_name with multi-query document."""
197-
dc_client = dataconnect.client(CONNECTOR_CONFIG)
198205
options = dataconnect.GraphqlOptions(operation_name='ListEmails')
199206
resp = dc_client.execute_graphql(MULTIPLE_QUERIES, options=options)
200207
assert resp.data['emails'] == INITIAL_STATE['emails']
201208

202-
def test_execute_graphql_query_error_missing_variables(self):
209+
def test_execute_graphql_query_error_missing_variables(self, dc_client):
203210
"""Tests query error when required variables are missing."""
204-
dc_client = dataconnect.client(CONNECTOR_CONFIG)
205211
with pytest.raises(dataconnect.QueryError) as excinfo:
206212
dc_client.execute_graphql(QUERY_GET_USER_BY_ID)
207213
assert excinfo.value.code == 'query-error'
208214

209-
def test_execute_graphql_mutation(self):
215+
def test_execute_graphql_mutation(self, dc_client):
210216
"""Tests executing mutations via execute_graphql."""
211-
dc_client = dataconnect.client(CONNECTOR_CONFIG)
212217
fred_resp = dc_client.execute_graphql(UPSERT_FRED_USER)
213218
assert fred_resp.data['user_upsert']['id'] == FRED_USER['id']
214219

@@ -229,17 +234,15 @@ def test_execute_graphql_mutation(self):
229234
class TestExecuteGraphqlRead:
230235
"""Integration tests for execute_graphql_read method."""
231236

232-
def test_execute_graphql_read_query(self):
237+
def test_execute_graphql_read_query(self, dc_client):
233238
"""Tests read-only query execution."""
234-
dc_client = dataconnect.client(CONNECTOR_CONFIG)
235239
resp = dc_client.execute_graphql_read(QUERY_LIST_USERS)
236240
assert sorted(resp.data['users'], key=lambda user: user['id']) == sorted(
237241
INITIAL_STATE['users'], key=lambda user: user['id']
238242
)
239243

240-
def test_execute_graphql_read_mutation_fails(self):
244+
def test_execute_graphql_read_mutation_fails(self, dc_client):
241245
"""Tests that execute_graphql_read rejects mutation queries."""
242-
dc_client = dataconnect.client(CONNECTOR_CONFIG)
243246
with pytest.raises(exceptions.PermissionDeniedError):
244247
dc_client.execute_graphql_read(UPSERT_FRED_USER)
245248

@@ -250,43 +253,38 @@ class TestExecuteGraphqlImpersonation:
250253
class TestUserAuthPolicy:
251254
"""Integration tests for @auth(level: USER) policy."""
252255

253-
def test_execute_graphql_read_impersonated_authenticated(self):
256+
def test_execute_graphql_read_impersonated_authenticated(self, dc_client):
254257
"""Tests read query with authenticated impersonation."""
255-
dc_client = dataconnect.client(CONNECTOR_CONFIG)
256258
resp = dc_client.execute_graphql_read(
257259
QUERY_LIST_USERS_IMPERSONATION, options=OPTS_AUTHORIZED_FRED_CLAIMS
258260
)
259261
assert len(resp.data['users']) == 1
260262
assert resp.data['users'][0] == FRED_USER
261263

262-
def test_execute_graphql_impersonated_authenticated(self):
264+
def test_execute_graphql_impersonated_authenticated(self, dc_client):
263265
"""Tests query with authenticated impersonation."""
264-
dc_client = dataconnect.client(CONNECTOR_CONFIG)
265266
resp = dc_client.execute_graphql(
266267
QUERY_LIST_USERS_IMPERSONATION, options=OPTS_AUTHORIZED_FRED_CLAIMS
267268
)
268269
assert len(resp.data['users']) == 1
269270
assert resp.data['users'][0] == FRED_USER
270271

271-
def test_execute_graphql_impersonated_unauthenticated_fails(self):
272+
def test_execute_graphql_impersonated_unauthenticated_fails(self, dc_client):
272273
"""Tests query with unauthenticated impersonation fails."""
273-
dc_client = dataconnect.client(CONNECTOR_CONFIG)
274274
with pytest.raises(exceptions.UnauthenticatedError):
275275
dc_client.execute_graphql(
276276
QUERY_LIST_USERS_IMPERSONATION, options=OPTS_UNAUTHORIZED_CLAIMS
277277
)
278278

279-
def test_execute_graphql_impersonated_non_existing_claims(self):
279+
def test_execute_graphql_impersonated_non_existing_claims(self, dc_client):
280280
"""Tests query with non-existing user claims returns empty list."""
281-
dc_client = dataconnect.client(CONNECTOR_CONFIG)
282281
resp = dc_client.execute_graphql(
283282
QUERY_LIST_USERS_IMPERSONATION, options=OPTS_NON_EXISTING_CLAIMS
284283
)
285284
assert resp.data['users'] == []
286285

287-
def test_execute_graphql_impersonated_mutation_authenticated(self):
286+
def test_execute_graphql_impersonated_mutation_authenticated(self, dc_client):
288287
"""Tests mutation with authenticated impersonation."""
289-
dc_client = dataconnect.client(CONNECTOR_CONFIG)
290288
update_resp = dc_client.execute_graphql(
291289
UPDATE_FREDRICK_USER_IMPERSONATED, options=OPTS_AUTHORIZED_FRED_CLAIMS
292290
)
@@ -297,17 +295,15 @@ def test_execute_graphql_impersonated_mutation_authenticated(self):
297295
query_resp = dc_client.execute_graphql(QUERY_GET_USER_BY_ID, options=query_options)
298296
assert query_resp.data['user'] == FREDRICK_USER
299297

300-
def test_execute_graphql_impersonated_mutation_unauthenticated_fails(self):
298+
def test_execute_graphql_impersonated_mutation_unauthenticated_fails(self, dc_client):
301299
"""Tests mutation with unauthenticated impersonation fails."""
302-
dc_client = dataconnect.client(CONNECTOR_CONFIG)
303300
with pytest.raises(exceptions.UnauthenticatedError):
304301
dc_client.execute_graphql(
305302
UPDATE_FREDRICK_USER_IMPERSONATED, options=OPTS_UNAUTHORIZED_CLAIMS
306303
)
307304

308-
def test_execute_graphql_impersonated_mutation_non_existing_claims(self):
305+
def test_execute_graphql_impersonated_mutation_non_existing_claims(self, dc_client):
309306
"""Tests mutation with non-existing claims returns None."""
310-
dc_client = dataconnect.client(CONNECTOR_CONFIG)
311307
resp = dc_client.execute_graphql(
312308
UPDATE_FREDRICK_USER_IMPERSONATED, options=OPTS_NON_EXISTING_CLAIMS
313309
)
@@ -317,29 +313,26 @@ def test_execute_graphql_impersonated_mutation_non_existing_claims(self):
317313
class TestPublicAuthPolicy:
318314
"""Integration tests for @auth(level: PUBLIC) policy."""
319315

320-
def test_impersonated_authenticated(self):
316+
def test_impersonated_authenticated(self, dc_client):
321317
"""Tests public query with authenticated claims."""
322-
dc_client = dataconnect.client(CONNECTOR_CONFIG)
323318
resp = dc_client.execute_graphql(
324319
QUERY_LIST_USERS, options=OPTS_AUTHORIZED_FRED_CLAIMS
325320
)
326321
assert sorted(resp.data['users'], key=lambda user: user['id']) == sorted(
327322
INITIAL_STATE['users'], key=lambda user: user['id']
328323
)
329324

330-
def test_impersonated_unauthenticated(self):
325+
def test_impersonated_unauthenticated(self, dc_client):
331326
"""Tests public query with unauthenticated claims."""
332-
dc_client = dataconnect.client(CONNECTOR_CONFIG)
333327
resp = dc_client.execute_graphql(
334328
QUERY_LIST_USERS, options=OPTS_UNAUTHORIZED_CLAIMS
335329
)
336330
assert sorted(resp.data['users'], key=lambda user: user['id']) == sorted(
337331
INITIAL_STATE['users'], key=lambda user: user['id']
338332
)
339333

340-
def test_impersonated_non_existing_claims(self):
334+
def test_impersonated_non_existing_claims(self, dc_client):
341335
"""Tests public query with non-existing user claims."""
342-
dc_client = dataconnect.client(CONNECTOR_CONFIG)
343336
resp = dc_client.execute_graphql(
344337
QUERY_LIST_USERS, options=OPTS_NON_EXISTING_CLAIMS
345338
)
@@ -351,25 +344,22 @@ def test_impersonated_non_existing_claims(self):
351344
class TestNoAccessAuthPolicy:
352345
"""Integration tests for @auth(level: NO_ACCESS) policy."""
353346

354-
def test_impersonated_authenticated_fails(self):
347+
def test_impersonated_authenticated_fails(self, dc_client):
355348
"""Tests no-access query with authenticated claims fails."""
356-
dc_client = dataconnect.client(CONNECTOR_CONFIG)
357349
with pytest.raises(exceptions.PermissionDeniedError):
358350
dc_client.execute_graphql(
359351
QUERY_LIST_EMAILS, options=OPTS_AUTHORIZED_FRED_CLAIMS
360352
)
361353

362-
def test_impersonated_unauthenticated_fails(self):
354+
def test_impersonated_unauthenticated_fails(self, dc_client):
363355
"""Tests no-access query with unauthenticated claims fails."""
364-
dc_client = dataconnect.client(CONNECTOR_CONFIG)
365356
with pytest.raises(exceptions.PermissionDeniedError):
366357
dc_client.execute_graphql(
367358
QUERY_LIST_EMAILS, options=OPTS_UNAUTHORIZED_CLAIMS
368359
)
369360

370-
def test_impersonated_non_existing_claims_fails(self):
361+
def test_impersonated_non_existing_claims_fails(self, dc_client):
371362
"""Tests no-access query with non-existing user claims fails."""
372-
dc_client = dataconnect.client(CONNECTOR_CONFIG)
373363
with pytest.raises(exceptions.PermissionDeniedError):
374364
dc_client.execute_graphql(
375365
QUERY_LIST_EMAILS, options=OPTS_NON_EXISTING_CLAIMS

0 commit comments

Comments
 (0)