Skip to content

Commit

Permalink
wip: create multiple clients for testing session that persists across…
Browse files Browse the repository at this point in the history
… modules
  • Loading branch information
slugb0t committed Nov 14, 2023
1 parent 1f41d74 commit bf75074
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 79 deletions.
162 changes: 114 additions & 48 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,27 +101,30 @@ def _create_user(_test_client):

# Fixture to sign in the user for module testing
@pytest.fixture(scope="session")
def _logged_in_client(_test_client):
def _logged_in_client():
"""Sign in the user for testing."""
with unittest.mock.patch("pytest_config.TestConfig", TestConfig):
response = _test_client.post(
"/auth/login",
json={
"email_address": "[email protected]",
"password": "Testingyeshello11!",
},
)
flask_app = create_app(config_module="pytest_config")
with flask_app.test_client() as _test_client:
with unittest.mock.patch("pytest_config.TestConfig", TestConfig):
response = _test_client.post(
"/auth/login",
json={
"email_address": "[email protected]",
"password": "Testingyeshello11!",
},
)

assert response.status_code == 200
assert response.status_code == 200

yield _test_client

yield _test_client

@pytest.fixture(scope="session")
def _test_invite_study_contributor(_test_client):
def _test_invite_study_contributor(_logged_in_client):
"""Test invite study contributor."""
study_id = pytest.global_study_id["id"] # type: ignore

response = _test_client.post(
response = _logged_in_client.post(
f"/study/{study_id}/contributor",
json={"email_address": "[email protected]", "role": "editor"},
)
Expand All @@ -131,7 +134,7 @@ def _test_invite_study_contributor(_test_client):

pytest.global_editor_token = response_data["token"]

response = _test_client.post(
response = _logged_in_client.post(
f"/study/{study_id}/contributor",
json={"email_address": "[email protected]", "role": "admin"},
)
Expand All @@ -140,7 +143,7 @@ def _test_invite_study_contributor(_test_client):
response_data = json.loads(response.data)
pytest.global_admin_token = response_data["token"]

response = _test_client.post(
response = _logged_in_client.post(
f"/study/{study_id}/contributor",
json={"email_address": "[email protected]", "role": "viewer"},
)
Expand All @@ -151,48 +154,111 @@ def _test_invite_study_contributor(_test_client):


@pytest.fixture(scope="session")
def _create_admin_user(_test_client):
def _create_admin_user():
"""Create an admin user for testing."""
with unittest.mock.patch("pytest_config.TestConfig", TestConfig):
response = _test_client.post(
"/auth/signup",
json={
"email_address": "[email protected]",
"password": "Testingyeshello11!",
"code": pytest.global_admin_token
}
)
flask_app = create_app(config_module="pytest_config")
with flask_app.test_client() as _test_client:
with unittest.mock.patch("pytest_config.TestConfig", TestConfig):
response = _test_client.post(
"/auth/signup",
json={
"email_address": "[email protected]",
"password": "Testingyeshello11!",
"code": pytest.global_admin_token
}
)

assert response.status_code == 201
assert response.status_code == 201


@pytest.fixture(scope="session")
def _create_editor_user(_test_client):
def _create_editor_user():
"""Create an editor user for testing."""
with unittest.mock.patch("pytest_config.TestConfig", TestConfig):
response = _test_client.post(
"/auth/signup",
json={
"email_address": "[email protected]",
"password": "Testingyeshello11!",
"code": pytest.global_editor_token
}
)
flask_app = create_app(config_module="pytest_config")
with flask_app.test_client() as _test_client:
with unittest.mock.patch("pytest_config.TestConfig", TestConfig):
response = _test_client.post(
"/auth/signup",
json={
"email_address": "[email protected]",
"password": "Testingyeshello11!",
"code": pytest.global_editor_token
}
)

assert response.status_code == 201
assert response.status_code == 201


@pytest.fixture(scope="session")
def _create_viewer_user(_test_client):
def _create_viewer_user():
"""Create a viewer user for testing."""
with unittest.mock.patch("pytest_config.TestConfig", TestConfig):
response = _test_client.post(
"/auth/signup",
json={
"email_address": "[email protected]",
"password": "Testingyeshello11!",
"code": pytest.global_viewer_token
}
)
flask_app = create_app(config_module="pytest_config")
with flask_app.test_client() as _test_client:
with unittest.mock.patch("pytest_config.TestConfig", TestConfig):
response = _test_client.post(
"/auth/signup",
json={
"email_address": "[email protected]",
"password": "Testingyeshello11!",
"code": pytest.global_viewer_token
}
)

assert response.status_code == 201
assert response.status_code == 201


@pytest.fixture(scope="session")
def _admin_client():
"""Create an admin user for testing."""
flask_app = create_app(config_module="pytest_config")
with flask_app.test_client() as _test_client:
with unittest.mock.patch("pytest_config.TestConfig", TestConfig):
response = _test_client.post(
"/auth/login",
json={
"email_address": "[email protected]",
"password": "Testingyeshello11!",
},
)

assert response.status_code == 200

yield _test_client


@pytest.fixture(scope="session")
def _editor_client():
"""Create an admin user for testing."""
flask_app = create_app(config_module="pytest_config")
with flask_app.test_client() as _test_client:
with unittest.mock.patch("pytest_config.TestConfig", TestConfig):
response = _test_client.post(
"/auth/login",
json={
"email_address": "[email protected]",
"password": "Testingyeshello11!",
},
)

assert response.status_code == 200

yield _test_client


@pytest.fixture(scope="session")
def _viewer_client():
"""Create an admin user for testing."""
flask_app = create_app(config_module="pytest_config")
with flask_app.test_client() as _test_client:
with unittest.mock.patch("pytest_config.TestConfig", TestConfig):
response = _test_client.post(
"/auth/login",
json={
"email_address": "[email protected]",
"password": "Testingyeshello11!",
},
)

assert response.status_code == 200

yield _test_client
31 changes: 0 additions & 31 deletions tests/functional/test_server_launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,3 @@ def test_db_empty(_test_client, _empty_db, _create_user):
def test_signin_user(_logged_in_client):
"""Signs in user before testing."""
print("User signed in for testing")


def test_invite_study_contributor(_test_client):
"""Test invite study contributor."""
response = _test_client.post(
"/study/1/contributor",
json={"email_address": "[email protected]", "role": "editor"},
)

assert response.status_code == 201
response_data = json.loads(response.data)

pytest.global_editor_token = response_data["token"]

response = _test_client.post(
"/study/1/contributor",
json={"email_address": "[email protected]", "role": "admin"},
)

assert response.status_code == 201
response_data = json.loads(response.data)
pytest.global_admin_token = response_data["token"]

response = _test_client.post(
"/study/1/contributor",
json={"email_address": "[email protected]", "role": "viewer"},
)

assert response.status_code == 201
response_data = json.loads(response.data)
pytest.global_viewer_token = response_data["token"]
16 changes: 16 additions & 0 deletions tests/functional/test_study_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ def test_viewer_editor_user(_create_viewer_user):
"""Viewer User created for permissions testing"""
print("Viewer user created for testing")


def test_signin_admin(_admin_client):
"""Admin user signed in for testing"""
print("Admin user signed in for testing")


def test_signin_editor(_editor_client):
"""Editor user signed in for testing"""
print("Editor user signed in for testing")


def test_signin_viewer(_viewer_client):
"""Viewer user signed in for testing"""
print("Viewer user signed in for testing")


def test_get_all_studies(_logged_in_client):
"""
GIVEN a Flask application configured for testing
Expand Down

0 comments on commit bf75074

Please sign in to comment.