catalog.py 2.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
"""
Tools to create catalog-related data for use in bok choy tests.
"""
import json

import requests

from common.test.acceptance.fixtures import CATALOG_STUB_URL
from common.test.acceptance.fixtures.config import ConfigModelFixture


class CatalogFixture(object):
    """
    Interface to set up mock responses from the Catalog stub server.
    """
16 17 18 19 20 21 22 23
    def install_programs(self, programs):
        """
        Stub the discovery service's program list and detail API endpoints.

        Arguments:
            programs (list): A list of programs. Both list and detail endpoints
                will be stubbed using data from this list.
        """
24 25
        key = 'catalog.programs'

26 27 28 29 30 31
        uuids = []
        for program in programs:
            uuid = program['uuid']
            uuids.append(uuid)

            program_key = '{base}.{uuid}'.format(base=key, uuid=uuid)
32 33
            requests.put(
                '{}/set_config'.format(CATALOG_STUB_URL),
34
                data={program_key: json.dumps(program)},
35
            )
36

37 38 39 40 41 42
        # Stub list endpoint as if the uuids_only query param had been passed.
        requests.put(
            '{}/set_config'.format(CATALOG_STUB_URL),
            data={key: json.dumps(uuids)},
        )

43 44 45 46 47 48 49 50 51 52 53 54
    def install_program_types(self, program_types):
        """
        Stub the discovery service's program type list API endpoints.

        Arguments:
            program_types (list): A list of program types. List endpoint will be stubbed using data from this list.
        """
        requests.put(
            '{}/set_config'.format(CATALOG_STUB_URL),
            data={'catalog.programs_types': json.dumps(program_types)},
        )

55

56
class CatalogIntegrationMixin(object):
57
    """Mixin providing a method used to configure the catalog integration."""
58 59
    def set_catalog_integration(self, is_enabled=False, service_username=None):
        """Use this to change the catalog integration config model during tests."""
60 61
        ConfigModelFixture('/config/catalog', {
            'enabled': is_enabled,
62
            'internal_api_url': '{}/api/v1/'.format(CATALOG_STUB_URL),
63
            'cache_ttl': 0,
64
            'service_username': service_username,
65
        }).install()