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
0
Merge Requests
0
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
Léo-Paul Géneau
gitlab-ce
Commits
48ebfaa4
Commit
48ebfaa4
authored
Dec 16, 2016
by
Kamil Trzcinski
Committed by
Z.J. van de Weg
Dec 17, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve Mattermost Session specs
parent
9bcc4d4d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
26 deletions
+50
-26
lib/mattermost/session.rb
lib/mattermost/session.rb
+12
-11
spec/lib/mattermost/session_spec.rb
spec/lib/mattermost/session_spec.rb
+38
-15
No files found.
lib/mattermost/session.rb
View file @
48ebfaa4
...
@@ -17,7 +17,7 @@ module Mattermost
...
@@ -17,7 +17,7 @@ module Mattermost
include
Doorkeeper
::
Helpers
::
Controller
include
Doorkeeper
::
Helpers
::
Controller
include
HTTParty
include
HTTParty
attr_accessor
:current_resource_owner
attr_accessor
:current_resource_owner
,
:token
def
initialize
(
uri
,
current_user
)
def
initialize
(
uri
,
current_user
)
# Sets the base uri for HTTParty, so we can use paths
# Sets the base uri for HTTParty, so we can use paths
...
@@ -64,9 +64,9 @@ module Mattermost
...
@@ -64,9 +64,9 @@ module Mattermost
return
unless
oauth_uri
return
unless
oauth_uri
return
unless
token_uri
return
unless
token_uri
self
.
class
.
headers
(
"Cookie"
=>
"MMAUTHTOKEN=
#{
request_token
}
"
)
self
.
token
=
request_token
self
.
class
.
headers
(
"Cookie"
=>
"MMAUTHTOKEN=
#{
self
.
token
}
"
)
request_
token
self
.
token
end
end
def
destroy
def
destroy
...
@@ -84,16 +84,17 @@ module Mattermost
...
@@ -84,16 +84,17 @@ module Mattermost
end
end
def
token_uri
def
token_uri
@token_uri
||=
if
@oauth_uri
@token_uri
||=
authorization
.
authorize
.
redirect_uri
if
pre_auth
.
authorizable?
if
@oauth_uri
end
authorization
.
authorize
.
redirect_uri
if
pre_auth
.
authorizable?
end
end
end
def
request_token
def
request_token
@request_token
||=
begin
response
=
get
(
@token_uri
,
follow_redirects:
false
)
response
=
get
(
@token_uri
,
follow_redirects:
false
)
if
200
<=
response
.
code
&&
response
.
code
<
400
response
.
headers
[
'token'
]
if
200
<=
response
.
code
&&
response
.
code
<
400
response
.
headers
[
'token'
]
end
end
end
end
def
get
(
path
,
options
=
{})
def
get
(
path
,
options
=
{})
...
...
spec/lib/mattermost/session_spec.rb
View file @
48ebfaa4
require
'spec_helper'
require
'spec_helper'
describe
Mattermost
::
Session
do
describe
Mattermost
::
Session
,
type: :request
do
let
(
:user
)
{
create
(
:user
)
}
let
(
:user
)
{
create
(
:user
)
}
subject
{
described_class
.
new
(
'http://localhost:8065'
,
user
)
}
let
(
:gitlab_url
)
{
"http://gitlab.com"
}
let
(
:mattermost_url
)
{
"http://mattermost.com"
}
subject
{
described_class
.
new
(
mattermost_url
,
user
)
}
# Needed for doorkeeper to function
# Needed for doorkeeper to function
it
{
is_expected
.
to
respond_to
(
:current_resource_owner
)
}
it
{
is_expected
.
to
respond_to
(
:current_resource_owner
)
}
...
@@ -14,7 +17,7 @@ describe Mattermost::Session do
...
@@ -14,7 +17,7 @@ describe Mattermost::Session do
describe
'#with session'
do
describe
'#with session'
do
let
(
:location
)
{
'http://location.tld'
}
let
(
:location
)
{
'http://location.tld'
}
let!
(
:stub
)
do
let!
(
:stub
)
do
WebMock
.
stub_request
(
:get
,
'http://localhost:8065/api/v3/oauth/gitlab/login'
).
WebMock
.
stub_request
(
:get
,
"
#{
mattermost_url
}
/api/v3/oauth/gitlab/login"
).
to_return
(
headers:
{
'location'
=>
location
},
status:
307
)
to_return
(
headers:
{
'location'
=>
location
},
status:
307
)
end
end
...
@@ -26,9 +29,10 @@ describe Mattermost::Session do
...
@@ -26,9 +29,10 @@ describe Mattermost::Session do
context
'with oauth_uri'
do
context
'with oauth_uri'
do
let!
(
:doorkeeper
)
do
let!
(
:doorkeeper
)
do
Doorkeeper
::
Application
.
create
(
name:
"GitLab Mattermost"
,
Doorkeeper
::
Application
.
create
(
redirect_uri:
"http://localhost:8065/signup/gitlab/complete
\n
http://localhost:8065/login/gitlab/complete"
,
name:
"GitLab Mattermost"
,
scopes:
""
)
redirect_uri:
"
#{
mattermost_url
}
/signup/gitlab/complete
\n
#{
mattermost_url
}
/login/gitlab/complete"
,
scopes:
""
)
end
end
context
'without token_uri'
do
context
'without token_uri'
do
...
@@ -40,24 +44,43 @@ describe Mattermost::Session do
...
@@ -40,24 +44,43 @@ describe Mattermost::Session do
end
end
context
'with token_uri'
do
context
'with token_uri'
do
let
(
:state
)
{
"eyJhY3Rpb24iOiJsb2dpbiIsImhhc2giOiIkMmEkMTAkVC9wYVlEaTdIUS8vcWdKRmdOOUllZUptaUNJWUlvNVNtNEcwU2NBMXFqelNOVmVPZ1cxWUsifQ%3D%3D"
}
let
(
:state
)
{
"state"
}
let
(
:location
)
{
"http://locahost:8065/oauth/authorize?response_type=code&client_id=
#{
doorkeeper
.
uid
}
&redirect_uri=http%3A%2F%2Flocalhost:8065%2Fsignup%2Fgitlab%2Fcomplete&state=
#{
state
}
"
}
let
(
:params
)
do
{
response_type:
"code"
,
client_id:
doorkeeper
.
uid
,
redirect_uri:
"
#{
mattermost_url
}
/signup/gitlab/complete"
,
state:
state
}
end
let
(
:location
)
do
"
#{
gitlab_url
}
/oauth/authorize?
#{
URI
.
encode_www_form
(
params
)
}
"
end
before
do
before
do
WebMock
.
stub_request
(
:get
,
/http:\/\/localhost:8065\/signup\/gitlab\/complete*/
).
WebMock
.
stub_request
(
:get
,
"
#{
mattermost_url
}
/signup/gitlab/complete"
).
to_return
(
headers:
{
'token'
=>
'thisworksnow'
},
status:
202
)
with
(
query:
hash_including
({
'state'
=>
state
})).
to_return
do
|
request
|
post
"/oauth/token"
,
client_id:
doorkeeper
.
uid
,
client_secret:
doorkeeper
.
secret
,
redirect_uri:
params
[
:redirect_uri
],
grant_type:
'authorization_code'
,
code:
request
.
uri
.
query_values
[
'code'
]
if
response
.
status
==
200
{
headers:
{
'token'
=>
'thisworksnow'
},
status:
202
}
end
end
WebMock
.
stub_request
(
:post
,
"
#{
mattermost_url
}
/api/v3/users/logout"
).
to_return
(
headers:
{
Cookie
:
'MMAUTHTOKEN=thisworksnow'
},
status:
200
)
end
end
it
'can setup a session'
do
it
'can setup a session'
do
expect
(
subject
).
to
receive
(
:destroy
)
subject
.
with_session
{
1
+
1
}
subject
.
with_session
{
1
+
1
}
expect
(
subject
.
token
).
not_to
be_nil
end
end
it
'returns the value of the block'
do
it
'returns the value of the block'
do
WebMock
.
stub_request
(
:post
,
"http://localhost:8065/api/v3/users/logout"
).
to_return
(
headers:
{
'token'
=>
'thisworksnow'
},
status:
200
)
value
=
subject
.
with_session
{
1
+
1
}
value
=
subject
.
with_session
{
1
+
1
}
expect
(
value
).
to
be
(
2
)
expect
(
value
).
to
be
(
2
)
...
...
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