Skip to content

Commit

Permalink
Add additional tests for the callback controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Aitor Magán committed Sep 26, 2014
1 parent bfaba2f commit ff73c40
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
4 changes: 2 additions & 2 deletions ckanext/oauth2/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ def callback(self):
if not error_description:
if e.message:
error_description = e.message
elif e.description:
elif hasattr(e, 'description') and e.description:
error_description = e.description
elif e.error:
elif hasattr(e, 'error') and e.error:
error_description = e.error
else:
error_description = type(e).__name__
Expand Down
26 changes: 23 additions & 3 deletions ckanext/oauth2/tests/test_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@
CAME_FROM_FIELD = 'came_from'


class CompleteException(Exception):
description = 'Exception description'
error = 'Exception error'


class ErrorException(Exception):
error = 'Exception error 2'


class VoidException(Exception):
pass


class OAuth2PluginTest(unittest.TestCase):

def setUp(self):
Expand Down Expand Up @@ -78,15 +91,22 @@ def test_controller_no_errors(self):
@parameterized.expand([
(),
('/',),
('/about', EXAMPLE_FLASH, EXAMPLE_FLASH)
('/', CompleteException(EXCEPTION_MSG), None, EXCEPTION_MSG),
('/', CompleteException(), None, CompleteException.description),
('/', ErrorException(EXCEPTION_MSG), None, EXCEPTION_MSG),
('/', ErrorException(), None, ErrorException.error),
('/', VoidException(EXCEPTION_MSG), None, EXCEPTION_MSG),
('/', VoidException(), None, type(VoidException()).__name__),
('/about', Exception(EXCEPTION_MSG), EXAMPLE_FLASH, EXAMPLE_FLASH)
])
def test_controller_errors(self, came_from=None, error_description=None, expected_flash=EXCEPTION_MSG):
def test_controller_errors(self, came_from=None, exception=Exception(EXCEPTION_MSG),
error_description=None, expected_flash=EXCEPTION_MSG):

# Recover function
controller.oauth2.get_came_from = self.get_came_from

oauth2Helper = controller.oauth2.OAuth2Helper.return_value
oauth2Helper.get_token.side_effect = Exception(EXCEPTION_MSG)
oauth2Helper.get_token.side_effect = exception

controller.toolkit.request.GET = {}
controller.toolkit.request.GET['state'] = self.generate_state(came_from)
Expand Down

0 comments on commit ff73c40

Please sign in to comment.