RSpec.describe ExpressPigeon::Lists do let(:auth_key) { ENV['EXPRESS_PIGEON_AUTH_KEY'] } let(:name) { 'TEST List Name' } let(:from_name) { 'Mike Hall' } let(:reply_to) { 'mike@just3ws.com' } let(:client) { ExpressPigeon::Lists.new(auth_key) } after do ExpressPigeon::Lists.new(auth_key).index.select do |list| list['name'] =~ /^TEST\s/i end.map do |list| list['id'] end.each do |list_id| ExpressPigeon::Lists.new(auth_key).delete(list_id) end end it '#index' do response = client.index expect(response.code).to eq(200) end it '#create' do response = client.create(name: name, from_name: from_name, reply_to: reply_to) expect(response['list']['id']).to be_a(Fixnum) expect(response['code']).to eq(200) expect(response['message']).to eq("list=#{response['list']['id']} created/updated successfully") expect(response['list']['name']).to eq(name) expect(response['list']['from_name']).to eq(from_name) expect(response['list']['reply_to']).to eq(reply_to) end context 'existing list' do before do @created = client.create(name: name, from_name: from_name, reply_to: reply_to) expect(@created['code']).to eq(200) @list_id = @created['list']['id'] expect(@list_id).to be_a(Fixnum) end describe '#update(list_id, name, from_name, reply_to)' do it 'modifies the list' do name = 'TEST updated' from_name = 'Changed' reply_to = 'different@email.com' response = client.update( @list_id, name: name, from_name: from_name, reply_to: reply_to ) expect(response['code']).to eq(200) expect(response['message']).to eq("list=#{@list_id} created/updated successfully") expect(response['list']['name']).to eq(name) expect(response['list']['from_name']).to eq(from_name) expect(response['list']['reply_to']).to eq(reply_to) end end describe '#upload(list_id, path)' do it 'fails if file does not exist' do path = 'thisisaninvalidfilepath' expect { client.upload(@list_id, path) }.to raise_error("No file found at '#{path}'.") end it 'uploads a csv file' do path = File.join(Dir.pwd, 'spec', 'fixtures', 'contacts.csv') response = client.upload(@list_id, path) expect(response['upload_id']).to be_a(Fixnum) expect(response['code']).to eq(200) expect(response['message']).to eq('file uploaded successfully') end it 'uploads a zipped csv' do path = File.join(Dir.pwd, 'spec', 'fixtures', 'contacts.csv.zip') response = client.upload(@list_id, path) expect(response['upload_id']).to be_a(Fixnum) expect(response['code']).to eq(200) expect(response['message']).to eq('file uploaded successfully') end describe '#upload_status(list_id)' do context 'already uploaded file' do before do path = File.join(Dir.pwd, 'spec', 'fixtures', 'contacts.csv') @uploaded = client.upload(@list_id, path) @upload_id = @uploaded['upload_id'] end it 'has a status' do response = client.upload_status(@upload_id) expect(response['code']).to eq(200) expect(response['report']['list_id']).to eq(@list_id) end end end describe '#download_csv(list_id, path)' do context 'already uploaded file' do before do path = File.join(Dir.pwd, 'spec', 'fixtures', 'contacts.csv') @uploaded = client.upload(@list_id, path) end it 'downloads a csv file' do download_path = File.join(Dir.pwd, 'tmp', 'contacts.csv') File.delete(download_path) if File.exist?(download_path) response = client.download_csv(@list_id, download_path) expect(response.code).to eq(200) expect(File.exist?(download_path)).to eq(true) # TODO: Parse the response. # Right now the response is "valid" but the # Ruby CSV parser is failing to handle the quotes. # csv = CSV.read(download_path, headers: :first_row, quote_char: '"') end end end end end end