|
1 | | -from mamba import description, context, it, before |
2 | | -from doublex import Spy, Stub, method_returning, method_raising |
3 | | -from doublex_expects import have_been_called_with, have_been_called |
4 | | -from expects import expect, equal, raise_error |
| 1 | +import pytest |
| 2 | +import mock |
| 3 | +import unittest.mock |
| 4 | +from amadeus import Client, Response, ResponseError |
5 | 5 | from urllib.error import URLError |
6 | 6 |
|
7 | | -from amadeus import Client, Response, ResponseError |
8 | 7 |
|
9 | | -with description('HTTP') as self: |
10 | | - with before.each: |
11 | | - self.client = Client( |
12 | | - client_id='123', |
13 | | - client_secret='234', |
14 | | - log_level='silent' |
15 | | - ) |
16 | | - self.response = Stub(Response) |
17 | | - self.request_method = method_returning(self.response) |
18 | | - |
19 | | - with context('Client.get'): |
20 | | - with it('should pass all details to the request method'): |
21 | | - self.client.request = self.request_method |
22 | | - response = self.client.get('/foo', foo='bar') |
23 | | - expect(response).to(equal(self.response)) |
24 | | - expect(self.client.request).to( |
25 | | - have_been_called_with('GET', '/foo', {'foo': 'bar'}) |
26 | | - ) |
27 | | - |
28 | | - with context('Client.delete'): |
29 | | - with it('should pass all details to the request method'): |
30 | | - self.client.request = self.request_method |
31 | | - response = self.client.delete('/foo', foo='bar') |
32 | | - expect(response).to(equal(self.response)) |
33 | | - expect(self.client.request).to( |
34 | | - have_been_called_with('DELETE', '/foo', {'foo': 'bar'}) |
35 | | - ) |
36 | | - |
37 | | - with context('Client.post'): |
38 | | - with it('should pass all details to the request method'): |
39 | | - self.client.request = self.request_method |
40 | | - response = self.client.post('/foo', {'foo': 'bar'}) |
41 | | - expect(response).to(equal(self.response)) |
42 | | - expect(self.client.request).to( |
43 | | - have_been_called_with('POST', '/foo', {'foo': 'bar'}) |
44 | | - ) |
45 | | - |
46 | | - with context('Client.request'): |
47 | | - with it('should pass on to the _unauthenticated_request method'): |
48 | | - self.response.result = {'access_token': '123'} |
49 | | - self.client._unauthenticated_request = self.request_method |
50 | | - response = self.client.request('POST', '/foo', {'foo': 'bar'}) |
51 | | - expect(response).to(equal(self.response)) |
52 | | - expect(self.client._unauthenticated_request).to( |
53 | | - have_been_called_with( |
54 | | - 'POST', '/foo', {'foo': 'bar'}, 'Bearer 123' |
55 | | - ) |
56 | | - ) |
57 | | - |
58 | | - with it('should use the same access token when cashed'): |
59 | | - self.response.result = {'access_token': '123', 'expires_in': 2000} |
60 | | - self.client._unauthenticated_request = self.request_method |
61 | | - self.client.request('POST', '/foo', {'foo': 'bar'}) |
62 | | - |
63 | | - self.response.result = {'access_token': '234', 'expires_in': 2000} |
64 | | - self.client._unauthenticated_request = self.request_method |
65 | | - self.client.request('POST', '/foo', {'foo': 'bar'}) |
66 | | - |
67 | | - expect(self.client._unauthenticated_request).not_to( |
68 | | - have_been_called_with( |
69 | | - 'POST', '/foo', {'foo': 'bar'}, 'Bearer 234' |
70 | | - ) |
71 | | - ) |
72 | | - |
73 | | - with context('Client._unauthenticated_request'): |
74 | | - with it('should execute a full request'): |
75 | | - with Stub() as http_response: |
76 | | - http_response.code = 200 |
77 | | - http_response.getheaders().returns( |
78 | | - [('Content-Type', 'application/json')] |
79 | | - ) |
80 | | - http_response.read().returns('{ "data" : { "a" : 1 } }') |
81 | | - |
82 | | - self.client.http = method_returning(http_response) |
83 | | - response = self.client._unauthenticated_request( |
84 | | - 'GET', '/foo', {}, None |
85 | | - ) |
86 | | - expect(self.client.http).to(have_been_called.once) |
87 | | - expect(self.response).to(equal(self.response)) |
88 | | - |
89 | | - with it('should catch a HTTPError'): |
90 | | - self.client.http = method_raising(URLError('Error')) |
91 | | - expect( |
92 | | - lambda: self.client._unauthenticated_request( |
93 | | - 'GET', '/foo', {}, None |
94 | | - ) |
95 | | - ).to(raise_error(ResponseError)) |
96 | | - |
97 | | - with it('should log when in debug mode'): |
98 | | - with Stub() as http_response: |
99 | | - http_response.code = 200 |
100 | | - http_response.getheaders().returns( |
101 | | - [('Content-Type', 'application/json')] |
102 | | - ) |
103 | | - http_response.read().returns('{ "data" : { "a" : 1 } }') |
104 | | - |
105 | | - self.client.http = method_returning(http_response) |
106 | | - self.client.logger = Spy() |
107 | | - self.client.log_level = 'debug' |
| 8 | +@pytest.fixture |
| 9 | +def self(): |
| 10 | + self = mock.MagicMock() |
| 11 | + self.client = Client(client_id='123', client_secret='234', log_level='silent') |
| 12 | + self.response = Response(None, None) |
| 13 | + self.request_method = mock.MagicMock(return_value=self.response) |
| 14 | + return self |
| 15 | + |
| 16 | + |
| 17 | +def test_client_get(self): |
| 18 | + self.client.request = self.request_method |
| 19 | + response = self.client.get('/foo', foo='bar') |
| 20 | + assert response == self.response |
| 21 | + self.client.request.assert_called_with('GET', '/foo', {'foo': 'bar'}) |
| 22 | + |
| 23 | + |
| 24 | +def test_client_delete(self): |
| 25 | + self.client.request = self.request_method |
| 26 | + response = self.client.delete('/foo', foo='bar') |
| 27 | + assert response == self.response |
| 28 | + self.client.request.assert_called_with( |
| 29 | + 'DELETE', '/foo', {'foo': 'bar'}) |
| 30 | + |
| 31 | + |
| 32 | +def test_client_post(self): |
| 33 | + self.client.request = self.request_method |
| 34 | + response = self.client.post('/foo', {'foo': 'bar'}) |
| 35 | + assert response == self.response |
| 36 | + self.client.request.assert_called_with('POST', '/foo', {'foo': 'bar'}) |
| 37 | + |
| 38 | + |
| 39 | +def test_client_request(self): |
| 40 | + self.response.result = {'access_token': '123'} |
| 41 | + self.client._unauthenticated_request = self.request_method |
| 42 | + response = self.client.request('POST', '/foo', {'foo': 'bar'}) |
| 43 | + assert response == self.response |
| 44 | + assert self.client._unauthenticated_request.call_args == mock.call( |
| 45 | + 'POST', '/foo', {'foo': 'bar'}, 'Bearer 123' |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +def test_client_request_use_same_access_token(self): |
| 50 | + self.response.result = {'access_token': '123', 'expires_in': 2000} |
| 51 | + self.client._unauthenticated_request = self.request_method |
| 52 | + self.client.request('POST', '/foo', {'foo': 'bar'}) |
| 53 | + |
| 54 | + self.response.result = {'access_token': '234', 'expires_in': 2000} |
| 55 | + self.client._unauthenticated_request = self.request_method |
| 56 | + self.client.request('POST', '/foo', {'foo': 'bar'}) |
| 57 | + assert not self.client._unauthenticated_request.assert_called_with( |
| 58 | + 'POST', '/foo', {'foo': 'bar'}, 'Bearer 123') |
| 59 | + |
| 60 | + |
| 61 | +def test_unauthenticated_request(self): |
| 62 | + http_response = mock.MagicMock() |
| 63 | + http_response.code = 200 |
| 64 | + http_response.getheaders.return_value = [('Content-Type', 'application/json')] |
| 65 | + http_response.read.return_value = '{ "data" : { "a" : 1 } }' |
| 66 | + |
| 67 | + # Patch the client's `http` method to return the mock HTTP response |
| 68 | + unittest.mock.patch.object(self.client, 'http', return_value=http_response) |
| 69 | + |
| 70 | + # Test the client's _unauthenticated_request method |
| 71 | + with pytest.raises(ResponseError): |
| 72 | + response = self.client._unauthenticated_request('GET', '/foo', {}, None) |
| 73 | + assert self.client.call_count == 1 |
| 74 | + assert response == http_response |
| 75 | + |
| 76 | + # Test that a HTTPError is caught |
| 77 | + self.client.http.side_effect = URLError('Error') |
| 78 | + with pytest.raises(ResponseError): |
| 79 | + self.client._unauthenticated_request('GET', '/foo', {}, None) |
| 80 | + |
| 81 | + # Test logging in debug mode |
| 82 | + with mock.patch.object(self.client, 'http', return_value=http_response): |
| 83 | + logger = mock.MagicMock() |
| 84 | + self.client.logger = logger |
| 85 | + self.client.log_level = 'debug' |
| 86 | + with pytest.raises(ResponseError): |
108 | 87 | response = self.client._unauthenticated_request( |
109 | | - 'GET', '/foo', {}, None |
110 | | - ) |
111 | | - expect(self.client.logger.debug).to(have_been_called.twice) |
| 88 | + 'GET', '/foo', {}, None) |
| 89 | + assert logger.debug.call_count == 2 |
0 commit comments