require 'rails_helper' module Logistics module Core RSpec.describe OperationsController, type: :controller do routes { Logistics::Core::Engine.routes } let(:valid_attributes) { { operation_number: FFaker::Name.name, contract_number: FFaker::Name.name, is_new_customer: false, status: FFaker::Name.name, offer_request_id: create(:offer_request).id, transaction_type_id: create(:transaction_type).id, declarant_id: create(:declarant).id, declaration_type_id: create(:declaration_type).id, transport_mode_id: create(:transport_mode).id, customs_office_id: create(:customs_office).id, customs_office_unit_id: create(:customs_office_unit).id, country_of_origin_id: create(:country).id, acquisition_mode_id: create(:acquisition_mode).id, payment_term_id: create(:payment_term).id, delivery_term_id: create(:delivery_term).id, warehouse_id: create(:warehouse).id } } let(:invalid_attributes) { { operation_number: nil, contract_number: FFaker::Name.name, is_new_customer: false, status: FFaker::Name.name, offer_request_id: create(:offer_request).id, transaction_type_id: create(:transaction_type).id, declarant_id: create(:declarant).id, declaration_type_id: create(:declaration_type).id, transport_mode_id: create(:transport_mode).id, customs_office_id: create(:customs_office).id, customs_office_unit_id: create(:customs_office_unit).id, country_of_origin_id: create(:country).id, acquisition_mode_id: create(:acquisition_mode).id, payment_term_id: create(:payment_term).id, delivery_term_id: create(:delivery_term).id, warehouse_id: create(:warehouse).id, } } describe "GET #index" do it "assigns all operations" do 2.times {create(:operation)} get :index, format: :json result = JSON(response.body) expect(result['data'].count).to eq 2 end it 'serializes operation per specification' do op = create(:operation) get :index, format: :json result = JSON(response.body) data = result['data'][0] expect(data.count).to eq 39 expect(data['id']).to eq op.id expect(data['operation_number']).to eq op.operation_number expect(data['contract_number']).to eq op.contract_number end end describe 'GET #fresh' do it 'gets only fresh operations' do 2.times { create(:operation) } 2.times { create(:operation, tasks_generated: true) } get :fresh, format: :json result = JSON(response.body) expect(result['data'].count).to eq 2 end end describe 'GET #filter_fresh_by_client' do it 'filters fresh operations for client' do client = create(:client) offer1 = create(:offer_request, client: client) offer2 = create(:offer_request, client: client) op1 = create(:operation, offer_request: offer1, tasks_generated: true) op2 = create(:operation, offer_request: offer2) 2.times { create(:operation) } get :filter_fresh_by_client, params: { id: client.id }, format: :json result = JSON(response.body) expect(result['data'].count).to eq 1 end end describe 'GET #assigned' do it 'fetches operations with owner' do 2.times { create(:operation) } create(:operation, owner: nil) get :assigned result = JSON(response.body) expect(result['data'].count).to eq 2 end end describe 'GET #unassigned' do it 'fetches operations with owner' do 2.times { create(:operation) } 2.times { create(:operation, owner: nil) } get :assigned result = JSON(response.body) expect(result['data'].count).to eq 2 end end describe "POST #create" do context "with valid params" do it "creates a new operation" do expect { post :create, params: {operation: valid_attributes}, format: :json }.to change(Operation, :count).by(1) end it "should return a success message" do post :create, params: {operation: valid_attributes}, format: :json result = JSON(response.body) expect(result['message']).to eq('Operation saved successfully!') end end context "with invalid params" do it 'should return an error message' do post :create, params: {operation: invalid_attributes}, format: :json result = JSON(response.body) expect(result['errors']).to include("Operation Operation number can't be blank") end end end describe 'POST #assign' do it 'assigns owners to operations' do u1 = create(:user) u2 = create(:user) op1 = create(:operation, owner: nil) op2 = create(:operation, owner: nil) op3 = create(:operation, owner: nil) params = { payload: [ {owner_id: u1.id, op_ids: [op1.id, op2.id]}, {owner_id: u2.id, op_ids: [op3.id]} ] } resp = Mks::Common::MethodResponse.new(true, nil, nil, nil, nil) expect_any_instance_of(OperationService).to receive(:assign_owner).and_return(resp) post :assign, params: params, format: :json result = JSON(response.body) expect(result['success']).to be_truthy end end describe "PUT #update" do context "with valid params" do let(:new_attributes) { { contract_number: FFaker::Name.name } } it "updates the requested operation" do operation = create(:operation) old_data = operation.contract_number put :update, params: {id: operation.to_param, operation: new_attributes}, format: :json operation.reload expect(operation.contract_number).not_to eq old_data end it "should return a success message" do operation = create(:operation) put :update, params: {id: operation.to_param, operation: valid_attributes}, format: :json result = JSON(response.body) expect(result['message']).to eq('Operation updated successfully!') end end context "with invalid params" do it "should return an error message" do operation = create(:operation) put :update, params: {id: operation.to_param, operation: invalid_attributes}, format: :json result = JSON(response.body) expect(result['errors']).to include("Operation Operation number can't be blank") end end end end end end