Commit a6894233 authored by nicolasdular's avatar nicolasdular

Add api endpoints to subscription portal client

Adds more endpoints to our Subscription Portal Client. It's now possible
to create a customer, a subscription, receive payment form params and
get information about a payment method.
parent cb759668
...@@ -15,7 +15,7 @@ module GitlabSubscriptions ...@@ -15,7 +15,7 @@ module GitlabSubscriptions
private private
def client def client
Gitlab::SubscriptionPortal::Client.new Gitlab::SubscriptionPortal::Client
end end
end end
end end
...@@ -15,7 +15,7 @@ module GitlabSubscriptions ...@@ -15,7 +15,7 @@ module GitlabSubscriptions
private private
def client def client
Gitlab::SubscriptionPortal::Client.new Gitlab::SubscriptionPortal::Client
end end
end end
end end
...@@ -5,5 +5,7 @@ module EE ...@@ -5,5 +5,7 @@ module EE
SUBSCRIPTIONS_COMPARISON_URL = "https://about.gitlab.com/pricing/gitlab-com/feature-comparison".freeze SUBSCRIPTIONS_COMPARISON_URL = "https://about.gitlab.com/pricing/gitlab-com/feature-comparison".freeze
SUBSCRIPTIONS_MORE_MINUTES_URL = "#{SUBSCRIPTIONS_URL}/buy_pipeline_minutes".freeze SUBSCRIPTIONS_MORE_MINUTES_URL = "#{SUBSCRIPTIONS_URL}/buy_pipeline_minutes".freeze
SUBSCRIPTIONS_PLANS_URL = "#{SUBSCRIPTIONS_URL}/plans".freeze SUBSCRIPTIONS_PLANS_URL = "#{SUBSCRIPTIONS_URL}/plans".freeze
SUBSCRIPTION_PORTAL_ADMIN_EMAIL = ENV.fetch('SUBSCRIPTION_PORTAL_ADMIN_EMAIL', 'gl_com_api@gitlab.com')
SUBSCRIPTION_PORTAL_ADMIN_TOKEN = ENV.fetch('SUBSCRIPTION_PORTAL_ADMIN_TOKEN', 'customer_admin_token')
CUSTOMER_SUPPORT_URL = 'https://support.gitlab.com'.freeze CUSTOMER_SUPPORT_URL = 'https://support.gitlab.com'.freeze
end end
...@@ -3,29 +3,74 @@ ...@@ -3,29 +3,74 @@
module Gitlab module Gitlab
module SubscriptionPortal module SubscriptionPortal
class Client class Client
class << self
def generate_trial(params) def generate_trial(params)
response = Gitlab::HTTP.post("#{base_url}/trials", body: params.to_json, headers: headers) http_post("trials", admin_headers, params)
end
def create_customer(params)
http_post("api/customers", admin_headers, params)
end
def create_subscription(params, email, token)
http_post("subscriptions", customer_headers(email, token), params)
end
def payment_form_params(payment_type)
http_get("payment_forms/#{payment_type}", admin_headers)
end
def payment_method(id)
http_get("api/payment_methods/#{id}", admin_headers)
end
private
def http_get(path, headers)
response = Gitlab::HTTP.get("#{base_url}/#{path}", headers: headers)
parse_response(response) parse_response(response)
rescue *Gitlab::HTTP::HTTP_ERRORS => e rescue *Gitlab::HTTP::HTTP_ERRORS => e
{ success: false, data: { errors: e.message } } { success: false, data: { errors: e.message } }
end end
private def http_post(path, headers, params = {})
response = Gitlab::HTTP.post("#{base_url}/#{path}", body: params.to_json, headers: headers)
parse_response(response)
rescue *Gitlab::HTTP::HTTP_ERRORS => e
{ success: false, data: { errors: e.message } }
end
def base_url def base_url
EE::SUBSCRIPTIONS_URL EE::SUBSCRIPTIONS_URL
end end
def headers def json_headers
{ {
'Accept' => 'application/json', 'Accept' => 'application/json',
'Content-Type' => 'application/json', 'Content-Type' => 'application/json'
'X-Admin-Email' => ENV['SUBSCRIPTION_PORTAL_ADMIN_EMAIL'],
'X-Admin-Token' => ENV['SUBSCRIPTION_PORTAL_ADMIN_TOKEN']
} }
end end
def admin_headers
json_headers.merge(
{
'X-Admin-Email' => EE::SUBSCRIPTION_PORTAL_ADMIN_EMAIL,
'X-Admin-Token' => EE::SUBSCRIPTION_PORTAL_ADMIN_TOKEN
}
)
end
def customer_headers(email, token)
json_headers.merge(
{
'X-Customer-Email' => email,
'X-Customer-Token' => token
}
)
end
def parse_response(http_response) def parse_response(http_response)
parsed_response = http_response.parsed_response parsed_response = http_response.parsed_response
...@@ -40,4 +85,5 @@ module Gitlab ...@@ -40,4 +85,5 @@ module Gitlab
end end
end end
end end
end
end end
...@@ -3,44 +3,93 @@ ...@@ -3,44 +3,93 @@
require 'spec_helper' require 'spec_helper'
describe Gitlab::SubscriptionPortal::Client do describe Gitlab::SubscriptionPortal::Client do
describe '#create_trial_account' do
let(:http_response) { nil } let(:http_response) { nil }
let(:httparty_response) do let(:httparty_response) do
double(code: http_response.code, response: http_response, body: {}, parsed_response: {}) double(code: http_response.code, response: http_response, body: {}, parsed_response: {})
end end
let(:http_method) { :post }
subject do shared_examples 'when response is successful' do
described_class.new.generate_trial({})
end
context 'when response is successful' do
let(:http_response) { Net::HTTPSuccess.new(1.0, '201', 'OK') } let(:http_response) { Net::HTTPSuccess.new(1.0, '201', 'OK') }
it 'has a successful status' do it 'has a successful status' do
allow(Gitlab::HTTP).to receive(:post).and_return(httparty_response) allow(Gitlab::HTTP).to receive(http_method).and_return(httparty_response)
expect(subject[:success]).to eq(true) expect(subject[:success]).to eq(true)
end end
end end
context 'when response code is 422' do shared_examples 'when response code is 422' do
let(:http_response) { Net::HTTPUnprocessableEntity.new(1.0, '422', 'Error') } let(:http_response) { Net::HTTPUnprocessableEntity.new(1.0, '422', 'Error') }
it 'has a unprocessable entity status' do it 'has a unprocessable entity status' do
allow(Gitlab::HTTP).to receive(:post).and_return(httparty_response) allow(Gitlab::HTTP).to receive(http_method).and_return(httparty_response)
expect(subject[:success]).to eq(false) expect(subject[:success]).to eq(false)
end end
end end
context 'when response code is 500' do shared_examples 'when response code is 500' do
let(:http_response) { Net::HTTPServerError.new(1.0, '500', 'Error') } let(:http_response) { Net::HTTPServerError.new(1.0, '500', 'Error') }
it 'has a server error status' do it 'has a server error status' do
allow(Gitlab::HTTP).to receive(:post).and_return(httparty_response) allow(Gitlab::HTTP).to receive(http_method).and_return(httparty_response)
expect(subject[:success]).to eq(false) expect(subject[:success]).to eq(false)
end end
end end
describe '#create_trial_account' do
subject do
described_class.generate_trial({})
end
it_behaves_like 'when response is successful'
it_behaves_like 'when response code is 422'
it_behaves_like 'when response code is 500'
end
describe '#create_subscription' do
subject do
described_class.create_subscription({}, 'customer@mail.com', 'token')
end
it_behaves_like 'when response is successful'
it_behaves_like 'when response code is 422'
it_behaves_like 'when response code is 500'
end
describe '#create_customer' do
subject do
described_class.create_customer({})
end
it_behaves_like 'when response is successful'
it_behaves_like 'when response code is 422'
it_behaves_like 'when response code is 500'
end
describe '#payment_form_params' do
subject do
described_class.payment_form_params('cc')
end
let(:http_method) { :get }
it_behaves_like 'when response is successful'
it_behaves_like 'when response code is 422'
it_behaves_like 'when response code is 500'
end
describe '#payment_method' do
subject do
described_class.payment_method('1')
end
let(:http_method) { :get }
it_behaves_like 'when response is successful'
it_behaves_like 'when response code is 422'
it_behaves_like 'when response code is 500'
end end
end end
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