Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
heptapod
heptapod
Commits
c799de69da92
Commit
e046e4c1
authored
Dec 16, 2016
by
Rémy Coutable
Browse files
Namespace access token session key in `Import::GithubController`
Signed-off-by:
Rémy Coutable
<
remy@rymai.me
>
parent
a04632a69b31
Changes
6
Hide whitespace changes
Inline
Side-by-side
app/controllers/import/gitea_controller.rb
View file @
c799de69
class
Import::GiteaController
<
Import
::
GithubController
def
new
if
session
[
:
access_token
].
present?
&&
session
[
:
host_
url
].
present?
if
session
[
access_token
_key
].
present?
&&
session
[
host_
key
].
present?
redirect_to
status_import_url
end
end
def
personal_access_token
session
[
:
host_
url
]
=
params
[
:gitea_
host_
url
]
session
[
host_
key
]
=
params
[
host_
key
]
super
end
def
status
@gitea_host_url
=
session
[
:
host_
url
]
@gitea_host_url
=
session
[
host_
key
]
super
end
private
def
host_key
:"
#{
provider
}
_host_url"
end
# Overriden methods
def
provider
:gitea
...
...
@@ -29,13 +33,13 @@ def logged_in_with_provider?
end
def
provider_auth
if
session
[
:
access_token
].
blank?
||
session
[
:
host_
url
].
blank?
if
session
[
access_token
_key
].
blank?
||
session
[
host_
key
].
blank?
redirect_to
new_import_gitea_url
,
alert:
'You need to specify both an Access Token and a Host URL.'
end
end
def
client_options
{
host:
session
[
:
host_
url
],
api_version:
'v1'
}
{
host:
session
[
host_
key
],
api_version:
'v1'
}
end
end
app/controllers/import/github_controller.rb
View file @
c799de69
...
...
@@ -7,18 +7,18 @@ class Import::GithubController < Import::BaseController
def
new
if
logged_in_with_provider?
go_to_provider_for_permissions
elsif
session
[
:
access_token
]
elsif
session
[
access_token
_key
]
redirect_to
status_import_url
end
end
def
callback
session
[
:
access_token
]
=
client
.
get_token
(
params
[
:code
])
session
[
access_token
_key
]
=
client
.
get_token
(
params
[
:code
])
redirect_to
status_import_url
end
def
personal_access_token
session
[
:
access_token
]
=
params
[
:personal_access_token
]
session
[
access_token
_key
]
=
params
[
:personal_access_token
]
redirect_to
status_import_url
end
...
...
@@ -52,7 +52,7 @@ def create
private
def
client
@client
||=
Gitlab
::
GithubImport
::
Client
.
new
(
session
[
:
access_token
],
client_options
)
@client
||=
Gitlab
::
GithubImport
::
Client
.
new
(
session
[
access_token
_key
],
client_options
)
end
def
verify_import_enabled
...
...
@@ -80,13 +80,17 @@ def callback_import_url
end
def
provider_unauthorized
session
[
:
access_token
]
=
nil
session
[
access_token
_key
]
=
nil
redirect_to
new_import_url
,
alert:
"Access denied to your
#{
Gitlab
::
ImportSources
.
title
(
provider
.
to_s
)
}
account."
end
def
access_token_key
:"
#{
provider
}
_access_token"
end
def
access_params
{
github_access_token:
session
[
:
access_token
]
}
{
github_access_token:
session
[
access_token
_key
]
}
end
# The following methods are overriden in subclasses
...
...
@@ -99,7 +103,7 @@ def logged_in_with_provider?
end
def
provider_auth
if
session
[
:
access_token
].
blank?
if
session
[
access_token
_key
].
blank?
go_to_provider_for_permissions
end
end
...
...
spec/controllers/import/gitea_controller_spec.rb
View file @
c799de69
...
...
@@ -9,7 +9,7 @@
include_context
'a GitHub-ish import controller'
def
assign_host_url
session
[
:host_url
]
=
host_url
session
[
:
gitea_
host_url
]
=
host_url
end
describe
"GET new"
do
...
...
spec/controllers/import/github_controller_spec.rb
View file @
c799de69
...
...
@@ -29,7 +29,7 @@
get
:callback
expect
(
session
[
:access_token
]).
to
eq
(
token
)
expect
(
session
[
:
github_
access_token
]).
to
eq
(
token
)
expect
(
controller
).
to
redirect_to
(
status_import_github_url
)
end
end
...
...
spec/support/controllers/githubish_import_controller_shared_examples.rb
View file @
c799de69
...
...
@@ -4,6 +4,10 @@
# Note: You have access to `email_value` which is the email address value
# being currently tested).
def
assign_session_token
(
provider
)
session
[
:"
#{
provider
}
_access_token"
]
=
'asdasd12345'
end
shared_examples
'a GitHub-ish import controller: POST personal_access_token'
do
let
(
:status_import_url
)
{
public_send
(
"status_import_
#{
provider
}
_url"
)
}
...
...
@@ -15,7 +19,7 @@
post
:personal_access_token
,
personal_access_token:
token
expect
(
session
[
:access_token
]).
to
eq
(
token
)
expect
(
session
[
:
"
#{
provider
}
_
access_token
"
]).
to
eq
(
token
)
expect
(
controller
).
to
redirect_to
(
status_import_url
)
end
end
...
...
@@ -24,7 +28,7 @@
let
(
:status_import_url
)
{
public_send
(
"status_import_
#{
provider
}
_url"
)
}
it
"redirects to status if we already have a token"
do
assign_session_token
assign_session_token
(
provider
)
allow
(
controller
).
to
receive
(
:logged_in_with_provider?
).
and_return
(
false
)
get
:new
...
...
@@ -48,7 +52,7 @@
let
(
:extra_assign_expectations
)
{
{}
}
before
do
assign_session_token
assign_session_token
(
provider
)
end
it
"assigns variables"
do
...
...
@@ -80,7 +84,7 @@
get
:status
expect
(
session
[
:access_token
]).
to
eq
(
nil
)
expect
(
session
[
:
"
#{
provider
}
_
access_token
"
]).
to
be_
nil
expect
(
controller
).
to
redirect_to
(
new_import_url
)
expect
(
flash
[
:alert
]).
to
eq
(
"Access denied to your
#{
Gitlab
::
ImportSources
.
title
(
provider
.
to_s
)
}
account."
)
end
...
...
@@ -100,11 +104,11 @@
before
do
stub_client
(
user:
provider_user
,
repo:
provider_repo
)
assign_session_token
assign_session_token
(
provider
)
end
context
"when the repository owner is the
Gitea
user"
do
context
"when the
Gitea
user and GitLab user's usernames match"
do
context
"when the repository owner is the
provider
user"
do
context
"when the
provider
user and GitLab user's usernames match"
do
it
"takes the current user's namespace"
do
expect
(
Gitlab
::
GithubImport
::
ProjectCreator
).
to
receive
(
:new
).
with
(
provider_repo
,
provider_repo
.
name
,
user
.
namespace
,
user
,
access_params
,
type:
provider
).
...
...
@@ -114,7 +118,7 @@
end
end
context
"when the
Gitea
user and GitLab user's usernames don't match"
do
context
"when the
provider
user and GitLab user's usernames don't match"
do
let
(
:provider_username
)
{
"someone_else"
}
it
"takes the current user's namespace"
do
...
...
@@ -127,15 +131,15 @@
end
end
context
"when the repository owner is not the
Gitea
user"
do
context
"when the repository owner is not the
provider
user"
do
let
(
:other_username
)
{
"someone_else"
}
before
do
provider_repo
.
owner
=
OpenStruct
.
new
(
login:
other_username
)
assign_session_token
assign_session_token
(
provider
)
end
context
"when a namespace with the
Gitea
user's username already exists"
do
context
"when a namespace with the
provider
user's username already exists"
do
let!
(
:existing_namespace
)
{
create
(
:namespace
,
name:
other_username
,
owner:
user
)
}
context
"when the namespace is owned by the GitLab user"
do
...
...
@@ -164,7 +168,7 @@
end
end
context
"when a namespace with the
Gitea
user's username doesn't exist"
do
context
"when a namespace with the
provider
user's username doesn't exist"
do
context
"when current user can create namespaces"
do
it
"creates the namespace"
do
expect
(
Gitlab
::
GithubImport
::
ProjectCreator
).
...
...
spec/support/import_spec_helper.rb
View file @
c799de69
...
...
@@ -30,8 +30,4 @@ def stub_omniauth_provider(name)
)
allow
(
Gitlab
.
config
.
omniauth
).
to
receive
(
:providers
).
and_return
([
provider
])
end
def
assign_session_token
session
[
:access_token
]
=
'asdasd12345'
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