Commit dbecccbe authored by Corinna Wiesner's avatar Corinna Wiesner Committed by Jarka Košanová

Send namespace id in subscription plan request

Without the namespace id the subscription plan request cannot return the
correct information if a namespace is eligible for a free midterm
upgrade.
parent 189a4921
...@@ -15,8 +15,9 @@ class Groups::BillingsController < Groups::ApplicationController ...@@ -15,8 +15,9 @@ class Groups::BillingsController < Groups::ApplicationController
def index def index
@top_most_group = @group.root_ancestor if @group.has_parent? @top_most_group = @group.root_ancestor if @group.has_parent?
current_plan = (@top_most_group || @group).plan_name_for_upgrading relevant_group = (@top_most_group || @group)
@plans_data = FetchSubscriptionPlansService.new(plan: current_plan).execute current_plan = relevant_group.plan_name_for_upgrading
@plans_data = FetchSubscriptionPlansService.new(plan: current_plan, namespace_id: relevant_group.id).execute
track_experiment_event(:contact_sales_btn_in_app, 'page_view:billing_plans:group') track_experiment_event(:contact_sales_btn_in_app, 'page_view:billing_plans:group')
record_experiment_user(:contact_sales_btn_in_app) record_experiment_user(:contact_sales_btn_in_app)
end end
......
...@@ -7,7 +7,7 @@ class Profiles::BillingsController < Profiles::ApplicationController ...@@ -7,7 +7,7 @@ class Profiles::BillingsController < Profiles::ApplicationController
def index def index
@plans_data = FetchSubscriptionPlansService @plans_data = FetchSubscriptionPlansService
.new(plan: current_user.namespace.plan_name_for_upgrading) .new(plan: current_user.namespace.plan_name_for_upgrading, namespace_id: current_user.namespace_id)
.execute .execute
track_experiment_event(:contact_sales_btn_in_app, 'page_view:billing_plans:profile') track_experiment_event(:contact_sales_btn_in_app, 'page_view:billing_plans:profile')
record_experiment_user(:contact_sales_btn_in_app) record_experiment_user(:contact_sales_btn_in_app)
......
...@@ -3,8 +3,9 @@ ...@@ -3,8 +3,9 @@
class FetchSubscriptionPlansService class FetchSubscriptionPlansService
URL = "#{EE::SUBSCRIPTIONS_URL}/gitlab_plans".freeze URL = "#{EE::SUBSCRIPTIONS_URL}/gitlab_plans".freeze
def initialize(plan:) def initialize(plan:, namespace_id: nil)
@plan = plan @plan = plan
@namespace_id = namespace_id
end end
def execute def execute
...@@ -14,10 +15,12 @@ class FetchSubscriptionPlansService ...@@ -14,10 +15,12 @@ class FetchSubscriptionPlansService
private private
def send_request def send_request
response = Gitlab::HTTP.get(URL, response = Gitlab::HTTP.get(
allow_local_requests: true, URL,
query: { plan: @plan }, allow_local_requests: true,
headers: { 'Accept' => 'application/json' }) query: { plan: @plan, namespace_id: @namespace_id },
headers: { 'Accept' => 'application/json' }
)
Gitlab::Json.parse(response.body).map { |plan| Hashie::Mash.new(plan) } Gitlab::Json.parse(response.body).map { |plan| Hashie::Mash.new(plan) }
rescue => e rescue => e
...@@ -39,6 +42,10 @@ class FetchSubscriptionPlansService ...@@ -39,6 +42,10 @@ class FetchSubscriptionPlansService
end end
def cache_key def cache_key
"subscription-plan-#{@plan}" if @namespace_id.present?
"subscription-plan-#{@plan}-#{@namespace_id}"
else
"subscription-plan-#{@plan}"
end
end end
end end
...@@ -20,7 +20,7 @@ RSpec.describe 'Billing plan pages', :feature do ...@@ -20,7 +20,7 @@ RSpec.describe 'Billing plan pages', :feature do
before do before do
stub_feature_flags(hide_deprecated_billing_plans: false) stub_feature_flags(hide_deprecated_billing_plans: false)
stub_experiment_for_subject(contact_sales_btn_in_app: true) stub_experiment_for_subject(contact_sales_btn_in_app: true)
stub_full_request("#{EE::SUBSCRIPTIONS_URL}/gitlab_plans?plan=#{plan.name}") stub_full_request("#{EE::SUBSCRIPTIONS_URL}/gitlab_plans?plan=#{plan.name}&namespace_id=#{namespace.id}")
.to_return(status: 200, body: plans_data.to_json) .to_return(status: 200, body: plans_data.to_json)
stub_application_setting(check_namespace_plan: true) stub_application_setting(check_namespace_plan: true)
allow(Gitlab).to receive(:com?) { true } allow(Gitlab).to receive(:com?) { true }
...@@ -371,7 +371,7 @@ RSpec.describe 'Billing plan pages', :feature do ...@@ -371,7 +371,7 @@ RSpec.describe 'Billing plan pages', :feature do
let!(:group_member) { create(:group_member, :owner, group: namespace, user: user) } let!(:group_member) { create(:group_member, :owner, group: namespace, user: user) }
before do before do
stub_full_request("#{EE::SUBSCRIPTIONS_URL}/gitlab_plans?plan=free") stub_full_request("#{EE::SUBSCRIPTIONS_URL}/gitlab_plans?plan=free&namespace_id=#{namespace.id}")
.to_return(status: 200, body: plans_data.to_json) .to_return(status: 200, body: plans_data.to_json)
end end
......
...@@ -18,7 +18,7 @@ RSpec.describe 'Groups > Billing', :js do ...@@ -18,7 +18,7 @@ RSpec.describe 'Groups > Billing', :js do
end end
before do before do
stub_full_request("#{EE::SUBSCRIPTIONS_URL}/gitlab_plans?plan=#{plan}") stub_full_request("#{EE::SUBSCRIPTIONS_URL}/gitlab_plans?plan=#{plan}&namespace_id=#{group.id}")
.with(headers: { 'Accept' => 'application/json' }) .with(headers: { 'Accept' => 'application/json' })
.to_return(status: 200, body: File.new(Rails.root.join('ee/spec/fixtures/gitlab_com_plans.json'))) .to_return(status: 200, body: File.new(Rails.root.join('ee/spec/fixtures/gitlab_com_plans.json')))
......
...@@ -12,7 +12,7 @@ RSpec.describe 'Welcome screen', :js do ...@@ -12,7 +12,7 @@ RSpec.describe 'Welcome screen', :js do
before do before do
group.add_owner(user) group.add_owner(user)
gitlab_sign_in(user) gitlab_sign_in(user)
stub_request(:get, "#{EE::SUBSCRIPTIONS_URL}/gitlab_plans?plan=free") stub_request(:get, "#{EE::SUBSCRIPTIONS_URL}/gitlab_plans?plan=free&namespace_id=")
.to_return(status: 200, body: '{}', headers: {}) .to_return(status: 200, body: '{}', headers: {})
visit edit_subscriptions_group_path(group.path, params) visit edit_subscriptions_group_path(group.path, params)
......
...@@ -17,7 +17,7 @@ RSpec.describe 'Show trial banner', :js do ...@@ -17,7 +17,7 @@ RSpec.describe 'Show trial banner', :js do
before do before do
stub_application_setting(check_namespace_plan: true) stub_application_setting(check_namespace_plan: true)
allow(Gitlab).to receive(:com?).and_return(true).at_least(:once) allow(Gitlab).to receive(:com?).and_return(true).at_least(:once)
stub_full_request("#{EE::SUBSCRIPTIONS_URL}/gitlab_plans?plan=free") stub_full_request("#{EE::SUBSCRIPTIONS_URL}/gitlab_plans?plan=free&namespace_id=#{namespace_id}")
.to_return(status: 200, body: plans_data.to_json) .to_return(status: 200, body: plans_data.to_json)
group.add_owner(user) group.add_owner(user)
...@@ -28,6 +28,8 @@ RSpec.describe 'Show trial banner', :js do ...@@ -28,6 +28,8 @@ RSpec.describe 'Show trial banner', :js do
end end
context "when user's trial is active" do context "when user's trial is active" do
let(:namespace_id) { user.namespace_id }
it 'renders congratulations banner for user in profile billing page' do it 'renders congratulations banner for user in profile billing page' do
visit profile_billings_path + '?trial=true' visit profile_billings_path + '?trial=true'
...@@ -36,6 +38,8 @@ RSpec.describe 'Show trial banner', :js do ...@@ -36,6 +38,8 @@ RSpec.describe 'Show trial banner', :js do
end end
context "when group's trial is active" do context "when group's trial is active" do
let(:namespace_id) { group.id }
it 'renders congratulations banner for group in group details page' do it 'renders congratulations banner for group in group details page' do
visit group_path(group, trial: true) visit group_path(group, trial: true)
......
...@@ -4,20 +4,60 @@ require 'spec_helper' ...@@ -4,20 +4,60 @@ require 'spec_helper'
RSpec.describe FetchSubscriptionPlansService do RSpec.describe FetchSubscriptionPlansService do
describe '#execute' do describe '#execute' do
let(:endpoint_url) { "#{EE::SUBSCRIPTIONS_URL}/gitlab_plans" } subject(:execute_service) { described_class.new(plan: plan).execute }
subject { described_class.new(plan: 'bronze').execute } let(:endpoint_url) { "#{EE::SUBSCRIPTIONS_URL}/gitlab_plans" }
let(:plan) { 'bronze' }
let(:response_mock) { double(body: [{ 'foo' => 'bar' }].to_json) }
context 'when successully fetching plans data' do context 'when successully fetching plans data' do
it 'returns parsed JSON' do it 'returns parsed JSON' do
json_mock = double(body: [{ 'foo' => 'bar' }].to_json)
expect(Gitlab::HTTP).to receive(:get) expect(Gitlab::HTTP).to receive(:get)
.with(endpoint_url, allow_local_requests: true, query: { plan: 'bronze' }, headers: { 'Accept' => 'application/json' }) .with(
.and_return(json_mock) endpoint_url,
allow_local_requests: true,
query: { plan: plan, namespace_id: nil },
headers: { 'Accept' => 'application/json' }
)
.and_return(response_mock)
is_expected.to eq([Hashie::Mash.new('foo' => 'bar')]) is_expected.to eq([Hashie::Mash.new('foo' => 'bar')])
end end
it 'uses only the plan within the cache key name' do
allow(Gitlab::HTTP).to receive(:get).and_return(response_mock)
expect(Rails.cache).to receive(:read).with("subscription-plan-#{plan}")
execute_service
end
context 'with given namespace_id' do
subject(:execute_service) { described_class.new(plan: plan, namespace_id: namespace_id).execute }
let(:namespace_id) { 87 }
it 'returns parsed JSON' do
expect(Gitlab::HTTP).to receive(:get)
.with(
endpoint_url,
allow_local_requests: true,
query: { plan: plan, namespace_id: namespace_id },
headers: { 'Accept' => 'application/json' }
)
.and_return(response_mock)
is_expected.to eq([Hashie::Mash.new('foo' => 'bar')])
end
it 'uses the namespace id within the cache key name' do
allow(Gitlab::HTTP).to receive(:get).and_return(response_mock)
expect(Rails.cache).to receive(:read).with("subscription-plan-#{plan}-#{namespace_id}")
execute_service
end
end
end end
context 'when failing to fetch plans data' do context 'when failing to fetch plans data' do
...@@ -28,7 +68,7 @@ RSpec.describe FetchSubscriptionPlansService do ...@@ -28,7 +68,7 @@ RSpec.describe FetchSubscriptionPlansService do
it 'logs failure' do it 'logs failure' do
expect(Gitlab::AppLogger).to receive(:info).with('Unable to connect to GitLab Customers App Error message') expect(Gitlab::AppLogger).to receive(:info).with('Unable to connect to GitLab Customers App Error message')
subject execute_service
end end
it 'returns nil' do it 'returns nil' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment