diff --git a/.gitignore b/.gitignore index 47a5468..d2b9aba 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ htmlcov .tox lib/createsend.egg-info .rake_tasks~ +.idea \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 66c5044..31581d4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,10 @@ language: python python: - 2.7 - - 3.3 - 3.4 - 3.5 - 3.6 + - 3.7 install: - pip install coverage coveralls script: diff --git a/README.md b/README.md index e09827d..612ef6d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # createsend -A Python library which implements the complete functionality of the [Campaign Monitor API](http://www.campaignmonitor.com/api/). Requires Python 2.7, 3.3, 3.4, or 3.5. +A Python library which implements the complete functionality of the [Campaign Monitor API](http://www.campaignmonitor.com/api/). Requires Python 2.7, 3.4, 3.5, 3.6 or 3.7. ## Installation diff --git a/lib/createsend/__init__.py b/lib/createsend/__init__.py index 5e14516..a90460f 100644 --- a/lib/createsend/__init__.py +++ b/lib/createsend/__init__.py @@ -27,6 +27,8 @@ ExpiredOAuthToken, CreateSendBase, CreateSend) +from createsend.journey_email import JourneyEmail +from createsend.journey import Journey from createsend.list import List from createsend.person import Person from createsend.segment import Segment diff --git a/lib/createsend/client.py b/lib/createsend/client.py index 2ee36fc..1054384 100644 --- a/lib/createsend/client.py +++ b/lib/createsend/client.py @@ -167,5 +167,10 @@ def delete(self): """Deletes this client.""" response = self._delete("/clients/%s.json" % self.client_id) + def journeys(self): + """Retrieves the journeys associated with the client""" + response = self._get(self.uri_for('journeys')) + return json_to_py(response) + def uri_for(self, action): return "/clients/%s/%s.json" % (self.client_id, action) diff --git a/lib/createsend/journey.py b/lib/createsend/journey.py new file mode 100644 index 0000000..42bce86 --- /dev/null +++ b/lib/createsend/journey.py @@ -0,0 +1,17 @@ +from __future__ import absolute_import + +from createsend.createsend import CreateSendBase +from createsend.utils import json_to_py + + +class Journey(CreateSendBase): + """Represents a journey and associated functionality""" + + def __init__(self, auth=None, journey_id=None): + self.journey_id = journey_id + super(Journey, self).__init__(auth) + + def summary(self): + """Gets the summary of the journey""" + response = self._get("/journeys/%s.json" % self.journey_id) + return json_to_py(response) diff --git a/lib/createsend/journey_email.py b/lib/createsend/journey_email.py new file mode 100644 index 0000000..fd01394 --- /dev/null +++ b/lib/createsend/journey_email.py @@ -0,0 +1,50 @@ +from __future__ import absolute_import + +from createsend.createsend import CreateSendBase +from createsend.utils import json_to_py + + +class JourneyEmail(CreateSendBase): + """Represents a journey and associated functionality""" + + def __init__(self, auth=None, journey_email_id=None): + self.journey_email_id = journey_email_id + super(JourneyEmail, self).__init__(auth) + + def bounces(self, date=None, page=None, page_size=None, order_direction=None): + """Retrieves the bounces for this journey email.""" + return self.get_journey_email_response(date, page, page_size, order_direction, "bounces") + + def clicks(self, date=None, page=None, page_size=None, order_direction=None): + """Retrieves the clicks for this journey email.""" + return self.get_journey_email_response(date, page, page_size, order_direction, "clicks") + + def opens(self, date=None, page=None, page_size=None, order_direction=None): + """Retrieves the opens for this journey email.""" + return self.get_journey_email_response(date, page, page_size, order_direction, "opens") + + def recipients(self, date=None, page=None, page_size=None, order_direction=None): + """Retrieves the recipients for this journey email.""" + return self.get_journey_email_response(date, page, page_size, order_direction, "recipients") + + def unsubscribes(self, date=None, page=None, page_size=None, order_direction=None): + """Retrieves the unsubscribes for this journey email.""" + return self.get_journey_email_response(date, page, page_size, order_direction, "unsubscribes") + + def get_journey_email_response(self, date, page, page_size, order_direction, uri): + """Retrieves information for the journey email - based on theuri""" + params = {} + if date is not None: + params["date"] = date + if page is not None: + params["page"] = page + if page_size is not None: + params["pagesize"] = page_size + if order_direction is not None: + params["orderdirection"] = order_direction + response = self._get(self.uri_for(uri), params=params) + return json_to_py(response) + + def uri_for(self, action): + return "/journeys/email/%s/%s.json" % (self.journey_email_id, action) + diff --git a/test/fixtures/client_journeys.json b/test/fixtures/client_journeys.json new file mode 100644 index 0000000..47f8066 --- /dev/null +++ b/test/fixtures/client_journeys.json @@ -0,0 +1,14 @@ +[ + { + "ListID": "a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1", + "JourneyID": "b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2", + "Name": "Journey 1", + "Status": "Active" + }, + { + "ListID": "a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1", + "JourneyID": "b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2", + "Name": "New journey", + "Status": "Active" + } +] diff --git a/test/fixtures/journey_email_bounces_no_params.json b/test/fixtures/journey_email_bounces_no_params.json new file mode 100644 index 0000000..719dfc9 --- /dev/null +++ b/test/fixtures/journey_email_bounces_no_params.json @@ -0,0 +1,23 @@ +{ + "Results": [ + { + "EmailAddress": "asdf@softbouncemyemail.comX", + "BounceType": "Soft", + "Date": "2019-08-20 14:24:00", + "Reason": "Soft Bounce - Dns Failure" + }, + { + "EmailAddress": "asdf@hardbouncemyemail.com", + "BounceType": "Hard", + "Date": "2019-08-21 04:26:00", + "Reason": "Hard Bounce" + } + ], + "ResultsOrderedBy": "Date", + "OrderDirection": "ASC", + "PageNumber": 1, + "PageSize": 1000, + "RecordsOnThisPage": 2, + "TotalNumberOfRecords": 2, + "NumberOfPages": 1 +} diff --git a/test/fixtures/journey_email_bounces_with_params.json b/test/fixtures/journey_email_bounces_with_params.json new file mode 100644 index 0000000..a05c27e --- /dev/null +++ b/test/fixtures/journey_email_bounces_with_params.json @@ -0,0 +1,23 @@ +{ + "Results": [ + { + "EmailAddress": "asdf@hardbouncemyemail.com", + "BounceType": "Hard", + "Date": "2019-08-21 04:26:00", + "Reason": "Hard Bounce" + }, + { + "EmailAddress": "asdf@softbouncemyemail.comX", + "BounceType": "Soft", + "Date": "2019-08-20 14:24:00", + "Reason": "Soft Bounce - Dns Failure" + } + ], + "ResultsOrderedBy": "Date", + "OrderDirection": "DESC", + "PageNumber": 1, + "PageSize": 10, + "RecordsOnThisPage": 2, + "TotalNumberOfRecords": 2, + "NumberOfPages": 1 +} diff --git a/test/fixtures/journey_email_clicks_no_params.json b/test/fixtures/journey_email_clicks_no_params.json new file mode 100644 index 0000000..a3ca47a --- /dev/null +++ b/test/fixtures/journey_email_clicks_no_params.json @@ -0,0 +1,35 @@ +{ + "Results": [ + { + "EmailAddress": "asdf@example.com", + "Date": "2019-08-19 10:23:00", + "URL": "http://mail.google.com/mail/?hl=en&tab=wm", + "IPAddress": "198.148.196.144", + "Latitude": -33.8591, + "Longitude": 151.200195, + "City": "Sydney", + "Region": "New South Wales", + "CountryCode": "AU", + "CountryName": "Australia" + }, + { + "EmailAddress": "asdf+2@example.com", + "Date": "2019-08-19 10:24:00", + "URL": "https://example.com", + "IPAddress": "198.148.196.144", + "Latitude": -33.8591, + "Longitude": 151.200195, + "City": "Sydney", + "Region": "New South Wales", + "CountryCode": "AU", + "CountryName": "Australia" + } + ], + "ResultsOrderedBy": "Date", + "OrderDirection": "ASC", + "PageNumber": 1, + "PageSize": 1000, + "RecordsOnThisPage": 2, + "TotalNumberOfRecords": 2, + "NumberOfPages": 1 +} diff --git a/test/fixtures/journey_email_clicks_with_params.json b/test/fixtures/journey_email_clicks_with_params.json new file mode 100644 index 0000000..8a68d13 --- /dev/null +++ b/test/fixtures/journey_email_clicks_with_params.json @@ -0,0 +1,35 @@ +{ + "Results": [ + { + "EmailAddress": "asdf+2@example.com", + "Date": "2019-08-19 10:24:00", + "URL": "https://example.com", + "IPAddress": "198.148.196.144", + "Latitude": -33.8591, + "Longitude": 151.200195, + "City": "Sydney", + "Region": "New South Wales", + "CountryCode": "AU", + "CountryName": "Australia" + }, + { + "EmailAddress": "asdf@example.com", + "Date": "2019-08-19 10:23:00", + "URL": "http://mail.google.com/mail/?hl=en&tab=wm", + "IPAddress": "198.148.196.144", + "Latitude": -33.8591, + "Longitude": 151.200195, + "City": "Sydney", + "Region": "New South Wales", + "CountryCode": "AU", + "CountryName": "Australia" + } + ], + "ResultsOrderedBy": "Date", + "OrderDirection": "DESC", + "PageNumber": 1, + "PageSize": 10, + "RecordsOnThisPage": 2, + "TotalNumberOfRecords": 2, + "NumberOfPages": 1 +} diff --git a/test/fixtures/journey_email_opens_no_params.json b/test/fixtures/journey_email_opens_no_params.json new file mode 100644 index 0000000..7bd0109 --- /dev/null +++ b/test/fixtures/journey_email_opens_no_params.json @@ -0,0 +1,33 @@ +{ + "Results": [ + { + "EmailAddress": "asdf@example.com", + "Date": "2019-08-19 10:23:00", + "IPAddress": "198.148.196.144", + "Latitude": -33.8591, + "Longitude": 151.200195, + "City": "Sydney", + "Region": "New South Wales", + "CountryCode": "AU", + "CountryName": "Australia" + }, + { + "EmailAddress": "asdf+2@example.com", + "Date": "2019-08-19 10:24:00", + "IPAddress": "198.148.196.144", + "Latitude": -33.8591, + "Longitude": 151.200195, + "City": "Sydney", + "Region": "New South Wales", + "CountryCode": "AU", + "CountryName": "Australia" + } + ], + "ResultsOrderedBy": "Date", + "OrderDirection": "ASC", + "PageNumber": 1, + "PageSize": 1000, + "RecordsOnThisPage": 2, + "TotalNumberOfRecords": 2, + "NumberOfPages": 1 +} diff --git a/test/fixtures/journey_email_opens_with_params.json b/test/fixtures/journey_email_opens_with_params.json new file mode 100644 index 0000000..79a76e7 --- /dev/null +++ b/test/fixtures/journey_email_opens_with_params.json @@ -0,0 +1,33 @@ +{ + "Results": [ + { + "EmailAddress": "asdf+2@example.com", + "Date": "2019-08-19 10:24:00", + "IPAddress": "198.148.196.144", + "Latitude": -33.8591, + "Longitude": 151.200195, + "City": "Sydney", + "Region": "New South Wales", + "CountryCode": "AU", + "CountryName": "Australia" + }, + { + "EmailAddress": "asdf@example.com", + "Date": "2019-08-19 10:23:00", + "IPAddress": "198.148.196.144", + "Latitude": -33.8591, + "Longitude": 151.200195, + "City": "Sydney", + "Region": "New South Wales", + "CountryCode": "AU", + "CountryName": "Australia" + } + ], + "ResultsOrderedBy": "Date", + "OrderDirection": "DESC", + "PageNumber": 1, + "PageSize": 10, + "RecordsOnThisPage": 2, + "TotalNumberOfRecords": 2, + "NumberOfPages": 1 +} diff --git a/test/fixtures/journey_email_recipients_no_params.json b/test/fixtures/journey_email_recipients_no_params.json new file mode 100644 index 0000000..7329a90 --- /dev/null +++ b/test/fixtures/journey_email_recipients_no_params.json @@ -0,0 +1,27 @@ +{ + "Results": [ + { + "EmailAddress": "asdf@example.com", + "SentDate": "2019-08-19 10:23:00" + }, + { + "EmailAddress": "asdf+2@example.com", + "SentDate": "2019-08-19 10:23:00" + }, + { + "EmailAddress": "asdf@softbouncemyemail.comX", + "SentDate": "2019-08-20 14:24:00" + }, + { + "EmailAddress": "asdf@hardbouncemyemail.com", + "SentDate": "2019-08-21 04:26:00" + } + ], + "ResultsOrderedBy": "SentDate", + "OrderDirection": "ASC", + "PageNumber": 1, + "PageSize": 1000, + "RecordsOnThisPage": 4, + "TotalNumberOfRecords": 4, + "NumberOfPages": 1 +} diff --git a/test/fixtures/journey_email_recipients_with_params.json b/test/fixtures/journey_email_recipients_with_params.json new file mode 100644 index 0000000..edd87ce --- /dev/null +++ b/test/fixtures/journey_email_recipients_with_params.json @@ -0,0 +1,27 @@ +{ + "Results": [ + { + "EmailAddress": "asdf@hardbouncemyemail.com", + "SentDate": "2019-08-21 04:26:00" + }, + { + "EmailAddress": "asdf@softbouncemyemail.comX", + "SentDate": "2019-08-20 14:24:00" + }, + { + "EmailAddress": "asdf+2@example.com", + "SentDate": "2019-08-19 10:23:00" + }, + { + "EmailAddress": "asdf@example.com", + "SentDate": "2019-08-19 10:23:00" + } + ], + "ResultsOrderedBy": "SentDate", + "OrderDirection": "DESC", + "PageNumber": 1, + "PageSize": 10, + "RecordsOnThisPage": 4, + "TotalNumberOfRecords": 4, + "NumberOfPages": 1 +} diff --git a/test/fixtures/journey_email_unsubscribes_no_params.json b/test/fixtures/journey_email_unsubscribes_no_params.json new file mode 100644 index 0000000..7e14403 --- /dev/null +++ b/test/fixtures/journey_email_unsubscribes_no_params.json @@ -0,0 +1,16 @@ +{ + "Results": [ + { + "EmailAddress": "asdf@example.com", + "Date": "2019-08-19 10:24:00", + "IPAddress": "198.148.196.144" + } + ], + "ResultsOrderedBy": "Date", + "OrderDirection": "ASC", + "PageNumber": 1, + "PageSize": 1000, + "RecordsOnThisPage": 1, + "TotalNumberOfRecords": 1, + "NumberOfPages": 1 +} diff --git a/test/fixtures/journey_email_unsubscribes_with_params.json b/test/fixtures/journey_email_unsubscribes_with_params.json new file mode 100644 index 0000000..8c80538 --- /dev/null +++ b/test/fixtures/journey_email_unsubscribes_with_params.json @@ -0,0 +1,16 @@ +{ + "Results": [ + { + "EmailAddress": "asdf@example.com", + "Date": "2019-08-19 10:24:00", + "IPAddress": "198.148.196.144" + } + ], + "ResultsOrderedBy": "Date", + "OrderDirection": "DESC", + "PageNumber": 1, + "PageSize": 10, + "RecordsOnThisPage": 1, + "TotalNumberOfRecords": 1, + "NumberOfPages": 1 +} diff --git a/test/fixtures/journey_summary.json b/test/fixtures/journey_summary.json new file mode 100644 index 0000000..7e57424 --- /dev/null +++ b/test/fixtures/journey_summary.json @@ -0,0 +1,18 @@ +{ + "JourneyID": "a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1", + "Name": "New journey", + "TriggerType": "On Subscription", + "Status": "Active", + "Emails": [ + { + "EmailID": "b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1", + "Name": "1", + "Bounced": 7, + "Clicked": 1, + "Opened": 12, + "Sent": 11, + "UniqueOpened": 4, + "Unsubscribed": 1 + } + ] +} diff --git a/test/test_client.py b/test/test_client.py index 3477ac5..a63efa2 100644 --- a/test/test_client.py +++ b/test/test_client.py @@ -216,6 +216,15 @@ def test_delete(self): self.cl.stub_request("clients/%s.json" % self.cl.client_id, None) self.cl.delete() + def test_journeys(self): + self.cl.stub_request("clients/%s/journeys.json" % self.cl.client_id, "client_journeys.json") + journeys = self.cl.journeys() + journey_one = journeys[0] + self.assertEqual(journey_one.JourneyID, "b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2") + self.assertEqual(journey_one.ListID, "a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1") + self.assertEqual(journey_one.Name, "Journey 1") + self.assertEqual(journey_one.Status, "Active") + class OAuthClientTestCase(unittest.TestCase, ClientTestCase): """Test when using OAuth to authenticate""" diff --git a/test/test_journey.py b/test/test_journey.py new file mode 100644 index 0000000..ba9a8b8 --- /dev/null +++ b/test/test_journey.py @@ -0,0 +1,42 @@ +import unittest + +from createsend.journey import Journey + + +class JourneyTestCase(object): + + def test_summary(self): + self.journey.stub_request( + "journeys/%s.json" % self.journey_id, "journey_summary.json") + summary = self.journey.summary() + self.assertEqual(summary.JourneyID, "a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1") + self.assertEqual(summary.Name, "New journey") + self.assertEqual(summary.TriggerType, "On Subscription") + self.assertEqual(summary.Status, "Active") + email_one = summary.Emails[0] + self.assertEqual(email_one.EmailID, "b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1") + self.assertEqual(email_one.Name, "1") + self.assertEqual(email_one.Bounced, 7) + self.assertEqual(email_one.Clicked, 1) + self.assertEqual(email_one.Opened, 12) + self.assertEqual(email_one.Sent, 11) + self.assertEqual(email_one.UniqueOpened, 4) + self.assertEqual(email_one.Unsubscribed, 1) + + +class OAuthJourneyTestCase(unittest.TestCase, JourneyTestCase): + """Test when using OAuth to authenticate""" + + def setUp(self): + self.journey_id = "787y87y87y87y87y87y87" + self.journey = Journey( + {"access_token": "ASP95S4aR+9KsgfHB0dapTYxNA==", "refresh_token": "5S4aASP9R+9KsgfHB0dapTYxNA=="}, self.journey_id) + + +class ApiKeyJourneyTestCase(unittest.TestCase, JourneyTestCase): + """Test when using an API key to authenticate""" + + def setUp(self): + self.journey_id = "787y87y87y87y87y87y87" + self.journey = Journey( + {'api_key': '123123123123123123123'}, self.journey_id) diff --git a/test/test_journey_email.py b/test/test_journey_email.py new file mode 100644 index 0000000..1a5a531 --- /dev/null +++ b/test/test_journey_email.py @@ -0,0 +1,219 @@ +from six.moves.urllib.parse import quote +import unittest + +from createsend.journey_email import JourneyEmail + + +class JourneyEmailTestCase(object): + + def test_bounces_no_params(self): + self.journey_email.stub_request(self.no_param_uri_for("bounces"), "journey_email_bounces_no_params.json") + bounces = self.journey_email.bounces() + self.assertEqual(len(bounces.Results), 2) + bounce_one = bounces.Results[0] + self.assertEqual(bounce_one.EmailAddress, "asdf@softbouncemyemail.comX") + self.assertEqual(bounce_one.BounceType, "Soft") + self.assertEqual(bounce_one.Date, "2019-08-20 14:24:00") + self.assertEqual(bounce_one.Reason, "Soft Bounce - Dns Failure") + self.assertEqual(bounces.ResultsOrderedBy, "Date") + self.assertEqual(bounces.OrderDirection, "ASC") + self.assertEqual(bounces.PageNumber, 1) + self.assertEqual(bounces.PageSize, 1000) + self.assertEqual(bounces.RecordsOnThisPage, 2) + self.assertEqual(bounces.TotalNumberOfRecords, 2) + self.assertEqual(bounces.NumberOfPages, 1) + + def test_bounces_with_params(self): + self.journey_email.stub_request(self.param_uri_for("bounces", "2019-01-01", 1, 10, "desc"), "journey_email_bounces_with_params.json") + bounces = self.journey_email.bounces(date="2019-01-01", page=1, page_size=10, order_direction="desc") + self.assertEqual(len(bounces.Results), 2) + bounce_one = bounces.Results[0] + self.assertEqual(bounce_one.EmailAddress, "asdf@hardbouncemyemail.com") + self.assertEqual(bounce_one.BounceType, "Hard") + self.assertEqual(bounce_one.Date, "2019-08-21 04:26:00") + self.assertEqual(bounce_one.Reason, "Hard Bounce") + self.assertEqual(bounces.ResultsOrderedBy, "Date") + self.assertEqual(bounces.OrderDirection, "DESC") + self.assertEqual(bounces.PageNumber, 1) + self.assertEqual(bounces.PageSize, 10) + self.assertEqual(bounces.RecordsOnThisPage, 2) + self.assertEqual(bounces.TotalNumberOfRecords, 2) + self.assertEqual(bounces.NumberOfPages, 1) + + def test_clicks_no_params(self): + self.journey_email.stub_request(self.no_param_uri_for("clicks"), "journey_email_clicks_no_params.json") + clicks = self.journey_email.clicks() + self.assertEqual(len(clicks.Results), 2) + click_one = clicks.Results[0] + self.assertEqual(click_one.EmailAddress, "asdf@example.com") + self.assertEqual(click_one.Date, "2019-08-19 10:23:00") + self.assertEqual(click_one.URL, "http://mail.google.com/mail/?hl=en&tab=wm") + self.assertEqual(click_one.IPAddress, "198.148.196.144") + self.assertEqual(click_one.Latitude, -33.8591) + self.assertEqual(click_one.Longitude, 151.200195) + self.assertEqual(click_one.City, "Sydney") + self.assertEqual(click_one.Region, "New South Wales") + self.assertEqual(click_one.CountryCode, "AU") + self.assertEqual(click_one.CountryName, "Australia") + self.assertEquals(clicks.ResultsOrderedBy, "Date") + self.assertEquals(clicks.OrderDirection, "ASC") + self.assertEquals(clicks.PageNumber, 1) + self.assertEquals(clicks.PageSize, 1000) + self.assertEquals(clicks.RecordsOnThisPage, 2) + self.assertEquals(clicks.TotalNumberOfRecords, 2) + self.assertEquals(clicks.NumberOfPages, 1) + + def test_clicks_with_params(self): + self.journey_email.stub_request(self.param_uri_for("clicks", "2019-01-01", 1, 10, "desc"), "journey_email_clicks_with_params.json") + clicks = self.journey_email.clicks(date="2019-01-01", page=1, page_size=10, order_direction="desc") + self.assertEqual(len(clicks.Results), 2) + click_one = clicks.Results[0] + self.assertEqual(click_one.EmailAddress, "asdf+2@example.com") + self.assertEqual(click_one.Date, "2019-08-19 10:24:00") + self.assertEqual(click_one.URL, "https://example.com") + self.assertEqual(click_one.IPAddress, "198.148.196.144") + self.assertEqual(click_one.Latitude, -33.8591) + self.assertEqual(click_one.Longitude, 151.200195) + self.assertEqual(click_one.City, "Sydney") + self.assertEqual(click_one.Region, "New South Wales") + self.assertEqual(click_one.CountryCode, "AU") + self.assertEqual(click_one.CountryName, "Australia") + self.assertEquals(clicks.ResultsOrderedBy, "Date") + self.assertEquals(clicks.OrderDirection, "DESC") + self.assertEquals(clicks.PageNumber, 1) + self.assertEquals(clicks.PageSize, 10) + self.assertEquals(clicks.RecordsOnThisPage, 2) + self.assertEquals(clicks.TotalNumberOfRecords, 2) + self.assertEquals(clicks.NumberOfPages, 1) + + def test_opens_no_params(self): + self.journey_email.stub_request(self.no_param_uri_for("opens"), "journey_email_opens_no_params.json") + opens = self.journey_email.opens() + self.assertEqual(len(opens.Results), 2) + open_one = opens.Results[0] + self.assertEqual(open_one.EmailAddress, "asdf@example.com") + self.assertEqual(open_one.Date, "2019-08-19 10:23:00") + self.assertEqual(open_one.IPAddress, "198.148.196.144") + self.assertEqual(open_one.Latitude, -33.8591) + self.assertEqual(open_one.Longitude, 151.200195) + self.assertEqual(open_one.City, "Sydney") + self.assertEqual(open_one.Region, "New South Wales") + self.assertEqual(open_one.CountryCode, "AU") + self.assertEqual(open_one.CountryName, "Australia") + self.assertEquals(opens.ResultsOrderedBy, "Date") + self.assertEquals(opens.OrderDirection, "ASC") + self.assertEquals(opens.PageNumber, 1) + self.assertEquals(opens.PageSize, 1000) + self.assertEquals(opens.RecordsOnThisPage, 2) + self.assertEquals(opens.TotalNumberOfRecords, 2) + self.assertEquals(opens.NumberOfPages, 1) + + def test_opens_with_params(self): + self.journey_email.stub_request(self.param_uri_for("opens", "2019-01-01", 1, 10, "desc"), "journey_email_opens_with_params.json") + opens = self.journey_email.opens(date="2019-01-01", page=1, page_size=10, order_direction="desc") + self.assertEqual(len(opens.Results), 2) + open_one = opens.Results[0] + self.assertEqual(open_one.EmailAddress, "asdf+2@example.com") + self.assertEqual(open_one.Date, "2019-08-19 10:24:00") + self.assertEqual(open_one.IPAddress, "198.148.196.144") + self.assertEqual(open_one.Latitude, -33.8591) + self.assertEqual(open_one.Longitude, 151.200195) + self.assertEqual(open_one.City, "Sydney") + self.assertEqual(open_one.Region, "New South Wales") + self.assertEqual(open_one.CountryCode, "AU") + self.assertEqual(open_one.CountryName, "Australia") + self.assertEquals(opens.ResultsOrderedBy, "Date") + self.assertEquals(opens.OrderDirection, "DESC") + self.assertEquals(opens.PageNumber, 1) + self.assertEquals(opens.PageSize, 10) + self.assertEquals(opens.RecordsOnThisPage, 2) + self.assertEquals(opens.TotalNumberOfRecords, 2) + self.assertEquals(opens.NumberOfPages, 1) + + def test_recipients_no_params(self): + self.journey_email.stub_request(self.no_param_uri_for("recipients"), "journey_email_recipients_no_params.json") + recipients = self.journey_email.recipients() + self.assertEqual(len(recipients.Results), 4) + recipient_one = recipients.Results[0] + self.assertEqual(recipient_one.EmailAddress, "asdf@example.com") + self.assertEqual(recipient_one.SentDate, "2019-08-19 10:23:00") + self.assertEqual(recipients.ResultsOrderedBy, "SentDate") + self.assertEqual(recipients.OrderDirection, "ASC") + self.assertEqual(recipients.PageNumber, 1) + self.assertEqual(recipients.PageSize, 1000) + self.assertEqual(recipients.RecordsOnThisPage, 4) + self.assertEqual(recipients.TotalNumberOfRecords, 4) + self.assertEqual(recipients.NumberOfPages, 1) + + def test_recipients_with_params(self): + self.journey_email.stub_request(self.param_uri_for("recipients", "2019-01-01", 1, 10, "desc"), "journey_email_recipients_with_params.json") + recipients = self.journey_email.recipients(date="2019-01-01", page=1, page_size=10, order_direction="desc") + self.assertEqual(len(recipients.Results), 4) + recipient_one = recipients.Results[0] + self.assertEqual(recipient_one.EmailAddress, "asdf@hardbouncemyemail.com") + self.assertEqual(recipient_one.SentDate, "2019-08-21 04:26:00") + self.assertEqual(recipients.ResultsOrderedBy, "SentDate") + self.assertEqual(recipients.OrderDirection, "DESC") + self.assertEqual(recipients.PageNumber, 1) + self.assertEqual(recipients.PageSize, 10) + self.assertEqual(recipients.RecordsOnThisPage, 4) + self.assertEqual(recipients.TotalNumberOfRecords, 4) + self.assertEqual(recipients.NumberOfPages, 1) + + def test_unsubscribes_no_params(self): + self.journey_email.stub_request(self.no_param_uri_for("unsubscribes"), "journey_email_unsubscribes_no_params.json") + unsubscribes = self.journey_email.unsubscribes() + self.assertEqual(len(unsubscribes.Results), 1) + unsubscribe_one = unsubscribes.Results[0] + self.assertEqual(unsubscribe_one.EmailAddress, "asdf@example.com") + self.assertEqual(unsubscribe_one.Date, "2019-08-19 10:24:00") + self.assertEqual(unsubscribe_one.IPAddress, "198.148.196.144") + self.assertEqual(unsubscribes.ResultsOrderedBy, "Date") + self.assertEqual(unsubscribes.OrderDirection, "ASC") + self.assertEqual(unsubscribes.PageNumber, 1) + self.assertEqual(unsubscribes.PageSize, 1000) + self.assertEqual(unsubscribes.RecordsOnThisPage, 1) + self.assertEqual(unsubscribes.TotalNumberOfRecords, 1) + self.assertEqual(unsubscribes.NumberOfPages, 1) + + def test_unsubscribes_with_params(self): + self.journey_email.stub_request(self.param_uri_for("unsubscribes", "2019-01-01", 1, 10, "desc"), "journey_email_unsubscribes_with_params.json") + unsubscribes = self.journey_email.unsubscribes(date="2019-01-01", page=1, page_size=10, order_direction="desc") + self.assertEqual(len(unsubscribes.Results), 1) + unsubscribe_one = unsubscribes.Results[0] + self.assertEqual(unsubscribe_one.EmailAddress, "asdf@example.com") + self.assertEqual(unsubscribe_one.Date, "2019-08-19 10:24:00") + self.assertEqual(unsubscribe_one.IPAddress, "198.148.196.144") + self.assertEqual(unsubscribes.ResultsOrderedBy, "Date") + self.assertEqual(unsubscribes.OrderDirection, "DESC") + self.assertEqual(unsubscribes.PageNumber, 1) + self.assertEqual(unsubscribes.PageSize, 10) + self.assertEqual(unsubscribes.RecordsOnThisPage, 1) + self.assertEqual(unsubscribes.TotalNumberOfRecords, 1) + self.assertEqual(unsubscribes.NumberOfPages, 1) + + def no_param_uri_for(self, action): + return "journeys/email/%s/%s.json" %\ + (self.journey_email_id, action) + + def param_uri_for(self, action, date, page, pagesize, orderdirection): + return "journeys/email/%s/%s.json?date=%s&page=%s&pagesize=%s&orderdirection=%s" %\ + (self.journey_email_id, action, quote(date, ''), page, pagesize, orderdirection) + + +class OAuthCampaignTestCase(unittest.TestCase, JourneyEmailTestCase): + """Test when using OAuth to authenticate""" + + def setUp(self): + self.journey_email_id = "787y87y87y87y87y87y87" + self.journey_email = JourneyEmail( + {"access_token": "ASP95S4aR+9KsgfHB0dapTYxNA==", "refresh_token": "5S4aASP9R+9KsgfHB0dapTYxNA=="}, self.journey_email_id) + + +class ApiKeyCampaignTestCase(unittest.TestCase, JourneyEmailTestCase): + """Test when using an API key to authenticate""" + + def setUp(self): + self.journey_email_id = "787y87y87y87y87y87y87" + self.journey_email = JourneyEmail( + {'api_key': '123123123123123123123'}, self.journey_email_id)