12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- import json
15
+ import mock
16
16
import re
17
17
18
18
from google .appengine .api import users
19
19
from google .appengine .ext import ndb
20
- import httplib2
20
+ from six . moves import http_client
21
21
import pytest
22
22
import webtest
23
23
24
24
import firetactoe
25
25
26
26
27
- class MockHttp (object ):
28
- """Mock the Http object, so we can set what the response will be."""
29
- def __init__ (self , status , content = '' ):
30
- self .content = content
31
- self .status = status
32
- self .request_url = None
27
+ class MockResponse :
28
+ def __init__ (self , json_data , status_code ):
29
+ self .json_data = json_data
30
+ self .status_code = status_code
33
31
34
- def __call__ (self , * args , ** kwargs ):
35
- return self
36
-
37
- def request (self , url , method , content = '' , * args , ** kwargs ):
38
- self .request_url = url
39
- self .request_method = method
40
- self .request_content = content
41
- return self , self .content
32
+ def json (self ):
33
+ return self .json_data
42
34
43
35
44
36
@pytest .fixture
45
37
def app (testbed , monkeypatch , login ):
46
38
# Don't let the _get_http function memoize its value
47
- firetactoe ._get_http .cache_clear ()
39
+ firetactoe ._get_session .cache_clear ()
48
40
49
41
# Provide a test firebase config. The following will set the databaseURL
50
42
# databaseURL: "http://firebase.com/test-db-url"
@@ -58,104 +50,147 @@ def app(testbed, monkeypatch, login):
58
50
59
51
60
52
def test_index_new_game (app , monkeypatch ):
61
- mock_http = MockHttp (200 , content = json .dumps ({'access_token' : '123' }))
62
- monkeypatch .setattr (httplib2 , 'Http' , mock_http )
53
+ with mock .patch (
54
+ "google.auth.transport.requests.AuthorizedSession.request" , autospec = True
55
+ ) as auth_session :
56
+ data = {'access_token' : '123' }
57
+ auth_session .return_value = MockResponse (data , http_client .OK )
63
58
64
- response = app .get ('/' )
59
+ response = app .get ('/' )
65
60
66
- assert 'g=' in response .body
67
- # Look for the unique game token
68
- assert re .search (
69
- r'initGame[^\n]+\'[\w+/=]+\.[\w+/=]+\.[\w+/=]+\'' , response .body )
61
+ assert 'g=' in response .body
62
+ # Look for the unique game token
63
+ assert re .search (
64
+ r'initGame[^\n]+\'[\w+/=]+\.[\w+/=]+\.[\w+/=]+\'' , response .body )
70
65
71
- assert firetactoe .Game .query ().count () == 1
66
+ assert firetactoe .Game .query ().count () == 1
72
67
73
- assert mock_http .request_url .startswith (
74
- 'http://firebase.com/test-db-url/channels/' )
75
- assert mock_http .request_method == 'PATCH'
68
+ auth_session .assert_called_once_with (
69
+ mock .ANY , # AuthorizedSession object
70
+ method = "PATCH" ,
71
+ url = "http://firebase.com/test-db-url/channels/3838.json" ,
72
+ body = '{"winner": null, "userX": "38", "moveX": true, "winningBoard": null, "board": " ", "userO": null}' ,
73
+ data = None ,
74
+ )
76
75
77
76
78
77
def test_index_existing_game (app , monkeypatch ):
79
- mock_http = MockHttp (200 , content = json .dumps ({'access_token' : '123' }))
80
- monkeypatch .setattr (httplib2 , 'Http' , mock_http )
81
- userX = users .
User (
'[email protected] ' ,
_user_id = '123' )
82
- firetactoe .Game (id = 'razem' , userX = userX ).put ()
78
+ with mock .patch (
79
+ "google.auth.transport.requests.AuthorizedSession.request" , autospec = True
80
+ ) as auth_session :
81
+ data = {'access_token' : '123' }
82
+ auth_session .return_value = MockResponse (data , http_client .OK )
83
+
84
+ userX = users .
User (
'[email protected] ' ,
_user_id = '123' )
85
+ firetactoe .Game (id = 'razem' , userX = userX ).put ()
83
86
84
- response = app .get ('/?g=razem' )
87
+ response = app .get ('/?g=razem' )
85
88
86
- assert 'g=' in response .body
87
- # Look for the unique game token
88
- assert re .search (
89
- r'initGame[^\n]+\'[\w+/=]+\.[\w+/=]+\.[\w+/=]+\'' , response .body )
89
+ assert 'g=' in response .body
90
+ # Look for the unique game token
91
+ assert re .search (
92
+ r'initGame[^\n]+\'[\w+/=]+\.[\w+/=]+\.[\w+/=]+\'' , response .body )
90
93
91
- assert firetactoe .Game .query ().count () == 1
92
- game = ndb .Key ('Game' , 'razem' ).get ()
93
- assert game is not None
94
- assert game .userO .user_id () == '38'
94
+ assert firetactoe .Game .query ().count () == 1
95
+ game = ndb .Key ('Game' , 'razem' ).get ()
96
+ assert game is not None
97
+ assert game .userO .user_id () == '38'
95
98
96
- assert mock_http .request_url .startswith (
97
- 'http://firebase.com/test-db-url/channels/' )
98
- assert mock_http .request_method == 'PATCH'
99
+ auth_session .assert_called_once_with (
100
+ mock .ANY , # AuthorizedSession object
101
+ method = "PATCH" ,
102
+ url = "http://firebase.com/test-db-url/channels/38razem.json" ,
103
+ body = '{"winner": null, "userX": "123", "moveX": null, "winningBoard": null, "board": null, "userO": "38"}' ,
104
+ data = None ,
105
+ )
99
106
100
107
101
108
def test_index_nonexisting_game (app , monkeypatch ):
102
- mock_http = MockHttp (200 , content = json .dumps ({'access_token' : '123' }))
103
- monkeypatch .setattr (httplib2 , 'Http' , mock_http )
104
- firetactoe .Game (id = 'razem' , userX = users .get_current_user ()).put ()
109
+ with mock .patch (
110
+ "google.auth.transport.requests.AuthorizedSession.request" , autospec = True
111
+ ) as auth_session :
112
+ data = {'access_token' : '123' }
113
+ auth_session .return_value = MockResponse (data , http_client .OK )
105
114
106
- app . get ( '/?g=razemfrazem' , status = 404 )
115
+ firetactoe . Game ( id = 'razem' , userX = users . get_current_user ()). put ( )
107
116
108
- assert mock_http .request_url is None
117
+ app .get ('/?g=razemfrazem' , status = 404 )
118
+
119
+ assert not auth_session .called
109
120
110
121
111
122
def test_opened (app , monkeypatch ):
112
- mock_http = MockHttp (200 , content = json .dumps ({'access_token' : '123' }))
113
- monkeypatch .setattr (httplib2 , 'Http' , mock_http )
114
- firetactoe .Game (id = 'razem' , userX = users .get_current_user ()).put ()
123
+ with mock .patch (
124
+ "google.auth.transport.requests.AuthorizedSession.request" , autospec = True
125
+ ) as auth_session :
126
+ data = {'access_token' : '123' }
127
+ auth_session .return_value = MockResponse (data , http_client .OK )
128
+ firetactoe .Game (id = 'razem' , userX = users .get_current_user ()).put ()
115
129
116
- app .post ('/opened?g=razem' , status = 200 )
130
+ app .post ('/opened?g=razem' , status = 200 )
117
131
118
- assert mock_http .request_url .startswith (
119
- 'http://firebase.com/test-db-url/channels/' )
120
- assert mock_http .request_method == 'PATCH'
132
+ auth_session .assert_called_once_with (
133
+ mock .ANY , # AuthorizedSession object
134
+ method = "PATCH" ,
135
+ url = "http://firebase.com/test-db-url/channels/38razem.json" ,
136
+ body = '{"winner": null, "userX": "38", "moveX": null, "winningBoard": null, "board": null, "userO": null}' ,
137
+ data = None ,
138
+ )
121
139
122
140
123
141
def test_bad_move (app , monkeypatch ):
124
- mock_http = MockHttp ( 200 , content = json . dumps ({ 'access_token' : '123' }))
125
- monkeypatch . setattr ( httplib2 , 'Http' , mock_http )
126
- firetactoe . Game (
127
- id = 'razem' , userX = users . get_current_user (), board = 9 * ' ' ,
128
- moveX = True ). put ( )
142
+ with mock . patch (
143
+ "google.auth.transport.requests.AuthorizedSession.request" , autospec = True
144
+ ) as auth_session :
145
+ data = { 'access_token' : '123' }
146
+ auth_session . return_value = MockResponse ( data , http_client . OK )
129
147
130
- app .post ('/move?g=razem' , {'i' : 10 }, status = 400 )
148
+ firetactoe .Game (
149
+ id = 'razem' , userX = users .get_current_user (), board = 9 * ' ' ,
150
+ moveX = True ).put ()
131
151
132
- assert mock_http . request_url is None
152
+ app . post ( '/move?g=razem' , { 'i' : 10 }, status = 400 )
133
153
154
+ assert not auth_session .called
134
155
135
- def test_move (app , monkeypatch ):
136
- mock_http = MockHttp (200 , content = json .dumps ({'access_token' : '123' }))
137
- monkeypatch .setattr (httplib2 , 'Http' , mock_http )
138
- firetactoe .Game (
139
- id = 'razem' , userX = users .get_current_user (), board = 9 * ' ' ,
140
- moveX = True ).put ()
141
156
142
- app .post ('/move?g=razem' , {'i' : 0 }, status = 200 )
157
+ def test_move (app , monkeypatch ):
158
+ with mock .patch (
159
+ "google.auth.transport.requests.AuthorizedSession.request" , autospec = True
160
+ ) as auth_session :
161
+ data = {'access_token' : '123' }
162
+ auth_session .return_value = MockResponse (data , http_client .OK )
143
163
144
- game = ndb .Key ('Game' , 'razem' ).get ()
145
- assert game .board == 'X' + (8 * ' ' )
164
+ firetactoe .Game (
165
+ id = 'razem' , userX = users .get_current_user (), board = 9 * ' ' ,
166
+ moveX = True ).put ()
146
167
147
- assert mock_http .request_url .startswith (
148
- 'http://firebase.com/test-db-url/channels/' )
149
- assert mock_http .request_method == 'PATCH'
168
+ app .post ('/move?g=razem' , {'i' : 0 }, status = 200 )
150
169
170
+ game = ndb .Key ('Game' , 'razem' ).get ()
171
+ assert game .board == 'X' + (8 * ' ' )
151
172
152
- def test_delete (app , monkeypatch ):
153
- mock_http = MockHttp (200 , content = json .dumps ({'access_token' : '123' }))
154
- monkeypatch .setattr (httplib2 , 'Http' , mock_http )
155
- firetactoe .Game (id = 'razem' , userX = users .get_current_user ()).put ()
173
+ auth_session .assert_called_once_with (
174
+ mock .ANY , # AuthorizedSession object
175
+ method = "PATCH" ,
176
+ url = "http://firebase.com/test-db-url/channels/38razem.json" ,
177
+ body = '{"winner": null, "userX": "38", "moveX": false, "winningBoard": null, "board": "X ", "userO": null}' ,
178
+ data = None ,
179
+ )
156
180
157
- app .post ('/delete?g=razem' , status = 200 )
158
181
159
- assert mock_http .request_url .startswith (
160
- 'http://firebase.com/test-db-url/channels/' )
161
- assert mock_http .request_method == 'DELETE'
182
+ def test_delete (app , monkeypatch ):
183
+ with mock .patch (
184
+ "google.auth.transport.requests.AuthorizedSession.request" , autospec = True
185
+ ) as auth_session :
186
+ data = {'access_token' : '123' }
187
+ auth_session .return_value = MockResponse (data , http_client .OK )
188
+ firetactoe .Game (id = 'razem' , userX = users .get_current_user ()).put ()
189
+
190
+ app .post ('/delete?g=razem' , status = 200 )
191
+
192
+ auth_session .assert_called_once_with (
193
+ mock .ANY , # AuthorizedSession object
194
+ method = "DELETE" ,
195
+ url = "http://firebase.com/test-db-url/channels/38razem.json" ,
196
+ )
0 commit comments