Commit 44a2410e authored by Jarka Košanová's avatar Jarka Košanová

Merge branch '2411_send_subscription_id_to_retrieve_plans' into 'master'

Send namespace id in subscription plan request

See merge request gitlab-org/gitlab!50939
parents d6ec8b7b dbecccbe
......@@ -15,8 +15,9 @@ class Groups::BillingsController < Groups::ApplicationController
def index
@top_most_group = @group.root_ancestor if @group.has_parent?
current_plan = (@top_most_group || @group).plan_name_for_upgrading
@plans_data = FetchSubscriptionPlansService.new(plan: current_plan).execute
relevant_group = (@top_most_group || @group)
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')
record_experiment_user(:contact_sales_btn_in_app)
end
......
......@@ -7,7 +7,7 @@ class Profiles::BillingsController < Profiles::ApplicationController
def index
@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
track_experiment_event(:contact_sales_btn_in_app, 'page_view:billing_plans:profile')
record_experiment_user(:contact_sales_btn_in_app)
......
......@@ -3,8 +3,9 @@
class FetchSubscriptionPlansService
URL = "#{EE::SUBSCRIPTIONS_URL}/gitlab_plans".freeze
def initialize(plan:)
def initialize(plan:, namespace_id: nil)
@plan = plan
@namespace_id = namespace_id
end
def execute
......@@ -14,10 +15,12 @@ class FetchSubscriptionPlansService
private
def send_request
response = Gitlab::HTTP.get(URL,
response = Gitlab::HTTP.get(
URL,
allow_local_requests: true,
query: { plan: @plan },
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) }
rescue => e
......@@ -39,6 +42,10 @@ class FetchSubscriptionPlansService
end
def cache_key
if @namespace_id.present?
"subscription-plan-#{@plan}-#{@namespace_id}"
else
"subscription-plan-#{@plan}"
end
end
end
......@@ -20,7 +20,7 @@ RSpec.describe 'Billing plan pages', :feature do
before do
stub_feature_flags(hide_deprecated_billing_plans: false)
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)
stub_application_setting(check_namespace_plan: true)
allow(Gitlab).to receive(:com?) { true }
......@@ -371,7 +371,7 @@ RSpec.describe 'Billing plan pages', :feature do
let!(:group_member) { create(:group_member, :owner, group: namespace, user: user) }
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)
end
......
......@@ -18,7 +18,7 @@ RSpec.describe 'Groups > Billing', :js do
end
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' })
.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
before do
group.add_owner(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: {})
visit edit_subscriptions_group_path(group.path, params)
......
......@@ -17,7 +17,7 @@ RSpec.describe 'Show trial banner', :js do
before do
stub_application_setting(check_namespace_plan: true)
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)
group.add_owner(user)
......@@ -28,6 +28,8 @@ RSpec.describe 'Show trial banner', :js do
end
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
visit profile_billings_path + '?trial=true'
......@@ -36,6 +38,8 @@ RSpec.describe 'Show trial banner', :js do
end
context "when group's trial is active" do
let(:namespace_id) { group.id }
it 'renders congratulations banner for group in group details page' do
visit group_path(group, trial: true)
......
......@@ -4,20 +4,60 @@ require 'spec_helper'
RSpec.describe FetchSubscriptionPlansService 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
it 'returns parsed JSON' do
json_mock = double(body: [{ 'foo' => 'bar' }].to_json)
expect(Gitlab::HTTP).to receive(:get)
.with(
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')])
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: 'bronze' }, headers: { 'Accept' => 'application/json' })
.and_return(json_mock)
.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
context 'when failing to fetch plans data' do
......@@ -28,7 +68,7 @@ RSpec.describe FetchSubscriptionPlansService do
it 'logs failure' do
expect(Gitlab::AppLogger).to receive(:info).with('Unable to connect to GitLab Customers App Error message')
subject
execute_service
end
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