spec/resources/media_spec.rb in flapjack-diner-1.4.0 vs spec/resources/media_spec.rb in flapjack-diner-2.0.0.a4

- old
+ new

@@ -1,276 +1,262 @@ require 'spec_helper' require 'flapjack-diner' -describe Flapjack::Diner::Resources::Media, :pact => true do +describe Flapjack::Diner::Resources, :pact => true do before(:each) do Flapjack::Diner.base_uri('localhost:19081') Flapjack::Diner.logger = nil end context 'create' do it "submits a POST request for a medium" do - data = [{ - :type => 'sms', - :address => '0123456789', - :interval => 300, - :rollup_threshold => 5 - }] + req_data = medium_json(sms_data).merge( + :relationships => { + :contact => { + :data => { + :type => 'contact', + :id => contact_data[:id] + } + } + } + ) + resp_data = medium_json(sms_data).merge(:relationships => medium_rel(sms_data)) - flapjack.given("a contact with id 'abc' exists"). + flapjack.given("a contact exists"). upon_receiving("a POST request with one medium"). - with(:method => :post, :path => '/contacts/abc/media', + with(:method => :post, + :path => '/media', :headers => {'Content-Type' => 'application/vnd.api+json'}, - :body => {:media => data}). + :body => {:data => req_data}). will_respond_with( :status => 201, - :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'}, - :body => ['abc_sms'] ) + :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'}, + :body => { :data => resp_data } + ) - result = Flapjack::Diner.create_contact_media('abc', data) + result = Flapjack::Diner.create_media(sms_data.merge(:contact => contact_data[:id])) expect(result).not_to be_nil - expect(result).to eq(['abc_sms']) + expect(result).to eq(resultify(resp_data)) end it "submits a POST request for several media" do - data = [{ - :type => 'sms', - :address => '0123456789', - :interval => 300, - :rollup_threshold => 5 - }, { - :type => 'email', - :address => 'ablated@example.org', - :interval => 180, - :rollup_threshold => 3 - }] + req_data = [medium_json(email_data).merge( + :relationships => { + :contact => { + :data => { + :type => 'contact', + :id => contact_data[:id] + } + } + } + ), medium_json(sms_data).merge( + :relationships => { + :contact => { + :data => { + :type => 'contact', + :id => contact_data[:id] + } + } + } + )] + resp_data = [ + medium_json(email_data).merge(:relationships => medium_rel(email_data)), + medium_json(sms_data).merge(:relationships => medium_rel(sms_data)) + ] - flapjack.given("a contact with id 'abc' exists"). + flapjack.given("a contact exists"). upon_receiving("a POST request with two media"). - with(:method => :post, :path => '/contacts/abc/media', - :headers => {'Content-Type' => 'application/vnd.api+json'}, - :body => {:media => data}). + with(:method => :post, + :path => '/media', + :headers => {'Content-Type' => 'application/vnd.api+json; ext=bulk'}, + :body => {:data => req_data}). will_respond_with( :status => 201, - :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'}, - :body => ['abc_sms', 'abc_email'] ) + :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'}, + :body => {:data => resp_data}) - result = Flapjack::Diner.create_contact_media('abc', data) + result = Flapjack::Diner.create_media(email_data.merge(:contact => contact_data[:id]), + sms_data.merge(:contact => contact_data[:id])) expect(result).not_to be_nil - expect(result).to eq(['abc_sms', 'abc_email']) + expect(result).to eq(resultify(resp_data)) end - it "can't find the contact to create a medium for" do - data = [{ - :type => 'sms', - :address => '0123456789', - :interval => 300, - :rollup_threshold => 5 - }] - - flapjack.given("no contact exists"). - upon_receiving("a POST request with one medium"). - with(:method => :post, :path => '/contacts/abc/media', - :headers => {'Content-Type' => 'application/vnd.api+json'}, - :body => {:media => data}). - will_respond_with( - :status => 422, - :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'}, - :body => {:errors => ["Contact id: 'abc' could not be loaded"]} ) - - result = Flapjack::Diner.create_contact_media('abc', data) - expect(result).to be_nil - expect(Flapjack::Diner.last_error).to eq(:status_code => 422, - :errors => ["Contact id: 'abc' could not be loaded"]) - end - end context 'read' do - let(:sms_data) { - { - :type => 'sms', - :address => '0123456789', - :interval => 300, - :rollup_threshold => 5 - } - } - - let(:email_data) { - { - :type => 'email', - :address => 'ablated@example.org', - :interval => 180, - :rollup_threshold => 3 - } - } - - let(:links) { {:links => {:contacts => ['abc']}} } - it "submits a GET request for all media" do - media_data = [email_data.merge(links), sms_data.merge(links)] + result_data = [ + medium_json(email_data), + medium_json(sms_data) + ] + resp_data = [ + result_data[0].merge(:relationships => medium_rel(email_data)), + result_data[1].merge(:relationships => medium_rel(sms_data)) + ] - flapjack.given("a contact with id 'abc' has email and sms media"). + flapjack.given("two media exist"). upon_receiving("a GET request for all media"). - with(:method => :get, :path => '/media'). + with(:method => :get, + :path => '/media'). will_respond_with( :status => 200, - :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'}, - :body => {:media => media_data} ) + :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'}, + :body => {:data => resp_data}) result = Flapjack::Diner.media - expect(result).to eq(media_data) + expect(result).to contain_exactly(resultify(resp_data[0]), resultify(resp_data[1])) end it "submits a GET request for one medium" do - media_data = [sms_data.merge(links)] + resp_data = medium_json(sms_data).merge(:relationships => medium_rel(sms_data)) - flapjack.given("a contact with id 'abc' has email and sms media"). - upon_receiving("a GET request for sms media"). - with(:method => :get, :path => '/media/abc_sms'). + flapjack.given("a medium exists"). + upon_receiving("a GET request for one medium"). + with(:method => :get, :path => "/media/#{sms_data[:id]}"). will_respond_with( :status => 200, - :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'}, - :body => {:media => media_data} ) + :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'}, + :body => {:data => resp_data} ) - result = Flapjack::Diner.media('abc_sms') - expect(result).to eq(media_data) + result = Flapjack::Diner.media(sms_data[:id]) + expect(result).to eq(resultify(resp_data)) end it "submits a GET request for several media" do - media_data = [email_data.merge(links), sms_data.merge(links)] + resp_data = [ + medium_json(email_data).merge(:relationships => medium_rel(email_data)), + medium_json(sms_data).merge(:relationships => medium_rel(sms_data)) + ] - flapjack.given("a contact with id 'abc' has email and sms media"). - upon_receiving("a GET request for email and sms media"). - with(:method => :get, :path => '/media/abc_email,abc_sms'). + flapjack.given("two media exist"). + upon_receiving("a GET request for two media"). + with(:method => :get, :path => '/media', + :query => "filter%5B%5D=id%3A#{email_data[:id]}%7C#{sms_data[:id]}"). will_respond_with( :status => 200, - :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'}, - :body => {:media => media_data} ) + :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'}, + :body => {:data => resp_data} ) - result = Flapjack::Diner.media('abc_email', 'abc_sms') - expect(result).to eq(media_data) + result = Flapjack::Diner.media(email_data[:id], sms_data[:id]) + expect(result).to eq(resultify(resp_data)) end - it "can't find the contact with media to read" do - flapjack.given("no contact exists"). - upon_receiving("a GET request for sms media"). - with(:method => :get, :path => '/media/abc_sms'). - will_respond_with( - :status => 404, - :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'}, - :body => {:errors => ["could not find contact 'abc'"]} ) - - result = Flapjack::Diner.media('abc_sms') - expect(result).to be_nil - expect(Flapjack::Diner.last_error).to eq(:status_code => 404, - :errors => ["could not find contact 'abc'"]) - end - end context 'update' do - it "submits a PATCH request for one medium" do - flapjack.given("a contact with id 'abc' has email and sms media"). - upon_receiving("a PATCH request for email media"). + it 'submits a PATCH request for a medium' do + flapjack.given("a medium exists"). + upon_receiving("a PATCH request for a single medium"). with(:method => :patch, - :path => '/media/abc_email', - :headers => {'Content-Type'=>'application/json-patch+json'}, - :body => [{:op => 'replace', :path => '/media/0/interval', :value => 50}, - {:op => 'replace', :path => '/media/0/rollup_threshold', :value => 3}]). + :path => "/media/#{sms_data[:id]}", + :body => {:data => {:id => sms_data[:id], :type => 'medium', :attributes => {:interval => 50}}}, + :headers => {'Content-Type' => 'application/vnd.api+json'}). will_respond_with( :status => 204, :body => '' ) - result = Flapjack::Diner.update_media('abc_email', :interval => 50, :rollup_threshold => 3) - expect(result).not_to be_nil - expect(result).to be_truthy + result = Flapjack::Diner.update_media(:id => sms_data[:id], :interval => 50) + expect(result).to be_a(TrueClass) end - it "submits a PATCH request for several media" do - flapjack.given("a contact with id 'abc' has email and sms media"). - upon_receiving("a PATCH request for email and sms media"). + it 'submits a PATCH request for several media' do + flapjack.given("two media exist"). + upon_receiving("a PATCH request for two media"). with(:method => :patch, - :path => '/media/abc_email,abc_sms', - :headers => {'Content-Type'=>'application/json-patch+json'}, - :body => [{:op => 'replace', :path => '/media/0/interval', :value => 50}, - {:op => 'replace', :path => '/media/0/rollup_threshold', :value => 3}]). + :path => "/media", + :headers => {'Content-Type' => 'application/vnd.api+json; ext=bulk'}, + :body => {:data => [{:id => email_data[:id], :type => 'medium', :attributes => {:interval => 50}}, + {:id => sms_data[:id], :type => 'medium', :attributes => {:rollup_threshold => 5}}]}). will_respond_with( :status => 204, :body => '' ) - result = Flapjack::Diner.update_media('abc_email', 'abc_sms', :interval => 50, :rollup_threshold => 3) - expect(result).not_to be_nil - expect(result).to be_truthy + result = Flapjack::Diner.update_media( + {:id => email_data[:id], :interval => 50}, + {:id => sms_data[:id], :rollup_threshold => 5}) + expect(result).to be_a(TrueClass) end - it "can't find the contact with media to update" do - flapjack.given("no contact exists"). - upon_receiving("a PATCH request for email media"). + it "can't find the medium to update" do + flapjack.given("no data exists"). + upon_receiving("a PATCH request for a single medium"). with(:method => :patch, - :path => '/media/abc_email', - :headers => {'Content-Type'=>'application/json-patch+json'}, - :body => [{:op => 'replace', :path => '/media/0/interval', :value => 50}]). - will_respond_with(:status => 404, - :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'}, - :body => {:errors => ["could not find contact 'abc'"]} ) + :path => "/media/#{email_data[:id]}", + :body => {:data => {:id => email_data[:id], :type => 'medium', :attributes => {:interval => 50}}}, + :headers => {'Content-Type' => 'application/vnd.api+json'}). + will_respond_with( + :status => 404, + :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'}, + :body => {:errors => [{ + :status => '404', + :detail => "could not find Medium record, id: '#{email_data[:id]}'" + }]} + ) - result = Flapjack::Diner.update_media('abc_email', :interval => 50) + result = Flapjack::Diner.update_media(:id => email_data[:id], :interval => 50) expect(result).to be_nil - expect(Flapjack::Diner.last_error).to eq(:status_code => 404, - :errors => ["could not find contact 'abc'"]) + expect(Flapjack::Diner.error).to eq([{:status => '404', + :detail => "could not find Medium record, id: '#{email_data[:id]}'"}]) end end context 'delete' do + it "submits a DELETE request for one medium" do - flapjack.given("a contact with id 'abc' has email and sms media"). + flapjack.given("a medium exists"). upon_receiving("a DELETE request for one medium"). with(:method => :delete, - :path => '/media/abc_email', + :path => "/media/#{sms_data[:id]}", :body => nil). will_respond_with(:status => 204, :body => '') - result = Flapjack::Diner.delete_media('abc_email') - expect(result).not_to be_nil - expect(result).to be_truthy + result = Flapjack::Diner.delete_media(sms_data[:id]) + expect(result).to be_a(TrueClass) end it "submits a DELETE request for several media" do - flapjack.given("a contact with id 'abc' has email and sms media"). + media_data = [{:type => 'medium', :id => sms_data[:id]}, + {:type => 'medium', :id => email_data[:id]}] + + flapjack.given("two media exist"). upon_receiving("a DELETE request for two media"). with(:method => :delete, - :path => '/media/abc_email,abc_sms', - :body => nil). + :headers => {'Content-Type' => 'application/vnd.api+json; ext=bulk'}, + :path => "/media", + :body => {:data => media_data}). will_respond_with(:status => 204, :body => '') - result = Flapjack::Diner.delete_media('abc_email', 'abc_sms') - expect(result).not_to be_nil - expect(result).to be_truthy + result = Flapjack::Diner.delete_media(sms_data[:id], email_data[:id]) + expect(result).to be_a(TrueClass) end it "can't find the contact with media to delete" do - flapjack.given("no contact exists"). + flapjack.given("no data exists"). upon_receiving("a DELETE request for one medium"). with(:method => :delete, - :path => '/media/abc_email', + :path => "/media/#{sms_data[:id]}", :body => nil). - will_respond_with(:status => 404, - :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'}, - :body => {:errors => ["could not find contact 'abc'"]} ) + will_respond_with( + :status => 404, + :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'}, + :body => {:errors => [{ + :status => '404', + :detail => "could not find Medium record, id: '#{sms_data[:id]}'" + }]} + ) - result = Flapjack::Diner.delete_media('abc_email') + result = Flapjack::Diner.delete_media(sms_data[:id]) expect(result).to be_nil - expect(Flapjack::Diner.last_error).to eq(:status_code => 404, - :errors => ["could not find contact 'abc'"]) + expect(Flapjack::Diner.error).to eq([{:status => '404', + :detail => "could not find Medium record, id: '#{sms_data[:id]}'"}]) end end end