<commit_before>require 'spec_helper'
require 'flapjack-diner'

describe Flapjack::Diner::Resources::Contacts, :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 contact" do
      flapjack.given("no data exists").
        upon_receiving("a POST request with one contact").
        with(:method => :post, :path => '/contacts',
             :headers => {'Content-Type' => 'application/vnd.api+json'},
             :body => {'data' => contact_data.merge('type' => 'contact')}).
        will_respond_with(
          :status => 201,
          :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
          :body => {'data' => contact_data.merge('type' => 'contact')})

      result = Flapjack::Diner.create_contacts(contact_data)
      expect(result).not_to be_nil
      expect(result).to eq(contact_data.merge(:type => 'contact'))
    end

    it "submits a POST request for several contacts" do
      contacts_data = [contact_data.merge(:type => 'contact'),
                       contact_2_data.merge(:type => 'contact')]

      flapjack.given("no data exists").
        upon_receiving("a POST request with two contacts").
        with(:method => :post, :path => '/contacts',
             :headers => {'Content-Type' => 'application/vnd.api+json; ext=bulk'},
             :body => {:data => contacts_data}).
        will_respond_with(
          :status => 201,
          :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
          :body => {:data => contacts_data})

      result = Flapjack::Diner.create_contacts(*contacts_data)
      expect(result).not_to be_nil
      expect(result).to eq(contacts_data)
    end

    it "submits a POST request but a contact with that id exists" do
      flapjack.given("a contact exists").
        upon_receiving("a POST request with one contact").
        with(:method => :post, :path => '/contacts',
             :headers => {'Content-Type' => 'application/vnd.api+json'},
             :body => {:data => contact_data.merge(:type => 'contact')}).
        will_respond_with(
          :status => 409,
          :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
            :body => {:errors => [{
              :status => '409',
              :detail => "Contacts already exist with the following ids: #{contact_data[:id]}"
            }]}
          )

      result = Flapjack::Diner.create_contacts(contact_data)
      expect(result).to be_nil
      expect(Flapjack::Diner.last_error).to eq([{:status => '409',
        :detail => "Contacts already exist with the following ids: #{contact_data[:id]}"}])
    end

  end

  context 'read' do

    context 'GET all contacts' do

      it "has some data" do
        flapjack.given("a contact exists").
          upon_receiving("a GET request for all contacts").
          with(:method => :get, :path => '/contacts').
          will_respond_with(
            :status => 200,
            :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
            :body => {:data => [contact_data.merge('type' => 'contact')]})

        result = Flapjack::Diner.contacts
        expect(result).not_to be_nil
        expect(result).to eq([contact_data.merge(:type => 'contact')])
      end

      it "has no data" do
        flapjack.given("no data exists").
          upon_receiving("a GET request for all contacts").
          with(:method => :get, :path => '/contacts').
          will_respond_with(
            :status => 200,
            :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
            :body => {:data => []})

        result = Flapjack::Diner.contacts
        expect(result).not_to be_nil
        expect(result).to be_an_instance_of(Array)
        expect(result).to be_empty
      end

    end

    context 'GET a single contact' do

      it "finds the contact" do
        flapjack.given("a contact exists").
          upon_receiving("a GET request for a single contact").
          with(:method => :get, :path => "/contacts/#{contact_data[:id]}").
          will_respond_with(
            :status => 200,
            :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
            :body => {:data => contact_data.merge('type' => 'contact')})

        result = Flapjack::Diner.contacts(contact_data[:id])
        expect(result).not_to be_nil
        expect(result).to eq(contact_data.merge(:type => 'contact'))
      end

      it "can't find the contact" do
        flapjack.given("no data exists").
          upon_receiving("a GET request for a single contact").
          with(:method => :get, :path => "/contacts/#{contact_data[:id]}").
          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 Contact record, id: '#{contact_data[:id]}'"
              }]}
            )

        result = Flapjack::Diner.contacts(contact_data[:id])
        expect(result).to be_nil
        expect(Flapjack::Diner.last_error).to eq([{:status => '404',
          :detail => "could not find Contact record, id: '#{contact_data[:id]}'"}])
      end

    end

    context 'GET a single contact by name, including media' do

      it 'returns a contact with media' do
        data = contact_data.merge(:type => 'contact', :links => {
          :self  => "http://example.org/contacts/#{contact_data[:id]}",
          :media => {
            :self => "http://example.org/contacts/#{contact_data[:id]}/links/media",
            :related => "http://example.org/contacts/#{contact_data[:id]}/media",
            :linkage => [
              {:type => 'medium', :id => email_data[:id]}
            ]
          },
          :rules => "http://example.org/contacts/#{contact_data[:id]}/rules",
        })

        context = {:included => [
          email_data.merge(:type => 'medium', :links => {
            :self => "http://example.org/media/#{email_data[:id]}",
            :contact => "http://example.org/media/#{email_data[:id]}/contact",
            :rules => "http://example.org/media/#{email_data[:id]}/rules"
          })],
          :links => {
            :self  => "http://example.org/contacts/#{contact_data[:id]}?include=media"
          }
        }

        flapjack.given("a contact with one medium exists").
          upon_receiving("a GET request for a single contact with media").
          with(:method => :get, :path => "/contacts/#{contact_data[:id]}",
            :query => 'include=media').
          will_respond_with(
            :status => 200,
            :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
            :body => {:data => data, :included => context[:included],
                      :links => context[:links]})

        result = Flapjack::Diner.contacts(contact_data[:id], :include => 'media')
        expect(result).not_to be_nil
        expect(result).to eq(data)
        expect(Flapjack::Diner.context).to eq(context)
      end

    end

  end

  context 'update' do

    it 'submits a PATCH request for a contact' do
      flapjack.given("a contact exists").
        upon_receiving("a PATCH request for a single contact").
        with(:method => :patch,
             :path => "/contacts/#{contact_data[:id]}",
             :body => {:data => {:id => contact_data[:id], :type => 'contact', :name => '<NAME>'}},
             :headers => {'Content-Type' => 'application/vnd.api+json'}).
        will_respond_with(
          :status => 204,
          :body => '' )

      result = Flapjack::Diner.update_contacts(:id => contact_data[:id], :name => 'Hello There')
      expect(result).to be_a(TrueClass)
    end

    it 'submits a PATCH request for several contacts' do<commit_msg>spec for multiple include params<commit_after>require 'spec_helper'
require 'flapjack-diner'

describe Flapjack::Diner::Resources::Contacts, :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 contact" do
      flapjack.given("no data exists").
        upon_receiving("a POST request with one contact").
        with(:method => :post, :path => '/contacts',
             :headers => {'Content-Type' => 'application/vnd.api+json'},
             :body => {'data' => contact_data.merge('type' => 'contact')}).
        will_respond_with(
          :status => 201,
          :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
          :body => {'data' => contact_data.merge('type' => 'contact')})

      result = Flapjack::Diner.create_contacts(contact_data)
      expect(result).not_to be_nil
      expect(result).to eq(contact_data.merge(:type => 'contact'))
    end

    it "submits a POST request for several contacts" do
      contacts_data = [contact_data.merge(:type => 'contact'),
                       contact_2_data.merge(:type => 'contact')]

      flapjack.given("no data exists").
        upon_receiving("a POST request with two contacts").
        with(:method => :post, :path => '/contacts',
             :headers => {'Content-Type' => 'application/vnd.api+json; ext=bulk'},
             :body => {:data => contacts_data}).
        will_respond_with(
          :status => 201,
          :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
          :body => {:data => contacts_data})

      result = Flapjack::Diner.create_contacts(*contacts_data)
      expect(result).not_to be_nil
      expect(result).to eq(contacts_data)
    end

    it "submits a POST request but a contact with that id exists" do
      flapjack.given("a contact exists").
        upon_receiving("a POST request with one contact").
        with(:method => :post, :path => '/contacts',
             :headers => {'Content-Type' => 'application/vnd.api+json'},
             :body => {:data => contact_data.merge(:type => 'contact')}).
        will_respond_with(
          :status => 409,
          :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
            :body => {:errors => [{
              :status => '409',
              :detail => "Contacts already exist with the following ids: #{contact_data[:id]}"
            }]}
          )

      result = Flapjack::Diner.create_contacts(contact_data)
      expect(result).to be_nil
      expect(Flapjack::Diner.last_error).to eq([{:status => '409',
        :detail => "Contacts already exist with the following ids: #{contact_data[:id]}"}])
    end

  end

  context 'read' do

    context 'GET all contacts' do

      it "has some data" do
        flapjack.given("a contact exists").
          upon_receiving("a GET request for all contacts").
          with(:method => :get, :path => '/contacts').
          will_respond_with(
            :status => 200,
            :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
            :body => {:data => [contact_data.merge('type' => 'contact')]})

        result = Flapjack::Diner.contacts
        expect(result).not_to be_nil
        expect(result).to eq([contact_data.merge(:type => 'contact')])
      end

      it "has no data" do
        flapjack.given("no data exists").
          upon_receiving("a GET request for all contacts").
          with(:method => :get, :path => '/contacts').
          will_respond_with(
            :status => 200,
            :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
            :body => {:data => []})

        result = Flapjack::Diner.contacts
        expect(result).not_to be_nil
        expect(result).to be_an_instance_of(Array)
        expect(result).to be_empty
      end

    end

    context 'GET a single contact' do

      it "finds the contact" do
        flapjack.given("a contact exists").
          upon_receiving("a GET request for a single contact").
          with(:method => :get, :path => "/contacts/#{contact_data[:id]}").
          will_respond_with(
            :status => 200,
            :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
            :body => {:data => contact_data.merge('type' => 'contact')})

        result = Flapjack::Diner.contacts(contact_data[:id])
        expect(result).not_to be_nil
        expect(result).to eq(contact_data.merge(:type => 'contact'))
      end

      it "can't find the contact" do
        flapjack.given("no data exists").
          upon_receiving("a GET request for a single contact").
          with(:method => :get, :path => "/contacts/#{contact_data[:id]}").
          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 Contact record, id: '#{contact_data[:id]}'"
              }]}
            )

        result = Flapjack::Diner.contacts(contact_data[:id])
        expect(result).to be_nil
        expect(Flapjack::Diner.last_error).to eq([{:status => '404',
          :detail => "could not find Contact record, id: '#{contact_data[:id]}'"}])
      end

    end

    context 'GET a single contact by name, including media' do

      it 'returns a contact with media' do
        data = contact_data.merge(:type => 'contact', :links => {
          :self  => "http://example.org/contacts/#{contact_data[:id]}",
          :media => {
            :self => "http://example.org/contacts/#{contact_data[:id]}/links/media",
            :related => "http://example.org/contacts/#{contact_data[:id]}/media",
            :linkage => [
              {:type => 'medium', :id => email_data[:id]}
            ]
          },
          :rules => "http://example.org/contacts/#{contact_data[:id]}/rules",
        })

        context = {:included => [
          email_data.merge(:type => 'medium', :links => {
            :self => "http://example.org/media/#{email_data[:id]}",
            :contact => "http://example.org/media/#{email_data[:id]}/contact",
            :rules => "http://example.org/media/#{email_data[:id]}/rules"
          })],
          :links => {
            :self  => "http://example.org/contacts/#{contact_data[:id]}?include=media"
          }
        }

        flapjack.given("a contact with one medium exists").
          upon_receiving("a GET request for a single contact with media").
          with(:method => :get, :path => "/contacts/#{contact_data[:id]}",
            :query => 'include=media').
          will_respond_with(
            :status => 200,
            :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
            :body => {:data => data, :included => context[:included],
                      :links => context[:links]})

        result = Flapjack::Diner.contacts(contact_data[:id], :include => 'media')
        expect(result).not_to be_nil
        expect(result).to eq(data)
        expect(Flapjack::Diner.context).to eq(context)
      end

    end

    context 'GET a single contact by name, including media and rules' do

      it 'returns a contact with media' do
        data = contact_data.merge(:type => 'contact', :links => {
          :self  => "http://example.org/contacts/#{contact_data[:id]}",
          :media => {
            :self => "http://example.org/contacts/#{contact_data[:id]}/links/media",
            :related => "http://example.org/contacts/#{contact_data[:id]}/media",
            :linkage => [
              {:type => 'medium', :id => email_data[:id]}
            ]
          },
          :rules => {
            :self => "http://example.org/contacts/#{contact_data[:id]}/links/rules",
            :related => "http://example.org/contacts/#{contact_data[:id]}/rules",
            :linkage => [
              {:type => 'rule', :id => rule_data[:id]}
            ]
          },
        })

        context = {:included => [
          email_data.merge(:type => 'medium', :links => {
            :self => "http://example.org/media/#{email_data[:id]}",
            :contact => "http://example.org/media/#{email_data[:id]}/contact",
            :rules => "http://example.org/media/#{email_data[:id]}/rules"
          }),
          rule_data.merge(:type => 'rule', :links => {
            :self => "http://example.org/rules/#{rule_data[:id]}",
            :contact => "http://example.org/rules/#{rule_data[:id]}/contact",
            :media => "http://example.org/rules/#{rule_data[:id]}/media",
            :tags => "http://example.org/rules/#{rule_data[:id]}/tags"
          })],
          :links => {
            :self  => "http://example.org/contacts/#{contact_data[:id]}?include=media%2Crules"
          }
        }

        flapjack.given("a contact with one medium and one rule exists").
          upon_receiving("a GET request for a single contact with media and rules").
          with(:method => :get, :path => "/contacts/#{contact_data[:id]}",
            :query => 'include=media%2Crules').
          will_respond_with(
            :status => 200,
            :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
            :body => {:data => data, :included => context[:included],
                      :links => context[:links]})

        result = Flapjack::Diner.contacts(contact_data[:id], :include => 'media,rules')
        expect(result).not_to be_nil
        expect(result).to eq(data)
        expect(Flapjack::Diner.context).to eq(context)
      end

    end

  end

  context 'update' do

    it 'submits a PATCH request for a contact' do
      flapjack.given("a contact exists").
        upon_receiving("a PATCH request for a single contact").
        with(:method => :patch,
             :path => "/contacts/#{contact_data[:id]}",
             :body => {:data => {:id => contact_data[:id], :type => 'contact', :name => '<NAME>'}},
             :headers => {'Content-Type' => 'application/vnd.api+json'}).
        will_respond_with(
          :status => 204,
          :body => '' )

      result = Flapjack::Diner.update_contacts(:id => contact_data[:id], :name => '<NAME>')
      expect(result).to be_a(TrueClass)
    end

    it 'submits a PATCH request for several contacts' do
{'fraction_non_alphanumeric': 0.16781951686708524, 'fraction_numerical': 0.005959348728317548, 'mean_word_length': 2.3568494373995357, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 290, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '222585', 'n_tokens_mistral': 5800, 'n_tokens_neox': 5502, 'n_words': 1193}