Skip to content

Commit

Permalink
Merge pull request rapidpro#207 from praekelt/develop
Browse files Browse the repository at this point in the history
Update production
  • Loading branch information
KaitCrawford authored Oct 30, 2017
2 parents 76e1aca + 6637c6b commit 2c9cfc3
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 9 deletions.
6 changes: 5 additions & 1 deletion casepro/backend/junebug.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,13 @@ def build_outgoing_message_json(self, outgoing, to_addr):
def send_helpdesk_outgoing_message(self, outgoing, to_addr):
if self.base_url and self.auth_token:
json_data = self.build_outgoing_message_json(outgoing, to_addr)
self.session.post(
r = self.session.post(
'%s/jembi/helpdesk/outgoing/' % self.base_url,
json=json_data)
if not r.ok:
logger.error(
"Submission to Hub unsuccessful. Code: %s Response: %s"
% (r.status_code, r.text))


class IdentityStore(object):
Expand Down
78 changes: 70 additions & 8 deletions casepro/backend/tests/test_junebug.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,14 +611,14 @@ def message_send_callback(request):
self.assertEqual(data, {'to': "+1234", 'from': "+4321", 'content': "That's great"})
headers = {'Content-Type': "application/json"}
resp = {
'status': 201,
'status': 200,
'code': "created",
'description': "message submitted",
'result': {
'id': "message-uuid-1234",
},
}
return (201, headers, json.dumps(resp))
return (200, headers, json.dumps(resp))

def hub_outgoing_callback(request):
data = json_decode(request.body)
Expand All @@ -630,14 +630,14 @@ def hub_outgoing_callback(request):
'inbound_channel_id': '66412231-4a4c-45d8-bbe1-95676ec3c5b7'})
headers = {'Content-Type': "application/json"}
resp = {
'status': 201,
'status': 200,
'code': "created",
'description': "message submitted",
'result': {
'id': "message-uuid-1234",
},
}
return (201, headers, json.dumps(resp))
return (200, headers, json.dumps(resp))

responses.add_callback(
responses.POST, "http://localhost:8080/channels/replace-me/messages/",
Expand Down Expand Up @@ -675,14 +675,14 @@ def message_send_callback(request):
self.assertEqual(data, {'to': "+1234", 'from': "+4321", 'content': "That's great"})
headers = {'Content-Type': "application/json"}
resp = {
'status': 201,
'status': 200,
'code': "created",
'description': "message submitted",
'result': {
'id': "message-uuid-1234",
},
}
return (201, headers, json.dumps(resp))
return (200, headers, json.dumps(resp))

def hub_outgoing_callback(request):
data = json_decode(request.body)
Expand All @@ -693,14 +693,14 @@ def hub_outgoing_callback(request):
'inbound_channel_id': ''})
headers = {'Content-Type': "application/json"}
resp = {
'status': 201,
'status': 200,
'code': "created",
'description': "message submitted",
'result': {
'id': "message-uuid-1234",
},
}
return (201, headers, json.dumps(resp))
return (200, headers, json.dumps(resp))

responses.add_callback(
responses.POST, "http://localhost:8080/channels/replace-me/messages/",
Expand All @@ -719,6 +719,68 @@ def hub_outgoing_callback(request):
self.backend.push_outgoing(self.unicef, [out_msg])
self.assertEqual(len(responses.calls), 2)

@responses.activate
@override_settings(
JUNEBUG_DEFAULT_CHANNEL_ID='replace-me',
JUNEBUG_CHANNELS={
'replace-me': {
'API_ROOT': 'http://localhost:8080/',
'FROM_ADDRESS': '+4321',
}
},
JUNEBUG_HUB_BASE_URL='http://localhost:8082/api/v1',
JUNEBUG_HUB_AUTH_TOKEN='sample-token')
def test_outgoing_with_hub_push_failing(self):
def message_send_callback(request):
data = json_decode(request.body)
self.assertEqual(data, {'to': "+1234", 'from': "+4321", 'content': "That's great"})
headers = {'Content-Type': "application/json"}
resp = {
'status': 200,
'code': "created",
'description': "message submitted",
'result': {
'id': "message-uuid-1234",
},
}
return (200, headers, json.dumps(resp))

def hub_outgoing_callback(request):
data = json_decode(request.body)
self.assertEqual(data, {
'content': "That's great", 'inbound_created_on': '2016-11-17T10:30:00+00:00',
'outbound_created_on': '2016-11-17T10:30:00+00:00',
'label': '', 'reply_to': '', 'to': '+1234', 'user_id': 'C-002', 'helpdesk_operator_id': self.user1.id,
'inbound_channel_id': ''})
headers = {'Content-Type': "application/json"}
resp = {
"to": [
"This field is required."
]
}
return (400, headers, json.dumps(resp))

responses.add_callback(
responses.POST, "http://localhost:8080/channels/replace-me/messages/",
callback=message_send_callback,
content_type="application/json")
self.add_hub_outgoing_callback(hub_outgoing_callback)

bob = self.create_contact(self.unicef, "C-002", "Bob")

# for messages created manually, there is not "reply-to"
self.backend = JunebugBackend()
out_msg = self.create_outgoing(
self.unicef, self.user1, None, "B", "That's great", bob, urn="tel:+1234",
created_on=datetime(2016, 11, 17, 10, 30, tzinfo=pytz.utc))

with mock.patch('casepro.backend.junebug.logger.error') as mock_logger:
self.backend.push_outgoing(self.unicef, [out_msg])
self.assertEqual(len(responses.calls), 2)
mock_logger.assert_called_once_with(
'Submission to Hub unsuccessful. Code: 400 Response: '
'{"to": ["This field is required."]}')

def test_outgoing_no_urn_no_contact(self):
"""
If the outgoing message has no URN or contact, then we cannot send it.
Expand Down
6 changes: 6 additions & 0 deletions casepro/settings_production_momza.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
# import our default settings
from settings_production import * # noqa


LOGGING['loggers']['casepro.backend.junebug'] = { # noqa: F405
'handlers': ['console'],
'level': 'INFO',
}

PODS[0]['contact_id_fieldname'] = os.environ.get( # noqa: F405
'REGISTRATION_CONTACT_ID_FIELDNAME',
'registrant_id',
Expand Down

0 comments on commit 2c9cfc3

Please sign in to comment.