This repository has been archived by the owner on Jun 20, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import unittest | ||
import mocker | ||
from contextlib import nested | ||
from google.appengine.api import users | ||
from nose.tools import assert_equals | ||
|
||
class TestDecorators(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.mocker = mocker.Mocker() | ||
|
||
def bad_mock(self): | ||
kwargs = {} | ||
|
||
google_user = self.mocker.replace('google.appengine.api.users.User') | ||
google_user() | ||
self.mocker.throw(users.UserNotFoundError) | ||
|
||
url_for_mock = self.mocker.replace('flask.url_for') | ||
url_for_mock('say_hi', _external = True, **kwargs) | ||
self.mocker.result('anything') | ||
|
||
login_url_mock = self.mocker.replace('google.appengine.api.users.create_login_url') | ||
login_url_mock('anything') | ||
self.mocker.result('/') | ||
|
||
redirect_mock = self.mocker.replace('flask.redirect') | ||
redirect_mock('/') | ||
self.mocker.result('Bye!') | ||
|
||
self.mocker.replay() | ||
|
||
def good_mock(self): | ||
google_user = self.mocker.replace('google.appengine.api.users.User') | ||
google_user() | ||
self.mocker.result('Anything else') | ||
|
||
self.mocker.replay() | ||
|
||
def test_changes_function(self): | ||
'When there is no user logged, should not call the original function' | ||
|
||
self.bad_mock() | ||
|
||
from talks_application.decorators import login_required | ||
@login_required | ||
def say_hi(): | ||
return 'Hi!' | ||
|
||
assert_equals(say_hi(), 'Bye!'); | ||
|
||
def test_no_changes_function(self): | ||
"When there is a user logged, should call the original function" | ||
self.good_mock() | ||
|
||
from talks_application.decorators import login_required | ||
@login_required | ||
def say_hi(): | ||
return 'Hi!' | ||
|
||
assert_equals(say_hi(), 'Hi!') | ||
|
||
def test_ignores_function(self): | ||
"When a function is not decorated, should not touch it" | ||
def say_hi(): | ||
return 'Hi!' | ||
|
||
assert_equals(say_hi(), 'Hi!') |