Commit 585deba6 authored by Corinna Wiesner's avatar Corinna Wiesner Committed by Miguel Rincon

Return license with activation mutation

parent b3b3c59b
......@@ -3148,6 +3148,7 @@ Autogenerated return type of GitlabSubscriptionActivate.
| ----- | ---- | ----------- |
| `clientMutationId` | [`String`](#string) | A unique identifier for the client performing the mutation. |
| `errors` | [`[String!]!`](#string) | Errors encountered during execution of the mutation. |
| `license` | [`CurrentLicense`](#currentlicense) | The current license. |
### `GrafanaIntegration`
......
mutation($gitlabSubscriptionActivateInput: GitlabSubscriptionActivateInput!) {
gitlabSubscriptionActivate(input: $gitlabSubscriptionActivateInput) {
clientMutationId
errors
license {
id
type
plan
name
email
company
startsAt
expiresAt
activatedAt
lastSync
usersInLicenseCount
billableUsersCount
maximumUserCount
usersOverLicenseCount
}
}
}
......@@ -11,12 +11,16 @@ module Mutations
required: true,
description: 'Activation code received after purchasing a GitLab subscription.'
field :license, Types::Admin::CloudLicenses::CurrentLicenseType,
null: true,
description: 'The current license.'
def resolve(activation_code:)
authorize! :global
result = ::GitlabSubscriptions::ActivateService.new.execute(activation_code)
{ errors: Array(result[:errors]) }
{ errors: Array(result[:errors]), license: result[:license] }
end
end
end
......
......@@ -20,7 +20,7 @@ module GitlabSubscriptions
license = License.new(data: response[:license_key])
if license.save
{ success: true }
{ success: true, license: license }
else
error(license.errors.full_messages)
end
......
......@@ -75,7 +75,7 @@ export const activateLicenseMutationResponse = {
FAILURE_IN_DISGUISE: {
data: {
gitlabSubscriptionActivate: {
clientMutationId: null,
license: null,
errors: ["undefined method `[]' for nil:NilClass"],
__typename: 'GitlabSubscriptionActivatePayload',
},
......@@ -84,7 +84,22 @@ export const activateLicenseMutationResponse = {
SUCCESS: {
data: {
gitlabSubscriptionActivate: {
clientMutationId: null,
license: {
id: 'gid://gitlab/License/3',
type: 'legacy',
plan: 'ultimate',
name: 'Test license',
email: 'user@example.com',
company: 'Example Inc',
startsAt: '2020-01-01',
expiresAt: '2022-01-01',
activatedAt: '2021-01-02',
lastSync: null,
usersInLicenseCount: 100,
billableUsersCount: 50,
maximumUserCount: 50,
usersOverLicenseCount: 0,
},
errors: [],
},
},
......
......@@ -8,8 +8,9 @@ RSpec.describe Mutations::GitlabSubscriptions::Activate do
subject(:mutation) { described_class.new(object: nil, context: { current_user: user }, field: nil) }
let_it_be(:user) { create(:admin) }
let_it_be(:created_license) { License.last }
let(:activation_code) { 'activation_code' }
let(:result) { { success: true } }
let(:result) { { success: true, license: created_license } }
describe '#resolve' do
before do
......@@ -24,7 +25,7 @@ RSpec.describe Mutations::GitlabSubscriptions::Activate do
it 'adds the issue to the epic' do
result = mutation.resolve(activation_code: activation_code)
expect(result).to eq({ errors: [] })
expect(result).to eq({ errors: [], license: created_license })
end
end
......@@ -34,7 +35,7 @@ RSpec.describe Mutations::GitlabSubscriptions::Activate do
it 'returns errors' do
result = mutation.resolve(activation_code: activation_code)
expect(result).to eq({ errors: ['foo'] })
expect(result).to eq({ errors: ['foo'], license: nil })
end
end
......
......@@ -45,8 +45,29 @@ RSpec.describe 'Activate a subscription' do
post_graphql_mutation(mutation, current_user: current_user)
mutation_response = graphql_mutation_response(:gitlab_subscription_activate)
created_license = License.last
expect(response).to have_gitlab_http_status(:success)
expect(graphql_mutation_response(:gitlab_subscription_activate)['errors']).to be_empty
expect(License.last.data).to eq(license_key)
expect(mutation_response['errors']).to be_empty
expect(mutation_response['license']).to eq(
{
'id' => "gid://gitlab/License/#{created_license.id}",
'type' => License::LEGACY_LICENSE_TYPE,
'plan' => created_license.plan,
'name' => created_license.licensee_name,
'email' => created_license.licensee_email,
'company' => created_license.licensee_company,
'startsAt' => created_license.starts_at.to_s,
'expiresAt' => created_license.expires_at.to_s,
'activatedAt' => created_license.created_at.to_date.to_s,
'lastSync' => nil,
'usersInLicenseCount' => nil,
'billableUsersCount' => 1,
'maximumUserCount' => 1,
'usersOverLicenseCount' => 0
}
)
expect(created_license.data).to eq(license_key)
end
end
......@@ -3,6 +3,8 @@
require 'spec_helper'
RSpec.describe GitlabSubscriptions::ActivateService do
subject(:execute_service) { described_class.new.execute(activation_code) }
let!(:application_settings) do
stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
create(:application_setting, cloud_license_enabled: cloud_license_enabled)
......@@ -30,16 +32,19 @@ RSpec.describe GitlabSubscriptions::ActivateService do
end
it 'persists license' do
expect(subject.execute(activation_code)).to eq({ success: true })
result = execute_service
created_license = License.last
expect(result).to eq({ success: true, license: created_license })
expect(License.last.data).to eq(license_key)
expect(created_license.data).to eq(license_key)
end
context 'when persisting fails' do
let(:license_key) { 'invalid key' }
it 'returns error' do
expect(subject.execute(activation_code)).to match({ errors: [be_a_kind_of(String)], success: false })
expect(execute_service).to match({ errors: [be_a_kind_of(String)], success: false })
end
end
end
......@@ -50,7 +55,7 @@ RSpec.describe GitlabSubscriptions::ActivateService do
it 'returns error' do
stub_client_activate
expect(subject.execute(activation_code)).to eq(customer_dot_response)
expect(execute_service).to eq(customer_dot_response)
expect(License.last&.data).not_to eq(license_key)
end
......@@ -63,7 +68,7 @@ RSpec.describe GitlabSubscriptions::ActivateService do
allow(Gitlab).to receive(:com?).and_return(true)
expect(Gitlab::SubscriptionPortal::Client).not_to receive(:activate)
expect(subject.execute(activation_code)).to eq(customer_dot_response)
expect(execute_service).to eq(customer_dot_response)
end
end
......@@ -74,7 +79,7 @@ RSpec.describe GitlabSubscriptions::ActivateService do
it 'returns error' do
expect(Gitlab::SubscriptionPortal::Client).not_to receive(:activate)
expect(subject.execute(activation_code)).to eq(customer_dot_response)
expect(execute_service).to eq(customer_dot_response)
end
end
......@@ -82,7 +87,7 @@ RSpec.describe GitlabSubscriptions::ActivateService do
it 'captures error' do
expect(Gitlab::SubscriptionPortal::Client).to receive(:activate).and_raise('foo')
expect(subject.execute(activation_code)).to eq({ success: false, errors: ['foo'] })
expect(execute_service).to eq({ success: false, errors: ['foo'] })
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