Skip to content

Commit

Permalink
test: fix project route tests
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Jan 31, 2024
1 parent de02ed7 commit 1c4fcdf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
34 changes: 18 additions & 16 deletions src/backend/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
from sqlalchemy.orm import sessionmaker
from sqlalchemy_utils import create_database, database_exists

from app.auth.osm import AuthUser
from app.central import central_crud
from app.config import settings
from app.db.database import Base, get_db
from app.db.db_models import DbOrganisation, DbUser
from app.main import get_application
from app.projects import project_crud
from app.projects.project_schemas import ODKCentral, ProjectInfo, ProjectUpload
from app.users.user_schemas import User
from app.projects.project_schemas import ODKCentralDecrypted, ProjectInfo, ProjectUpload

engine = create_engine(settings.FMTM_DB_URL.unicode_string())
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Expand Down Expand Up @@ -102,6 +102,7 @@ def organisation(db):
description="test org",
url="https://test.org",
logo="none",
approved=True,
)
db.add(db_org)
db.commit()
Expand All @@ -112,32 +113,30 @@ def organisation(db):
async def project(db, user, organisation):
"""A test project, using the test user and org."""
project_metadata = ProjectUpload(
author=User(username=user.username, id=user.id),
project_info=ProjectInfo(
name="test project",
short_description="test",
description="test",
),
xform_title="buildings",
odk_central=ODKCentral(
odk_central_url=os.getenv("ODK_CENTRAL_URL"),
odk_central_user=os.getenv("ODK_CENTRAL_USER"),
odk_central_password=os.getenv("ODK_CENTRAL_PASSWD"),
),
odk_central_url=os.getenv("ODK_CENTRAL_URL"),
odk_central_user=os.getenv("ODK_CENTRAL_USER"),
odk_central_password=os.getenv("ODK_CENTRAL_PASSWD"),
hashtags=["hot-fmtm"],
organisation_id=organisation.id,
)

# Create ODK Central Project
if project_metadata.odk_central.odk_central_url.endswith("/"):
# Remove trailing slash
project_metadata.odk_central.odk_central_url = (
project_metadata.odk_central.odk_central_url[:-1]
)
odk_creds_decrypted = ODKCentralDecrypted(
odk_central_url=project_metadata.odk_central_url,
odk_central_user=project_metadata.odk_central_user,
odk_central_password=project_metadata.odk_central_password,
)

# Create ODK Central Project
try:
odkproject = central_crud.create_odk_project(
project_metadata.project_info.name, project_metadata.odk_central
project_metadata.project_info.name,
odk_creds_decrypted,
)
log.debug(f"ODK project returned: {odkproject}")
assert odkproject is not None
Expand All @@ -148,7 +147,10 @@ async def project(db, user, organisation):
# Create FMTM Project
try:
new_project = await project_crud.create_project_with_project_info(
db, project_metadata, odkproject["id"]
db,
project_metadata,
odkproject["id"],
AuthUser(username=user.username, id=user.id),
)
log.debug(f"Project returned: {new_project.__dict__}")
assert new_project is not None
Expand Down
24 changes: 13 additions & 11 deletions src/backend/tests/test_projects_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,37 @@
from shapely.geometry import shape

from app.central.central_crud import create_odk_project
from app.config import settings
from app.config import encrypt_value, settings
from app.db import db_models
from app.projects import project_crud, project_schemas
from app.tasks import tasks_crud
from tests.test_data import test_data_path

odk_central_url = os.getenv("ODK_CENTRAL_URL")
odk_central_user = os.getenv("ODK_CENTRAL_USER")
odk_central_password = os.getenv("ODK_CENTRAL_PASSWD")
odk_central_password = encrypt_value(os.getenv("ODK_CENTRAL_PASSWD", ""))


async def test_create_project(client, organisation, user):
async def test_create_project(client, organisation):
"""Test project creation endpoint."""
odk_credentials = {
"odk_central_url": odk_central_url,
"odk_central_user": odk_central_user,
"odk_central_password": odk_central_password,
}
odk_credentials = project_schemas.ODKCentralDecrypted(**odk_credentials)

project_data = {
"author": {"username": user.username, "id": user.id},
"project_info": {
"name": "test project",
"short_description": "test",
"description": "test",
},
"xform_title": "buildings",
"odk_central": {
"odk_central_url": odk_central_url,
"odk_central_user": odk_central_user,
"odk_central_password": odk_central_password,
},
"hashtags": ["hot-fmtm"],
"hashtags": ["#FMTM"],
"organisation_id": organisation.id,
}
project_data.update(**odk_credentials.model_dump())

response = client.post("/projects/create_project", json=project_data)

Expand Down Expand Up @@ -133,7 +135,7 @@ async def test_generate_appuser_files(db, project):
"odk_central_user": odk_central_user,
"odk_central_password": odk_central_password,
}
odk_credentials = project_schemas.ODKCentral(**odk_credentials)
odk_credentials = project_schemas.ODKCentralDecrypted(**odk_credentials)

project_id = project.id
log.debug(f"Testing project ID: {project_id}")
Expand Down

0 comments on commit 1c4fcdf

Please sign in to comment.