This repository has been archived by the owner on Feb 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #400 from multinet-app/api-testing
Add pytest fixtures that allow for api testing
- Loading branch information
Showing
6 changed files
with
101 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
FLASK_SERVE_PORT=5000 | ||
DOC_SERVE_PORT=8000 | ||
|
||
FLASK_APP=multinet | ||
FLASK_ENV=development | ||
|
||
MYPYPATH=mypy_stubs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
"""Pytest configurations for multinet tests.""" | ||
|
||
import pytest | ||
from uuid import uuid4 | ||
from dataclasses import asdict | ||
|
||
from multinet import create_app | ||
from multinet.db import create_workspace, delete_workspace | ||
from multinet.user import register_user, set_user_cookie, user_collection, UserInfo | ||
|
||
|
||
@pytest.fixture | ||
def app(): | ||
"""Yield a testing app.""" | ||
app = create_app({"TESTING": True}) | ||
yield app | ||
|
||
|
||
@pytest.fixture | ||
def server(app): | ||
"""Return a test client to `app`.""" | ||
client = app.test_client() | ||
return client | ||
|
||
|
||
@pytest.fixture | ||
def handled_user(): | ||
""" | ||
Create a user, and yield that user. | ||
On teardown, deletes the user. | ||
""" | ||
user = set_user_cookie( | ||
register_user( | ||
UserInfo( | ||
family_name="test", | ||
given_name="test", | ||
name="test test", | ||
picture="", | ||
email="test@test.test", | ||
sub=uuid4().hex, | ||
) | ||
) | ||
) | ||
|
||
yield user | ||
|
||
# TODO: Once the function exists, use `delete_user` here instead | ||
user_collection().delete(asdict(user)) | ||
|
||
|
||
@pytest.fixture | ||
def generated_workspace(handled_user): | ||
"""Create a workspace, and yield the name of the workspace.""" | ||
|
||
workspace_name = uuid4().hex | ||
|
||
create_workspace(workspace_name, handled_user) | ||
return workspace_name | ||
|
||
|
||
@pytest.fixture | ||
def handled_workspace(generated_workspace): | ||
""" | ||
Create a workspace, and yield the name of the workspace. | ||
On teardown, deletes the workspace. | ||
""" | ||
yield generated_workspace | ||
|
||
delete_workspace(generated_workspace) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"""Tests that ensure fixtures act properly.""" | ||
|
||
from multinet.user import MULTINET_COOKIE | ||
|
||
|
||
def test_generated_workspace(handled_workspace, handled_user, server): | ||
"""Test that a generated workspace exists when querying the API.""" | ||
|
||
with server.session_transaction() as session: | ||
session[MULTINET_COOKIE] = handled_user.multinet.session | ||
|
||
resp = server.get(f"/api/workspaces/{handled_workspace}") | ||
assert resp.status_code == 200 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters