Commit 7af509f1 authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch 'add-api-client-for-subscription-portal' into 'master'

[Backend]Add api client for subscription portal

See merge request gitlab-org/gitlab-ee!16156
parents f1c6f0bb b6a256a4
......@@ -3,7 +3,19 @@
module GitlabSubscriptions
class CreateLeadService
def execute(company_params)
{ success: true }
response = client.generate_trial(company_params)
if response[:success]
{ success: true }
else
{ success: false, errors: response.dig(:data, :errors) }
end
end
private
def client
Gitlab::SubscriptionPortal::Client.new
end
end
end
# frozen_string_literal: true
module Gitlab
module SubscriptionPortal
class Client
def generate_trial(params)
response = Gitlab::HTTP.post("#{base_url}/trials", body: params.to_json, headers: headers)
parse_response(response)
rescue *Gitlab::HTTP::HTTP_ERRORS => e
{ success: false, data: { errors: e.message } }
end
private
def base_url
EE::SUBSCRIPTIONS_URL
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 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}" } }
end
end
end
end
end
# frozen_string_literal: true
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: {})
end
subject do
described_class.new.generate_trial({})
end
context '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(:post).and_return(httparty_response)
expect(subject[:success]).to eq(true)
end
end
context '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(:post).and_return(httparty_response)
expect(subject[:success]).to eq(false)
end
end
context '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(:post).and_return(httparty_response)
expect(subject[:success]).to eq(false)
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