require 'spec_helper'
require 'mailchimpv3'

describe Mailchimpv3 do
  let(:correct_key)   { ENV.fetch("MAILCHIMP_API_KEY") }
  let(:incorrect_key) { "dfc927722778f7870dfc92772778f78" }
  describe "with correct api key" do
    it "se inicializa corrrectamente" do
      expect(Mailchimpv3::API.new correct_key).to be_truthy
    end
    it "obtiene los detalles de la cuenta" do
      api = Mailchimpv3::API.new correct_key
      expect(api.account_details()["account_id"]).to be_truthy
    end
  end
  describe "with incorrect api key" do
    it "no se inicializa corrrectamente" do
      expect {Mailchimpv3::API.new incorrect_key}.to raise_error(StandardError)
    end
  end
  describe Mailchimpv3::Lists do
    it "lists all lists from Mailchimp" do
      api = Mailchimpv3::API.new correct_key
      expect(api.lists().all()["total_items"]).to be_truthy
    end
  end
  describe Mailchimpv3::List do
    it "find the list from api" do
      api = Mailchimpv3::API.new correct_key
      expect(api.list().find("f355f76299").id).to be_truthy
    end
    it "save the list info to the api" do
      api = Mailchimpv3::API.new correct_key
      expect(api.list(
              name: "alive",
              contact:
                { :company => "MailChimp",
                  :address1 => "675 Ponce De Leon Ave NE",
                  :address2 => "Suite 5000",
                  :city=> "Atlanta",
                  :state=>"GA",
                  :zip=> "30308",
                  :country=>"US",
                  :phone => ""
                },
              permission_reminder: "You're receiving this email because you signed up for updates about",
              campaign_defaults:
                { :from_name=>"Freddie",
                  :from_email=>"freddie@freddiehats.com",
                  :subject=>"News",
                  :language=>"es"
                }
            ).save().id).to be_truthy
    end
    it 'add members to itself' do
      api = Mailchimpv3::API.new correct_key
      list = api.list().find("f355f76299")
      expect(list.addmember("popolokm@mail.com","suscribed")["id"]).to be_truthy
    end
  end
end
