<commit_before>from webtest import TestApp
import unittest
import json
import os

from tokenserver import main
from vep import DummyVerifier

here = os.path.dirname(__file__)


class TestService(unittest.TestCase):

    def setUp(self):

        global_config = {'__file__': os.path.join(here, 'test.ini'),
                         'here': here}

        settings = {'pyramid.includes': 'pyramid_debugtoolbar',
                    'pyramid.debug_authorization': 'false',
                    'pyramid.default_locale_name': 'en',
                    'pyramid.reload_templates': 'true',
                    'pyramid.debug_notfound': 'false',
                    'pyramid.debug_templates': 'true',
                    'mako.directories': 'cornice:templates',
                    'pyramid.debug_routematch': 'false'}

        self.app = TestApp(main(global_config, **settings))
        self.verifier = DummyVerifier

        def urlopen(url, data): # NOQA
            class response(object):
                @staticmethod
                def read():
                    key = DummyVerifier.fetch_public_key("browserid.org")
                    return json.dumps({"public-key": key})
            return response

        self.verifier.urlopen = urlopen

    def _getassertion(self):
        email = '<EMAIL>'
        url = 'http://tokenserver.services.mozilla.com'
        return self.verifier.make_assertion(email, url)

    def test_unknown_app(self):
        headers = {'Authorization': 'Browser-ID %s' % self._getassertion()}
        res = self.app.get('/1.0/xXx/token', headers=headers, status=404)
        res = json.loads(res.body)
        self.assertEqual(res['errors'][0], 'Unknown application')

    def test_no_auth(self):
        self.app.get('/1.0/sync/token', status=401)<commit_msg>Add the mozsvc ErrorCatcher to transform exceptions into HTTP responses with the appropriate codes.<commit_after>from webtest import TestApp
import unittest
import json
import os

from tokenserver import main
from vep import DummyVerifier
from mozsvc.util import CatchErrors

here = os.path.dirname(__file__)


class TestService(unittest.TestCase):

    def setUp(self):

        global_config = {'__file__': os.path.join(here, 'test.ini'),
                         'here': here}

        settings = {'pyramid.includes': 'pyramid_debugtoolbar',
                    'pyramid.debug_authorization': 'false',
                    'pyramid.default_locale_name': 'en',
                    'pyramid.reload_templates': 'true',
                    'pyramid.debug_notfound': 'false',
                    'pyramid.debug_templates': 'true',
                    'mako.directories': 'cornice:templates',
                    'pyramid.debug_routematch': 'false'}

        app = CatchErrors(main(global_config, **settings))
        self.app = TestApp(app)
        self.verifier = DummyVerifier

        def urlopen(url, data): # NOQA
            class response(object):
                @staticmethod
                def read():
                    key = DummyVerifier.fetch_public_key("browserid.org")
                    return json.dumps({"public-key": key})
            return response

        self.verifier.urlopen = urlopen

    def _getassertion(self):
        email = '<EMAIL>'
        url = 'http://tokenserver.services.mozilla.com'
        return self.verifier.make_assertion(email, url)

    def test_unknown_app(self):
        headers = {'Authorization': 'Browser-ID %s' % self._getassertion()}
        self.app.get('/1.0/xXx/token', headers=headers, status=404)

    def test_no_auth(self):
        self.app.get('/1.0/sync/token', status=401)
{'fraction_non_alphanumeric': 0.11769570011025358, 'fraction_numerical': 0.005788313120176406, 'mean_word_length': 2.22864768683274, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, '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': '908795', 'n_tokens_mistral': 1084, 'n_tokens_neox': 1025, 'n_words': 210}