require 'rails_helper' module Mks module Rate RSpec.describe ServiceBundlesController, type: :controller do routes { Mks::Rate::Engine.routes } before(:each) do u = create(:user) token = Mks::Auth::TokenAuth.issue(name: u.full_name, email: u.email, id: u.id) request.headers['Authorization'] = "Bearer #{token}" end let(:valid_attributes) do { code: FFaker::Name.unique.name, name: FFaker::Name.name, description: FFaker::Name.name, service_delivery_unit_id: create(:service_delivery_unit).id } end let(:invalid_attributes) do { code: nil, name: FFaker::Name.name, description: FFaker::Name.name, service_delivery_unit_id: create(:service_delivery_unit).id } end describe 'GET #index' do it 'returns a success response' do create(:service_bundle) get :index expect(response).to be_successful end end describe 'GET #sdu_bundles' do it 'returns a success response' do sdu = create(:service_delivery_unit) 3.times { create(:service_bundle, service_delivery_unit: sdu) } get :sdu_bundles, params: { id: sdu.id } expect(response).to be_successful end it 'returns only bundles belonging to an SDU' do sdu = create(:service_delivery_unit) 3.times { create(:service_bundle, service_delivery_unit: sdu) } 3.times { create(:service_bundle) } get :sdu_bundles, params: { id: sdu.id } data = JSON(response.body) expect(data.count).to eq 3 end end describe 'GET #show' do it 'returns a success response' do service_bundle = create(:service_bundle) get :show, params: { id: service_bundle.to_param } expect(response).to be_successful end end describe 'GET #filter' do it 'filters bundles by name' do create(:service_bundle, name: 'First Service Bundle') create(:service_bundle, name: 'Second Service Bundle') get :filter, params: { search: 'First' } data = JSON(response.body) expect(data.count).to eq 1 get :filter, params: { search: 'Service' } data = JSON(response.body) expect(data.count).to eq 2 end it 'does case insensitive search' do create(:service_bundle, name: 'FIRst Service Bundle') create(:service_bundle, name: 'Second Service Bundle for the firST time') get :filter, params: { search: 'First' } data = JSON(response.body) expect(data.count).to eq 2 end end describe 'POST #create' do context 'with valid params' do it 'creates a new ServiceBundle' do expect do post :create, params: { service_bundle: valid_attributes } end.to change(ServiceBundle, :count).by(1) end it 'renders a JSON response with the new service_bundle' do post :create, params: { service_bundle: valid_attributes } expect(response).to have_http_status(:created) expect(response.content_type).to eq('application/json') expect(response.location).to eq(service_bundle_url(ServiceBundle.last)) end end context 'with invalid params' do it 'renders a JSON response with errors for the new service_bundle' do post :create, params: { service_bundle: invalid_attributes } expect(response).to have_http_status(:unprocessable_entity) expect(response.content_type).to eq('application/json') end end end describe 'POST #add_services' do it 'adds services to bundle' do expect_any_instance_of(ServiceBundleService).to receive(:add_services).and_return(true) bundle = create(:service_bundle) s1 = create(:chargeable_service) expect(bundle.chargeable_services.count).to eq 0 post :add_services, params: { id: bundle.id, ids: [s1.id] } result = JSON(response.body) expect(result['success']).to be_truthy end end describe 'PUT #update' do context 'with valid params' do let(:new_attributes) do { name: FFaker::Name.name } end it 'updates the requested service_bundle' do service_bundle = create(:service_bundle) put :update, params: { id: service_bundle.to_param, service_bundle: new_attributes } service_bundle.reload expect(service_bundle.name).to eq new_attributes[:name] end it 'renders a JSON response with the service_bundle' do service_bundle = create(:service_bundle) put :update, params: { id: service_bundle.to_param, service_bundle: valid_attributes } expect(response).to have_http_status(:ok) expect(response.content_type).to eq('application/json') end end context 'with invalid params' do it 'renders a JSON response with errors for the service_bundle' do service_bundle = create(:service_bundle) put :update, params: { id: service_bundle.to_param, service_bundle: invalid_attributes } expect(response).to have_http_status(:unprocessable_entity) expect(response.content_type).to eq('application/json') end end end end end end