Skip to content
This repository has been archived by the owner on Feb 23, 2022. It is now read-only.

Commit

Permalink
Merge pull request #400 from multinet-app/api-testing
Browse files Browse the repository at this point in the history
Add pytest fixtures that allow for api testing
  • Loading branch information
jjnesbitt committed Jun 29, 2020
2 parents 42f937b + eb24f3f commit 652f1f3
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 35 deletions.
7 changes: 7 additions & 0 deletions .env.testing
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
6 changes: 5 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
architecture: 'x64'

# Copy Env File
- run: cp .env.default .env
- run: cp .env.testing .env

# Build and test Multinet server.
- run: pip install pipenv
Expand All @@ -46,3 +46,7 @@ jobs:
- run: pipenv run format
- run: pipenv run typecheck
- run: pipenv run test
env:
ARANGO_HOST: ${{ secrets.CI_ARANGO_HOST }}
ARANGO_PASSWORD: ${{ secrets.CI_ARANGO_PASSWORD }}
ARANGO_PROTOCOL: ${{ secrets.CI_ARANGO_PROTOCOL }}
3 changes: 3 additions & 0 deletions multinet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ def create_app(config: Optional[MutableMapping] = None) -> Flask:
"""Create a Multinet app instance."""
app = Flask(__name__)

if config is not None:
app.config.update(config)

allowed_origins = get_allowed_origins()
CORS(app, origins=allowed_origins, supports_credentials=True)
Swagger(app, template_file="swagger/template.yaml")
Expand Down
71 changes: 71 additions & 0 deletions test/conftest.py
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)
13 changes: 13 additions & 0 deletions test/test_meta.py
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
36 changes: 2 additions & 34 deletions test/test_workspace.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""Test that workspace operations act like we expect them to."""
import pytest

from uuid import uuid4
from multinet.db import (
create_workspace,
Expand All @@ -11,32 +9,6 @@
)


@pytest.fixture
def generated_workspace():
"""Create a workspace, and yield the name of the workspace."""

workspace_name = uuid4().hex

create_workspace(workspace_name)
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)


# BEGIN TESTS


@pytest.mark.skip()
def test_present_workspace(handled_workspace):
"""Test that workspace caching works as expected on present workspaces."""

Expand All @@ -53,7 +25,6 @@ def test_present_workspace(handled_workspace):
assert first_resp == second_resp


@pytest.mark.skip()
def test_absent_workspace():
"""Test that workspace caching works as expected on absent workspaces."""

Expand All @@ -71,13 +42,12 @@ def test_absent_workspace():
assert second_resp is None


@pytest.mark.skip()
def test_workspace_create():
def test_workspace_create(handled_user):
"""Test that creating a workspace doesn't result in invalid caching."""
workspace_name = uuid4().hex

pre_create = workspace_mapping(workspace_name)
create_workspace(workspace_name)
create_workspace(workspace_name, handled_user)
post_create = workspace_mapping(workspace_name)
post_create_exists = workspace_exists(workspace_name)

Expand All @@ -90,7 +60,6 @@ def test_workspace_create():
assert post_create_exists


@pytest.mark.skip()
def test_workspace_delete(generated_workspace):
"""Tests that deleting a workspace doesn't result in invalid caching."""

Expand All @@ -105,7 +74,6 @@ def test_workspace_delete(generated_workspace):
assert not exists_post_delete


@pytest.mark.skip()
def test_workspace_rename(generated_workspace):
"""Test that renaming a workspace doesn't result in invalid caching."""
new_workspace_name = uuid4().hex
Expand Down

0 comments on commit 652f1f3

Please sign in to comment.