require 'rails_helper' module Logistics module Core RSpec.describe AgenciesController, type: :controller do routes { Logistics::Core::Engine.routes } let(:valid_attributes) { { code: FFaker::Name.name, name: FFaker::Name.name, city: FFaker::Name.name, subcity: FFaker::Name.name, telephone: '+251114151515', contact_person: FFaker::Name.name, contact_telephone: '+251911757575', agency_category_id: create(:agency_category).id } } let(:valid_agency_category_attributes) { { id: 1, code: 'GOA', name: 'Government Agency', description: 'Owned by the Government' } } let(:valid_document_type_category_attributes) { { id: 1, code: 'TRD', name: 'Transport Document', description: 'A document that is submitted for goods that are imported/exported via land, sea or air' } } let(:valid_document_type_origin_attributes) { { id: 1, code: 'SHP', name: 'Shipper', description: 'One who transports goods on a water body' } } let(:valid_document_type_attributes) { { code: 'B/L', name: 'Bill of lading', description: 'Document issued by a carrier which details a shipment and gives title of that shipment to a specified party', document_type_category_id: 1, document_type_origin_id: 1 } } let(:invalid_attributes) { { code: '', name: '', city: '', telephone: '+251114151515', contact_person: 'Abebe Kebede', contact_telephone: '+251911757575' } } let(:valid_session) { {} } describe 'GET #index' do it 'assigns all agencies as @agencies' do 2.times { create(:agency) } get :index, format: :json result = JSON(response.body) expect(result['data'].count).to eq 2 end end describe 'GET #get_permit_documents' do it 'return all permit documents to be given by a specific agency' do agency = create(:agency) dt = create(:document_type) create(:agency_permit_document, agency: agency, document_type: dt) get :get_permit_documents, params: { :id => agency.to_param }, format: :json result = JSON(response.body) expect(result['data'][0]['agency_id']).to eq agency.id expect(result['data'][0]['document_type_id']).to eq dt.id end end describe "GET #get_document_type_with_no_agency" do it "return all document types that are not associated with a specific agency" do agency_permit = create(:agency_permit_document) create(:document_type) get :get_document_type_with_no_agency, params: { :id => agency_permit.agency.to_param }, format: :json result = JSON(response.body) expect(result['data'][0]['id']).to eq agency_permit.document_type.id expect(result['data'][0]['name']).to eq agency_permit.document_type.name end end describe "POST #create" do context "with valid params" do it "creates a new Agency" do expect { post :create, params: { :agency => valid_attributes }, format: :json }.to change(Agency, :count).by(1) end it "should return a success message" do post :create, params: { :agency => valid_attributes }, format: :json result = JSON(response.body) expect(result['message']).to eq('Agency information saved successfully!') end end context "with invalid params" do it "should return an error message" do post :create, params: { :agency => invalid_attributes }, format: :json result = JSON(response.body) expect(result['errors']).to include("Agency Code can't be blank") end end end describe "PUT #update" do context "with valid params" do let(:new_attributes) { { telephone: '+251911757575' } } it "updates the requested agency" do agency = Agency.create! valid_attributes put :update, params: { :id => agency.to_param, :agency => new_attributes }, format: :json agency.reload expect(agency.telephone).not_to eq valid_attributes[:telephone] end it "should return a success message" do agency = Agency.create! valid_attributes put :update, params: { :id => agency.to_param, :agency => valid_attributes }, format: :json result = JSON(response.body) expect(result['message']).to eq('Agency information updated successfully!') end end context "with invalid params" do it "should return an error message" do agency = Agency.create! valid_attributes put :update, params: { :id => agency.to_param, :agency => invalid_attributes }, format: :json result = JSON(response.body) expect(result['errors']).to include("Agency Code can't be blank") end end end end end end