def test_get_template(self): template = { u"to_email": u"{{rcpt_email}}", u"from_name": u"{{sender_name}}", u"subject": u"blah blah {{unique_subject_value}}", u"from_email": u"{{sender_email}}", u"return_path": u"{{return_path}}", u"to_name": u"{{rcpt_name}}", u"custom_headers": {u"x-header": u"{{x_value}}"}, u"session_key": u"DEFAULT", u"options": {u"track_clicks": True}, u"plaintext_body": u"Lacus sociis congue", u"html_body": u"<HTML>{{some_key}}</HTML><br>", u"template_key": u"foo_key", } mb = MessageBusTemplatesClient(self.api_key) expected_resp = json.dumps( dict(template=template, status_code=200, status_message="template", status_time="2013-02-21T21:27:35.338Z"), sort_keys=True, ) mb.__dict__["_MessageBusBase__connection"] = self.__setup_mock_connection__( "GET", "/v5/template/foo_key", "", 200, expected_resp ) received_resp = json.dumps(mb.get_template("foo_key"), sort_keys=True) self._validate_results(expected_resp, received_resp) self.mocker.UnsetStubs()
def test_non_2xx(self): message = { "rcpt_email": "*****@*****.**", "sender_email": "*****@*****.**", "x_value": "42", "foo_key": "some key value", "unique_subject_value": "test subject", } mb = MessageBusTemplatesClient(self.api_key) expected_resp_out = json.dumps( { u"statusCode": 400, u"statusMessage": u"A non 2xx message - test", u"statusTime": u"2013-03-02T22:39:03.581Z", } ) mb.__dict__["_MessageBusBase__connection"] = self.__setup_mock_connection__( "POST", "/v5/templates/send", json.dumps(dict(templateKey="template_key", messages=[message]), sort_keys=True), 202, expected_resp_out, ) self.assertRaisesWithMessage( "Error: status_code:400 , status_message:A non 2xx message - test", mb.send_messages, "template_key", [message], ) self.mocker.UnsetStubs()
def test_create_template(self): template = { u"to_email": u"{{rcpt_email}}", u"from_name": u"{{sender_name}}", u"subject": u"blah blah {{unique_subject_value}}", u"from_email": u"{{sender_email}}", u"return_path": u"{{return_path}}", u"to_name": u"{{rcpt_name}}", u"custom_headers": {u"x-header": u"{{x_value}}"}, u"session_key": u"DEFAULT", u"options": {u"track_clicks": True}, u"plaintext_body": u"Lacus sociis congue", u"html_body": u"<HTML>{{some_key}}</HTML><br>", } template_out = { u"toEmail": u"{{rcpt_email}}", u"fromName": u"{{sender_name}}", u"subject": u"blah blah {{unique_subject_value}}", u"fromEmail": u"{{sender_email}}", u"returnPath": u"{{return_path}}", u"toName": u"{{rcpt_name}}", u"customHeaders": {u"x-header": u"{{x_value}}"}, u"sessionKey": u"DEFAULT", u"options": {u"trackClicks": True}, u"plaintextBody": u"Lacus sociis congue", u"htmlBody": u"<HTML>{{some_key}}</HTML><br>", } mb = MessageBusTemplatesClient(self.api_key) expected_resp = json.dumps( { "template_key": "0eef707e-f25f-45bd-ae52-35090b69f13b", "status_code": 201, "status_message": "Template saved", "status_time": "2013-02-21T21:27:35.338Z", }, sort_keys=True, ) expect_resp_out = json.dumps( { "templateKey": "0eef707e-f25f-45bd-ae52-35090b69f13b", "statusCode": 201, "statusMessage": "Template saved", "statusTime": "2013-02-21T21:27:35.338Z", }, sort_keys=True, ) mb.__dict__["_MessageBusBase__connection"] = self.__setup_mock_connection__( "POST", "/v5/templates/", json.dumps(template_out, sort_keys=True), 201, expect_resp_out ) received_resp = json.dumps(mb.create_template(template), sort_keys=True) self._validate_results(expected_resp, received_resp) self.mocker.UnsetStubs()
def test_send_mock_multiple(self): messages = [ { "rcpt_email": "*****@*****.**", "sender_email": "*****@*****.**", "x_value": "42", "foo_key": "some key value", "unique_subject_value": "test subject", }, { "rcpt_email": "*****@*****.**", "sender_email": "*****@*****.**", "x_value": "43", "foo_key": "some key value #2", "unique_subject_value": "test subject", }, ] mb = MessageBusTemplatesClient(self.api_key) expected_resp_out = json.dumps( { "failureCount": 0, "results": [ {"toEmail": "*****@*****.**", "messageId": "8a37a4a07c7011e29a5890b8d09e776a", "messageStatus": 0} ], "statusCode": 202, "statusMessage": "", "statusTime": "2013-02-21T21:49:17.284Z", "successCount": 1, } ) expected_resp = json.dumps( { "failure_count": 0, "results": [ {"to_email": "*****@*****.**", "message_id": "8a37a4a07c7011e29a5890b8d09e776a", "message_status": 0} ], "status_code": 202, "status_message": "", "status_time": "2013-02-21T21:49:17.284Z", "success_count": 1, }, sort_keys=True, ) mb.__dict__["_MessageBusBase__connection"] = self.__setup_mock_connection__( "POST", "/v5/templates/send", json.dumps(dict(templateKey="template_key", messages=messages), sort_keys=True), 202, expected_resp_out, ) received_resp = json.dumps(mb.send_messages(template="template_key", messages=messages), sort_keys=True) self._validate_results(expected_resp, received_resp) self.mocker.UnsetStubs()
def send_email(): message = { 'rcpt_name': 'Tim', 'rcpt_email': '*****@*****.**', 'sender_name': 'Bob', 'sender_email': '*****@*****.**', 'plaintext_body': 'Plain text body', 'some_key': 'some text value' } templates_client = MessageBusTemplatesClient(api_key, uri=uri) try: result = templates_client.send_messages(template=template_key, messages=[message]) except MessageBusResponseError, error: print error.message
def test_version_mock(self): expected_resp = json.dumps( { u"status_code": 200, u"status_message": u"API Version Lookup", u"status_time": u"2010-10-22T17:42:59.556Z", }, sort_keys=True, ) mb = MessageBusTemplatesClient(self.api_key) mb.__dict__["_MessageBusBase__connection"] = self.__setup_mock_connection__( "GET", "/v5/templates/version", "", 200, expected_resp ) received_resp = json.dumps(mb.api_version(), sort_keys=True) self._validate_results(expected_resp, received_resp) self.mocker.UnsetStubs()
def create_template(): template = { "to_name": "{{rcpt_name}}", "to_email": "{{rcpt_email}}", "from_name": "{{sender_name}}", "from_email": "{{sender_email}}", "return_path": "*****@*****.**", "custom_headers": { "x-messagebus-sdk": "py" }, "subject": "Hey! {{rcpt_name}}", "plaintext_body": "Plain text content", "html_body": "<HTML>Hey! <br>{{rcpt_name}}</br></HTML>" } try: template_client = MessageBusTemplatesClient(api_key, uri=uri) results = template_client.create_template(template) except MessageBusResponseError, error: print error.message
def get_templates(): try: template_client = MessageBusTemplatesClient(api_key, uri=uri) results = template_client.get_templates() except MessageBusResponseError, error: print error.message
def test_version(self): mb = MessageBusTemplatesClient(self.api_key) result = mb.api_version() # Templates version is of the form major.minor.path.build self.assertEquals(len(result["version"].split(".")), 4)