# encoding: UTF-8
require 'spec_helper'

describe Suppliers::SectionsController, type: :controller do
  before :each do
    setup_supplier_for_controller
  end

  def valid_attributes
    {title: 'Terrace'}
  end

  def invalid_attributes
    {title: ''}
  end

  describe "GET #index" do
    it "does not include sections from another supplier" do
      base_section = @supplier.sections.first
      section1 = create :section, supplier: @supplier
      section2 = create :section
      get :index, format: :json
      assigns(:sections).should =~ [base_section, section1]
    end
  end

  describe "GET #show" do
    it "assigns the requested section to @section" do
      section = create :section, supplier: @supplier
      get :show, id: section.id, format: :json
      assigns(:section).should eq(section)
    end

    it "should not display a section of another supplier" do
      section = create :section
      get :show, id: section, format: :json
      response.status.should == 404
    end
  end

  describe "POST #create" do
    context "with valid attributes" do
      it "creates a new section" do
        expect{
          post :create, section: valid_attributes
        }.to change(Section, :count).by(1)
        expect( Section.last.supplier_id ).to eq @supplier.id
      end

      it "should not be possible to create a section for another supplier" do
        supplier2 = create :supplier
        post :create, section: valid_attributes.merge(title: 'Trying to hack', supplier_id: supplier2.id)
        expect( Section.find_by_title('Trying to hack').supplier_id ).to eq @supplier.id
      end
    end

    context "with invalid attributes" do
      it "does not save the new section" do
        expect{
          post :create, format: :json, section: invalid_attributes
        }.to_not change(Section, :count)
      end

      it "returns an error response" do
         post :create, format: :json, section: invalid_attributes
         expect( JSON.parse(response.body )['errors'] ).to be_present
      end
    end
  end

  describe 'PUT update' do
    before :each do
      @section = create :section, supplier: @supplier
    end

    context "valid attributes" do
      it "located the requested section" do
        put :update, id: @section, format: :json, section: valid_attributes
        @section.reload
        assigns(:section).should eq(@section)
      end

      it "changes @section's attributes" do
        put :update, id: @section.id, format: :json, section: valid_attributes.merge(title: "ChangedByTest")
        @section.reload
        expect( @section.title ).to eq "ChangedByTest"
      end

      it "should not be possible to update a section to another supplier" do
        supplier2 = create :supplier
        put :update, id: @section, section: valid_attributes.merge(title: "Trying to hack", supplier_id: supplier2.id)
        @section.reload
        @section.supplier_id.should == @supplier.id
      end

      it "should not be possible to update a section of another supplier" do
        section = create :section, title: 'Other supplier section'
        put :update, id: section.id, format: :json, section: {title: "Trying to hack"}
        expect( response ).to be_not_found
        section.reload
        section.title.should == 'Other supplier section'
      end
    end

    context "invalid attributes" do
      it "locates the requested section" do
        put :update, id: @section, format: :json,  section: invalid_attributes
        assigns(:section).should eq(@section)
      end

      it "returns an error response" do
         put :update, id: @section.id, format: :json, section: invalid_attributes
         expect( JSON.parse(response.body )['errors'] ).to be_present
      end
    end
  end

  describe 'DELETE destroy' do
    before :each do
      @section = create :section, supplier: @supplier
    end

    it "deletes the section" do
      expect{
        delete :destroy, id: @section
      }.to change(Section, :count).by(-1)
    end

    it "should not be possible to delete a section of another supplier" do
      section = create :section
      expect{
        delete :destroy, id: section
      }.to_not change(Section, :count)
    end
  end

  describe 'GET #manage_tables' do
    pending "Add tests"
  end

  describe 'GET #tables_view' do
    pending "Add tests"
  end

  describe 'POST #add_tables' do
    pending "Add tests"
  end

  describe 'POST #arrange_tables' do
    pending "Add tests"
  end
end
