Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
D
dex
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
dex
Commits
d3d2db8e
Commit
d3d2db8e
authored
Apr 14, 2016
by
Adrián López Gómez
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
tests: add HandleTokenFunc test
Fixes #408
parent
ed89be44
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
131 additions
and
0 deletions
+131
-0
http_test.go
server/http_test.go
+131
-0
No files found.
server/http_test.go
View file @
d3d2db8e
...
...
@@ -332,6 +332,137 @@ func TestHandleAuthFuncResponsesMultipleRedirectURLs(t *testing.T) {
}
}
func
TestHandleTokenFunc
(
t
*
testing
.
T
)
{
fx
,
err
:=
makeTestFixtures
()
if
err
!=
nil
{
t
.
Fatalf
(
"could not run test fixtures: %v"
,
err
)
}
tests
:=
[]
struct
{
query
url
.
Values
user
string
passwd
string
wantCode
int
}{
// bad grant_type
{
query
:
url
.
Values
{
"grant_type"
:
[]
string
{
"invalid!"
},
"code"
:
[]
string
{
"someCode"
},
},
user
:
"XXX"
,
passwd
:
base64
.
URLEncoding
.
EncodeToString
([]
byte
(
"secrete"
)),
wantCode
:
http
.
StatusBadRequest
,
},
// authorization_code needs code param
{
query
:
url
.
Values
{
"grant_type"
:
[]
string
{
"authorization_code"
},
},
user
:
"XXX"
,
passwd
:
base64
.
URLEncoding
.
EncodeToString
([]
byte
(
"secrete"
)),
wantCode
:
http
.
StatusBadRequest
,
},
// empty code
{
query
:
url
.
Values
{
"grant_type"
:
[]
string
{
"authorization_code"
},
"code"
:
[]
string
{
""
},
},
user
:
"XXX"
,
passwd
:
base64
.
URLEncoding
.
EncodeToString
([]
byte
(
"secrete"
)),
wantCode
:
http
.
StatusBadRequest
,
},
// valid code but bad creds
{
query
:
url
.
Values
{
"grant_type"
:
[]
string
{
"authorization_code"
},
"code"
:
[]
string
{
"code-2"
},
},
user
:
"XASD"
,
passwd
:
base64
.
URLEncoding
.
EncodeToString
([]
byte
(
"failSecrete"
)),
wantCode
:
http
.
StatusUnauthorized
,
},
// bad code
{
query
:
url
.
Values
{
"grant_type"
:
[]
string
{
"authorization_code"
},
"code"
:
[]
string
{
"asdasd"
},
},
user
:
"XXX"
,
passwd
:
base64
.
URLEncoding
.
EncodeToString
([]
byte
(
"secrete"
)),
wantCode
:
http
.
StatusBadRequest
,
},
// OK testcase
{
query
:
url
.
Values
{
"grant_type"
:
[]
string
{
"authorization_code"
},
"code"
:
[]
string
{
"code-2"
},
},
user
:
"XXX"
,
passwd
:
base64
.
URLEncoding
.
EncodeToString
([]
byte
(
"secrete"
)),
wantCode
:
http
.
StatusOK
,
},
}
for
i
,
tt
:=
range
tests
{
hdlr
:=
handleTokenFunc
(
fx
.
srv
)
w
:=
httptest
.
NewRecorder
()
req
,
err
:=
http
.
NewRequest
(
"POST"
,
"http://example.com/token"
,
strings
.
NewReader
(
tt
.
query
.
Encode
()))
if
err
!=
nil
{
t
.
Errorf
(
"unable to create HTTP request, error=%v"
,
err
)
continue
}
req
.
Header
.
Set
(
"Content-Type"
,
"application/x-www-form-urlencoded"
)
req
.
SetBasicAuth
(
tt
.
user
,
tt
.
passwd
)
// need to create session in order to exchange the code (generated by the NewSessionKey func) for token
setSession
:=
func
()
error
{
sid
,
err
:=
fx
.
sessionManager
.
NewSession
(
"local"
,
"XXX"
,
""
,
testRedirectURL
,
""
,
true
,
[]
string
{
"openid"
})
if
err
!=
nil
{
return
fmt
.
Errorf
(
"case %d: cannot create session, error=%v"
,
i
,
err
)
}
_
,
err
=
fx
.
sessionManager
.
AttachRemoteIdentity
(
sid
,
oidc
.
Identity
{})
if
err
!=
nil
{
return
fmt
.
Errorf
(
"case %d: cannot attach remoteID, error=%v"
,
i
,
err
)
}
_
,
err
=
fx
.
sessionManager
.
AttachUser
(
sid
,
"ID-Verified"
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"case %d: cannot attach user, error=%v"
,
i
,
err
)
}
_
,
err
=
fx
.
sessionManager
.
NewSessionKey
(
sid
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"case %d: cannot create session code, error=%v"
,
i
,
err
)
}
return
nil
}
if
err
:=
setSession
();
err
!=
nil
{
t
.
Errorf
(
"case %d: %v"
,
i
,
err
)
continue
}
hdlr
.
ServeHTTP
(
w
,
req
)
if
tt
.
wantCode
!=
w
.
Code
{
t
.
Errorf
(
"case %d: expected HTTP %d, got %v"
,
i
,
tt
.
wantCode
,
w
.
Code
)
}
}
}
func
TestHandleTokenFuncMethodNotAllowed
(
t
*
testing
.
T
)
{
for
_
,
m
:=
range
[]
string
{
"GET"
,
"PUT"
,
"DELETE"
}
{
hdlr
:=
handleTokenFunc
(
nil
)
...
...
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