Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
tank
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
AURA
tank
Commits
612ceb2d
Commit
612ceb2d
authored
6 years ago
by
Christian Pointner
Browse files
Options
Downloads
Patches
Plain Diff
more testcases for auth subsystem
parent
34d569b5
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
auth/auth_test.go
+60
-0
60 additions, 0 deletions
auth/auth_test.go
with
60 additions
and
0 deletions
auth/auth_test.go
+
60
−
0
View file @
612ceb2d
...
@@ -27,8 +27,12 @@ package auth
...
@@ -27,8 +27,12 @@ package auth
import
(
import
(
"io/ioutil"
"io/ioutil"
"log"
"log"
"net/http"
"net/http/httptest"
"os"
"os"
"testing"
"testing"
"github.com/gorilla/mux"
)
)
func
TestMain
(
m
*
testing
.
M
)
{
func
TestMain
(
m
*
testing
.
M
)
{
...
@@ -37,3 +41,59 @@ func TestMain(m *testing.M) {
...
@@ -37,3 +41,59 @@ func TestMain(m *testing.M) {
auth
.
dbgLog
=
log
.
New
(
ioutil
.
Discard
,
""
,
0
)
auth
.
dbgLog
=
log
.
New
(
ioutil
.
Discard
,
""
,
0
)
os
.
Exit
(
m
.
Run
())
os
.
Exit
(
m
.
Run
())
}
}
//
// Testing
//
func
TestAuthDisabled
(
t
*
testing
.
T
)
{
if
err
:=
Init
(
nil
,
nil
,
nil
,
nil
);
err
!=
nil
{
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
}
if
auth
.
sessions
!=
nil
{
t
.
Fatalf
(
"authentication should be disabled but Init created a session manager"
)
}
router
:=
mux
.
NewRouter
()
InstallHTTPHandler
(
router
)
req
,
err
:=
http
.
NewRequest
(
"GET"
,
"/backends"
,
nil
)
if
err
!=
nil
{
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
}
rr
:=
httptest
.
NewRecorder
()
router
.
ServeHTTP
(
rr
,
req
)
if
rr
.
Code
!=
http
.
StatusNotFound
{
t
.
Fatalf
(
"list backends should return 404 when authentication is disabled but returened: %d (%s)"
,
rr
.
Code
,
http
.
StatusText
(
rr
.
Code
))
}
if
req
,
err
=
http
.
NewRequest
(
"GET"
,
"/session"
,
nil
);
err
!=
nil
{
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
}
rr
=
httptest
.
NewRecorder
()
router
.
ServeHTTP
(
rr
,
req
)
if
rr
.
Code
!=
http
.
StatusBadRequest
{
t
.
Fatalf
(
"get session should return 400 when authentication is disabled but returened: %d (%s)"
,
rr
.
Code
,
http
.
StatusText
(
rr
.
Code
))
}
var
sOut
*
Session
protected
:=
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
s
,
ok
:=
SessionFromRequest
(
r
)
if
!
ok
{
t
.
Fatalf
(
"protected handler did not get a valid session"
)
}
sOut
=
s
})
wrapped
:=
Middleware
(
protected
)
if
req
,
err
=
http
.
NewRequest
(
"GET"
,
"/api/endpoint"
,
nil
);
err
!=
nil
{
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
}
rr
=
httptest
.
NewRecorder
()
wrapped
.
ServeHTTP
(
rr
,
req
)
if
sOut
==
anonAllowAll
{
t
.
Fatalf
(
"authentication middleware did not attach the correct session to the proteceted handler"
)
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment