<commit_before><commit_msg>Implement a method for the Authenticator to extract the authenticated user from the token cookie.<commit_after>"""Unit test for the AncientAuth authenticator class."""

from datetime import datetime
from Crypto.PublicKey.RSA import importKey

import authenticator
import calendar
import token_cookie
import token_pb2
import unittest


_TEST_KEY = """-----<KEY>
"""

_TEST_CERT = """-----BEGIN CER<KEY>
-----END CERTIFICATE-----
"""

_TEST_CA = """-----BEGIN CERTIFICATE-----
<KEY>
-----END CERTIFICATE-----
"""

class AuthenticatorTest(unittest.TestCase):
	"""Test the different methods of the AncientAuth authenticator."""

	def test_authenticated_user(self):
		"""Test if we can extract the user from a TokenCookie."""

		key = importKey(_TEST_KEY)

		token = token_pb2.TokenCookie()
		token.basic_creds.user_name = 'testosteronius'
		token.basic_creds.scope.append('users')
		token.basic_creds.expires = calendar.timegm(
			datetime.utcnow().timetuple()) + 30

		codec = token_cookie.TokenCookieCodec(token, privkey=key)
		cookie = codec.encode()

		auth = authenticator.Authenticator("Unit Test", cert=_TEST_CERT)
		self.assertEquals(auth.get_authenticated_user(cookie),
			'testosteronius')
{'fraction_non_alphanumeric': 0.13338788870703763, 'fraction_numerical': 0.0032733224222585926, 'mean_word_length': 4.741784037558685, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '2602488', 'n_tokens_mistral': 407, 'n_tokens_neox': 371, 'n_words': 96}