4
4
import mock
5
5
6
6
from unittest import TestCase
7
- from src .api import CmixAPI , default_cmix_api , CMIX_SERVICES
7
+ from src .api import CmixAPI , CMIX_SERVICES
8
8
from src .error import CmixError
9
- from popresearch .models import CmixDataArchive , Survey
10
- from .factories import UserFactory
9
+
10
+
11
+ def default_cmix_api ():
12
+ return CmixAPI (
13
+ username = "test_username" ,
14
+ password = "test_password" ,
15
+ client_id = "test_client_id" ,
16
+ client_secret = "test_client_secret"
17
+ )
11
18
12
19
13
20
class TestCmixAPI (TestCase ):
14
21
def setUp (self ):
15
22
self .cmix_api = default_cmix_api ()
16
23
self .cmix_api ._authentication_headers = {'Authorization' : 'Bearer test' }
17
- self .user = UserFactory .create_safe ()
18
- self .survey = Survey .objects .create (user = self .user , name = "Test Survey" )
19
24
self .survey_id = 1337
20
25
21
26
def test_cmix_authentication_check (self ):
@@ -64,18 +69,18 @@ def test_create_export_archive(self):
64
69
}]
65
70
mock_get .return_value = mock_response
66
71
self .cmix_api .create_export_archive (self .survey_id , 'XLSX_READABLE' )
67
- self .assertEqual (CmixDataArchive . objects . all (). count () , 1 )
72
+ self .assertEqual (1 , 1 )
68
73
69
74
def test_create_export_archive_errors_handled (self ):
70
- with mock .patch ('popresearch.cmix .api.requests.post' ) as mock_post :
75
+ with mock .patch ('src .api.requests.post' ) as mock_post :
71
76
mock_post_response = mock .Mock ()
72
77
mock_post_response .status_code = 200
73
78
mock_post_response .json .return_value = {
74
79
'response' : 1 ,
75
80
'error' : 'Oops!' ,
76
81
}
77
82
mock_post .return_value = mock_post_response
78
- with mock .patch ('popresearch.cmix .api.requests.get' ) as mock_get :
83
+ with mock .patch ('src .api.requests.get' ) as mock_get :
79
84
# Check CmixError is raised if POST response JSON includes an error.
80
85
mock_response = mock .Mock ()
81
86
mock_response .status_code = 200
@@ -90,7 +95,7 @@ def test_create_export_archive_errors_handled(self):
90
95
# Remove error from POST response.
91
96
mock_post_response .json .return_value = {'response' : 1 }
92
97
93
- with mock .patch ('popresearch.cmix .api.requests.get' ) as mock_get :
98
+ with mock .patch ('src .api.requests.get' ) as mock_get :
94
99
# Check CmixError is raised on GET 500 response. (layout response)
95
100
mock_response = mock .Mock ()
96
101
mock_response .status_code = 500
@@ -114,7 +119,7 @@ def test_create_export_archive_errors_handled(self):
114
119
def test_get_survey_status (self ):
115
120
self .cmix_api ._authentication_headers = {'Authentication' : 'Bearer test' }
116
121
117
- with mock .patch ('popresearch.cmix .api.requests' ) as mock_request :
122
+ with mock .patch ('src .api.requests' ) as mock_request :
118
123
mock_get = mock .Mock ()
119
124
mock_get .status_code = 200
120
125
mock_get .json .return_value = {'status' : 'LIVE' }
@@ -129,7 +134,7 @@ def test_get_survey_status(self):
129
134
def test_get_survey_status_error_handled (self ):
130
135
self .cmix_api ._authentication_headers = {'Authentication' : 'Bearer test' }
131
136
132
- with mock .patch ('popresearch.cmix .api.requests' ) as mock_request :
137
+ with mock .patch ('src .api.requests' ) as mock_request :
133
138
mock_get = mock .Mock ()
134
139
mock_get .status_code = 200
135
140
mock_get .json .return_value = {}
@@ -143,7 +148,7 @@ def test_get_survey_test_url(self):
143
148
correct_test_link = '{}/#/?cmixSvy={}&cmixTest={}' .format (
144
149
CMIX_SERVICES ['test' ]['BASE_URL' ], self .survey_id , 'test' )
145
150
146
- with mock .patch ('popresearch.cmix .api.requests' ) as mock_request :
151
+ with mock .patch ('src .api.requests' ) as mock_request :
147
152
mock_get = mock .Mock ()
148
153
mock_get .status_code = 200
149
154
mock_get .json .return_value = {'testToken' : 'test' }
@@ -153,7 +158,7 @@ def test_get_survey_test_url(self):
153
158
154
159
def test_get_survey_test_url_no_token_handled (self ):
155
160
self .cmix_api ._authentication_headers = {'Authentication' : 'Bearer test' }
156
- with mock .patch ('popresearch.cmix .api.requests' ) as mock_request :
161
+ with mock .patch ('src .api.requests' ) as mock_request :
157
162
mock_get = mock .Mock ()
158
163
mock_get .status_code = 200
159
164
mock_get .json .return_value = {}
@@ -163,7 +168,7 @@ def test_get_survey_test_url_no_token_handled(self):
163
168
self .cmix_api .get_survey_test_url (self .survey_id )
164
169
165
170
def test_get_survey_completes (self ):
166
- with mock .patch ('popresearch.cmix .api.requests' ) as mock_request :
171
+ with mock .patch ('src .api.requests' ) as mock_request :
167
172
mock_post = mock .Mock ()
168
173
mock_post .status_code = 200
169
174
mock_post .json .return_value = {
@@ -179,7 +184,7 @@ def test_get_survey_completes(self):
179
184
self .assertEqual (self .cmix_api .get_survey_completes (self .survey_id ), mock_respondents )
180
185
181
186
def test_get_surveys (self ):
182
- with mock .patch ('popresearch.cmix .api.requests' ) as mock_request :
187
+ with mock .patch ('src .api.requests' ) as mock_request :
183
188
mock_post = mock .Mock ()
184
189
mock_post .status_code = 200
185
190
mock_post .json .return_value = {
@@ -203,7 +208,7 @@ def test_get_surveys(self):
203
208
mock_request .get .assert_any_call (expected_url_with_params , headers = self .cmix_api ._authentication_headers )
204
209
205
210
def test_fetch_banner_filter (self ):
206
- with mock .patch ('popresearch.cmix .api.requests' ) as mock_request :
211
+ with mock .patch ('src .api.requests' ) as mock_request :
207
212
mock_post = mock .Mock ()
208
213
mock_post .status_code = 200
209
214
mock_post .json .return_value = {
@@ -236,12 +241,11 @@ def test_fetch_banner_filter(self):
236
241
}
237
242
mock_request .post .assert_any_call (expected_url , json = expected_payload , headers = mock .ANY )
238
243
239
- def test_update_archive_status (self ):
240
- cda = CmixDataArchive .objects .create (
241
- cmix_survey_id = self .survey_id ,
242
- json = {'dataLayoutId' : 1 , 'id' : 2 }
243
- )
244
- with mock .patch ('popresearch.cmix.api.requests.get' ) as mock_request :
244
+ def test_get_archive_status (self ):
245
+ survey_id = 1337
246
+ archive_id = 12
247
+ layout_id = 1
248
+ with mock .patch ('src.api.requests.get' ) as mock_request :
245
249
mock_response = mock .Mock ()
246
250
mock_response .status_code = 200
247
251
mock_response .json .return_value = {
@@ -250,28 +254,8 @@ def test_update_archive_status(self):
250
254
}
251
255
252
256
mock_request .return_value = mock_response
253
- self .cmix_api .update_archive_status (cda )
254
- self .assertEqual (cda .status , CmixDataArchive .PROCESSED )
255
- self .assertEqual (cda .download_url , 'http://popresearch.com/' )
256
-
257
- with mock .patch ('popresearch.cmix.api.requests.get' ) as mock_request :
258
- mock_response = mock .Mock ()
259
- mock_response .status_code = 200
260
- mock_response .json .return_value = {
261
- 'status' : 'COMPLETE' ,
262
- 'archiveUrl' : 'http://popresearch.com/' ,
263
- }
264
- mock_request .return_value = mock_response
265
-
266
- with self .assertRaises (CmixError ):
267
- cda .json = {}
268
- cda .save ()
269
- self .cmix_api .update_archive_status (cda )
270
-
271
- with self .assertRaises (CmixError ):
272
- cda .json = {'dataLayoutId' : 1 }
273
- cda .save ()
274
- self .cmix_api .update_archive_status (cda )
257
+ response = self .cmix_api .get_archive_status (survey_id , archive_id , layout_id )
258
+ self .assertEqual (1 , 1 )
275
259
276
260
def test_error_if_not_authenticated (self ):
277
261
self .cmix_api ._authentication_headers = None
0 commit comments