Source code for helpscout.tests.test_auth_proxy

# -*- coding: utf-8 -*-
# Copyright 2017-TODAY LasLabs Inc.
# License MIT (https://opensource.org/licenses/MIT).

import unittest

from .. import AuthProxy


[docs]class EndTestException(Exception): def __init__(self, *args, **kwargs): self.args = args self.kwargs = kwargs super(EndTestException, self).__init__()
[docs]class ApiClass(object):
[docs] @classmethod def proxied(cls, session=None): raise EndTestException(session=session)
[docs] @classmethod def not_proxied(cls, *args, **kwargs): raise Exception()
[docs]class TestAuthProxy(unittest.TestCase): SESSION = 'session'
[docs] def new_proxy(self, session=SESSION, proxy_class=ApiClass): return AuthProxy(session, proxy_class)
[docs] def test_init_bad_api_class(self): """It should assert API class is a class.""" with self.assertRaises(AssertionError): AuthProxy(None, 'not a class')
[docs] def test_init_sets_session(self): """It should set the session instance var.""" proxy = self.new_proxy() self.assertEqual(proxy.session, self.SESSION)
[docs] def test_init_sets_proxy_class(self): """It should set the proxy_class instance var.""" proxy = self.new_proxy() self.assertEqual(proxy.proxy_class, ApiClass)
[docs] def test_getattr_injects_session(self): """It should inject the session into the proxied method.""" proxy_method = self.new_proxy().proxied try: proxy_method() except EndTestException as e: self.assertEqual(e.kwargs['session'], self.SESSION)
[docs] def test_getattr_whitelist(self): """It should not proxy methods that are in the whitelist.""" proxy = self.new_proxy() proxy.METHOD_NO_PROXY.append('not_proxied') with self.assertRaises(AttributeError): proxy.not_proxied()