forked from mailjet/mailjet-apiv3-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
97 lines (80 loc) · 3.68 KB
/
test.py
File metadata and controls
97 lines (80 loc) · 3.68 KB
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
import unittest
from mailjet_rest import Client
import os
import random
import string
class TestSuite(unittest.TestCase):
def setUp(self):
self.auth = (
os.environ['MJ_APIKEY_PUBLIC'],
os.environ['MJ_APIKEY_PRIVATE']
)
self.client = Client(auth=self.auth)
def test_get_no_param(self):
result = self.client.contact.get().json()
self.assertTrue(('Data' in result and 'Count' in result))
def test_get_valid_params(self):
result = self.client.contact.get(filters={'limit': 2}).json()
self.assertTrue(result['Count'] >= 0 or result['Count'] <= 2)
def test_get_invalid_parameters(self):
# invalid parameters are ignored
result = self.client.contact.get(filters={'invalid': 'false'}).json()
self.assertTrue('Count' in result)
def test_get_with_data(self):
# it shouldn't use data
result = self.client.contact.get(data={'Email': 'api@mailjet.com'})
self.assertTrue(result.status_code == 200)
def test_get_with_action(self):
get_contact = self.client.contact.get(filters={'limit': 1}).json()
if get_contact['Count'] != 0:
contact_id = get_contact['Data'][0]['ID']
else:
contact_random_email = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(10)) + '@mailjet.com'
post_contact = self.client.contact.create(data={'Email': contact_random_email})
self.assertTrue(post_contact.status_code == 201)
contact_id = post_contact.json()['Data'][0]['ID']
get_contact_list = self.client.contactslist.get(filters={'limit': 1}).json()
if get_contact_list['Count'] != 0:
list_id = get_contact_list['Data'][0]['ID']
else:
contact_list_random_name = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(10)) + '@mailjet.com'
post_contact_list = self.client.contactslist.create(data={'Name': contact_list_random_name})
self.assertTrue(post_contact_list.status_code == 201)
list_id = post_contact_list.json()['Data'][0]['ID']
data = {
'ContactsLists': [
{
"ListID": list_id,
"Action": "addnoforce"
}
]
}
result_add_list = self.client.contact_managecontactslists.create(id=contact_id, data=data)
self.assertTrue(result_add_list.status_code == 201)
result = self.client.contact_getcontactslists.get(contact_id).json()
self.assertTrue('Count' in result)
def test_get_with_id_filter(self):
result_contact = self.client.contact.get(filters={'limit': 1}).json()
result_contact_with_id = self.client.contact.get(filter={'Email': result_contact['Data'][0]['Email']}).json()
self.assertTrue(result_contact_with_id['Data'][0]['Email'] == result_contact['Data'][0]['Email'])
def test_post_with_no_param(self):
result = self.client.sender.create(data={}).json()
self.assertTrue('StatusCode' in result and result['StatusCode'] is not 400)
def test_client_custom_version(self):
self.client = Client(
auth=self.auth,
version='v3.1'
)
self.assertEqual(self.client.config.version, 'v3.1')
self.assertEqual(
self.client.config['send'][0],
'https://api.eu.mailjet.com/v3.1/send'
)
def test_user_agent(self):
self.client = Client(
auth=self.auth,
version='v3.1'
)
self.assertEqual(self.client.config.user_agent, 'mailjet-apiv3-python/v1.3.3')
if __name__ == '__main__':
unittest.main()