Example #1
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': '*****@*****.**'
            }
        })
Example #2
0
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'
            }
        }
    }
Example #3
0
def test_success_send_message_with_timeout():
    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': '*****@*****.**'
            },
            'timeout': {
                'message': 86400000,
                'channel': {
                    'email': 30000
                },
                'provider': {
                    'sendgrid': 0
                }
            }
        })
    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': '*****@*****.**'
        },
        'timeout': {
            'message': 86400000,
            'channel': {
                'email': 30000
            },
            'provider': {
                'sendgrid': 0
            }
        }
    }
Example #4
0
def test_success_send_message_idempotent():
    responses.add(responses.POST,
                  'https://api.courier.com/send',
                  status=200,
                  content_type='application/json',
                  body='{"status": "accepted"}')
    c = Courier(auth_token='123456789ABCDF')
    expiration_date = (datetime.now() + timedelta(days=7)).isoformat()
    r = c.send_message(message={
        'template': 'my-template',
        'to': {
            'email': '*****@*****.**'
        }
    },
                       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": "accepted"}
Example #5
0
def test_success_send_message_with_granular_metadata():
    responses.add(
        responses.POST,
        'https://api.courier.com/send',
        status=202,
        content_type='application/json',
        body='{"requestId": "1-sdksdiwe-sdm3id9wojdksdlkssdsdfijkkd"}')
    c = Courier(auth_token='123456789ABCDF')
    r = c.send_message(
        message={
            'template': 'my-template',
            'to': {
                'email': '*****@*****.**'
            },
            'metadata': {
                'utm': {
                    'source': 'python'
                }
            },
            'channels': {
                'email': {
                    'metadata': {
                        'utm': {
                            'medium': 'email'
                        }
                    }
                }
            },
            'providers': {
                'sendgrid': {
                    'metadata': {
                        'utm': {
                            'campaign': 'sendgrid'
                        }
                    }
                }
            }
        })
    request_params = json.loads(
        responses.calls[0].request.body.decode('utf-8'))

    assert r == {"requestId": "1-sdksdiwe-sdm3id9wojdksdlkssdsdfijkkd"}
    assert request_params["message"] == {
        'template': 'my-template',
        'to': {
            'email': '*****@*****.**'
        },
        'metadata': {
            'utm': {
                'source': 'python'
            }
        },
        'channels': {
            'email': {
                'metadata': {
                    'utm': {
                        'medium': 'email'
                    }
                }
            }
        },
        'providers': {
            'sendgrid': {
                'metadata': {
                    'utm': {
                        'campaign': 'sendgrid'
                    }
                }
            }
        }
    }