Skip to content

Instantly share code, notes, and snippets.

@yeukhon
Last active December 14, 2015 15:09
Show Gist options
  • Select an option

  • Save yeukhon/5105983 to your computer and use it in GitHub Desktop.

Select an option

Save yeukhon/5105983 to your computer and use it in GitHub Desktop.
from mock import Mock
from functools import wraps
class AuthenticationRequiredError(Exception):
pass
def authentication_required(fn):
@wraps(fn)
def wrapper(self, *args, **kwargs):
username = hasattr(self, 'username')
password = hasattr(self, 'password')
auth_tuple = (username, password)
if not all(auth_tuple): # only True if all elements are not False-like value
raise AuthenticationRequiredError
return fn(self, *args, **kwargs)
return wrapper
cls = Mock()
cls.__class__.__name__ = 'cls'
cls.username = '123'
cls.password = '456'
authentication_required(cls.method)
====
Traceback (most recent call last):
File "test_auth.py", line 12, in <module>
authentication_required(cls.method)
File "/home/yeukhon/hg/bitbucket-python-api/bitbucket/auth.py", line 7, in authentication_required
@wraps(fn)
File "/usr/lib/python2.7/functools.py", line 33, in update_wrapper
setattr(wrapper, attr, getattr(wrapped, attr))
File "/home/yeukhon/hg/bbpy/local/lib/python2.7/site-packages/mock.py", line 660, in __getattr__
raise AttributeError(name)
AttributeError: __name__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment