diff --git a/tests/test_dbsupport.py b/tests/test_dbsupport.py new file mode 100755 index 00000000..063bb125 --- /dev/null +++ b/tests/test_dbsupport.py @@ -0,0 +1,53 @@ +#!/usr/bin/python3 + +# Copyright (c) 2022, 2023 Humanitarian OpenStreetMap Team +# +# This file is part of tm-admin. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with tm-admin. If not, see . +# + +import logging +import os +import argparse +import sys + +# import tm_admin as tma +# rootdir = tma.__path__[0] + +# Instantiate logger +log = logging.getLogger(__name__) + +# def test_dummy(): +# """Placeholder to allow CI to pass.""" +# assert True + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("-v", "--verbose", nargs="?", const="0", help="verbose output") + args = parser.parse_args() + + # if verbose, dump to the terminal. + log_level = os.getenv("LOG_LEVEL", default="INFO") + if args.verbose is not None: + log_level = logging.DEBUG + logging.basicConfig( + level=log_level, + format=("%(asctime)s.%(msecs)03d [%(levelname)s] " "%(name)s | %(funcName)s:%(lineno)d | %(message)s"), + datefmt="%y-%m-%d %H:%M:%S", + stream=sys.stdout, + ) + + # test_dummy() + log.debug("foo") diff --git a/tests/test_orgs.py b/tests/test_orgs.py index 613e10e8..00838169 100755 --- a/tests/test_orgs.py +++ b/tests/test_orgs.py @@ -41,19 +41,105 @@ organization = OrganizationsDB('localhost/tm_admin') -def test_all(): - all = organization.getAll() - assert len(all) > 0 - -def test_by_id(): - id = 100 - all = organization.getByID(id) - assert len(all) > 0 - -def test_by_name(): - name = 'Peace Corps' - all = organization.getByName(name) - assert len(all) > 0 +def get_organisation_by_id(): + log.debug(f"get_organisation_by_id()") + # organisation_id: int) -> Organisation: + pass + +def get_organisation_by_name(): + # organisation_name: str) -> Organisation: + log.debug(f"get_organisation_by_name() unimplemented!") + pass + +def get_organisation_name_by_id(): + # organisation_id: int) -> str: + log.debug(f"get_organisation_name_by_id() unimplemented!") + pass + +def create_organisation(): + # new_organisation_dto: NewOrganisationDTO) -> int: + log.debug(f"create_organisation() unimplemented!") + pass + +def update_organisation(): + # organisation_dto: UpdateOrganisationDTO) -> Organisation: + log.debug(f"update_organisation() unimplemented!") + pass + +def delete_organisation(): + # organisation_id: int): + log.debug(f"delete_organisation() unimplemented!") + pass + +def get_organisations(): + # manager_user_id: int): + log.debug(f"get_organisations() unimplemented!") + pass + +def get_organisations_managed_by_user(): + # user_id: int): + log.debug(f"get_organisations_managed_by_user() unimplemented!") + pass + +def get_organisations_managed_by_user_as_dto(): + # user_id: int) -> ListOrganisationsDTO: + log.debug(f"get_organisations_managed_by_user_as_dto() unimplemented!") + pass + +def get_projects_by_organisation_id(): + # organisation_id: int) -> Organisation: + log.debug(f"get_projects_by_organisation_id() unimplemented!") + pass + +def get_organisation_stats(): + # + log.debug(f"get_organisation_stats() unimplemented!") + pass + +def assert_validate_name(): + # org: Organisation, name: str): + log.debug(f"assert_validate_name() unimplemented!") + pass + +def assert_validate_users(): + # organisation_dto: OrganisationDTO): + log.debug(f"assert_validate_users() unimplemented!") + pass + +def can_user_manage_organisation(): + # organisation_id: int, user_id: int): + log.debug(f"can_user_manage_organisation() unimplemented!") + pass + +def is_user_an_org_manager(): + # organisation_id: int, user_id: int): + log.debug(f"is_user_an_org_manager() unimplemented!") + pass + +def get_campaign_organisations_as_dto(): + # campaign_id: int, user_id: int): + log.debug(f"get_campaign_organisations_as_dto() unimplemented!") + pass + +def get_organisation_by_id_as_dto(): + # + log.debug(f"get_organisation_by_id_as_dto() unimplemented!") + pass + +def get_organisation_by_slug_as_dto(): + # slug: str, user_id: int, abbreviated: bool): + log.debug(f"get_organisation_by_slug_as_dto() unimplemented!") + pass + +def get_organisation_dto(): + # org, user_id: int, abbreviated: bool): + log.debug(f"get_organisation_dto() unimplemented!") + pass + +def get_organisations_as_dto(): + # + log.debug(f"get_organisations_as_dto() unimplemented!") + pass if __name__ == "__main__": parser = argparse.ArgumentParser() @@ -72,13 +158,23 @@ def test_by_name(): stream=sys.stdout, ) - print("--- test_by_id() ---") - test_by_id() - - print("--- test_by_name() ---") - test_by_name() - - print("--- test_all() ---") - test_all() - - + get_organisation_by_id() + get_organisation_by_name() + get_organisation_name_by_id() + create_organisation() + update_organisation() + delete_organisation() + get_organisations() + get_organisations_managed_by_user() + get_projects_by_organisation_id() + get_organisation_stats() + assert_validate_name() + assert_validate_users() + can_user_manage_organisation() + is_user_an_org_manager() + # get_campaign_organisations_as_dto() + # get_organisations_managed_by_user_as_dto() + # get_organisation_by_id_as_dto() + # get_organisation_by_slug_as_dto() + # get_organisation_dto() + # get_organisations_as_dto( diff --git a/tests/test_placeholder.py b/tests/test_placeholder.py old mode 100644 new mode 100755 diff --git a/tests/test_projects.py b/tests/test_projects.py index d3d90a8c..704ba870 100755 --- a/tests/test_projects.py +++ b/tests/test_projects.py @@ -48,121 +48,122 @@ def get_project_by_id(): # project_id: int) -> Project: - pass + log.debug(f"get_project_by_id() unimplemented!") def exists(): # project_id: int) -> bool: - pass + log.debug(f"exists() unimplemented!") def get_project_by_name(): # project_id: int) -> Project: - pass + log.debug(f"get_project_by_name() unimplemented!") def auto_unlock_tasks(): # project_id: int): - pass + log.debug(f"auto_unlock_tasks() unimplemented!") def delete_tasks(): # project_id: int, tasks_ids): - pass + log.debug(f"delete_tasks() unimplemented!") def get_contribs_by_day(): # project_id: int) -> ProjectContribsDTO: - pass + log.debug(f"get_contribs_by_day() unimplemented!") def get_project_dto_for_mapper(): - pass + log.debug(f"get_project_dto_for_mapper() unimplemented!") def get_project_tasks(): - pass + # + log.debug(f"get_project_tasks() unimplemented!") def get_project_aoi(): # project_id): - pass + log.debug(f"get_project_aoi() unimplemented!") def get_project_priority_areas(): # project_id): - pass + log.debug(f"get_project_priority_areas() unimplemented!") def get_task_for_logged_in_user(): # user_id: int): - pass + log.debug(f"get_task_for_logged_in_user() unimplemented!") def get_task_details_for_logged_in_user(): # user_id: int, preferred_locale: str): - pass + log.debug(f"get_task_details_for_logged_in_user() unimplemented!") def is_user_in_the_allowed_list(): # allowed_users: list, current_user_id: int): - pass + log.debug(f"is_user_in_the_allowed_list() unimplemented!") def evaluate_mapping_permission(): - pass + log.debug(f"evaluate_mapping_permission() unimplemented!") def evaluate_validation_permission(): - pass + log.debug(f"evaluate_validation_permission() unimplemented!") def is_user_permitted_to_validate(): # project_id, user_id): - pass + log.debug(f") unimplemented!") def is_user_permitted_to_map(): # project_id, user_id): - pass + log.debug(f"is_user_permitted_to_map() unimplemented!") def get_cached_project_summary(): - pass + log.debug(f"get_cached_project_summary() unimplemented!") def get_project_summary(): - pass + log.debug(f") unimplemented!") def set_project_as_featured(): # project_id: int): - pass + log.debug(f"set_project_as_featured() unimplemented!") def unset_project_as_featured(): # project_id: int): - pass + log.debug(f"unset_project_as_featured() unimplemented!") def get_featured_projects(): # preferred_locale): - pass + log.debug(f"get_featured_projects() unimplemented!") def is_favorited(): # project_id: int, user_id: int) -> bool: - pass + log.debug(f"is_favorited() unimplemented!") def favorite(): # project_id: int, user_id: int): - pass + log.debug(f"favorite() unimplemented!") def unfavorite(): # project_id: int, user_id: int): - pass + log.debug(f"unfavorite() unimplemented!") def get_project_title(): # project_id: int, preferred_locale: str = "en") -> str: - pass + log.debug(f"get_project_title() unimplemented!") def get_project_stats(): # project_id: int) -> ProjectStatsDTO: - pass + log.debug(f"get_project_stats() unimplemented!") def get_project_user_stats(): # project_id: int, username: str) -> ProjectUserStatsDTO: - pass + log.debug(f"get_project_user_stats() unimplemented!") def get_project_teams(): # project_id: int): - pass + log.debug(f"get_project_teams() unimplemented!") def get_project_organisation(): # project_id: int) -> Organisation: - pass + log.debug(f"get_project_organisation() unimplemented!") def send_email_on_project_progress(): # project_id): - pass + log.debug(f"send_email_on_project_progress() unimplemented!") if __name__ == "__main__": parser = argparse.ArgumentParser() diff --git a/tests/test_users.py b/tests/test_users.py index db1937a8..e7cd387a 100755 --- a/tests/test_users.py +++ b/tests/test_users.py @@ -30,6 +30,7 @@ from tm_admin.projects.projects import ProjectsDB from tm_admin.types_tm import Userrole, Mappinglevel from datetime import datetime +from tm_admin.users.users_class import UsersTable # Instantiate logger log = logging.getLogger(__name__) @@ -116,7 +117,7 @@ def test_expert(): def test_registered(): log.debug("--- test_registered() ---") - start = '2020-11-20 08:36:55' + start = '2015-11-20 08:36:55' stime = datetime.strptime(start, '%Y-%m-%d %H:%M:%S') end = '2023-02-07 12:28:30' etime = datetime.strptime(end, '%Y-%m-%d %H:%M:%S') @@ -146,6 +147,7 @@ def is_user_validator(): assert len(result) == 0 def get_projects_mapped(): + log.debug("--- get_projects_mapped() ---") id = 4606673 result = user.getByID(id) mapped = result['projects_mapped'] @@ -157,107 +159,123 @@ def get_projects_mapped(): assert hits == 1 def is_user_blocked(): + log.debug("--- is_user_blocked() ---") id = 4606673 result = user.getByID(id) assert result['role'] != Userrole.USER_READ_ONLY -def get_contributions_by_day(): - # user_id: int - pass +def get_mapped_projects(): + log.debug("--- get_mapped_projects() ---") + # user_name: str, preferred_locale: str + id = 4606673 + result = user.getByID(id) + # mapped = result['projects_mapped'] + mapped = [1090, 173] + data = list() + for proj in mapped: + record = project.getByID(proj) + data.append(record) -def get_general_admins(): - pass - + assert len(data) == 2 + def update_user(): + log.debug("--- update_user() ---") # user_id: int, osm_username: str, picture_url: str): - pass - -def get_projects_favorited(): - # user_id: int - pass + id = 4606673 + user.updateColumn(id, {'osm_username': 'osmfoo'}) + user.updateColumn(id, {'picture_url': 'no pic'}) + result = user.getByID(id) + assert result['picture_url'] == 'no pic' +# FIXME: I'm not really sure of the difference of these two internal API functions. def register_user(): + log.debug("--- update_user() ---") # osm_id, username, changeset_count, picture_url, email - pass + # The id is generated by postgres, so we don't supply it. + ut = UsersTable(username='foobar', name='barfoo', picture_url='URI', email_address="bar@foo.com", mapping_level='INTERMEDIATE', role='VALIDATOR') + new = user.createTable(ut) + entry = user.getByName(ut.data['name']) + assert entry['id'] > 0 -#def get_user_dto_by_username(): -# pass +def register_user_with_email(): + log.debug("--- register_user_with_email() ---") + # osm_id, username, changeset_count, picture_url, email + # The id is generated by postgres, so we don't supply it. + ut = UsersTable(username='foobar', name='barfoo', picture_url='URI', email_address="bar@foo.com", mapping_level='INTERMEDIATE', role='VALIDATOR') + new = user.createTable(ut) + entry = user.getByName(ut.data['name']) + assert entry['id'] > 0 + +def has_user_accepted_license(): + # user_id: int, license_id: int + log.debug("--- has_user_accepted_license() ---") + id = 4606673 + result = user.getByID(id) + assert result['id'] == id -#def get_user_dto_by_id(user: int, request_user: int): -# pass +def get_projects_favorited(): + # user_id: int + log.debug(f"get_projects_favorited() unimplemented!") +def accept_license_terms(): + # user_id: int, license_id: int + log.debug(f"accept_license_terms() unimplemented!") + +def get_interests(): + log.debug(f"get_interests() unimplemented!") + def get_interests_stats(): # user_id - pass - + log.debug(f"get_interests_stats() unimplemented!") + def get_detailed_stats(): # username: str - pass - + log.debug(f"get_detailed_stats() unimplemented!") + def update_user_details(): # user_id: int, db: UsersDB - pass + log.debug(f"update_user_details() unimplemented!") def filter_users(): # username: str, project_id: int, page: int - pass + log.debug(f"filter_users() unimplemented!") def is_user_the_project_author(): # user_id: int, author_id: int - pass + log.debug(f"is_user_the_project_author() unimplemented!") def get_countries_contributed(): # user_id: int - pass + log.debug(f"get_countries_contributed() unimplemented!") def upsert_mapped_projects(): # user_id: int, project_id: int - pass + log.debug(f"upsert_mapped_projects() unimplemented!") -def get_mapped_projects(): - # user_name: str, preferred_locale: str - id = 4606673 - result = user.getByID(id) - # mapped = result['projects_mapped'] - mapped = [1090, 173] - data = list() - for proj in mapped: - record = project.getByID(proj) - data.append(record) +def get_contributions_by_day(): + # user_id: int + log.debug(f"get_contributions_by_day() unimplemented!") - assert len(data) == 2 - +def get_general_admins(): + log.debug(f"get_general_admins() unimplemented!") + def get_recommended_projects(): # user_name: str, preferred_locale: str # Get all projects that the user has contributed # Get all campaigns for all contributed projects. # Get projects with given campaign tags but without user contributions. # Get only user mapping level projects. - pass - -def accept_license_terms(): - # user_id: int, license_id: int - pass - -def has_user_accepted_license(): - # user_id: int, license_id: int - pass - -# def get_osm_details_for_user(): -# # username: str -# pass + log.debug(f"get_recommended_projects() unimplemented!") -def notify_level_upgrade(): - # user_id: int, username: str, level: str - pass - -def register_user_with_email(): - # db: UsersDB - pass - -def get_interests(): - pass - +# These both require accessing the OSM server, which we're not going to do yet. +# def get_osm_details_for_user(username: str) +# def notify_level_upgrade(user_id: int, username: str, level: str) + +# We don't need to test these they are for sqlachemy, which we're not using. Instead +# we use the UsersTable() to represent the table schema +# def get_user_dto_by_username(): +# def get_user_dto_by_id(user: int, request_user: int): + if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("-v", "--verbose", nargs="?", const="0", help="verbose output") @@ -290,7 +308,7 @@ def get_interests(): test_expert() - # test_registered() + test_registered() get_project_managers() @@ -320,11 +338,11 @@ def get_interests(): set_user_mapping_level() accept_license_terms() has_user_accepted_license() - # get_osm_details_for_user() # Not part of this API check_and_update_mapper_level() - notify_level_upgrade(), register_user_with_email() get_interests() + # get_osm_details_for_user() # Not part of this API + # notify_level_upgrade() # Not part of this API test_all()