Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
21b2cb01
Commit
21b2cb01
authored
Feb 17, 2021
by
Mark Chao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cloud License: Activation to persist license key
Allow GraphQL to use variables param. Drop authentication token
parent
77a98f10
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
68 additions
and
46 deletions
+68
-46
ee/app/services/gitlab_subscriptions/activate_service.rb
ee/app/services/gitlab_subscriptions/activate_service.rb
+5
-3
ee/lib/gitlab/subscription_portal/clients/graphql.rb
ee/lib/gitlab/subscription_portal/clients/graphql.rb
+26
-10
ee/spec/lib/gitlab/subscription_portal/clients/graphql_spec.rb
...ec/lib/gitlab/subscription_portal/clients/graphql_spec.rb
+4
-4
ee/spec/requests/api/graphql/mutations/gitlab_subscriptions/activate_spec.rb
...i/graphql/mutations/gitlab_subscriptions/activate_spec.rb
+18
-11
ee/spec/services/gitlab_subscriptions/activate_service_spec.rb
...ec/services/gitlab_subscriptions/activate_service_spec.rb
+15
-18
No files found.
ee/app/services/gitlab_subscriptions/activate_service.rb
View file @
21b2cb01
...
...
@@ -17,10 +17,12 @@ module GitlabSubscriptions
return
response
unless
response
[
:success
]
if
application_settings
.
update
(
cloud_license_auth_token:
response
[
:authentication_token
])
response
license
=
License
.
new
(
data:
response
[
:license_key
])
if
license
.
save
{
success:
true
}
else
error
(
application_settings
.
errors
.
full_messages
)
error
(
license
.
errors
.
full_messages
)
end
rescue
=>
e
error
(
e
.
message
)
...
...
ee/lib/gitlab/subscription_portal/clients/graphql.rb
View file @
21b2cb01
...
...
@@ -10,19 +10,37 @@ module Gitlab
def
activate
(
activation_code
)
uuid
=
Gitlab
::
CurrentSettings
.
uuid
variables
=
{
activationCode:
activation_code
,
instanceIdentifier:
uuid
}
query
=
<<~
GQL
mutation {
cloudActivationActivate(input: { activationCode: "
#{
activation_code
}
", instanceIdentifier: "
#{
uuid
}
" }) {
authenticationToken
mutation($activationCode: String!, $instanceIdentifier: String!) {
cloudActivationActivate(
input: {
activationCode: $activationCode,
instanceIdentifier: $instanceIdentifier
}
) {
licenseKey
errors
}
}
GQL
response
=
execute_graphql_query
(
query
).
dig
(
:data
,
'data'
,
'cloudActivationActivate'
)
response
=
execute_graphql_query
(
{
query:
query
,
variables:
variables
}
)
if
!
response
[
:success
]
||
response
.
dig
(
:data
,
'errors'
).
present?
return
{
success:
false
,
errors:
response
.
dig
(
:data
,
'errors'
)
}
end
response
=
response
.
dig
(
:data
,
'data'
,
'cloudActivationActivate'
)
if
response
[
'errors'
].
blank?
{
success:
true
,
authentication_token:
response
[
'authenticationToken
'
]
}
{
success:
true
,
license_key:
response
[
'licenseKey
'
]
}
else
{
success:
false
,
errors:
response
[
'errors'
]
}
end
...
...
@@ -39,7 +57,7 @@ module Gitlab
}
GQL
response
=
execute_graphql_query
(
query
).
dig
(
:data
)
response
=
execute_graphql_query
(
{
query:
query
}
).
dig
(
:data
)
if
response
[
'errors'
].
blank?
eligible
=
response
.
dig
(
'data'
,
'subscription'
,
'eoaStarterBronzeEligible'
)
...
...
@@ -59,13 +77,11 @@ module Gitlab
private
def
execute_graphql_query
(
query
)
def
execute_graphql_query
(
params
)
response
=
::
Gitlab
::
HTTP
.
post
(
graphql_endpoint
,
headers:
admin_headers
,
body:
{
query:
query
}.
to_json
body:
params
.
to_json
)
parse_response
(
response
)
...
...
ee/spec/lib/gitlab/subscription_portal/clients/graphql_spec.rb
View file @
21b2cb01
...
...
@@ -6,7 +6,7 @@ RSpec.describe Gitlab::SubscriptionPortal::Clients::Graphql do
let
(
:client
)
{
Gitlab
::
SubscriptionPortal
::
Client
}
describe
'#activate'
do
let
(
:
authentication_token
)
{
'authentication_token
'
}
let
(
:
license_key
)
{
'license_key
'
}
it
'returns success'
do
expect
(
client
).
to
receive
(
:execute_graphql_query
).
and_return
(
...
...
@@ -15,7 +15,7 @@ RSpec.describe Gitlab::SubscriptionPortal::Clients::Graphql do
data:
{
"data"
=>
{
"cloudActivationActivate"
=>
{
"
authenticationToken"
=>
authentication_token
,
"
licenseKey"
=>
license_key
,
"errors"
=>
[]
}
}
...
...
@@ -25,7 +25,7 @@ RSpec.describe Gitlab::SubscriptionPortal::Clients::Graphql do
result
=
client
.
activate
(
'activation_code_abc'
)
expect
(
result
).
to
eq
({
authentication_token:
authentication_token
,
success:
true
})
expect
(
result
).
to
eq
({
license_key:
license_key
,
success:
true
})
end
it
'returns failure'
do
...
...
@@ -35,7 +35,7 @@ RSpec.describe Gitlab::SubscriptionPortal::Clients::Graphql do
data:
{
"data"
=>
{
"cloudActivationActivate"
=>
{
"
authenticationToken
"
=>
nil
,
"
licenseKey
"
=>
nil
,
"errors"
=>
[
"invalid activation code"
]
}
}
...
...
ee/spec/requests/api/graphql/mutations/gitlab_subscriptions/activate_spec.rb
View file @
21b2cb01
...
...
@@ -6,24 +6,24 @@ RSpec.describe 'Activate a subscription' do
include
GraphqlHelpers
let_it_be
(
:current_user
)
{
create
(
:admin
)
}
let_it_be
(
:license_key
)
{
build
(
:gitlab_license
).
export
}
let
(
:activation_code
)
{
'activation_code'
}
let
(
:mutation
)
do
graphql_mutation
(
:gitlab_subscription_activate
,
{
activation_code:
activation_code
})
end
let!
(
:application_setting
)
do
stub_env
(
'IN_MEMORY_APPLICATION_SETTINGS'
,
'false'
)
create
(
:application_setting
,
cloud_license_enabled:
true
)
end
let
(
:authentication_token
)
{
'authentication_token'
}
let
(
:mutation
)
do
graphql_mutation
(
:gitlab_subscription_activate
,
{
activation_code:
'abc'
})
end
let
(
:remote_response
)
do
{
success:
true
,
data:
{
"data"
=>
{
"cloudActivationActivate"
=>
{
"
authenticationToken"
=>
authentication_token
,
"
licenseKey"
=>
license_key
,
"errors"
=>
[]
}
}
...
...
@@ -31,15 +31,22 @@ RSpec.describe 'Activate a subscription' do
}
end
before
do
allow
(
Gitlab
::
SubscriptionPortal
::
Client
).
to
receive
(
:execute_graphql_query
).
and_return
(
remote_response
)
end
it
'persists license key'
do
expect
(
Gitlab
::
SubscriptionPortal
::
Client
)
.
to
receive
(
:execute_graphql_query
)
.
with
({
query:
an_instance_of
(
String
),
variables:
{
activationCode:
activation_code
,
instanceIdentifier:
application_setting
.
uuid
}
})
.
and_return
(
remote_response
)
it
'persists authentication token'
do
post_graphql_mutation
(
mutation
,
current_user:
current_user
)
expect
(
response
).
to
have_gitlab_http_status
(
:success
)
expect
(
graphql_mutation_response
(
:gitlab_subscription_activate
)[
'errors'
]).
to
be_empty
expect
(
application_setting
.
reload
.
cloud_license_auth_token
).
to
eq
(
authentication_token
)
expect
(
License
.
last
.
data
).
to
eq
(
license_key
)
end
end
ee/spec/services/gitlab_subscriptions/activate_service_spec.rb
View file @
21b2cb01
...
...
@@ -8,14 +8,14 @@ RSpec.describe GitlabSubscriptions::ActivateService do
create
(
:application_setting
,
cloud_license_enabled:
cloud_license_enabled
)
end
let_it_be
(
:license_key
)
{
build
(
:gitlab_license
).
export
}
let
(
:cloud_license_enabled
)
{
true
}
let
(
:authentication_token
)
{
'authentication_token'
}
let
(
:activation_code
)
{
'activation_code'
}
def
stub_client_activate
expect
(
Gitlab
::
SubscriptionPortal
::
Client
).
to
receive
(
:activate
)
.
with
(
activation_code
)
.
and_return
(
response
)
.
and_return
(
customer_dot_
response
)
end
before
do
...
...
@@ -23,61 +23,58 @@ RSpec.describe GitlabSubscriptions::ActivateService do
end
context
'when CustomerDot returns success'
do
let
(
:
response
)
{
{
success:
true
,
authentication_token:
authentication_token
}
}
let
(
:
customer_dot_response
)
{
{
success:
true
,
license_key:
license_key
}
}
before
do
stub_client_activate
end
it
'persists
authentication_token
'
do
expect
(
subject
.
execute
(
activation_code
)).
to
eq
(
response
)
it
'persists
license
'
do
expect
(
subject
.
execute
(
activation_code
)).
to
eq
(
{
success:
true
}
)
expect
(
application_settings
.
reload
.
cloud_license_auth_token
).
to
eq
(
authentication_token
)
expect
(
License
.
last
.
data
).
to
eq
(
license_key
)
end
context
'when persisting fails'
do
let
(
:
response
)
{
{
success:
true
,
authentication_token:
authentication_token
}
}
let
(
:
license_key
)
{
'invalid key'
}
it
'returns error'
do
application_settings
.
errors
.
add
(
:base
,
:invalid
)
allow
(
application_settings
).
to
receive
(
:update
).
and_return
(
false
)
expect
(
subject
.
execute
(
activation_code
)).
to
eq
({
errors:
[
"is invalid"
],
success:
false
})
expect
(
subject
.
execute
(
activation_code
)).
to
match
({
errors:
[
be_a_kind_of
(
String
)],
success:
false
})
end
end
end
context
'when CustomerDot returns failure'
do
let
(
:response
)
{
{
success:
false
,
errors:
[
'foo'
]
}
}
let
(
:
customer_dot_
response
)
{
{
success:
false
,
errors:
[
'foo'
]
}
}
it
'returns error'
do
stub_client_activate
expect
(
subject
.
execute
(
activation_code
)).
to
eq
(
response
)
expect
(
subject
.
execute
(
activation_code
)).
to
eq
(
customer_dot_
response
)
expect
(
application_settings
.
reload
.
cloud_license_auth_token
).
to
be_nil
expect
(
License
.
last
&
.
data
).
not_to
eq
(
license_key
)
end
end
context
'when not self managed instance'
do
let
(
:response
)
{
{
success:
false
,
errors:
[
described_class
::
ERROR_MESSAGES
[
:not_self_managed
]]
}}
let
(
:
customer_dot_
response
)
{
{
success:
false
,
errors:
[
described_class
::
ERROR_MESSAGES
[
:not_self_managed
]]
}}
it
'returns error'
do
allow
(
Gitlab
).
to
receive
(
:com?
).
and_return
(
true
)
expect
(
Gitlab
::
SubscriptionPortal
::
Client
).
not_to
receive
(
:activate
)
expect
(
subject
.
execute
(
activation_code
)).
to
eq
(
response
)
expect
(
subject
.
execute
(
activation_code
)).
to
eq
(
customer_dot_
response
)
end
end
context
'when cloud licensing disabled'
do
let
(
:response
)
{
{
success:
false
,
errors:
[
described_class
::
ERROR_MESSAGES
[
:disabled
]]
}}
let
(
:
customer_dot_
response
)
{
{
success:
false
,
errors:
[
described_class
::
ERROR_MESSAGES
[
:disabled
]]
}}
let
(
:cloud_license_enabled
)
{
false
}
it
'returns error'
do
expect
(
Gitlab
::
SubscriptionPortal
::
Client
).
not_to
receive
(
:activate
)
expect
(
subject
.
execute
(
activation_code
)).
to
eq
(
response
)
expect
(
subject
.
execute
(
activation_code
)).
to
eq
(
customer_dot_
response
)
end
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment