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
349faff3
Commit
349faff3
authored
Aug 17, 2020
by
Giorgenes Gelatti
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor http helpers in specs
parent
6abf565a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
55 deletions
+46
-55
lib/api/helpers/packages_manager_clients_helpers.rb
lib/api/helpers/packages_manager_clients_helpers.rb
+3
-3
spec/lib/api/helpers/packages_manager_clients_helpers_spec.rb
.../lib/api/helpers/packages_manager_clients_helpers_spec.rb
+43
-52
No files found.
lib/api/helpers/packages_manager_clients_helpers.rb
View file @
349faff3
...
@@ -17,7 +17,7 @@ module API
...
@@ -17,7 +17,7 @@ module API
end
end
def
find_job_from_http_basic_auth
def
find_job_from_http_basic_auth
return
unless
headers
return
unless
request
.
headers
token
=
decode_token
token
=
decode_token
...
@@ -27,7 +27,7 @@ module API
...
@@ -27,7 +27,7 @@ module API
end
end
def
find_deploy_token_from_http_basic_auth
def
find_deploy_token_from_http_basic_auth
return
unless
headers
return
unless
request
.
headers
token
=
decode_token
token
=
decode_token
...
@@ -45,7 +45,7 @@ module API
...
@@ -45,7 +45,7 @@ module API
private
private
def
decode_token
def
decode_token
encoded_credentials
=
headers
[
'Authorization'
].
to_s
.
split
(
'Basic '
,
2
).
second
encoded_credentials
=
request
.
headers
[
'Authorization'
].
to_s
.
split
(
'Basic '
,
2
).
second
Base64
.
decode64
(
encoded_credentials
||
''
).
split
(
':'
,
2
).
second
Base64
.
decode64
(
encoded_credentials
||
''
).
split
(
':'
,
2
).
second
end
end
end
end
...
...
spec/lib/api/helpers/packages_manager_clients_helpers_spec.rb
View file @
349faff3
...
@@ -3,83 +3,78 @@
...
@@ -3,83 +3,78 @@
require
'spec_helper'
require
'spec_helper'
RSpec
.
describe
API
::
Helpers
::
PackagesManagerClientsHelpers
do
RSpec
.
describe
API
::
Helpers
::
PackagesManagerClientsHelpers
do
include
HttpBasicAuthHelpers
let_it_be
(
:personal_access_token
)
{
create
(
:personal_access_token
)
}
let_it_be
(
:personal_access_token
)
{
create
(
:personal_access_token
)
}
let_it_be
(
:username
)
{
personal_access_token
.
user
.
username
}
let_it_be
(
:username
)
{
personal_access_token
.
user
.
username
}
let_it_be
(
:helper
)
{
Class
.
new
.
include
(
described_class
).
new
}
let_it_be
(
:helper
)
{
Class
.
new
.
include
(
described_class
).
new
}
let
(
:password
)
{
personal_access_token
.
token
}
let
(
:password
)
{
personal_access_token
.
token
}
describe
'#find_job_from_http_basic_auth'
do
let
(
:env
)
do
let_it_be
(
:user
)
{
personal_access_token
.
user
}
{
'rack.input'
=>
''
}
end
let
(
:job
)
{
create
(
:ci_build
,
user:
user
)
}
let
(
:request
)
{
ActionDispatch
::
Request
.
new
(
env
)
}
let
(
:password
)
{
job
.
token
}
let
(
:headers
)
{
{
Authorization
:
basic_http_auth
(
username
,
password
)
}
}
subject
{
helper
.
find_job_from_http_basic_auth
}
before
do
allow
(
helper
).
to
receive
(
:request
).
and_return
(
request
)
end
before
do
shared_examples
'invalid auth header'
do
allow
(
helper
).
to
receive
(
:headers
).
and_return
(
headers
&
.
with_indifferent_access
)
context
'with an invalid Authorization header'
do
end
before
do
env
.
merge!
(
build_auth_headers
(
'Invalid'
))
end
context
'with a valid Authorization header'
do
it
{
is_expected
.
to
be
nil
}
it
{
is_expected
.
to
eq
job
}
end
end
end
context
'with an invalid Authorization header'
do
shared_examples
'valid auth header'
do
where
(
:headers
)
do
context
'with a valid Authorization header'
do
[
before
do
[{
Authorization
:
'Invalid'
}],
env
.
merge!
(
basic_auth_header
(
username
,
password
))
[{}],
[
nil
]
]
end
end
with_them
do
context
'with an unknown password'
do
let
(
:password
)
{
'Unknown'
}
it
{
is_expected
.
to
be
nil
}
it
{
is_expected
.
to
be
nil
}
end
end
it
{
is_expected
.
to
eq
expected_result
}
end
end
end
describe
'#find_job_from_http_basic_auth'
do
let_it_be
(
:user
)
{
personal_access_token
.
user
}
let
(
:job
)
{
create
(
:ci_build
,
user:
user
)
}
let
(
:password
)
{
job
.
token
}
context
'with an unknown Authorization header'
do
subject
{
helper
.
find_job_from_http_basic_auth
}
let
(
:password
)
{
'Unknown'
}
it
{
is_expected
.
to
be
nil
}
it_behaves_like
'valid auth header'
do
let
(
:expected_result
)
{
job
}
end
end
it_behaves_like
'invalid auth header'
end
end
describe
'#find_deploy_token_from_http_basic_auth'
do
describe
'#find_deploy_token_from_http_basic_auth'
do
let_it_be
(
:deploy_token
)
{
create
(
:deploy_token
)
}
let_it_be
(
:deploy_token
)
{
create
(
:deploy_token
)
}
let
(
:token
)
{
deploy_token
.
token
}
let
(
:token
)
{
deploy_token
.
token
}
let
(
:headers
)
{
{
Authorization
:
basic_http_auth
(
deploy_token
.
username
,
token
)
}
}
let
(
:username
)
{
deploy_token
.
username
}
let
(
:password
)
{
token
}
subject
{
helper
.
find_deploy_token_from_http_basic_auth
}
subject
{
helper
.
find_deploy_token_from_http_basic_auth
}
before
do
it_behaves_like
'valid auth header'
do
allow
(
helper
).
to
receive
(
:headers
).
and_return
(
headers
&
.
with_indifferent_access
)
let
(
:expected_result
)
{
deploy_token
}
end
context
'with a valid Authorization header'
do
it
{
is_expected
.
to
eq
deploy_token
}
end
end
context
'with an invalid Authorization header'
do
it_behaves_like
'invalid auth header'
where
(
:headers
)
do
[
[{
Authorization
:
'Invalid'
}],
[{}],
[
nil
]
]
end
with_them
do
it
{
is_expected
.
to
be
nil
}
end
end
context
'with an invalid token'
do
let
(
:token
)
{
'Unknown'
}
it
{
is_expected
.
to
be
nil
}
end
end
end
describe
'#uploaded_package_file'
do
describe
'#uploaded_package_file'
do
...
@@ -113,8 +108,4 @@ RSpec.describe API::Helpers::PackagesManagerClientsHelpers do
...
@@ -113,8 +108,4 @@ RSpec.describe API::Helpers::PackagesManagerClientsHelpers do
end
end
end
end
end
end
def
basic_http_auth
(
username
,
password
)
ActionController
::
HttpAuthentication
::
Basic
.
encode_credentials
(
username
,
password
)
end
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