def test_crud_campaign(header, campaign_data, campaign_data_ab):
    try:
        campaign_obj = Campaigns(header)
    except ValueError:
        return

    code, res = campaign_obj.create(campaign_data)
    assert code == 200

    code, res = campaign_obj.create(campaign_data_ab)
    assert code == 200
    assert isinstance(res, dict)

    html = '<head></head><body><h1>Title</h1><p>Content</p><p><small>'
    html += '<a href=\"{$unsubscribe}\">Unsubscribe</a></small></p></body>'
    plain = "Your email client does not support HTML emails. "
    plain += "Open newsletter here: {$url}. If you do not want"
    plain += " to receive emails from us, click here: {$unsubscribe}"

    updated = campaign_obj.update(res['id'], html=html, plain=plain)
    assert updated
    assert isinstance(updated, bool)

    code, res = campaign_obj.delete(res['id'])
    assert code == 200
    assert res['success']

    res = campaign_obj.all(status='draft', limit=1000)
    nb_draft = campaign_obj.count('draft')
    assert nb_draft > 0
    assert len(res) > 0
def test_campaign_error(header):
    try:
        campaign = Campaigns(header)
    except ValueError:
        return

    with pytest.raises(ValueError):
        campaign.count(status='inbox')

    with pytest.raises(IOError):
        campaign.all(order='inbox')

    with pytest.raises(ValueError):
        campaign.create(data=[("subject",
                               "Regular campaign subject"), ("type",
                                                             "regular")])

    with pytest.raises(ValueError):
        campaign.create(data={"random_keys": "test"})

    with pytest.raises(ValueError):
        campaign.create(data={"random_keys": "test", "type": "regular"})
def test_cancel_send_campaign(header):
    campaign_obj = Campaigns(header)

    if campaign_obj.count('outbox'):
        res = campaign_obj.all(status='outbox', limit=10)
        if not res:
            pytest.skip("No campaign found with outbox status")

        assert res[0].status == 'outbox'
        try:
            code, res_2 = campaign_obj.cancel(res[0].id)
        except OSError:
            pytest.skip("Campaign Not Found so can not be cancel")
        assert code == 200
        assert res_2["status"] == 'draft'
        assert res[0].id == res_2["id"]