From 94e246394922b59a881f5d58a41fa7b352c9b042 Mon Sep 17 00:00:00 2001 From: MAhsenArif Date: Thu, 1 Feb 2018 20:37:35 +0500 Subject: [PATCH 1/6] [MNOE-406] Added logic to load delisted apps if already in dashboard --- .../jpi/v1/app_instances/_resource.json.jbuilder | 1 + core/lib/mno_enterprise/concerns/models/app_instance.rb | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/api/app/views/mno_enterprise/jpi/v1/app_instances/_resource.json.jbuilder b/api/app/views/mno_enterprise/jpi/v1/app_instances/_resource.json.jbuilder index e09476215..3d37ec774 100644 --- a/api/app/views/mno_enterprise/jpi/v1/app_instances/_resource.json.jbuilder +++ b/api/app/views/mno_enterprise/jpi/v1/app_instances/_resource.json.jbuilder @@ -23,6 +23,7 @@ end # json.connector_version app_instance.oauth_keys[:version] # end +app_instance.without_tenant = true if params[:data] && params[:data] == 'unscoped' app_instance.app.tap do |a| json.app_id a.id json.app_name a.name diff --git a/core/lib/mno_enterprise/concerns/models/app_instance.rb b/core/lib/mno_enterprise/concerns/models/app_instance.rb index 9c7811baa..2b1e2ceac 100644 --- a/core/lib/mno_enterprise/concerns/models/app_instance.rb +++ b/core/lib/mno_enterprise/concerns/models/app_instance.rb @@ -42,6 +42,8 @@ module MnoEnterprise::Concerns::Models::AppInstance :owner_type, :terminated_at, :stopped_at, :billing_type, :autostop_at, :autostop_interval, :next_status, :soa_enabled, :oauth_company, :oauth_keys, :oauth_keys_valid, :free_trial_end_at, :per_user_licence, :active_licences_count + attr_accessor :without_tenant + #============================================================== # Constants #============================================================== @@ -52,7 +54,6 @@ module MnoEnterprise::Concerns::Models::AppInstance # Associations #============================================================== belongs_to :owner, class_name: 'MnoEnterprise::Organization' - belongs_to :app, class_name: 'MnoEnterprise::App' # Define connector_stack?, cloud_stack? etc. methods [:cube, :cloud, :connector].each do |stackname| @@ -62,6 +63,10 @@ module MnoEnterprise::Concerns::Models::AppInstance end scope :active, -> { where('status.in' => ACTIVE_STATUSES) } + + def app + MnoEnterprise::App.find(self.app_id, { unscoped: self.without_tenant }) + end end #================================================================== From 8f64f0023914a206d52007581c24a69566d89533 Mon Sep 17 00:00:00 2001 From: MAhsenArif Date: Fri, 9 Feb 2018 12:37:54 +0500 Subject: [PATCH 2/6] [MNOE-406] Updated condition based on param from frontend --- .../mno_enterprise/jpi/v1/app_instances/_resource.json.jbuilder | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/app/views/mno_enterprise/jpi/v1/app_instances/_resource.json.jbuilder b/api/app/views/mno_enterprise/jpi/v1/app_instances/_resource.json.jbuilder index 3d37ec774..e7cb2f0ee 100644 --- a/api/app/views/mno_enterprise/jpi/v1/app_instances/_resource.json.jbuilder +++ b/api/app/views/mno_enterprise/jpi/v1/app_instances/_resource.json.jbuilder @@ -23,7 +23,7 @@ end # json.connector_version app_instance.oauth_keys[:version] # end -app_instance.without_tenant = true if params[:data] && params[:data] == 'unscoped' +app_instance.without_tenant = params[:unscoped].present? app_instance.app.tap do |a| json.app_id a.id json.app_name a.name From 8e34be3c09a65b3488a9d90ac2b11df3fdf4c7d6 Mon Sep 17 00:00:00 2001 From: MAhsenArif Date: Fri, 16 Feb 2018 12:16:27 +0500 Subject: [PATCH 3/6] [MNOE-406] Updated test cases --- .../jpi/v1/admin/organizations_controller_spec.rb | 2 ++ .../mno_enterprise/jpi/v1/app_instances_controller_spec.rb | 4 ++++ api/spec/lib/mno_enterprise/intercom_events_listener_spec.rb | 1 + 3 files changed, 7 insertions(+) diff --git a/api/spec/controllers/mno_enterprise/jpi/v1/admin/organizations_controller_spec.rb b/api/spec/controllers/mno_enterprise/jpi/v1/admin/organizations_controller_spec.rb index 795044e44..1d474cb59 100644 --- a/api/spec/controllers/mno_enterprise/jpi/v1/admin/organizations_controller_spec.rb +++ b/api/spec/controllers/mno_enterprise/jpi/v1/admin/organizations_controller_spec.rb @@ -43,11 +43,13 @@ def partial_hash_for_arrears(arrear) let(:org_invite) { build(:org_invite, organization: organization) } let(:app_instance) { build(:app_instance, organization: organization) } let(:credit_card) { build(:credit_card, organization: organization) } + let(:app) { build(:app)} before do organizations = [organization] allow(organizations).to receive(:loaded?).and_return(true) allow_any_instance_of(MnoEnterprise::User).to receive(:organizations).and_return(organizations) + allow_any_instance_of(MnoEnterprise::AppInstance).to receive(:app).and_return(app) api_stub_for(get: "/organizations/#{organization.id}/invoices", response: from_api([invoice])) api_stub_for(get: "/organizations", response: from_api([organization])) api_stub_for(get: "/organizations/#{organization.id}", response: from_api(organization)) diff --git a/api/spec/controllers/mno_enterprise/jpi/v1/app_instances_controller_spec.rb b/api/spec/controllers/mno_enterprise/jpi/v1/app_instances_controller_spec.rb index de8c00125..f270adbfc 100644 --- a/api/spec/controllers/mno_enterprise/jpi/v1/app_instances_controller_spec.rb +++ b/api/spec/controllers/mno_enterprise/jpi/v1/app_instances_controller_spec.rb @@ -17,8 +17,12 @@ module MnoEnterprise before { api_stub_for(get: "/users/#{user.id}", response: from_api(user)) } # Stub organization + associations let(:organization) { build(:organization) } + let(:app) { build(:app)} before { allow_any_instance_of(MnoEnterprise::User).to receive(:organizations).and_return([organization]) } + before { allow_any_instance_of(MnoEnterprise::AppInstance).to receive(:without_tenant).and_return(true) } + before { allow_any_instance_of(MnoEnterprise::AppInstance).to receive(:app).and_return(app) } + describe 'GET #index' do let(:app_instance) { build(:app_instance, status: "running") } let(:app_instance) { build(:app_instance, under_free_trial: false) } diff --git a/api/spec/lib/mno_enterprise/intercom_events_listener_spec.rb b/api/spec/lib/mno_enterprise/intercom_events_listener_spec.rb index 1a4db6cb0..55e9895e6 100644 --- a/api/spec/lib/mno_enterprise/intercom_events_listener_spec.rb +++ b/api/spec/lib/mno_enterprise/intercom_events_listener_spec.rb @@ -8,6 +8,7 @@ module MnoEnterprise let(:credit_card) { build(:credit_card) } let(:organization) { build(:organization) } before do + allow_any_instance_of(MnoEnterprise::AppInstance).to receive(:app).and_return(app) api_stub_for(get: "/users/#{user.id}", response: from_api(user)) api_stub_for(get: "/users/#{user.id}/organizations", response: from_api([organization])) api_stub_for(get: "/organizations/#{organization.id}/app_instances", response: from_api([app_instance])) From a792bf19b0c80109a4740251258f2cae837673f2 Mon Sep 17 00:00:00 2001 From: MAhsenArif Date: Thu, 22 Feb 2018 12:29:40 +0500 Subject: [PATCH 4/6] [MNOE-406] Added testcase for unscoped app retrieval --- .../jpi/v1/app_instances_controller_spec.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/api/spec/controllers/mno_enterprise/jpi/v1/app_instances_controller_spec.rb b/api/spec/controllers/mno_enterprise/jpi/v1/app_instances_controller_spec.rb index f270adbfc..df516fe4d 100644 --- a/api/spec/controllers/mno_enterprise/jpi/v1/app_instances_controller_spec.rb +++ b/api/spec/controllers/mno_enterprise/jpi/v1/app_instances_controller_spec.rb @@ -20,7 +20,7 @@ module MnoEnterprise let(:app) { build(:app)} before { allow_any_instance_of(MnoEnterprise::User).to receive(:organizations).and_return([organization]) } - before { allow_any_instance_of(MnoEnterprise::AppInstance).to receive(:without_tenant).and_return(true) } + before { allow_any_instance_of(MnoEnterprise::AppInstance).to receive(:without_tenant).and_return(false) } before { allow_any_instance_of(MnoEnterprise::AppInstance).to receive(:app).and_return(app) } describe 'GET #index' do @@ -51,6 +51,15 @@ module MnoEnterprise end end + context 'with unscoped data' do + before { allow_any_instance_of(MnoEnterprise::AppInstance).to receive(:without_tenant).and_return(true) } + + it 'retrived the app instance with the app' do + subject + expect(app_instance.app).to eq(app) + end + end + context 'with timestamp' do let(:timestamp) { Time.current.to_i } From 98c770a1acafb7a20a9953ba376d306b9f9e141d Mon Sep 17 00:00:00 2001 From: Olivier Brisse Date: Fri, 23 Feb 2018 09:38:39 +1100 Subject: [PATCH 5/6] Fix typo --- .../mno_enterprise/jpi/v1/app_instances_controller_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/spec/controllers/mno_enterprise/jpi/v1/app_instances_controller_spec.rb b/api/spec/controllers/mno_enterprise/jpi/v1/app_instances_controller_spec.rb index df516fe4d..0c25a2f7e 100644 --- a/api/spec/controllers/mno_enterprise/jpi/v1/app_instances_controller_spec.rb +++ b/api/spec/controllers/mno_enterprise/jpi/v1/app_instances_controller_spec.rb @@ -54,7 +54,7 @@ module MnoEnterprise context 'with unscoped data' do before { allow_any_instance_of(MnoEnterprise::AppInstance).to receive(:without_tenant).and_return(true) } - it 'retrived the app instance with the app' do + it 'retrieved the app instance with the app' do subject expect(app_instance.app).to eq(app) end From f5c558b2f1b10c0849329e03cd960a1a8b90714b Mon Sep 17 00:00:00 2001 From: MAhsenArif Date: Fri, 23 Feb 2018 17:23:42 +0500 Subject: [PATCH 6/6] [MNOE-406] added new approach to use where callback logic with custom flag --- .../jpi/v1/app_instances/_resource.json.jbuilder | 3 +-- .../controllers/jpi/v1/app_instances_controller.rb | 2 ++ .../jpi/v1/admin/organizations_controller_spec.rb | 2 -- .../jpi/v1/app_instances_controller_spec.rb | 13 ++++++++----- .../mno_enterprise/intercom_events_listener_spec.rb | 1 - .../mno_enterprise/concerns/models/app_instance.rb | 7 +------ 6 files changed, 12 insertions(+), 16 deletions(-) diff --git a/api/app/views/mno_enterprise/jpi/v1/app_instances/_resource.json.jbuilder b/api/app/views/mno_enterprise/jpi/v1/app_instances/_resource.json.jbuilder index e7cb2f0ee..8bc43014a 100644 --- a/api/app/views/mno_enterprise/jpi/v1/app_instances/_resource.json.jbuilder +++ b/api/app/views/mno_enterprise/jpi/v1/app_instances/_resource.json.jbuilder @@ -23,8 +23,7 @@ end # json.connector_version app_instance.oauth_keys[:version] # end -app_instance.without_tenant = params[:unscoped].present? -app_instance.app.tap do |a| +@apps[app_instance.app_id].first.tap do |a| json.app_id a.id json.app_name a.name json.app_nid a.nid diff --git a/api/lib/mno_enterprise/concerns/controllers/jpi/v1/app_instances_controller.rb b/api/lib/mno_enterprise/concerns/controllers/jpi/v1/app_instances_controller.rb index cbae629a9..c8c2bb957 100644 --- a/api/lib/mno_enterprise/concerns/controllers/jpi/v1/app_instances_controller.rb +++ b/api/lib/mno_enterprise/concerns/controllers/jpi/v1/app_instances_controller.rb @@ -20,6 +20,8 @@ def index i.owner = parent_organization can?(:access,i) end + ids = @app_instances.collect{ |i| i.app_id } + @apps = MnoEnterprise::App.where('id.in' => ids, unscoped: params[:unscoped].present?).group_by(&:id) end # POST /mnoe/jpi/v1/organization/1/app_instances diff --git a/api/spec/controllers/mno_enterprise/jpi/v1/admin/organizations_controller_spec.rb b/api/spec/controllers/mno_enterprise/jpi/v1/admin/organizations_controller_spec.rb index 1d474cb59..795044e44 100644 --- a/api/spec/controllers/mno_enterprise/jpi/v1/admin/organizations_controller_spec.rb +++ b/api/spec/controllers/mno_enterprise/jpi/v1/admin/organizations_controller_spec.rb @@ -43,13 +43,11 @@ def partial_hash_for_arrears(arrear) let(:org_invite) { build(:org_invite, organization: organization) } let(:app_instance) { build(:app_instance, organization: organization) } let(:credit_card) { build(:credit_card, organization: organization) } - let(:app) { build(:app)} before do organizations = [organization] allow(organizations).to receive(:loaded?).and_return(true) allow_any_instance_of(MnoEnterprise::User).to receive(:organizations).and_return(organizations) - allow_any_instance_of(MnoEnterprise::AppInstance).to receive(:app).and_return(app) api_stub_for(get: "/organizations/#{organization.id}/invoices", response: from_api([invoice])) api_stub_for(get: "/organizations", response: from_api([organization])) api_stub_for(get: "/organizations/#{organization.id}", response: from_api(organization)) diff --git a/api/spec/controllers/mno_enterprise/jpi/v1/app_instances_controller_spec.rb b/api/spec/controllers/mno_enterprise/jpi/v1/app_instances_controller_spec.rb index 0c25a2f7e..30c2eeb78 100644 --- a/api/spec/controllers/mno_enterprise/jpi/v1/app_instances_controller_spec.rb +++ b/api/spec/controllers/mno_enterprise/jpi/v1/app_instances_controller_spec.rb @@ -17,11 +17,9 @@ module MnoEnterprise before { api_stub_for(get: "/users/#{user.id}", response: from_api(user)) } # Stub organization + associations let(:organization) { build(:organization) } - let(:app) { build(:app)} before { allow_any_instance_of(MnoEnterprise::User).to receive(:organizations).and_return([organization]) } - before { allow_any_instance_of(MnoEnterprise::AppInstance).to receive(:without_tenant).and_return(false) } - before { allow_any_instance_of(MnoEnterprise::AppInstance).to receive(:app).and_return(app) } + let(:app) { build(:app) } describe 'GET #index' do let(:app_instance) { build(:app_instance, status: "running") } @@ -34,6 +32,11 @@ module MnoEnterprise allow(app_instances).to receive(:active).and_return(app_instances) # Updated since last tick allow(app_instances).to receive(:where).and_return([app_instance]) + + allow(app_instance).to receive(:app_id).and_return(app.id) + + api_stub_for(get: "/apps?filter[id.in][]=#{app.id}&filter[unscoped]=false", response: from_api([app])) + api_stub_for(get: "/apps?filter[id.in]&filter[unscoped]=false", response: from_api(nil)) end before { sign_in user } @@ -52,11 +55,11 @@ module MnoEnterprise end context 'with unscoped data' do - before { allow_any_instance_of(MnoEnterprise::AppInstance).to receive(:without_tenant).and_return(true) } + before { api_stub_for(get: "/apps?filter[id.in][]=#{app.id}&filter[unscoped]=true", response: from_api([app])) } + subject { get :index, organization_id: organization.id, timestamp: timestamp, unscoped: true } it 'retrieved the app instance with the app' do subject - expect(app_instance.app).to eq(app) end end diff --git a/api/spec/lib/mno_enterprise/intercom_events_listener_spec.rb b/api/spec/lib/mno_enterprise/intercom_events_listener_spec.rb index 55e9895e6..1a4db6cb0 100644 --- a/api/spec/lib/mno_enterprise/intercom_events_listener_spec.rb +++ b/api/spec/lib/mno_enterprise/intercom_events_listener_spec.rb @@ -8,7 +8,6 @@ module MnoEnterprise let(:credit_card) { build(:credit_card) } let(:organization) { build(:organization) } before do - allow_any_instance_of(MnoEnterprise::AppInstance).to receive(:app).and_return(app) api_stub_for(get: "/users/#{user.id}", response: from_api(user)) api_stub_for(get: "/users/#{user.id}/organizations", response: from_api([organization])) api_stub_for(get: "/organizations/#{organization.id}/app_instances", response: from_api([app_instance])) diff --git a/core/lib/mno_enterprise/concerns/models/app_instance.rb b/core/lib/mno_enterprise/concerns/models/app_instance.rb index 2b1e2ceac..9c7811baa 100644 --- a/core/lib/mno_enterprise/concerns/models/app_instance.rb +++ b/core/lib/mno_enterprise/concerns/models/app_instance.rb @@ -42,8 +42,6 @@ module MnoEnterprise::Concerns::Models::AppInstance :owner_type, :terminated_at, :stopped_at, :billing_type, :autostop_at, :autostop_interval, :next_status, :soa_enabled, :oauth_company, :oauth_keys, :oauth_keys_valid, :free_trial_end_at, :per_user_licence, :active_licences_count - attr_accessor :without_tenant - #============================================================== # Constants #============================================================== @@ -54,6 +52,7 @@ module MnoEnterprise::Concerns::Models::AppInstance # Associations #============================================================== belongs_to :owner, class_name: 'MnoEnterprise::Organization' + belongs_to :app, class_name: 'MnoEnterprise::App' # Define connector_stack?, cloud_stack? etc. methods [:cube, :cloud, :connector].each do |stackname| @@ -63,10 +62,6 @@ module MnoEnterprise::Concerns::Models::AppInstance end scope :active, -> { where('status.in' => ACTIVE_STATUSES) } - - def app - MnoEnterprise::App.find(self.app_id, { unscoped: self.without_tenant }) - end end #==================================================================