forked from campaignmonitor/createsend-python
/
test_createsend.py
136 lines (112 loc) · 6.32 KB
/
test_createsend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import unittest
import urllib
from urlparse import urlparse
from createsend import *
class CreateSendTestCase(unittest.TestCase):
def setUp(self):
self.api_key = '123123123123123123123'
self.base_uri = 'http://api.createsend.com/api/v3'
CreateSend.api_key = self.api_key
self.cs = CreateSend()
# Mapping of http status codes to the exceptions expected to be raised
self.error_responses = {
400: BadRequest,
401: Unauthorized,
404: NotFound,
500: ServerError }
def test_apikey(self):
site_url = "http://iamadesigner.createsend.com"
username = "myusername"
password = "mypassword"
self.cs.stub_request("apikey.json?SiteUrl=%s" % urllib.quote(site_url, ''), "apikey.json")
apikey = self.cs.apikey(site_url, username, password)
self.assertEquals(apikey, "981298u298ue98u219e8u2e98u2")
def test_clients(self):
self.cs.stub_request("clients.json", "clients.json")
cl = self.cs.clients()
self.assertEquals(2, len(cl))
self.assertEquals("4a397ccaaa55eb4e6aa1221e1e2d7122", cl[0].ClientID)
self.assertEquals("Client One", cl[0].Name)
def test_countries(self):
self.cs.stub_request("countries.json", "countries.json")
countries = self.cs.countries()
self.assertEquals(245, len(countries))
self.assertEquals("Australia", countries[11])
def test_systemdate(self):
self.cs.stub_request("systemdate.json", "systemdate.json")
systemdate = self.cs.systemdate()
self.assertEquals(systemdate, "2010-10-15 09:27:00")
def test_timezones(self):
self.cs.stub_request("timezones.json", "timezones.json")
timezones = self.cs.timezones()
self.assertEquals(97, len(timezones))
self.assertEquals("(GMT+12:00) Fiji", timezones[61])
# Test that the corresponding exceptions are raised according to the returned http status code
def test_bad_request_on_get(self):
self.cs.stub_request('countries.json', 'custom_api_error.json', status=400)
self.assertRaises(self.error_responses[400], self.cs.countries)
def test_unauthorized_on_get(self):
self.cs.stub_request('countries.json', 'custom_api_error.json', status=401)
self.assertRaises(self.error_responses[401], self.cs.countries)
def test_not_found_on_get(self):
self.cs.stub_request('countries.json', None, status=404)
self.assertRaises(self.error_responses[404], self.cs.countries)
def test_server_error_on_get(self):
self.cs.stub_request('countries.json', None, status=500)
self.assertRaises(self.error_responses[500], self.cs.countries)
def test_bad_request_on_post(self):
client = Client("uhiuhiuhiuhiuhiuhiuh")
client.stub_request('clients.json', 'custom_api_error.json', status=400)
self.assertRaises(self.error_responses[400], client.create, "Client Company Name", "Client Contact Name", "client@example.com",
"(GMT+10:00) Canberra, Melbourne, Sydney", "Australia")
def test_unauthorized_on_post(self):
client = Client("uhiuhiuhiuhiuhiuhiuh")
client.stub_request('clients.json', 'custom_api_error.json', status=401)
self.assertRaises(self.error_responses[401], client.create, "Client Company Name", "Client Contact Name", "client@example.com",
"(GMT+10:00) Canberra, Melbourne, Sydney", "Australia")
def test_not_found_on_post(self):
client = Client("uhiuhiuhiuhiuhiuhiuh")
client.stub_request('clients.json', None, status=404)
self.assertRaises(self.error_responses[404], client.create, "Client Company Name", "Client Contact Name", "client@example.com",
"(GMT+10:00) Canberra, Melbourne, Sydney", "Australia")
def test_server_error_on_post(self):
client = Client("uhiuhiuhiuhiuhiuhiuh")
client.stub_request('clients.json', None, status=500)
self.assertRaises(self.error_responses[500], client.create, "Client Company Name", "Client Contact Name", "client@example.com",
"(GMT+10:00) Canberra, Melbourne, Sydney", "Australia")
def test_bad_request_on_put(self):
template = Template("uhiuhiuhiuhiuhiuhiuh")
template.stub_request('templates/uhiuhiuhiuhiuhiuhiuh.json', 'custom_api_error.json', status=400)
self.assertRaises(self.error_responses[400], template.update, "Template One Updated", "http://templates.org/index.html",
"http://templates.org/files.zip", "http://templates.org/screenshot.jpg")
def test_unauthorized_on_put(self):
template = Template("uhiuhiuhiuhiuhiuhiuh")
template.stub_request('templates/uhiuhiuhiuhiuhiuhiuh.json', 'custom_api_error.json', status=401)
self.assertRaises(self.error_responses[401], template.update, "Template One Updated", "http://templates.org/index.html",
"http://templates.org/files.zip", "http://templates.org/screenshot.jpg")
def test_not_found_on_put(self):
template = Template("uhiuhiuhiuhiuhiuhiuh")
template.stub_request('templates/uhiuhiuhiuhiuhiuhiuh.json', None, status=404)
self.assertRaises(self.error_responses[404], template.update, "Template One Updated", "http://templates.org/index.html",
"http://templates.org/files.zip", "http://templates.org/screenshot.jpg")
def test_server_error_on_put(self):
template = Template("uhiuhiuhiuhiuhiuhiuh")
template.stub_request('templates/uhiuhiuhiuhiuhiuhiuh.json', None, status=500)
self.assertRaises(self.error_responses[500], template.update, "Template One Updated", "http://templates.org/index.html",
"http://templates.org/files.zip", "http://templates.org/screenshot.jpg")
def test_bad_request_on_delete(self):
template = Template("uhiuhiuhiuhiuhiuhiuh")
template.stub_request('templates/uhiuhiuhiuhiuhiuhiuh.json', 'custom_api_error.json', status=400)
self.assertRaises(self.error_responses[400], template.delete)
def test_unauthorized_on_delete(self):
template = Template("uhiuhiuhiuhiuhiuhiuh")
template.stub_request('templates/uhiuhiuhiuhiuhiuhiuh.json', 'custom_api_error.json', status=401)
self.assertRaises(self.error_responses[401], template.delete)
def test_not_found_on_delete(self):
template = Template("uhiuhiuhiuhiuhiuhiuh")
template.stub_request('templates/uhiuhiuhiuhiuhiuhiuh.json', None, status=404)
self.assertRaises(self.error_responses[404], template.delete)
def test_server_error_on_delete(self):
template = Template("uhiuhiuhiuhiuhiuhiuh")
template.stub_request('templates/uhiuhiuhiuhiuhiuhiuh.json', None, status=500)
self.assertRaises(self.error_responses[500], template.delete)