Skip to content

Commit

Permalink
test: create odkcentral project with fmtm project
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Oct 17, 2023
1 parent fbcf36d commit 2fc46a8
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 22 deletions.
8 changes: 7 additions & 1 deletion src/backend/app/central/central_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ def create_appuser(
project_id: int, name: str, odk_credentials: project_schemas.ODKCentral = None
):
"""Create an app-user on a remote ODK Server.
If odk credentials of the project are provided, use them to create an app user.
"""
if odk_credentials:
Expand All @@ -177,8 +178,13 @@ def create_appuser(
pw = settings.ODK_CENTRAL_PASSWD

app_user = OdkAppUser(url, user, pw)

log.debug(
"ODKCentral: attempting user creation: name: " f"{name} | project: {project_id}"
)
result = app_user.create(project_id, name)
log.info(f"Created app user: {result.json()}")

log.debug(f"ODKCentral response: {result.json()}")
return result


Expand Down
2 changes: 1 addition & 1 deletion src/backend/app/projects/project_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,7 @@ def generate_task_files(
name = f"{project_name}_{category}_{task_id}"

# Create an app user for the task
project_log.info(f"Creating app user for task {task_id}")
project_log.info(f"Creating odkcentral app user for task {task_id}")
appuser = central_crud.create_appuser(odk_id, name, odk_credentials)

# If app user could not be created, raise an exception.
Expand Down
32 changes: 24 additions & 8 deletions src/backend/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from sqlalchemy.orm import sessionmaker
from sqlalchemy_utils import create_database, database_exists

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
Expand Down Expand Up @@ -104,10 +105,6 @@ def organization(db):

@pytest.fixture(scope="function")
def project(db, user, organization):
odk_central_url = os.getenv("ODK_CENTRAL_URL")
odk_central_user = os.getenv("ODK_CENTRAL_USER")
odk_central_password = os.getenv("ODK_CENTRAL_PASSWD")

project_metadata = BETAProjectUpload(
author=User(username=user.username, id=user.id),
project_info=ProjectInfo(
Expand All @@ -117,16 +114,35 @@ def project(db, user, organization):
),
xform_title="buildings",
odk_central=ODKCentral(
odk_central_url=odk_central_url,
odk_central_user=odk_central_user,
odk_central_password=odk_central_password,
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=organization.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]
)

try:
odkproject = central_crud.create_odk_project(
project_metadata.project_info.name, project_metadata.odk_central
)
log.debug(f"ODK project returned: {odkproject}")
assert odkproject is not None
except Exception as e:
log.error(e)
pytest.fail(f"Test failed with exception: {str(e)}")

# Create FMTM Project
try:
new_project = project_crud.create_project_with_project_info(
db, project_metadata, project_id=123
db, project_metadata, project_id=odkproject["id"]
)
log.debug(f"Project returned: {new_project.__dict__}")
assert new_project is not None
Expand Down
31 changes: 19 additions & 12 deletions src/backend/tests/test_projects_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,6 @@ def test_create_project_with_project_info(db, project):


def test_generate_appuser_files(db, project):
custom_form = f"{test_data_path}/buildings.xls"
with open(custom_form, "rb") as file:
contents = file.read()

data_extracts = f"{test_data_path}/building_footprint.zip"
with zipfile.ZipFile(data_extracts, "r") as zip_archive:
extract_contents = zip_archive.read("building_foot_jnk.geojson")
json.loads(extract_contents)

odk_credentials = {
"odk_central_url": odk_central_url,
"odk_central_user": odk_central_user,
Expand All @@ -126,6 +117,7 @@ def test_generate_appuser_files(db, project):
project_id = project.id
log.debug(f"Testing project ID: {project_id}")

# Set project boundary
boundary_geojson = json.loads(
json.dumps(
{
Expand All @@ -148,26 +140,41 @@ def test_generate_appuser_files(db, project):
)
assert boundary_created is True

# Load data extracts
data_extracts_file = f"{test_data_path}/building_footprint.zip"
with zipfile.ZipFile(data_extracts_file, "r") as zip_archive:
data_extracts = zip_archive.read("building_foot_jnk.geojson")

# Upload data extracts
log.debug(f"Uploading custom data extracts: {str(data_extracts)[:100]}...")
data_extract_uploaded = project_crud.upload_custom_data_extracts(
db, project_id, extract_contents
db, project_id, data_extracts
)
assert data_extract_uploaded is True

# Get project tasks list
task_list = tasks_crud.get_task_lists(db, project_id)
assert isinstance(task_list, list)

# Load custom form & extracts
xlsform_file = f"{test_data_path}/buildings.xls"
with open(xlsform_file, "rb") as file:
custom_form = file.read()

# Generate project task files
for task in task_list:
task_list = project_crud.generate_task_files(
db, project_id, task, custom_form, "xls", odk_credentials
)
assert task_list is True

# Generate appuser files
test_data = {
"db": db,
"project_id": project_id,
"extract_polygon": True,
"upload": contents,
"extracts_contents": extract_contents,
"upload": custom_form,
"extracts_contents": data_extracts,
"category": "buildings",
"form_type": "example_form_type",
"background_task_id": uuid.uuid4(),
Expand Down

0 comments on commit 2fc46a8

Please sign in to comment.