1import unittest2 as unittest
2
3from ..exceptions import SocialAuthBaseException, WrongBackend, \
4    AuthFailed, AuthTokenError, AuthMissingParameter, AuthStateMissing, \
5    NotAllowedToDisconnect, AuthException, AuthCanceled, AuthUnknownError, \
6    AuthStateForbidden, AuthAlreadyAssociated, AuthTokenRevoked, \
7    AuthForbidden, AuthUnreachableProvider, InvalidEmail, MissingBackend
8
9
10class BaseExceptionTestCase(unittest.TestCase):
11    exception = None
12    expected_message = ''
13
14    def test_exception_message(self):
15        if self.exception is None and self.expected_message == '':
16            return
17        try:
18            raise self.exception
19        except SocialAuthBaseException as err:
20            self.assertEqual(str(err), self.expected_message)
21
22
23class WrongBackendTest(BaseExceptionTestCase):
24    exception = WrongBackend('foobar')
25    expected_message = 'Incorrect authentication service "foobar"'
26
27
28class AuthFailedTest(BaseExceptionTestCase):
29    exception = AuthFailed('foobar', 'wrong_user')
30    expected_message = 'Authentication failed: wrong_user'
31
32
33class AuthFailedDeniedTest(BaseExceptionTestCase):
34    exception = AuthFailed('foobar', 'access_denied')
35    expected_message = 'Authentication process was canceled'
36
37
38class AuthTokenErrorTest(BaseExceptionTestCase):
39    exception = AuthTokenError('foobar', 'Incorrect tokens')
40    expected_message = 'Token error: Incorrect tokens'
41
42
43class AuthMissingParameterTest(BaseExceptionTestCase):
44    exception = AuthMissingParameter('foobar', 'username')
45    expected_message = 'Missing needed parameter username'
46
47
48class AuthStateMissingTest(BaseExceptionTestCase):
49    exception = AuthStateMissing('foobar')
50    expected_message = 'Session value state missing.'
51
52
53class NotAllowedToDisconnectTest(BaseExceptionTestCase):
54    exception = NotAllowedToDisconnect()
55    expected_message = ''
56
57
58class AuthExceptionTest(BaseExceptionTestCase):
59    exception = AuthException('foobar', 'message')
60    expected_message = 'message'
61
62
63class AuthCanceledTest(BaseExceptionTestCase):
64    exception = AuthCanceled('foobar')
65    expected_message = 'Authentication process canceled'
66
67
68class AuthCanceledWithExtraMessageTest(BaseExceptionTestCase):
69    exception = AuthCanceled('foobar', 'error_message')
70    expected_message = 'Authentication process canceled: error_message'
71
72
73class AuthUnknownErrorTest(BaseExceptionTestCase):
74    exception = AuthUnknownError('foobar', 'some error')
75    expected_message = 'An unknown error happened while ' \
76                       'authenticating some error'
77
78
79class AuthStateForbiddenTest(BaseExceptionTestCase):
80    exception = AuthStateForbidden('foobar')
81    expected_message = 'Wrong state parameter given.'
82
83
84class AuthAlreadyAssociatedTest(BaseExceptionTestCase):
85    exception = AuthAlreadyAssociated('foobar')
86    expected_message = ''
87
88
89class AuthTokenRevokedTest(BaseExceptionTestCase):
90    exception = AuthTokenRevoked('foobar')
91    expected_message = 'User revoke access to the token'
92
93
94class AuthForbiddenTest(BaseExceptionTestCase):
95    exception = AuthForbidden('foobar')
96    expected_message = 'Your credentials aren\'t allowed'
97
98
99class AuthUnreachableProviderTest(BaseExceptionTestCase):
100    exception = AuthUnreachableProvider('foobar')
101    expected_message = 'The authentication provider could not be reached'
102
103
104class InvalidEmailTest(BaseExceptionTestCase):
105    exception = InvalidEmail('foobar')
106    expected_message = 'Email couldn\'t be validated'
107
108
109class MissingBackendTest(BaseExceptionTestCase):
110    exception = MissingBackend('backend')
111    expected_message = 'Missing backend "backend" entry'
112