|
1 | 1 | from userlist_python.userlist_auth import UserlistApiAuth |
| 2 | +from userlist_python.userlist_client import UserlistApiClient |
2 | 3 |
|
3 | 4 | import os |
4 | 5 | import unittest |
5 | | -from userlist_python.userlist_client import UserlistApiClient |
| 6 | +import responses |
6 | 7 |
|
7 | 8 | class UserlistApiTest(unittest.TestCase): |
8 | 9 | def setUp(self): |
9 | 10 | self.key = "push_key" |
10 | | - self.api = UserlistApiClient(self.key) |
11 | | - |
12 | | - # def test_headers_auth(self): |
13 | | - # userlist = UserlistApiClient(self.key) |
14 | | - # try: |
15 | | - # request = userlist.push_companies('123456') |
16 | | - # except: |
17 | | - # pass |
18 | | - # assert userlist.request_method['Content-Type'] == 'application/json; charset=utf-8' |
19 | | - # assert userlist.request_method['Accept'] == 'application/json' |
20 | | - # assert userlist.request_method['Authorization'] == 'Push ' + self.key |
| 11 | + self.client = UserlistApiClient(self.key) |
| 12 | + |
| 13 | + responses.add(responses.POST, 'https://push.userlist.com/users', status=201) |
| 14 | + responses.add(responses.POST, 'https://push.userlist.com/companies', status=201) |
| 15 | + |
| 16 | + |
| 17 | + def test_users_missing_identifier_and_email(self): |
| 18 | + with self.assertRaises(ValueError) as context: |
| 19 | + self.client.push_users(properties={ 'name': 'Testing' }) |
| 20 | + |
| 21 | + self.assertEqual(str(context.exception), 'Missing required parameter identifier or email') |
| 22 | + |
| 23 | + @responses.activate |
| 24 | + def test_user_missing_identifier_with_email(self): |
| 25 | + try: |
| 26 | + self.client.push_users(email='test@example.com') |
| 27 | + except ValueError: |
| 28 | + self.self.fail('Unexpected ValueError') |
| 29 | + |
| 30 | + @responses.activate |
| 31 | + def test_user_missing_email_with_identifier(self): |
| 32 | + try: |
| 33 | + self.client.push_users(identifier='user-identifier') |
| 34 | + except ValueError: |
| 35 | + self.self.fail('Unexpected ValueError') |
| 36 | + |
| 37 | + @responses.activate |
| 38 | + def test_user_valid_request(self): |
| 39 | + self.client.push_users(email='test@example.com', identifier='user-identifier', properties={ 'name': 'Testing' }) |
| 40 | + |
| 41 | + self.assertEqual(len(responses.calls), 1) |
| 42 | + self.assertEqual(responses.calls[0].request.url, 'https://push.userlist.com/users') |
| 43 | + self.assertEqual(responses.calls[0].request.method, 'POST') |
| 44 | + self.assertEqual(responses.calls[0].request.headers['Authorization'], f'Push {self.key}') |
| 45 | + self.assertEqual(responses.calls[0].request.body, b'{"identifier": "user-identifier", "email": "test@example.com", "properties": {"name": "Testing"}}') |
| 46 | + |
| 47 | + |
| 48 | + def test_companies_missing_identifier(self): |
| 49 | + with self.assertRaises(TypeError) as context: |
| 50 | + self.client.push_companies(properties={ 'name': 'Testing' }) |
| 51 | + |
| 52 | + self.assertEqual(str(context.exception), "push_companies() missing 1 required positional argument: 'identifier'") |
| 53 | + |
| 54 | + @responses.activate |
| 55 | + def test_company_valid_request(self): |
| 56 | + self.client.push_companies(identifier='company-identifier', properties={ 'name': 'Testing' }) |
| 57 | + |
| 58 | + self.assertEqual(len(responses.calls), 1) |
| 59 | + self.assertEqual(responses.calls[0].request.url, 'https://push.userlist.com/companies') |
| 60 | + self.assertEqual(responses.calls[0].request.method, 'POST') |
| 61 | + self.assertEqual(responses.calls[0].request.headers['Authorization'], f'Push {self.key}') |
| 62 | + self.assertEqual(responses.calls[0].request.body, b'{"identifier": "company-identifier", "properties": {"name": "Testing"}}') |
0 commit comments