Skip to content

Commit 8d070c1

Browse files
committed
remove django settings
1 parent 4b358aa commit 8d070c1

File tree

3 files changed

+49
-28
lines changed

3 files changed

+49
-28
lines changed

pytest.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
env=
33
CMIX_USERNAME="test_username"
44
CMIX_PASSWORD="test_password"
5+
CMIX_V2_CLIENT_ID="test_client_id"
6+
CMIX_V2_CLIENT_SECRET="test_client_secret"

src/api.py

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,43 @@
22
from __future__ import unicode_literals
33
import requests
44
import logging
5+
import os
56

6-
from django.conf import settings
77
from popresearch.models import CmixDataArchive, CmixSurvey, CmixSurveyXml
88

9-
109
from .error import CmixError
1110
from .parsing import generate_survey_xml_strings_and_secondary_keys
1211

1312
log = logging.getLogger(__name__)
1413

14+
CMIX_SERVICES = {
15+
'auth': {
16+
'BASE_URL': os.getenv('CMIX_URL_AUTH'),
17+
},
18+
'launchpad': {
19+
'BASE_URL': os.getenv('CMIX_URL_LAUNCHPAD'),
20+
},
21+
'reporting': {
22+
'BASE_URL': os.getenv('CMIX_URL_REPORTING'),
23+
},
24+
'survey': {
25+
'BASE_URL': os.getenv('CMIX_URL_SURVEY'),
26+
},
27+
'file': {
28+
'BASE_URL': os.getenv('CMIX_URL_FILE'),
29+
},
30+
'test': {
31+
'BASE_URL': os.getenv('CMIX_URL_TEST'),
32+
},
33+
}
34+
1535

1636
def default_cmix_api():
1737
return CmixAPI(
18-
username=settings.CMIX_USERNAME,
19-
password=settings.CMIX_PASSWORD,
20-
client_id=settings.CMIX_V2_CLIENT_ID,
21-
client_secret=settings.CMIX_V2_CLIENT_SECRET
38+
username=os.getenv("CMIX_USERNAME"),
39+
password=os.getenv("CMIX_PASSWORD"),
40+
client_id=os.getenv("CMIX_V2_CLIENT_ID"),
41+
client_secret=os.getenv("CMIX_V2_CLIENT_SECRET")
2242
)
2343

2444

@@ -60,7 +80,7 @@ def authenticate(self, *args, **kwargs):
6080
"password": self.password
6181
}
6282

63-
auth_url = '{}/access-token'.format(settings.CMIX_SERVICES['auth']['BASE_URL'])
83+
auth_url = '{}/access-token'.format(CMIX_SERVICES['auth']['BASE_URL'])
6484
try:
6585
auth_response = requests.post(auth_url, json=auth_payload, headers={"Content-Type": "application/json"})
6686
if auth_response.status_code != 200:
@@ -88,7 +108,7 @@ def fetch_banner_filter(self, survey_id, question_a, question_b, response_id):
88108
response_id
89109
)
90110
)
91-
base_url = settings.CMIX_SERVICES['reporting']['BASE_URL']
111+
base_url = CMIX_SERVICES['reporting']['BASE_URL']
92112
url = '{}/surveys/{}/response-counts'.format(base_url, survey_id)
93113
payload = {
94114
'testYN': 'LIVE',
@@ -118,7 +138,7 @@ def fetch_raw_results(self, survey_id, payload):
118138
'''
119139
self.check_auth_headers()
120140
log.debug('Requesting raw results for CMIX survey {}'.format(survey_id))
121-
base_url = settings.CMIX_SERVICES['reporting']['BASE_URL']
141+
base_url = CMIX_SERVICES['reporting']['BASE_URL']
122142
url = '{}/surveys/{}/response-counts'.format(base_url, survey_id)
123143
response = requests.post(url, headers=self._authentication_headers, json=payload)
124144
return response.json()
@@ -133,7 +153,7 @@ def get_surveys(self, status, *args, **kwargs):
133153
get_surveys('status', extra_params=params)
134154
'''
135155
self.check_auth_headers()
136-
base_url = settings.CMIX_SERVICES['survey']['BASE_URL']
156+
base_url = CMIX_SERVICES['survey']['BASE_URL']
137157
surveys_url = '{}/surveys?status={}'.format(base_url, status)
138158
extra_params = kwargs.get('extra_params')
139159
if extra_params is not None:
@@ -149,25 +169,25 @@ def add_extra_url_params(self, url, params):
149169

150170
def get_survey_definition(self, cmix_survey_id):
151171
self.check_auth_headers()
152-
definition_url = '{}/surveys/{}/definition'.format(settings.CMIX_SERVICES['survey']['BASE_URL'], cmix_survey_id)
172+
definition_url = '{}/surveys/{}/definition'.format(CMIX_SERVICES['survey']['BASE_URL'], cmix_survey_id)
153173
definition_response = requests.get(definition_url, headers=self._authentication_headers)
154174
return definition_response.json()
155175

156176
def get_survey_xml(self, cmix_survey_id):
157177
self.check_auth_headers()
158-
xml_url = '{}/surveys/{}'.format(settings.CMIX_SERVICES['file']['BASE_URL'], cmix_survey_id)
178+
xml_url = '{}/surveys/{}'.format(CMIX_SERVICES['file']['BASE_URL'], cmix_survey_id)
159179
xml_response = requests.get(xml_url, headers=self._authentication_headers)
160180
return xml_response.content
161181

162182
def get_survey_test_url(self, cmix_survey_id):
163183
self.check_auth_headers()
164-
survey_url = '{}/surveys/{}'.format(settings.CMIX_SERVICES['survey']['BASE_URL'], cmix_survey_id)
184+
survey_url = '{}/surveys/{}'.format(CMIX_SERVICES['survey']['BASE_URL'], cmix_survey_id)
165185
survey_response = requests.get(survey_url, headers=self._authentication_headers)
166186
test_token = survey_response.json().get('testToken', None)
167187
if test_token is None:
168188
raise CmixError('Survey endpoint for CMIX ID {} did not return a test token.'.format(cmix_survey_id))
169189
test_link = '{}/#/?cmixSvy={}&cmixTest={}'.format(
170-
settings.CMIX_SERVICES['test']['BASE_URL'],
190+
CMIX_SERVICES['test']['BASE_URL'],
171191
cmix_survey_id,
172192
test_token
173193
)
@@ -176,7 +196,7 @@ def get_survey_test_url(self, cmix_survey_id):
176196
def get_survey_respondents(self, cmix_survey_id, respondent_type, live):
177197
self.check_auth_headers()
178198
respondents_url = '{}/surveys/{}/respondents?respondentType={}&respondentStatus={}'.format(
179-
settings.CMIX_SERVICES['reporting']['BASE_URL'],
199+
CMIX_SERVICES['reporting']['BASE_URL'],
180200
cmix_survey_id,
181201
"LIVE" if live else "TEST",
182202
respondent_type,
@@ -186,7 +206,7 @@ def get_survey_respondents(self, cmix_survey_id, respondent_type, live):
186206

187207
def get_survey_status(self, cmix_survey_id):
188208
self.check_auth_headers()
189-
status_url = '{}/surveys/{}'.format(settings.CMIX_SERVICES['survey']['BASE_URL'], cmix_survey_id)
209+
status_url = '{}/surveys/{}'.format(CMIX_SERVICES['survey']['BASE_URL'], cmix_survey_id)
190210
status_response = requests.get(status_url, headers=self._authentication_headers)
191211
status = status_response.json().get('status', None)
192212
if status is None:
@@ -198,7 +218,7 @@ def get_survey_completes(self, cmix_survey_id):
198218

199219
def create_export_archive(self, cmix_survey, export_type):
200220
self.check_auth_headers()
201-
archive_url = '{}/surveys/{}/archives'.format(settings.CMIX_SERVICES['survey']['BASE_URL'], cmix_survey.cmix_id)
221+
archive_url = '{}/surveys/{}/archives'.format(CMIX_SERVICES['survey']['BASE_URL'], cmix_survey.cmix_id)
202222
headers = self._authentication_headers.copy()
203223
headers['Content-Type'] = "application/json"
204224
payload = {
@@ -226,7 +246,7 @@ def create_export_archive(self, cmix_survey, export_type):
226246
)
227247
archive_json = archive_response.json()
228248

229-
layout_url = '{}/surveys/{}/data-layouts/'.format(settings.CMIX_SERVICES['survey']["BASE_URL"], cmix_survey.cmix_id)
249+
layout_url = '{}/surveys/{}/data-layouts/'.format(CMIX_SERVICES['survey']["BASE_URL"], cmix_survey.cmix_id)
230250
layout_response = requests.get(layout_url, headers=self._authentication_headers)
231251
if layout_response.status_code != 200:
232252
raise CmixError(
@@ -269,7 +289,7 @@ def update_archive_status(self, archive):
269289
'Error while updating archie status: CMIX archive ID is None. Pop Archive ID: {}'.format(archive.id)
270290
)
271291
archive_url = '{}/surveys/{}/data-layouts/{}/archives/{}'.format(
272-
settings.CMIX_SERVICES['survey']["BASE_URL"],
292+
CMIX_SERVICES['survey']["BASE_URL"],
273293
archive.cmix_survey.cmix_id,
274294
layout_id,
275295
archive_id # The archive ID on CMIX.
@@ -299,7 +319,7 @@ def update_project(self, projectId, status=None):
299319
if payload_json == {}:
300320
raise CmixError("No update data was provided for CMIX Project {}".format(projectId))
301321

302-
url = '{}/projects/{}'.format(settings.CMIX_SERVICES['survey']['BASE_URL'], projectId)
322+
url = '{}/projects/{}'.format(CMIX_SERVICES['survey']['BASE_URL'], projectId)
303323
response = requests.patch(url, json=payload_json, headers=self._authentication_headers)
304324
if response.status_code > 299:
305325
raise CmixError(
@@ -328,7 +348,7 @@ def create_survey(self, survey, wave_number=None):
328348
if survey.failed_creation_attempts >= 10:
329349
continue
330350

331-
url = '{}/surveys/data'.format(settings.CMIX_SERVICES['file']['BASE_URL'])
351+
url = '{}/surveys/data'.format(CMIX_SERVICES['file']['BASE_URL'])
332352
payload = {"data": xml_string}
333353
response = requests.post(url, payload, headers=self._authentication_headers)
334354
cmix_responses.append(response)

tests/test_api.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
import xmlformatter
99
from bs4 import BeautifulSoup
1010

11-
from django.conf import settings
1211
from django.test import TestCase
1312
from freezegun import freeze_time
1413

15-
from popresearch.cmix.api import CmixAPI, default_cmix_api
14+
from popresearch.cmix.api import CmixAPI, default_cmix_api, CMIX_SERVICES
1615
from popresearch.cmix.error import CmixError
1716
from popresearch.cmix.parsing.concepts import create_legacy_concept
1817
from popresearch.cmix.parsing.logic import create_page_logic
@@ -141,7 +140,7 @@ def test_get_survey_status(self):
141140

142141
self.assertEqual(self.cmix_api.get_survey_status(self.cmix_survey.id), 'live')
143142

144-
base_url = settings.CMIX_SERVICES['survey']['BASE_URL']
143+
base_url = CMIX_SERVICES['survey']['BASE_URL']
145144
surveys_url = '{}/surveys/{}'.format(base_url, self.cmix_survey.id)
146145
mock_request.get.assert_any_call(surveys_url, headers=self.cmix_api._authentication_headers)
147146

@@ -160,7 +159,7 @@ def test_get_survey_status_error_handled(self):
160159
def test_get_survey_test_url(self):
161160
self.cmix_api._authentication_headers = {'Authentication': 'Bearer test'}
162161
correct_test_link = '{}/#/?cmixSvy={}&cmixTest={}'.format(
163-
settings.CMIX_SERVICES['test']['BASE_URL'], self.cmix_survey.id, 'test')
162+
CMIX_SERVICES['test']['BASE_URL'], self.cmix_survey.id, 'test')
164163

165164
with mock.patch('popresearch.cmix.api.requests') as mock_request:
166165
mock_get = mock.Mock()
@@ -213,11 +212,11 @@ def test_get_surveys(self):
213212
mock.Mock(json=lambda: mock_surveys),
214213
]
215214
self.cmix_api.get_surveys('LIVE')
216-
expected_url = '{}/surveys?status={}'.format(settings.CMIX_SERVICES['survey']['BASE_URL'], 'LIVE')
215+
expected_url = '{}/surveys?status={}'.format(CMIX_SERVICES['survey']['BASE_URL'], 'LIVE')
217216
mock_request.get.assert_any_call(expected_url, headers=mock.ANY)
218217

219218
expected_url_with_params = '{}/surveys?status={}&hello=world&test=params'.format(
220-
settings.CMIX_SERVICES['survey']['BASE_URL'], 'LIVE')
219+
CMIX_SERVICES['survey']['BASE_URL'], 'LIVE')
221220
self.cmix_api.get_surveys('LIVE', extra_params=["hello=world", "test=params"])
222221
mock_request.get.assert_any_call(expected_url_with_params, headers=self.cmix_api._authentication_headers)
223222

@@ -240,7 +239,7 @@ def test_fetch_banner_filter(self):
240239
response_id = 125
241240
self.cmix_api.fetch_banner_filter(self.cmix_survey.id, question_a, question_b, response_id)
242241
expected_url = '{}/surveys/{}/response-counts'.format(
243-
settings.CMIX_SERVICES['reporting']['BASE_URL'], self.cmix_survey.id)
242+
CMIX_SERVICES['reporting']['BASE_URL'], self.cmix_survey.id)
244243
expected_payload = {
245244
'testYN': 'LIVE',
246245
'status': 'COMPLETE',

0 commit comments

Comments
 (0)