This repository has been archived on 2023-02-22. You can view files and clone it, but cannot push or open issues or pull requests.
passerelle-atreal-openads/tests/conftest.py

96 lines
2.8 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This file is part of passerelle-atreal-openads - a Publik connector to openADS
#
# Copyright (C) 2019 Atreal
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Configuration and fixtures for tests files."""
import pytest
from httmock import urlmatch, HTTMock, response
import django_webtest
from django.core.cache import cache
@pytest.fixture(autouse=True)
def media(settings, tmpdir):
"""Set the media root to a temp dir created."""
settings.MEDIA_ROOT = str(tmpdir.mkdir('media'))
@pytest.fixture
def app(request):
"""Return a Django WebTest application."""
wtm = django_webtest.WebTestMixin()
wtm._patch_settings() # pylint: disable=protected-access
request.addfinalizer(wtm._unpatch_settings) # pylint: disable=protected-access
cache.clear()
return django_webtest.DjangoTestApp()
@pytest.fixture
def endpoint_dummy_cache(monkeypatch):
"""Monkey patch the Django cache to a 'dummy' one for all passerelle views."""
from django.core.cache import caches
import passerelle.views
monkeypatch.setattr(
passerelle.views, 'cache', caches['dummy'])
@urlmatch()
def internal_server_error(url, request): # pylint: disable=unused-argument
"""Return an HTTP 500 error."""
return response(500, 'Internal server error')
@pytest.fixture
def mock_500():
"""Mock an Internal Server Error."""
with HTTMock(internal_server_error):
yield None
@pytest.fixture
def relax_openssl(tmpdir):
'''OpenSSL default configuration has been really strict for some years,
this fixture set a temporary really permisive ciphers list.'''
import os
openssl_cnf_path = tmpdir / 'openssl.cnf'
with openssl_cnf_path.open('w') as file_pt:
file_pt.write(u'''
[default_conf]
ssl_conf = ssl_sect
[ssl_sect]
system_default = system_default_sect
[system_default_sect]
CipherString = ALL''')
old_value = os.environ.get('OPENSSL_CONF', None)
try:
os.environ['OPENSSL_CONF'] = str(openssl_cnf_path)
yield
finally:
if old_value is None:
del os.environ['OPENSSL_CONF']
else:
os.environ['OPENSSL_CONF'] = old_value