<commit_before># -*- coding: utf-8 -*-


# Copyright 2017 Akamai Technologies http://developer.akamai.com.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import os
import sys
sys.path.append("akamai/authtoken")
sys.path.append("../akamai/authtoken")
if sys.version_info[0] >= 3:
    from urllib.parse import quote_plus
else:
    from urllib import quote_plus
import unittest

from authtoken import AuthToken

import requests


if 'TEST_MODE' in os.environ and os.environ['TEST_MODE'] == 'LOCAL':
    # export TEST_MODE=LOCAL
    import secrets
    AT_HOSTNAME = secrets.AT_HOSTNAME
    AT_ENCRYPTION_KEY = secrets.AT_ENCRYPTION_KEY
    AT_TRANSITION_KEY = secrets.AT_TRANSITION_KEY    
    AT_SALT = secrets.AT_SALT
else:
    AT_HOSTNAME = os.environ['AT_HOSTNAME']
    AT_ENCRYPTION_KEY = os.environ['AT_ENCRYPTION_KEY']
    AT_TRANSITION_KEY = os.environ['AT_TRANSITION_KEY']
    AT_SALT = os.environ['AT_SALT']

DEFAULT_WINDOW_SECONDS = 500


class TestAuthToken(unittest.TestCase):

    def setUp(self):
        # Test for Query String
        self.at = AuthToken(**{'key': AT_ENCRYPTION_KEY, 'window_seconds': DEFAULT_WINDOW_SECONDS})
        
        # Test for Cookie
        self.cat = AuthToken(key=AT_ENCRYPTION_KEY, algorithm='sha1', window_seconds=DEFAULT_WINDOW_SECONDS)

        # Test for Header
        self.hat = AuthToken(key=AT_ENCRYPTION_KEY, algorithm='md5', window_seconds=DEFAULT_WINDOW_SECONDS)

    def _token_setting(self, ttype, escape_early, transition):
        t = None
        if ttype == 'q':
            t = self.at
        elif ttype == 'c':
            t = self.cat
        elif ttype == 'h':
            t = self.hat
        
        if transition:
            t.key = AT_TRANSITION_KEY
        else:
            t.key = AT_ENCRYPTION_KEY
        
        t.escape_early = escape_early

    def _queryAssertEqual(self, path, expacted, query='', escape_early=False, transition=False,
                          payload=None, session_id=None, isUrl=True):
        self._token_setting('q', escape_early, transition)
        if isUrl:
            token = self.at.generateToken(url=path, payload=None, session_id=None)
        else:
            token = self.at.generateToken(acl=path, payload=None, session_id=None)
 
        url = "http://{0}{1}{4}{2}={3}".format(AT_HOSTNAME, path, self.at.token_name, token,
            '&' if '?' in path else '?')
        response = requests.get(url)
        self.assertEqual(expacted, response.status_code)
    
    def _cookieAssertEqual(self, path, expacted, escape_early=False, transition=False,
                           payload=None, session_id=None, isUrl=True):
        self._token_setting('c', escape_early, transition)
        if isUrl:
            token = self.cat.generateToken(url=path, payload=None, session_id=None)
        else:
            token = self.cat.generateToken(acl=path, payload=None, session_id=None)<commit_msg>remove unnecessary param<commit_after># -*- coding: utf-8 -*-


# Copyright 2017 Akamai Technologies http://developer.akamai.com.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import os
import sys
sys.path.append("akamai/authtoken")
sys.path.append("../akamai/authtoken")
if sys.version_info[0] >= 3:
    from urllib.parse import quote_plus
else:
    from urllib import quote_plus
import unittest

from authtoken import AuthToken

import requests


if 'TEST_MODE' in os.environ and os.environ['TEST_MODE'] == 'LOCAL':
    # export TEST_MODE=LOCAL
    import secrets
    AT_HOSTNAME = secrets.AT_HOSTNAME
    AT_ENCRYPTION_KEY = secrets.AT_ENCRYPTION_KEY
    AT_TRANSITION_KEY = secrets.AT_TRANSITION_KEY    
    AT_SALT = secrets.AT_SALT
else:
    AT_HOSTNAME = os.environ['AT_HOSTNAME']
    AT_ENCRYPTION_KEY = os.environ['AT_ENCRYPTION_KEY']
    AT_TRANSITION_KEY = os.environ['AT_TRANSITION_KEY']
    AT_SALT = os.environ['AT_SALT']

DEFAULT_WINDOW_SECONDS = 500


class TestAuthToken(unittest.TestCase):

    def setUp(self):
        # Test for Query String
        self.at = AuthToken(**{'key': AT_ENCRYPTION_KEY, 'window_seconds': DEFAULT_WINDOW_SECONDS})
        
        # Test for Cookie
        self.cat = AuthToken(key=AT_ENCRYPTION_KEY, algorithm='sha1', window_seconds=DEFAULT_WINDOW_SECONDS)

        # Test for Header
        self.hat = AuthToken(key=AT_ENCRYPTION_KEY, algorithm='md5', window_seconds=DEFAULT_WINDOW_SECONDS)

    def _token_setting(self, ttype, escape_early, transition):
        t = None
        if ttype == 'q':
            t = self.at
        elif ttype == 'c':
            t = self.cat
        elif ttype == 'h':
            t = self.hat
        
        if transition:
            t.key = AT_TRANSITION_KEY
        else:
            t.key = AT_ENCRYPTION_KEY
        
        t.escape_early = escape_early

    def _queryAssertEqual(self, path, expacted, escape_early=False, transition=False,
                          payload=None, session_id=None, isUrl=True):
        self._token_setting('q', escape_early, transition)
        if isUrl:
            token = self.at.generateToken(url=path, payload=None, session_id=None)
        else:
            token = self.at.generateToken(acl=path, payload=None, session_id=None)
 
        url = "http://{0}{1}{4}{2}={3}".format(AT_HOSTNAME, path, self.at.token_name, token,
            '&' if '?' in path else '?')
        response = requests.get(url)
        self.assertEqual(expacted, response.status_code)
    
    def _cookieAssertEqual(self, path, expacted, escape_early=False, transition=False,
                           payload=None, session_id=None, isUrl=True):
        self._token_setting('c', escape_early, transition)
        if isUrl:
            token = self.cat.generateToken(url=path, payload=None, session_id=None)
        else:
            token = self.cat.generateToken(acl=path, payload=None, session_id=None)
{'fraction_non_alphanumeric': 0.10066030814380043, 'fraction_numerical': 0.006162876008804109, 'mean_word_length': 2.995310668229777, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2722396', 'n_tokens_mistral': 2180, 'n_tokens_neox': 2094, 'n_words': 520}