예제 #1
0
def test_fail_send():
    responses.add(responses.POST,
                  'https://api.courier.com/send',
                  status=400,
                  content_type='application/json',
                  body='{"message": "An error occured"}')

    c = Courier(auth_token='123456789ABCDF')

    with pytest.raises(CourierAPIException):
        c.send(event='1234', recipient='4321')
예제 #2
0
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": []}
예제 #3
0
def test_success_get_events():
    responses.add(responses.GET,
                  'https://api.trycourier.app/events',
                  status=200,
                  content_type='application/json',
                  body='{"results": []}')

    c = Courier(auth_token='123456789ABCDF')
    r = c.get_events()

    assert r == {"results": []}
예제 #4
0
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')
예제 #5
0
def test_fail_get_events():
    responses.add(responses.GET,
                  'https://api.trycourier.app/events',
                  status=400,
                  content_type='application/json',
                  body='{"message": "An error occurred"}')

    c = Courier(auth_token='123456789ABCDF')

    with pytest.raises(CourierAPIException):
        c.get_events()
예제 #6
0
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"}
예제 #7
0
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={})
예제 #8
0
def test_success_get_message():
    responses.add(responses.GET,
                  'https://api.trycourier.app/messages/1234',
                  status=200,
                  content_type='application/json',
                  body='{"status": "DELIVERED"}')

    c = Courier(auth_token='123456789ABCDF')
    r = c.get_message(message_id='1234', )

    assert r == {"status": "DELIVERED"}
예제 #9
0
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"}
예제 #10
0
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")
예제 #11
0
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"}
예제 #12
0
def test_success_get_messages():
    responses.add(responses.GET,
                  'https://api.courier.com/messages',
                  status=200,
                  content_type='application/json',
                  body='{"paging": {}, "results": []}')

    c = Courier(auth_token='123456789ABCDF')
    r = c.get_messages()

    assert r == {"paging": {}, "results": []}
예제 #13
0
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": "*****@*****.**"}}
예제 #14
0
def test_success_send_idempotent():
    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', idempotency_key='1234ABCD')

    assert responses.calls[0].request.headers.get(
        'Idempotency-Key') == '1234ABCD'
    assert r == {"status": "ok"}
예제 #15
0
def test_fail_merge_profile():
    responses.add(responses.POST,
                  'https://api.courier.com/profiles/1234',
                  status=400,
                  content_type='application/json',
                  body='{"message": "An error occured"}')

    profile = {"email": "*****@*****.**"}

    c = Courier(auth_token='123456789ABCDF')

    with pytest.raises(CourierAPIException):
        c.merge_profile("1234", profile)
예제 #16
0
def test_success_merge_profile():
    responses.add(responses.POST,
                  'https://api.courier.com/profiles/1234',
                  status=200,
                  content_type='application/json',
                  body='{"status": "SUCCESS"}')

    profile = {"email": "*****@*****.**"}

    c = Courier(auth_token='123456789ABCDF')
    r = c.merge_profile("1234", profile)

    assert r == {"status": "SUCCESS"}
예제 #17
0
def test_success_create_brand_idempotent():
    responses.add(responses.POST,
                  'https://api.courier.com/brands',
                  status=200,
                  content_type='application/json',
                  body='{"id": "1234", "name": "my brand"}')

    c = Courier(auth_token='123456789ABCDF')
    r = c.create_brand(name="my brand",
                       settings={},
                       idempotency_key="1234ABCD")

    assert responses.calls[0].request.headers.get(
        'Idempotency-Key') == '1234ABCD'
    assert r == {"id": "1234", "name": "my brand"}
예제 #18
0
def test_success_merge_profile_idempotent():
    responses.add(responses.POST,
                  'https://api.courier.com/profiles/1234',
                  status=200,
                  content_type='application/json',
                  body='{"status": "SUCCESS"}')

    profile = {"email": "*****@*****.**"}

    c = Courier(auth_token='123456789ABCDF')
    r = c.merge_profile("1234", profile, idempotency_key="1234ABCD")

    assert responses.calls[0].request.headers.get(
        'Idempotency-Key') == '1234ABCD'
    assert r == {"status": "SUCCESS"}
예제 #19
0
def test_fail_send_message():
    responses.add(responses.POST,
                  'https://api.courier.com/send',
                  status=400,
                  content_type='application/json',
                  body='{"message": "An error occured"}')

    c = Courier(auth_token='123456789ABCDF')

    with pytest.raises(CourierAPIException):
        c.send_message(message={
            'template': 'my-template',
            'to': {
                'email': '*****@*****.**'
            }
        })
예제 #20
0
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'
                }
            },
        )
예제 #21
0
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"
        }
    }
예제 #22
0
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"
        }
    }
예제 #23
0
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"
    }
예제 #24
0
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"
        }
    }
예제 #25
0
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': '*****@*****.**'}}]
예제 #26
0
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'
예제 #27
0
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"}
예제 #28
0
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
예제 #29
0
def test_success_replace_brand_with_options():
    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={},
                        snippets={
                            'format': 'handlebars',
                            'name': 'test',
                            'value': '{{test}}'
                        })

    assert r == {"id": "1234", "name": "my brand"}
예제 #30
0
def test_success_send_idempotent():
    responses.add(responses.POST,
                  'https://api.courier.com/send',
                  status=200,
                  content_type='application/json',
                  body='{"status": "ok"}')
    c = Courier(auth_token='123456789ABCDF')
    expiration_date = (datetime.now() + timedelta(days=7)).isoformat()
    r = c.send(event='1234',
               recipient='4321',
               idempotency_key='1234ABCD',
               idempotency_expiration=expiration_date)

    assert responses.calls[0].request.headers.get(
        'Idempotency-Key') == '1234ABCD'
    assert responses.calls[0].request.headers.get(
        'x-idempotency-expiration') == expiration_date
    assert r == {"status": "ok"}