require 'rubygems' require 'campaign_monitor' require 'test/unit' CAMPAIGN_MONITOR_API_KEY = 'Your API key' CLIENT_NAME = 'TestClient' LIST_NAME = 'TestList' CAMPAIGN_NAME = 'TestCampaign' class CampaignMonitorTest < Test::Unit::TestCase def setup @cm = CampaignMonitor.new end def test_has_clients clients = @cm.clients assert clients.size > 0 end def test_default_client client = default_client assert_not_nil client assert_equal CLIENT_NAME, client.name end def test_has_lists lists = default_client.lists assert lists.size > 0 end def test_default_list list = default_list assert_not_nil list assert_equal LIST_NAME, list.name end def test_add_subscriber_fail result = @cm.add_subscriber(1, 2, 3) assert_equal result.code, 101 assert_not_nil result.message end def test_list_add_subscriber list = default_list assert_success list.add_and_resubscribe('first@example.com', 'Test Subscriber') assert_success list.remove_subscriber('first@example.com') end def test_list_add_subscriber_fail list = default_list list.remove_subscriber('first@example.com') assert_failure list.add_subscriber('first@example.com', 'Test Subscriber') end def test_add_and_resubscribe_with_custom_fields list = default_list assert_success list.add_and_resubscribe('third@example.com', 'Test Subscriber', 'TestKey' => 'TestValue') assert_success list.remove_subscriber('third@example.com') end def test_add_with_custom_fields list = default_list assert_success list.add_subscriber('fourth@example.com', 'Test Subscriber', 'TestKey' => 'TestValue') list.remove_subscriber('fourth@example.com') end def test_campaigns client = default_client assert_equal client.campaigns, [] end protected def assert_success(result) assert result.succeeded?, result.message end def assert_failure(result) assert result.failed? end def default_client(clients = @cm.clients) clients.detect { |c| c.name == CLIENT_NAME } end def default_list(lists = default_client.lists) lists.detect { |l| l.name == LIST_NAME } end def default_campaign(campaigns = default_client.campaigns) campaigns.detect { |c| c.name == CAMPAIGN_NAME } end end