<commit_before>import pytest
import unittest
import mock

from pyramid.testing import DummyRequest, testConfig

from twitcher.datatype import AccessToken
from twitcher.datatype import Service
from twitcher.utils import expires_at
from twitcher.owssecurity import OWSSecurity
from twitcher.owsexceptions import OWSAccessForbidden
from twitcher.store.memory import MemoryTokenStore
from twitcher.store.memory import MemoryServiceStore


class OWSSecurityTestCase(unittest.TestCase):
    def setUp(self):
        self.access_token = AccessToken(token="<PASSWORD>", expires_at=expires_at(hours=1))

        self.tokenstore = MemoryTokenStore()
        self.tokenstore.save_token(self.access_token)
        self.empty_tokenstore = MemoryTokenStore()

        self.service = Service(url='http://nowhere/wps', name='test_wps', public=False)
        self.servicestore = MemoryServiceStore()
        self.servicestore.save_service(self.service)

        self.security = OWSSecurity(tokenstore=self.tokenstore, servicestore=self.servicestore)

    def test_get_token_by_param(self):
        params = dict(request="Execute", service="WPS", access_token="abcdef")
        request = DummyRequest(params=params)
        token = self.security.get_token_param(request)
        assert token == "abcdef"

    def test_get_token_by_path(self):
        params = dict(request="Execute", service="WPS")
        request = DummyRequest(params=params, path="/ows/proxy/emu/12345")
        token = self.security.get_token_param(request)
        assert token == "<PASSWORD>"

    def test_get_token_by_header(self):
        params = dict(request="Execute", service="WPS")
        headers = {'Access-Token': '<PASSWORD>'}
        request = DummyRequest(params=params, headers=headers)
        token = self.security.get_token_param(request)
        assert token == "<PASSWORD>"

    def test_check_request(self):
        with testConfig() as config:
            params = dict(request="Execute", service="WPS", version="1.0.0", token="cdefg")
            request = DummyRequest(params=params, path='/ows/proxy/emu')
            self.security.check_request(request)

    def test_check_request_invalid(self):
        with testConfig() as config:
            security = OWSSecurity(tokenstore=self.empty_tokenstore, servicestore=self.servicestore)

            params = dict(request="Execute", service="WPS", version="1.0.0", token="xyz")
            request = DummyRequest(params=params, path='/ows/proxy/emu')
            with pytest.raises(OWSAccessForbidden) as e_info:
                security.check_request(request)

    def test_check_request_allowed_caps(self):
        security = OWSSecurity(tokenstore=self.empty_tokenstore, servicestore=self.servicestore)

        params = dict(request="GetCapabilities", service="WPS", version="1.0.0")
        request = DummyRequest(params=params, path='/ows/proxy/emu')
        security.check_request(request)

    def test_check_request_allowed_describeprocess(self):
        security = OWSSecurity(tokenstore=self.empty_tokenstore, servicestore=self.servicestore)

        params = dict(request="DescribeProcess", service="WPS", version="1.0.0")
        request = DummyRequest(params=params, path='/ows/proxy/emu')
        security.check_request(request)

    def test_check_request_public_access(self):
        with testConfig() as config:
            servicestore = MemoryServiceStore()
            servicestore.save_service(Service(
                url='http://nowhere/wps', name='test_wps', public=True))
            security = OWSSecurity(tokenstore=self.tokenstore, servicestore=servicestore)

            params = dict(request="Execute", service="WPS", version="1.0.0", token="cdefg")
            request = DummyRequest(params=params, path='/ows/proxy/emu')
            security.check_request(request)<commit_msg>fix tests: don't use testConfig<commit_after>import pytest
import unittest
import mock

from pyramid.testing import DummyRequest, testConfig

from twitcher.datatype import AccessToken
from twitcher.datatype import Service
from twitcher.utils import expires_at
from twitcher.owssecurity import OWSSecurity
from twitcher.owsexceptions import OWSAccessForbidden
from twitcher.store.memory import MemoryTokenStore
from twitcher.store.memory import MemoryServiceStore


class OWSSecurityTestCase(unittest.TestCase):
    def setUp(self):
        self.access_token = AccessToken(token="<PASSWORD>", expires_at=expires_at(hours=1))

        self.tokenstore = MemoryTokenStore()
        self.tokenstore.save_token(self.access_token)
        self.empty_tokenstore = MemoryTokenStore()

        self.service = Service(url='http://nowhere/wps', name='test_wps', public=False)
        self.servicestore = MemoryServiceStore()
        self.servicestore.save_service(self.service)

        self.security = OWSSecurity(tokenstore=self.tokenstore, servicestore=self.servicestore)

    def test_get_token_by_param(self):
        params = dict(request="Execute", service="WPS", access_token="abcdef")
        request = DummyRequest(params=params)
        token = self.security.get_token_param(request)
        assert token == "abcdef"

    def test_get_token_by_path(self):
        params = dict(request="Execute", service="WPS")
        request = DummyRequest(params=params, path="/ows/proxy/emu/12345")
        token = self.security.get_token_param(request)
        assert token == "12345"

    def test_get_token_by_header(self):
        params = dict(request="Execute", service="WPS")
        headers = {'Access-Token': '<PASSWORD>'}
        request = DummyRequest(params=params, headers=headers)
        token = self.security.get_token_param(request)
        assert token == "<PASSWORD>"

    def test_check_request(self):
        params = dict(request="Execute", service="WPS", version="1.0.0", token="cdefg")
        request = DummyRequest(params=params, path='/ows/proxy/emu')
        self.security.check_request(request)

    def test_check_request_invalid(self):
        security = OWSSecurity(tokenstore=self.empty_tokenstore, servicestore=self.servicestore)

        params = dict(request="Execute", service="WPS", version="1.0.0", token="xyz")
        request = DummyRequest(params=params, path='/ows/proxy/emu')
        with pytest.raises(OWSAccessForbidden) as e_info:
            security.check_request(request)

    def test_check_request_allowed_caps(self):
        security = OWSSecurity(tokenstore=self.empty_tokenstore, servicestore=self.servicestore)

        params = dict(request="GetCapabilities", service="WPS", version="1.0.0")
        request = DummyRequest(params=params, path='/ows/proxy/emu')
        security.check_request(request)

    def test_check_request_allowed_describeprocess(self):
        security = OWSSecurity(tokenstore=self.empty_tokenstore, servicestore=self.servicestore)

        params = dict(request="DescribeProcess", service="WPS", version="1.0.0")
        request = DummyRequest(params=params, path='/ows/proxy/emu')
        security.check_request(request)

    def test_check_request_public_access(self):
        servicestore = MemoryServiceStore()
        servicestore.save_service(Service(
            url='http://nowhere/wps', name='test_wps', public=True))
        security = OWSSecurity(tokenstore=self.tokenstore, servicestore=servicestore)

        params = dict(request="Execute", service="WPS", version="1.0.0", token="cdefg")
        request = DummyRequest(params=params, path='/ows/proxy/emu')
        security.check_request(request)
{'fraction_non_alphanumeric': 0.11399308326682628, 'fraction_numerical': 0.0062516626762436816, 'mean_word_length': 3.9564930784442978, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4493802', 'n_tokens_mistral': 2287, 'n_tokens_neox': 2208, 'n_words': 371}