From f61c30b8db0870cd0edbfe6677cbe612d2bbc09d Mon Sep 17 00:00:00 2001 From: mike wyszinski Date: Wed, 11 Jul 2018 11:43:44 +0200 Subject: [PATCH] added tests and support for invoice pagination --- .../jpi/v1/invoices_controller.rb | 2 +- .../jpi/v1/invoices/index.json.jbuilder | 9 ++++++ api/config/routes.rb | 2 ++ .../controllers/jpi/v1/invoices_controller.rb | 14 ++++++-- .../jpi/v1/invoices_controller_spec.rb | 32 ++++++++++++++++--- .../v1/invoices_controller_routing_spec.rb | 4 +++ api/{ | 0 7 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 api/app/views/mno_enterprise/jpi/v1/invoices/index.json.jbuilder delete mode 100644 api/{ diff --git a/api/app/controllers/mno_enterprise/jpi/v1/invoices_controller.rb b/api/app/controllers/mno_enterprise/jpi/v1/invoices_controller.rb index 7aec3f73a..73b286153 100644 --- a/api/app/controllers/mno_enterprise/jpi/v1/invoices_controller.rb +++ b/api/app/controllers/mno_enterprise/jpi/v1/invoices_controller.rb @@ -1,5 +1,5 @@ module MnoEnterprise - class Jpi::V1::InvoicesController < ApplicationController + class Jpi::V1::InvoicesController < Jpi::V1::BaseResourceController include MnoEnterprise::Concerns::Controllers::Jpi::V1::InvoicesController end end diff --git a/api/app/views/mno_enterprise/jpi/v1/invoices/index.json.jbuilder b/api/app/views/mno_enterprise/jpi/v1/invoices/index.json.jbuilder new file mode 100644 index 000000000..a86e851e2 --- /dev/null +++ b/api/app/views/mno_enterprise/jpi/v1/invoices/index.json.jbuilder @@ -0,0 +1,9 @@ +json.invoices do + json.array! @invoices do |invoice| + json.started_at invoice.started_at + json.ended_at invoice.ended_at + json.amount AccountingjsSerializer.serialize(invoice.total_due) + json.paid invoice.paid? + json.link mno_enterprise.jpi_v1_invoice_path(invoice.slug) + end +end diff --git a/api/config/routes.rb b/api/config/routes.rb index da0744255..1298d7417 100644 --- a/api/config/routes.rb +++ b/api/config/routes.rb @@ -150,6 +150,8 @@ end end + resources :invoices, only: [:index] + resources :app_instances_sync, only: [:create, :index] resources :audit_events, only: [:index] diff --git a/api/lib/mno_enterprise/concerns/controllers/jpi/v1/invoices_controller.rb b/api/lib/mno_enterprise/concerns/controllers/jpi/v1/invoices_controller.rb index 8324597db..e049ca558 100644 --- a/api/lib/mno_enterprise/concerns/controllers/jpi/v1/invoices_controller.rb +++ b/api/lib/mno_enterprise/concerns/controllers/jpi/v1/invoices_controller.rb @@ -8,13 +8,21 @@ module MnoEnterprise::Concerns::Controllers::Jpi::V1::InvoicesController # context where it is included rather than being executed in the module's context included do - before_filter :authenticate_user! - before_filter :redirect_to_lounge_if_unconfirmed + before_filter :authenticate_user!, only: :show + before_filter :redirect_to_lounge_if_unconfirmed, only: :show + before_filter :check_authorization, only: :index end #================================================================== # Instance methods - #================================================================ + #================================================================== + def index + authorize! :manage_billing, parent_organization + # @invoices = MnoEnterprise::Invoice.find('organization.id': parent_organization.id) + query = MnoEnterprise::Invoice.apply_query_params(params,MnoEnterprise::Invoice.where('organization.id': parent_organization.id)) + @invoices = query.to_a + response.headers['X-Total-Count'] = query.meta.record_count + end # GET /mnoe/jpi/v1/invoices/201504-NU4 # Invoices endpoint for admins of an organization, rather than admin of a tenant diff --git a/api/spec/controllers/mno_enterprise/jpi/v1/invoices_controller_spec.rb b/api/spec/controllers/mno_enterprise/jpi/v1/invoices_controller_spec.rb index f3a063bec..7a02416f9 100644 --- a/api/spec/controllers/mno_enterprise/jpi/v1/invoices_controller_spec.rb +++ b/api/spec/controllers/mno_enterprise/jpi/v1/invoices_controller_spec.rb @@ -2,6 +2,8 @@ module MnoEnterprise describe Jpi::V1::InvoicesController, type: :controller do + include MnoEnterprise::TestingSupport::JpiV1TestHelper + render_views routes { MnoEnterprise::Engine.routes } @@ -14,13 +16,14 @@ module MnoEnterprise let(:user) { build(:user, organizations: [organization]) } let(:invoice) {build(:invoice, organization: organization, organization_id: organization.id)} + let!(:current_user_stub) { stub_user(user) } before do - stub_api_v2(:get, '/invoices', [invoice], [:organization], {filter:{slug:invoice.slug}, page:{number: 1, size: 1}}) + organization.orga_relations << build(:orga_relation, user_id: user.id, organization_id: organization.id, role: "Super Admin") end - - let!(:current_user_stub) { stub_user(user) } - describe "GET #show" do + before do + stub_api_v2(:get, '/invoices', [invoice], [:organization], {filter:{slug:invoice.slug}, page:{number: 1, size: 1}}) + end before { sign_in user } subject { get :show, id: invoice.slug } @@ -30,6 +33,27 @@ module MnoEnterprise it { subject; expect(response).to be_success } end + describe "GET #index" do + before { request.env['HTTP_ACCEPT'] = 'application/json' } + before do + stub_api_v2(:get, "/organizations/#{organization.id}", [organization], [:orga_relations, :users]) + stub_api_v2(:get, "/invoices", [invoice], [], {filter:{'organization.id':organization.id}}) + end + + before { sign_in user } + subject { get :index, organization_id: organization.id } + + # it_behaves_like "a navigatable protected user action" + # it_behaves_like "a user protected resource" + it_behaves_like "jpi v1 protected action" + + it { subject; expect(response).to be_success } + it 'returns the correct invoices' do + subject + expect(assigns(:invoices).first.slug).to eq invoice.slug + end + + end end end diff --git a/api/spec/routing/mno_enterprise/jpi/v1/invoices_controller_routing_spec.rb b/api/spec/routing/mno_enterprise/jpi/v1/invoices_controller_routing_spec.rb index db3b66ca4..a3d44222e 100644 --- a/api/spec/routing/mno_enterprise/jpi/v1/invoices_controller_routing_spec.rb +++ b/api/spec/routing/mno_enterprise/jpi/v1/invoices_controller_routing_spec.rb @@ -4,6 +4,10 @@ module MnoEnterprise RSpec.describe Jpi::V1::InvoicesController, type: :routing do routes { MnoEnterprise::Engine.routes } + it "routes to #index" do + expect(get("/jpi/v1/organizations/1233/invoices")).to route_to("mno_enterprise/jpi/v1/invoices#index", organization_id: "1233") + end + it "routes to #show" do expect(get("/jpi/v1/invoices/201504-NU4")).to route_to("mno_enterprise/jpi/v1/invoices#show", id: '201504-NU4') end diff --git a/api/{ b/api/{ deleted file mode 100644 index e69de29bb..000000000