def test_success_ingest_users(): responses.add(responses.POST, 'https://api.courier.com/bulk/12345', status=200, content_type='application/json', body='{"errors": [], "total": 1}') c = Courier(auth_token='123456789ABCDF') expiration_date = (datetime.now() + timedelta(days=7)).isoformat() r = c.bulk.ingest_users(job_id='12345', users=[{ 'profile': { 'email': '*****@*****.**' } }], idempotency_key='1234ABCD', idempotency_expiration=expiration_date) request_params = json.loads( responses.calls[0].request.body.decode('utf-8')) assert r == {"errors": [], "total": 1} assert responses.calls[0].request.headers.get( 'Idempotency-Key') == '1234ABCD' assert responses.calls[0].request.headers.get( 'x-idempotency-expiration') == expiration_date assert request_params["users"] == [{'profile': {'email': '*****@*****.**'}}]
def test_init_basic_auth_env(): environ['COURIER_AUTH_TOKEN'] = "abcd" environ.pop('COURIER_AUTH_TOKEN') environ['COURIER_AUTH_USERNAME'] = '******' environ['COURIER_AUTH_PASSWORD'] = '******' c = Courier() assert 'Authorization' in c.session.headers
def test_success_invoke(): responses.add( responses.POST, 'https://api.courier.com/automations/invoke', status=200, content_type='application/json', body='{"runId": "12345"}' ) c = Courier(auth_token='123456789ABCDF') r = c.automations.invoke( automation={'steps': [{ 'action': 'send' }]}, brand='W50NC77P524K14M5300PGPEK4JMJ', data={'foo': 'bar'}, profile={'email': '*****@*****.**'}, recipient='4321', template='template-001' ) request_params = json.loads( responses.calls[0].request.body.decode('utf-8')) assert r == {"runId": "12345"} assert request_params["automation"] == {'steps': [{ 'action': 'send' }]} assert request_params["brand"] == 'W50NC77P524K14M5300PGPEK4JMJ' assert request_params["data"] == {'foo': 'bar'} assert request_params["profile"] == {'email': '*****@*****.**'} assert request_params["recipient"] == '4321' assert request_params["template"] == 'template-001'
def test_success_get_audience(): responses.add( responses.GET, 'https://api.courier.com/audiences/software-engineers-from-sf', status=200, content_type='application/json', body= '{"created_at":"2022-03-24T18:56:49.181Z","description":"Updated descriptionss","id":"software-engineers-from-sf","name":"Software Engineers From SF","filter":{"path":"title","value":"Software Engineer","operator":"EQ"},"updated_at":"2022-03-24T18:56:49.181Z"}' ) c = Courier(auth_token='123456789ABCDF') r = c.audiences.get_audience('software-engineers-from-sf') assert r == { "created_at": "2022-03-24T18:56:49.181Z", "description": "Updated descriptionss", "id": "software-engineers-from-sf", "name": "Software Engineers From SF", "filter": { "path": "title", "value": "Software Engineer", "operator": "EQ" }, "updated_at": "2022-03-24T18:56:49.181Z" }
def test_success_get_job_users(): responses.add( responses.GET, 'https://api.courier.com/bulk/job.id/users?cursor=abc', status=200, content_type='application/json', body= '{"paging": {"cursor": "def" }, "items": [{ "profile": {"email": "*****@*****.**"}, "recipient": "NEfgqX8CLFZ8uTBWq9zdH", "messageId": "1-61eb0bd1-9ecd21e534ba85c099211b63", "status": "ENQUEUED" }]}' ) c = Courier(auth_token='123456789ABCDF') r = c.bulk.get_job_users("job.id", cursor='abc') assert r == { "items": [{ "profile": { "email": "*****@*****.**" }, "recipient": "NEfgqX8CLFZ8uTBWq9zdH", "messageId": "1-61eb0bd1-9ecd21e534ba85c099211b63", "status": "ENQUEUED" }], "paging": { "cursor": "def" } }
def test_success_get_audiences(): responses.add( responses.GET, 'https://api.courier.com/audiences?cursor=abc', status=200, content_type='application/json', body= '{"paging": {"cursor": "def" }, "items": [{"created_at":"2022-03-24T18:56:49.181Z","description":"","id":"software-engineers-from-sf","name":"","filter":{"path":"title","value":"Software Engineer","operator":"EQ"},"updated_at":"2022-03-24T19:54:37.982Z"}]}' ) c = Courier(auth_token='123456789ABCDF') r = c.audiences.get_audiences(cursor='abc') assert r == { "items": [{ "created_at": "2022-03-24T18:56:49.181Z", "description": "", "id": "software-engineers-from-sf", "name": "", "filter": { "path": "title", "value": "Software Engineer", "operator": "EQ" }, "updated_at": "2022-03-24T19:54:37.982Z" }], "paging": { "cursor": "def" } }
def test_fail_put_audience(): responses.add( responses.PUT, 'https://api.courier.com/audiences/software-engineers-from-sf', status=400, content_type='application/json', body= '{ "message": "filteroperator should be equal to one of the allowed values AND, EQ, GT, GTE, INCLUDES, LT, LTE, NEQ, OMIT, OR", "type": "invalid_request_error"}' ) c = Courier(auth_token='123456789ABCDF') with pytest.raises(CourierAPIException): c.audiences.put_audience( 'software-engineers-from-sf', audience={ 'name': '', 'description': '', 'filter': { 'path': 'title', 'value': 'Software Engineer', 'operator': 'INVALID_FILTER_OPERATOR' } }, )
def test_success_get_audience_members(): responses.add( responses.GET, 'https://api.courier.com/audiences/software-engineers-from-sf/members?cursor=abc', status=200, content_type='application/json', body= '{"paging": {"cursor": "def" }, "items": [{"added_at":"2022-03-24T05:31:24.291Z","audience_id":"software-engineers-from-sf","audience_version":8,"member_id":"user_id_1","reason":"EQ(\'title\', \'Software Engineer\') => true"}]}' ) c = Courier(auth_token='123456789ABCDF') r = c.audiences.get_audience_members("software-engineers-from-sf", cursor='abc') assert r == { "items": [{ "added_at": "2022-03-24T05:31:24.291Z", "audience_id": "software-engineers-from-sf", "audience_version": 8, "member_id": "user_id_1", "reason": "EQ('title', 'Software Engineer') => true" }], "paging": { "cursor": "def" } }
def test_success_send_message_with_metadata(): responses.add(responses.POST, 'https://api.courier.com/send', status=202, content_type='application/json', body='{"status": "accepted"}') c = Courier(auth_token='123456789ABCDF') r = c.send_message( message={ 'template': 'my-template', 'to': { 'email': '*****@*****.**' }, 'metadata': { 'utm': { 'source': 'python' } } }) request_params = json.loads( responses.calls[0].request.body.decode('utf-8')) assert r == {"status": "accepted"} assert request_params["message"] == { 'template': 'my-template', 'to': { 'email': '*****@*****.**' }, 'metadata': { 'utm': { 'source': 'python' } } }
def test_success_profiles_patch(): responses.add( responses.PATCH, 'https://api.courier.com/profiles/profile.id', status=200, content_type='application/json', body='{"status": "SUCCESS"}' ) operations=[ { "op":"add", "path": "/number", "value": 4 }, { "op":"replace", "path": "/number", "value": 5 }, { "op":"copy", "from":"/number", "path":"/test_num" } ] c = Courier(auth_token='123456789ABCDF') r = c.profiles.patch("profile.id", operations) assert r == {"status": "SUCCESS"}
def test_success_delete_brand(): responses.add(responses.DELETE, 'https://api.courier.com/brands/1234', status=204) c = Courier(auth_token='123456789ABCDF') c.delete_brand('1234') assert len(responses.calls) == 1
def test_success_lists_put_subscriptions(): responses.add(responses.PUT, 'https://api.courier.com/lists/my.list.id/subscriptions', status=204, content_type='application/json') c = Courier(auth_token='123456789ABCDF') c.lists.put_subscriptions("my.list.id", ["1234", "4321"]) assert len(responses.calls) == 1
def test_success_lists_restore(): responses.add(responses.PUT, 'https://api.courier.com/lists/my.list.id/restore', status=204, content_type='application/json') c = Courier(auth_token='123456789ABCDF') c.lists.restore("my.list.id") assert len(responses.calls) == 1
def test_success_put_locale(): responses.add( responses.PUT, 'https://api.courier.com/notifications/my-notification/locales/my-locale', status=200, content_type='application/json') c = Courier(auth_token='123456789ABCDF') r = c.notifications.put_locale('my-notification', 'my-locale', [], []) assert len(responses.calls) == 1
def test_success_send(): responses.add(responses.POST, 'https://api.courier.com/send', status=200, content_type='application/json', body='{"status": "ok"}') c = Courier(auth_token='123456789ABCDF') r = c.send(event='1234', recipient='4321') assert r == {"status": "ok"}
def test_success_post_block_locales(): responses.add( responses.POST, 'https://api.courier.com/notifications/my-notification/blocks/my-block/locales', status=200, content_type='application/json') c = Courier(auth_token='123456789ABCDF') r = c.notifications.post_block_locales('my-notification', 'my-block', []) assert len(responses.calls) == 1
def test_success_get_profile(): responses.add(responses.GET, 'https://api.courier.com/profiles/1234', status=200, content_type='application/json', body='{"profile": { "email": "*****@*****.**"}}') c = Courier(auth_token='123456789ABCDF') r = c.get_profile("1234") assert r == {"profile": {"email": "*****@*****.**"}}
def test_success_messages_get(): responses.add(responses.GET, 'https://api.courier.com/messages/my.message.id', status=200, content_type='application/json', body='{"id": "my.message.id", "name": "My Message"}') c = Courier(auth_token='123456789ABCDF') r = c.messages.get("my.message.id") assert r == {"id": "my.message.id", "name": "My Message"}
def test_fail_delete_brand(): responses.add(responses.DELETE, 'https://api.courier.com/brands/1234', status=400, content_type='application/json', body='{"message": "An error occurred"}') c = Courier(auth_token='123456789ABCDF') with pytest.raises(CourierAPIException): c.delete_brand('1234')
def test_fail_replace_brand(): responses.add(responses.PUT, 'https://api.courier.com/brands/1234', status=400, content_type='application/json', body='{"message": "An error occured"}') c = Courier(auth_token='123456789ABCDF') with pytest.raises(CourierAPIException): c.replace_brand("1234", name="my brand", settings={})
def test_success_replace_brand(): responses.add(responses.PUT, 'https://api.courier.com/brands/1234', status=200, content_type='application/json', body='{"id": "1234", "name": "my brand"}') c = Courier(auth_token='123456789ABCDF') r = c.replace_brand("1234", name="my brand", settings={}) assert r == {"id": "1234", "name": "my brand"}
def test_success_messages_list(): responses.add(responses.GET, 'https://api.courier.com/messages', status=200, content_type='application/json', body='{"paging": {}, "items": []}') c = Courier(auth_token='123456789ABCDF') r = c.messages.list() assert r == {'paging': {}, 'items': []}
def test_success_get_brand(): responses.add(responses.GET, 'https://api.courier.com/brands/1234', status=200, content_type='application/json', body='{"status": "DELIVERED"}') c = Courier(auth_token='123456789ABCDF') r = c.get_brand(brand_id='1234') assert r == {"status": "DELIVERED"}
def test_success_get_brands_with_params(): responses.add(responses.GET, 'https://api.courier.com/brands?cursor=ABCD1234', status=200, content_type='application/json', body='{"paging": {}, "results": []}') c = Courier(auth_token='123456789ABCDF') r = c.get_brands(cursor="ABCD1234") assert r == {"paging": {}, "results": []}
def test_fail_messages_list(): responses.add(responses.GET, 'https://api.courier.com/messages', status=400, content_type='application/json', body='{"message": "An error occured"}') c = Courier(auth_token='123456789ABCDF') with pytest.raises(CourierAPIException): c.messages.list()
def test_success_get_events(): responses.add(responses.GET, 'https://api.courier.com/events', status=200, content_type='application/json', body='{"results": []}') c = Courier(auth_token='123456789ABCDF') r = c.get_events() assert r == {"results": []}
def test_fail_messages_get(): responses.add(responses.GET, 'https://api.courier.com/messages/my.message.id', status=400, content_type='application/json', body='{"message": "Not Found"}') c = Courier(auth_token='123456789ABCDF') with pytest.raises(CourierAPIException): c.messages.get("my.message.id")
def test_success_replace_event(): responses.add(responses.PUT, 'https://api.courier.com/events/1234', status=200, content_type='application/json', body='{"id": "notification-id-1", "type": "notification"}') c = Courier(auth_token='123456789ABCDF') r = c.replace_event("1234", notification_id="notification-id-1") assert r == {"id": "notification-id-1", "type": "notification"}
def test_success_messages_get_history(): responses.add(responses.GET, 'https://api.courier.com/messages/my.message.id/history', status=200, content_type='application/json', body='{"results": []}') c = Courier(auth_token='123456789ABCDF') r = c.messages.get_history(message_id="my.message.id") assert r == {'results': []}
def test_fail_replace_event(): responses.add(responses.PUT, 'https://api.courier.com/events/1234', status=400, content_type='application/json', body='{"message": "An error occured"}') c = Courier(auth_token='123456789ABCDF') with pytest.raises(CourierAPIException): c.replace_event("1234", notification_id="notification-id-1")