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
private
def client
Gitlab::SubscriptionPortal::Client.new
Gitlab::SubscriptionPortal::Client
end
end
end
......@@ -15,7 +15,7 @@ module GitlabSubscriptions
private
def client
Gitlab::SubscriptionPortal::Client.new
Gitlab::SubscriptionPortal::Client
end
end
end
......@@ -5,5 +5,7 @@ module EE
SUBSCRIPTIONS_COMPARISON_URL = "https://about.gitlab.com/pricing/gitlab-com/feature-comparison".freeze
SUBSCRIPTIONS_MORE_MINUTES_URL = "#{SUBSCRIPTIONS_URL}/buy_pipeline_minutes".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
end
......@@ -3,39 +3,85 @@
module Gitlab
module SubscriptionPortal
class Client
def generate_trial(params)
response = Gitlab::HTTP.post("#{base_url}/trials", body: params.to_json, headers: headers)
class << self
def generate_trial(params)
http_post("trials", admin_headers, params)
end
parse_response(response)
rescue *Gitlab::HTTP::HTTP_ERRORS => e
{ success: false, data: { errors: e.message } }
end
def create_customer(params)
http_post("api/customers", admin_headers, params)
end
private
def create_subscription(params, email, token)
http_post("subscriptions", customer_headers(email, token), params)
end
def base_url
EE::SUBSCRIPTIONS_URL
end
def payment_form_params(payment_type)
http_get("payment_forms/#{payment_type}", admin_headers)
end
def headers
{
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'X-Admin-Email' => ENV['SUBSCRIPTION_PORTAL_ADMIN_EMAIL'],
'X-Admin-Token' => ENV['SUBSCRIPTION_PORTAL_ADMIN_TOKEN']
}
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)
rescue *Gitlab::HTTP::HTTP_ERRORS => e
{ success: false, data: { errors: e.message } }
end
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
EE::SUBSCRIPTIONS_URL
end
def json_headers
{
'Accept' => 'application/json',
'Content-Type' => 'application/json'
}
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)
parsed_response = http_response.parsed_response
def parse_response(http_response)
parsed_response = http_response.parsed_response
case http_response.response
when Net::HTTPSuccess
{ success: true, data: parsed_response }
when Net::HTTPUnprocessableEntity
{ success: false, data: { errors: parsed_response['errors'] } }
else
{ success: false, data: { errors: "HTTP status code: #{http_response.code}" } }
case http_response.response
when Net::HTTPSuccess
{ success: true, data: parsed_response }
when Net::HTTPUnprocessableEntity
{ success: false, data: { errors: parsed_response['errors'] } }
else
{ success: false, data: { errors: "HTTP status code: #{http_response.code}" } }
end
end
end
end
......
......@@ -3,44 +3,93 @@
require 'spec_helper'
describe Gitlab::SubscriptionPortal::Client do
describe '#create_trial_account' do
let(:http_response) { nil }
let(:httparty_response) do
double(code: http_response.code, response: http_response, body: {}, parsed_response: {})
let(:http_response) { nil }
let(:httparty_response) do
double(code: http_response.code, response: http_response, body: {}, parsed_response: {})
end
let(:http_method) { :post }
shared_examples 'when response is successful' do
let(:http_response) { Net::HTTPSuccess.new(1.0, '201', 'OK') }
it 'has a successful status' do
allow(Gitlab::HTTP).to receive(http_method).and_return(httparty_response)
expect(subject[:success]).to eq(true)
end
end
subject do
described_class.new.generate_trial({})
shared_examples 'when response code is 422' do
let(:http_response) { Net::HTTPUnprocessableEntity.new(1.0, '422', 'Error') }
it 'has a unprocessable entity status' do
allow(Gitlab::HTTP).to receive(http_method).and_return(httparty_response)
expect(subject[:success]).to eq(false)
end
end
shared_examples 'when response code is 500' do
let(:http_response) { Net::HTTPServerError.new(1.0, '500', 'Error') }
it 'has a server error status' do
allow(Gitlab::HTTP).to receive(http_method).and_return(httparty_response)
expect(subject[:success]).to eq(false)
end
end
context 'when response is successful' do
let(:http_response) { Net::HTTPSuccess.new(1.0, '201', 'OK') }
describe '#create_trial_account' do
subject do
described_class.generate_trial({})
end
it 'has a successful status' do
allow(Gitlab::HTTP).to receive(:post).and_return(httparty_response)
it_behaves_like 'when response is successful'
it_behaves_like 'when response code is 422'
it_behaves_like 'when response code is 500'
end
expect(subject[:success]).to eq(true)
end
describe '#create_subscription' do
subject do
described_class.create_subscription({}, 'customer@mail.com', 'token')
end
context 'when response code is 422' do
let(:http_response) { Net::HTTPUnprocessableEntity.new(1.0, '422', 'Error') }
it_behaves_like 'when response is successful'
it_behaves_like 'when response code is 422'
it_behaves_like 'when response code is 500'
end
it 'has a unprocessable entity status' do
allow(Gitlab::HTTP).to receive(:post).and_return(httparty_response)
describe '#create_customer' do
subject do
described_class.create_customer({})
end
expect(subject[:success]).to eq(false)
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
context 'when response code is 500' do
let(:http_response) { Net::HTTPServerError.new(1.0, '500', 'Error') }
let(:http_method) { :get }
it 'has a server error status' do
allow(Gitlab::HTTP).to receive(:post).and_return(httparty_response)
it_behaves_like 'when response is successful'
it_behaves_like 'when response code is 422'
it_behaves_like 'when response code is 500'
end
expect(subject[:success]).to eq(false)
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
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