forked from mattrco/pyclientauth
/
tests.py
47 lines (34 loc) · 1.58 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Copyright 2012 Matt Cottingham
# Licensed under the Apache License, Version 2.0
import unittest2 as unittest
import requests
from mock import patch
from requests.exceptions import RequestException
from clientlogin import Authenticator
from clientlogin import AuthException
from clientlogin import ParseException
class AuthenticatorExceptionTests(unittest.TestCase):
def test_requests_raise(self):
"""Verify that requests exceptions are raised to caller."""
with patch.object(requests, 'post') as mock_method:
with self.assertRaises(RequestException):
mock_method.side_effect = RequestException
Authenticator.generate_token('mail@example.com','password','tag')
def test_auth_exception(self):
"""Verify that response 403 raises AuthException."""
with patch.object(requests, 'post') as mock_method:
with self.assertRaises(AuthException):
mock_method.return_value.status_code = 403
Authenticator.generate_token('mail@example.com','password','tag')
def test_parse_exception(self):
"""
Verify that a ParseException is raised if the response body doesn't
match the expected format.
"""
with patch.object(requests, 'post') as mock_method:
with self.assertRaises(ParseException):
mock_method.return_value.status_code = 200
mock_method.return_value.content = 'loremipsum'
Authenticator.generate_token('mail@example.com','password','tag')
if __name__ == '__main__':
unittest.main()