diff --git a/.cookiecutter/includes/tox/passenv b/.cookiecutter/includes/tox/passenv index 495e529e2ea..634d6e790da 100644 --- a/.cookiecutter/includes/tox/passenv +++ b/.cookiecutter/includes/tox/passenv @@ -14,5 +14,4 @@ dev: ORCID_HOST dev: ORCID_CLIENT_ID dev: ORCID_CLIENT_SECRET {tests,functests}: DATABASE_URL -{tests,functests}: ELASTICSEARCH_URL functests: BROKER_URL diff --git a/.cookiecutter/includes/tox/setenv b/.cookiecutter/includes/tox/setenv index 09de1d899f8..be5773802dd 100644 --- a/.cookiecutter/includes/tox/setenv +++ b/.cookiecutter/includes/tox/setenv @@ -11,6 +11,7 @@ dev: H_API_AUTH_COOKIE_SECRET_KEY = {env:H_API_AUTH_COOKIE_SECRET_KEY:"dev_h_api dev: H_API_AUTH_COOKIE_SALT = {env:H_API_AUTH_COOKIE_SALT:"dev_h_api_auth_cookie_salt"} dev: REPLICA_DATABASE_URL = {env:DATABASE_URL:postgresql://postgres@localhost/postgres} dev: MAILCHIMP_USER_ACTIONS_SUBACCOUNT = {env:MAILCHIMP_USER_ACTIONS_SUBACCOUNT:devdata} +tests,functests: ELASTICSEARCH_URL = {env:ELASTICSEARCH_URL:http://localhost:9200} tests: ELASTICSEARCH_INDEX = {env:ELASTICSEARCH_INDEX:hypothesis-tests} functests: ELASTICSEARCH_INDEX = {env:ELASTICSEARCH_INDEX:hypothesis-functests} {tests,functests}: AUTHORITY = {env:AUTHORITY:example.com} diff --git a/tests/common/fixtures/__init__.py b/tests/common/fixtures/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/tests/common/fixtures/elasticsearch.py b/tests/common/fixtures/elasticsearch.py deleted file mode 100644 index 3f000af6ee9..00000000000 --- a/tests/common/fixtures/elasticsearch.py +++ /dev/null @@ -1,84 +0,0 @@ -import os -from unittest.mock import create_autospec - -import elasticsearch_dsl -import pytest -from packaging.version import Version - -from h import search - -ELASTICSEARCH_INDEX = os.environ["ELASTICSEARCH_INDEX"] -ELASTICSEARCH_URL = os.environ.get("ELASTICSEARCH_URL", "http://localhost:9200") - -__all__ = ("es_client", "init_elasticsearch", "mock_es_client") - - -@pytest.fixture -def es_client(): - client = _es_client() - yield client - # Push all changes to segments to make sure all annotations that were added get removed. - elasticsearch_dsl.Index(client.index, using=client.conn).refresh() - - client.conn.delete_by_query( - index=client.index, - body={"query": {"match_all": {}}}, - # This query occasionally fails with a version conflict. - # Forcing the deletion resolves the issue, but the exact - # cause of the version conflict has not been found yet. - conflicts="proceed", - # Add refresh to make deletion changes show up in search results. - refresh=True, - ) - - # Close connection to ES server to avoid ResourceWarning about a leaked TCP socket. - client.close() - - -@pytest.fixture -def mock_es_client(es_client): - return create_autospec( - es_client, - instance=True, - spec_set=True, - index="hypothesis", - mapping_type="annotation", - server_version=Version("6.2.0"), - ) - - -@pytest.fixture(scope="session") -def init_elasticsearch(request): - """ - Initialize the elasticsearch cluster. - - Connect to the instance of Elasticsearch and initialize the index - once per test session and delete the index after the test is completed. - """ - es_client = _es_client() - - def maybe_delete_index(): - """Delete the test index if it exists.""" - if es_client.conn.indices.exists(index=ELASTICSEARCH_INDEX): - # The delete operation must be done on a concrete index, not an alias - # in ES6. See: - # https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-delete-index.html - concrete_indexes = es_client.conn.indices.get(index=ELASTICSEARCH_INDEX) - for index in concrete_indexes: - es_client.conn.indices.delete(index=index) - - # Delete the test search index at the end of the test run. - request.addfinalizer(maybe_delete_index) # noqa: PT021 - - # Delete the test search index at the start of the run, just in case it - # was somehow left behind by a previous test run. - maybe_delete_index() - - # Initialize the test search index. - search.init(es_client) - - -def _es_client(): - return search.get_client( - {"es.url": ELASTICSEARCH_URL, "es.index": ELASTICSEARCH_INDEX} - ) diff --git a/tests/common/fixtures/services.py b/tests/common/fixtures/services.py deleted file mode 100644 index 4b38653e07f..00000000000 --- a/tests/common/fixtures/services.py +++ /dev/null @@ -1,373 +0,0 @@ -from unittest.mock import create_autospec - -import pytest - -from h.services import ( - HTTPService, - MentionService, - NotificationService, - OpenIDClientService, - ORCIDClientService, -) -from h.services.analytics import AnalyticsService -from h.services.annotation_authority_queue import AnnotationAuthorityQueueService -from h.services.annotation_delete import AnnotationDeleteService -from h.services.annotation_json import AnnotationJSONService -from h.services.annotation_metadata import AnnotationMetadataService -from h.services.annotation_moderation import AnnotationModerationService -from h.services.annotation_read import AnnotationReadService -from h.services.annotation_stats import AnnotationStatsService -from h.services.annotation_sync import AnnotationSyncService -from h.services.annotation_write import AnnotationWriteService -from h.services.auth_ticket import AuthTicketService -from h.services.auth_token import AuthTokenService -from h.services.bulk_api import ( - BulkAnnotationService, - BulkGroupService, - BulkLMSStatsService, -) -from h.services.developer_token import DeveloperTokenService -from h.services.email import EmailService -from h.services.feature import FeatureService -from h.services.flag import FlagService -from h.services.group import GroupService -from h.services.group_create import GroupCreateService -from h.services.group_delete import GroupDeleteService -from h.services.group_links import GroupLinksService -from h.services.group_list import GroupListService -from h.services.group_members import GroupMembersService -from h.services.group_update import GroupUpdateService -from h.services.job_queue import JobQueueService -from h.services.links import LinksService -from h.services.list_organizations import ListOrganizationsService -from h.services.nipsa import NipsaService -from h.services.oauth.service import OAuthProviderService -from h.services.organization import OrganizationService -from h.services.search_index import SearchIndexService -from h.services.subscription import SubscriptionService -from h.services.task_done import TaskDoneService -from h.services.url_migration import URLMigrationService -from h.services.user import UserService -from h.services.user_delete import UserDeleteService -from h.services.user_password import UserPasswordService -from h.services.user_signup import UserSignupService -from h.services.user_unique import UserUniqueService -from h.services.user_update import UserUpdateService - -__all__ = ( - "analytics_service", - "annotation_authority_queue_service", - "annotation_delete_service", - "annotation_json_service", - "annotation_metadata_service", - "annotation_read_service", - "annotation_stats_service", - "annotation_sync_service", - "annotation_write_service", - "auth_ticket_service", - "auth_token_service", - "bulk_annotation_service", - "bulk_group_service", - "bulk_stats_service", - "developer_token_service", - "email_service", - "feature_service", - "flag_service", - "group_create_service", - "group_delete_service", - "group_links_service", - "group_list_service", - "group_members_service", - "group_service", - "group_update_service", - "http_service", - "links_service", - "list_organizations_service", - "mention_service", - "mock_service", - "moderation_service", - "nipsa_service", - "notification_service", - "oauth_provider_service", - "openid_client_service", - "orcid_client_service", - "organization_service", - "queue_service", - "search_index", - "subscription_service", - "task_done_service", - "url_migration_service", - "user_delete_service", - "user_password_service", - "user_password_service", - "user_service", - "user_signup_service", - "user_unique_service", - "user_update_service", -) - - -@pytest.fixture -def mock_service(pyramid_config): - def mock_service(service_class, name=None, iface=None, spec_set=True, **kwargs): # noqa: FBT002 - service = create_autospec( - service_class, instance=True, spec_set=spec_set, **kwargs - ) - if name: - pyramid_config.register_service(service, name=name) - else: - pyramid_config.register_service(service, iface=iface or service_class) - - return service - - return mock_service - - -@pytest.fixture -def analytics_service(mock_service): - return mock_service(AnalyticsService, name="analytics") - - -@pytest.fixture -def annotation_delete_service(mock_service): - return mock_service(AnnotationDeleteService, name="annotation_delete") - - -@pytest.fixture -def annotation_json_service(mock_service): - return mock_service(AnnotationJSONService, name="annotation_json") - - -@pytest.fixture -def annotation_stats_service(mock_service): - return mock_service(AnnotationStatsService, name="annotation_stats") - - -@pytest.fixture -def annotation_read_service(mock_service): - return mock_service(AnnotationReadService) - - -@pytest.fixture -def annotation_sync_service(mock_service): - return mock_service(AnnotationSyncService) - - -@pytest.fixture -def annotation_write_service(mock_service): - return mock_service(AnnotationWriteService) - - -@pytest.fixture -def annotation_metadata_service(mock_service): - return mock_service(AnnotationMetadataService) - - -@pytest.fixture -def auth_ticket_service(mock_service): - auth_ticket_service = mock_service(AuthTicketService) - auth_ticket_service.verify_ticket.return_value.deleted = False - return auth_ticket_service - - -@pytest.fixture -def auth_token_service(mock_service): - return mock_service(AuthTokenService, name="auth_token") - - -@pytest.fixture -def bulk_annotation_service(mock_service): - return mock_service(BulkAnnotationService) - - -@pytest.fixture -def bulk_group_service(mock_service): - return mock_service(BulkGroupService) - - -@pytest.fixture -def bulk_stats_service(mock_service): - return mock_service(BulkLMSStatsService) - - -@pytest.fixture -def developer_token_service(mock_service): - return mock_service(DeveloperTokenService, name="developer_token") - - -@pytest.fixture -def links_service(mock_service): - return mock_service(LinksService, name="links") - - -@pytest.fixture -def list_organizations_service(mock_service, default_organization): - list_organizations_service = mock_service( - ListOrganizationsService, name="list_organizations" - ) - - list_organizations_service.organizations.return_value = [default_organization] - return list_organizations_service - - -@pytest.fixture -def flag_service(pyramid_config): - service = create_autospec(FlagService, instance=True, spec_set=True) - pyramid_config.register_service(service, name="flag") - - return service - - -@pytest.fixture -def group_create_service(mock_service): - return mock_service(GroupCreateService, name="group_create") - - -@pytest.fixture -def group_delete_service(mock_service): - return mock_service(GroupDeleteService, name="group_delete") - - -@pytest.fixture -def group_links_service(mock_service): - return mock_service(GroupLinksService, name="group_links") - - -@pytest.fixture -def group_list_service(mock_service): - return mock_service(GroupListService, name="group_list") - - -@pytest.fixture -def group_members_service(mock_service): - return mock_service(GroupMembersService, name="group_members") - - -@pytest.fixture -def group_service(mock_service): - return mock_service(GroupService, name="group") - - -@pytest.fixture -def group_update_service(mock_service): - return mock_service(GroupUpdateService, name="group_update") - - -@pytest.fixture -def moderation_service(mock_service): - return mock_service(AnnotationModerationService, name="annotation_moderation") - - -@pytest.fixture -def nipsa_service(mock_service): - nipsa_service = mock_service(NipsaService, name="nipsa") - nipsa_service.is_flagged.return_value = False - - return nipsa_service - - -@pytest.fixture -def oauth_provider_service(mock_service): - return mock_service(OAuthProviderService, name="oauth_provider") - - -@pytest.fixture -def organization_service(mock_service): - return mock_service(OrganizationService, name="organization") - - -@pytest.fixture -def search_index(mock_service): - return mock_service(SearchIndexService, "search_index", spec_set=False) - - -@pytest.fixture -def queue_service(mock_service): - return mock_service(JobQueueService, name="queue_service") - - -@pytest.fixture -def subscription_service(mock_service): - return mock_service(SubscriptionService) - - -@pytest.fixture -def url_migration_service(mock_service): - return mock_service(URLMigrationService, name="url_migration") - - -@pytest.fixture -def user_delete_service(mock_service): - return mock_service(UserDeleteService, name="user_delete") - - -@pytest.fixture -def user_password_service(mock_service): - return mock_service(UserPasswordService, name="user_password") - - -@pytest.fixture -def user_service(mock_service): - user_service = mock_service(UserService, name="user") - user_service.fetch.return_value.deleted = False - return user_service - - -@pytest.fixture -def user_signup_service(mock_service): - return mock_service(UserSignupService, name="user_signup") - - -@pytest.fixture -def user_unique_service(mock_service): - return mock_service(UserUniqueService, name="user_unique") - - -@pytest.fixture -def user_update_service(mock_service): - return mock_service(UserUpdateService, name="user_update") - - -@pytest.fixture -def mention_service(mock_service): - return mock_service(MentionService) - - -@pytest.fixture -def notification_service(mock_service): - return mock_service(NotificationService) - - -@pytest.fixture -def annotation_authority_queue_service(mock_service): - return mock_service(AnnotationAuthorityQueueService) - - -@pytest.fixture -def feature_service(mock_service): - return mock_service(FeatureService, name="feature") - - -@pytest.fixture -def email_service(mock_service): - return mock_service(EmailService) - - -@pytest.fixture -def task_done_service(mock_service): - return mock_service(TaskDoneService) - - -@pytest.fixture -def http_service(mock_service): - return mock_service(HTTPService) - - -@pytest.fixture -def openid_client_service(mock_service): - return mock_service(OpenIDClientService) - - -@pytest.fixture -def orcid_client_service(mock_service): - return mock_service(ORCIDClientService) diff --git a/tests/conftest.py b/tests/conftest.py index 1e277a2e574..51f7756816d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,9 +1,14 @@ from os import environ +from unittest.mock import create_autospec +import elasticsearch_dsl import pytest +from packaging.version import Version from sqlalchemy import create_engine, text from sqlalchemy.orm import sessionmaker +from h import search + @pytest.fixture(scope="session") def db_engine(): @@ -47,3 +52,79 @@ def db_session(db_engine, db_sessionfactory): def db_session_replica(db_session): db_session.execute(text("SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY;")) return db_session + + +@pytest.fixture +def es_client(): + client = _es_client() + yield client + # Push all changes to segments to make sure all annotations that were added get removed. + elasticsearch_dsl.Index(client.index, using=client.conn).refresh() + + client.conn.delete_by_query( + index=client.index, + body={"query": {"match_all": {}}}, + # This query occasionally fails with a version conflict. + # Forcing the deletion resolves the issue, but the exact + # cause of the version conflict has not been found yet. + conflicts="proceed", + # Add refresh to make deletion changes show up in search results. + refresh=True, + ) + + # Close connection to ES server to avoid ResourceWarning about a leaked TCP socket. + client.close() + + +@pytest.fixture +def mock_es_client(es_client): + return create_autospec( + es_client, + instance=True, + spec_set=True, + index="hypothesis", + mapping_type="annotation", + server_version=Version("6.2.0"), + ) + + +@pytest.fixture(scope="session") +def init_elasticsearch(request): + """ + Initialize the elasticsearch cluster. + + Connect to the instance of Elasticsearch and initialize the index + once per test session and delete the index after the test is completed. + """ + es_client = _es_client() + + def maybe_delete_index(): + """Delete the test index if it exists.""" + if es_client.conn.indices.exists(index=environ["ELASTICSEARCH_INDEX"]): + # The delete operation must be done on a concrete index, not an alias + # in ES6. See: + # https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-delete-index.html + concrete_indexes = es_client.conn.indices.get( + index=environ["ELASTICSEARCH_INDEX"] + ) + for index in concrete_indexes: + es_client.conn.indices.delete(index=index) + + # Delete the test search index at the end of the test run. + request.addfinalizer(maybe_delete_index) # noqa: PT021 + + # Delete the test search index at the start of the run, just in case it + # was somehow left behind by a previous test run. + maybe_delete_index() + + # Initialize the test search index. + search.init(es_client) + + +def _es_client(): + return search.get_client( + { + "es.url": environ["ELASTICSEARCH_URL"], + "es.index": environ["ELASTICSEARCH_INDEX"], + } + ) diff --git a/tests/functional/conftest.py b/tests/functional/conftest.py index b267c18e302..721339586fc 100644 --- a/tests/functional/conftest.py +++ b/tests/functional/conftest.py @@ -8,18 +8,12 @@ from h import db from h.app import create_app from tests.common import factories as factories_common -from tests.common.fixtures.elasticsearch import ( - ELASTICSEARCH_INDEX, - ELASTICSEARCH_URL, - es_client, # noqa: F401 - init_elasticsearch, # noqa: F401 -) from tests.functional.fixtures.authentication import * # noqa: F403 from tests.functional.fixtures.groups import * # noqa: F403 TEST_SETTINGS = { - "es.url": ELASTICSEARCH_URL, - "es.index": ELASTICSEARCH_INDEX, + "es.url": os.environ["ELASTICSEARCH_URL"], + "es.index": os.environ["ELASTICSEARCH_INDEX"], "h.app_url": "http://example.com", "h.authority": "example.com", "h.sentry_dsn_frontend": "TEST_SENTRY_DSN_FRONTEND", diff --git a/tests/unit/h/conftest.py b/tests/unit/h/conftest.py index 50332be6f24..f79f56d64d3 100644 --- a/tests/unit/h/conftest.py +++ b/tests/unit/h/conftest.py @@ -1,6 +1,7 @@ import functools import os from unittest import mock +from unittest.mock import create_autospec import click.testing import deform @@ -12,9 +13,58 @@ from h.models import Organization from h.models.auth_client import GrantType from h.security import Identity +from h.services import ( + HTTPService, + MentionService, + NotificationService, + OpenIDClientService, + ORCIDClientService, +) +from h.services.analytics import AnalyticsService +from h.services.annotation_authority_queue import AnnotationAuthorityQueueService +from h.services.annotation_delete import AnnotationDeleteService +from h.services.annotation_json import AnnotationJSONService +from h.services.annotation_metadata import AnnotationMetadataService +from h.services.annotation_moderation import AnnotationModerationService +from h.services.annotation_read import AnnotationReadService +from h.services.annotation_stats import AnnotationStatsService +from h.services.annotation_sync import AnnotationSyncService +from h.services.annotation_write import AnnotationWriteService +from h.services.auth_ticket import AuthTicketService +from h.services.auth_token import AuthTokenService +from h.services.bulk_api import ( + BulkAnnotationService, + BulkGroupService, + BulkLMSStatsService, +) +from h.services.developer_token import DeveloperTokenService +from h.services.email import EmailService +from h.services.feature import FeatureService +from h.services.flag import FlagService +from h.services.group import GroupService +from h.services.group_create import GroupCreateService +from h.services.group_delete import GroupDeleteService +from h.services.group_links import GroupLinksService +from h.services.group_list import GroupListService +from h.services.group_members import GroupMembersService +from h.services.group_update import GroupUpdateService +from h.services.job_queue import JobQueueService +from h.services.links import LinksService +from h.services.list_organizations import ListOrganizationsService +from h.services.nipsa import NipsaService +from h.services.oauth.service import OAuthProviderService +from h.services.organization import OrganizationService +from h.services.search_index import SearchIndexService +from h.services.subscription import SubscriptionService +from h.services.task_done import TaskDoneService +from h.services.url_migration import URLMigrationService +from h.services.user import UserService +from h.services.user_delete import UserDeleteService +from h.services.user_password import UserPasswordService +from h.services.user_signup import UserSignupService +from h.services.user_unique import UserUniqueService +from h.services.user_update import UserUpdateService from tests.common import factories as common_factories -from tests.common.fixtures.elasticsearch import * # noqa: F403 -from tests.common.fixtures.services import * # noqa: F403 class DummyFeature: @@ -181,3 +231,269 @@ def with_auth_client(auth_client, pyramid_config): pyramid_config.testing_securitypolicy( identity=Identity.from_models(auth_client=auth_client) ) + + +@pytest.fixture +def mock_service(pyramid_config): + def mock_service(service_class, name=None, iface=None, spec_set=True, **kwargs): # noqa: FBT002 + service = create_autospec( + service_class, instance=True, spec_set=spec_set, **kwargs + ) + if name: + pyramid_config.register_service(service, name=name) + else: + pyramid_config.register_service(service, iface=iface or service_class) + + return service + + return mock_service + + +@pytest.fixture +def analytics_service(mock_service): + return mock_service(AnalyticsService, name="analytics") + + +@pytest.fixture +def annotation_delete_service(mock_service): + return mock_service(AnnotationDeleteService, name="annotation_delete") + + +@pytest.fixture +def annotation_json_service(mock_service): + return mock_service(AnnotationJSONService, name="annotation_json") + + +@pytest.fixture +def annotation_stats_service(mock_service): + return mock_service(AnnotationStatsService, name="annotation_stats") + + +@pytest.fixture +def annotation_read_service(mock_service): + return mock_service(AnnotationReadService) + + +@pytest.fixture +def annotation_sync_service(mock_service): + return mock_service(AnnotationSyncService) + + +@pytest.fixture +def annotation_write_service(mock_service): + return mock_service(AnnotationWriteService) + + +@pytest.fixture +def annotation_metadata_service(mock_service): + return mock_service(AnnotationMetadataService) + + +@pytest.fixture +def auth_ticket_service(mock_service): + auth_ticket_service = mock_service(AuthTicketService) + auth_ticket_service.verify_ticket.return_value.deleted = False + return auth_ticket_service + + +@pytest.fixture +def auth_token_service(mock_service): + return mock_service(AuthTokenService, name="auth_token") + + +@pytest.fixture +def bulk_annotation_service(mock_service): + return mock_service(BulkAnnotationService) + + +@pytest.fixture +def bulk_group_service(mock_service): + return mock_service(BulkGroupService) + + +@pytest.fixture +def bulk_stats_service(mock_service): + return mock_service(BulkLMSStatsService) + + +@pytest.fixture +def developer_token_service(mock_service): + return mock_service(DeveloperTokenService, name="developer_token") + + +@pytest.fixture +def links_service(mock_service): + return mock_service(LinksService, name="links") + + +@pytest.fixture +def list_organizations_service(mock_service, default_organization): + list_organizations_service = mock_service( + ListOrganizationsService, name="list_organizations" + ) + + list_organizations_service.organizations.return_value = [default_organization] + return list_organizations_service + + +@pytest.fixture +def flag_service(pyramid_config): + service = create_autospec(FlagService, instance=True, spec_set=True) + pyramid_config.register_service(service, name="flag") + + return service + + +@pytest.fixture +def group_create_service(mock_service): + return mock_service(GroupCreateService, name="group_create") + + +@pytest.fixture +def group_delete_service(mock_service): + return mock_service(GroupDeleteService, name="group_delete") + + +@pytest.fixture +def group_links_service(mock_service): + return mock_service(GroupLinksService, name="group_links") + + +@pytest.fixture +def group_list_service(mock_service): + return mock_service(GroupListService, name="group_list") + + +@pytest.fixture +def group_members_service(mock_service): + return mock_service(GroupMembersService, name="group_members") + + +@pytest.fixture +def group_service(mock_service): + return mock_service(GroupService, name="group") + + +@pytest.fixture +def group_update_service(mock_service): + return mock_service(GroupUpdateService, name="group_update") + + +@pytest.fixture +def moderation_service(mock_service): + return mock_service(AnnotationModerationService, name="annotation_moderation") + + +@pytest.fixture +def nipsa_service(mock_service): + nipsa_service = mock_service(NipsaService, name="nipsa") + nipsa_service.is_flagged.return_value = False + + return nipsa_service + + +@pytest.fixture +def oauth_provider_service(mock_service): + return mock_service(OAuthProviderService, name="oauth_provider") + + +@pytest.fixture +def organization_service(mock_service): + return mock_service(OrganizationService, name="organization") + + +@pytest.fixture +def search_index(mock_service): + return mock_service(SearchIndexService, "search_index", spec_set=False) + + +@pytest.fixture +def queue_service(mock_service): + return mock_service(JobQueueService, name="queue_service") + + +@pytest.fixture +def subscription_service(mock_service): + return mock_service(SubscriptionService) + + +@pytest.fixture +def url_migration_service(mock_service): + return mock_service(URLMigrationService, name="url_migration") + + +@pytest.fixture +def user_delete_service(mock_service): + return mock_service(UserDeleteService, name="user_delete") + + +@pytest.fixture +def user_password_service(mock_service): + return mock_service(UserPasswordService, name="user_password") + + +@pytest.fixture +def user_service(mock_service): + user_service = mock_service(UserService, name="user") + user_service.fetch.return_value.deleted = False + return user_service + + +@pytest.fixture +def user_signup_service(mock_service): + return mock_service(UserSignupService, name="user_signup") + + +@pytest.fixture +def user_unique_service(mock_service): + return mock_service(UserUniqueService, name="user_unique") + + +@pytest.fixture +def user_update_service(mock_service): + return mock_service(UserUpdateService, name="user_update") + + +@pytest.fixture +def mention_service(mock_service): + return mock_service(MentionService) + + +@pytest.fixture +def notification_service(mock_service): + return mock_service(NotificationService) + + +@pytest.fixture +def annotation_authority_queue_service(mock_service): + return mock_service(AnnotationAuthorityQueueService) + + +@pytest.fixture +def feature_service(mock_service): + return mock_service(FeatureService, name="feature") + + +@pytest.fixture +def email_service(mock_service): + return mock_service(EmailService) + + +@pytest.fixture +def task_done_service(mock_service): + return mock_service(TaskDoneService) + + +@pytest.fixture +def http_service(mock_service): + return mock_service(HTTPService) + + +@pytest.fixture +def openid_client_service(mock_service): + return mock_service(OpenIDClientService) + + +@pytest.fixture +def orcid_client_service(mock_service): + return mock_service(ORCIDClientService) diff --git a/tests/unit/h/streamer/messages_speed_test.py b/tests/unit/h/streamer/messages_speed_test.py index 3470a5c5da2..95daaa6e58c 100644 --- a/tests/unit/h/streamer/messages_speed_test.py +++ b/tests/unit/h/streamer/messages_speed_test.py @@ -1,4 +1,5 @@ from _datetime import datetime +from os import environ import pytest from pyramid import security @@ -8,7 +9,6 @@ from h.streamer.contexts import request_context from h.streamer.messages import handle_annotation_event from h.streamer.websocket import WebSocket -from tests.common.fixtures.elasticsearch import ELASTICSEARCH_INDEX, ELASTICSEARCH_URL @pytest.mark.skip("Only of use during development") @@ -59,8 +59,8 @@ def message(self, annotation): @pytest.fixture(scope="session") def registry(self): settings = { - "es.url": ELASTICSEARCH_URL, - "es.index": ELASTICSEARCH_INDEX, + "es.url": environ["ELASTICSEARCH_URL"], + "es.index": environ["ELASTICSEARCH_INDEX"], "h.app_url": "http://example.com", "h.authority": "example.com", "secret_key": "notasecret", diff --git a/tox.ini b/tox.ini index 80cf9a768db..4cfbc2feadf 100644 --- a/tox.ini +++ b/tox.ini @@ -35,6 +35,7 @@ setenv = dev: H_API_AUTH_COOKIE_SALT = {env:H_API_AUTH_COOKIE_SALT:"dev_h_api_auth_cookie_salt"} dev: REPLICA_DATABASE_URL = {env:DATABASE_URL:postgresql://postgres@localhost/postgres} dev: MAILCHIMP_USER_ACTIONS_SUBACCOUNT = {env:MAILCHIMP_USER_ACTIONS_SUBACCOUNT:devdata} + tests,functests: ELASTICSEARCH_URL = {env:ELASTICSEARCH_URL:http://localhost:9200} tests: ELASTICSEARCH_INDEX = {env:ELASTICSEARCH_INDEX:hypothesis-tests} functests: ELASTICSEARCH_INDEX = {env:ELASTICSEARCH_INDEX:hypothesis-functests} {tests,functests}: AUTHORITY = {env:AUTHORITY:example.com} @@ -63,7 +64,6 @@ passenv = dev: ORCID_CLIENT_ID dev: ORCID_CLIENT_SECRET {tests,functests}: DATABASE_URL - {tests,functests}: ELASTICSEARCH_URL functests: BROKER_URL deps = pip-tools