Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dex
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
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
hdacloud
dex
Commits
9f70c7da
Commit
9f70c7da
authored
2 years ago
by
nobuyo
Browse files
Options
Downloads
Patches
Plain Diff
Add test for skipping approval
Signed-off-by:
nobuyo
<
longzechangsheng@gmail.com
>
parent
b7fb7d93
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
server/handlers_test.go
+180
-0
180 additions, 0 deletions
server/handlers_test.go
server/server_test.go
+1
-1
1 addition, 1 deletion
server/server_test.go
with
181 additions
and
1 deletion
server/handlers_test.go
+
180
−
0
View file @
9f70c7da
...
@@ -5,10 +5,12 @@ import (
...
@@ -5,10 +5,12 @@ import (
"context"
"context"
"encoding/json"
"encoding/json"
"errors"
"errors"
"fmt"
"net/http"
"net/http"
"net/http/httptest"
"net/http/httptest"
"net/url"
"net/url"
"path"
"path"
"strings"
"testing"
"testing"
"time"
"time"
...
@@ -310,3 +312,181 @@ func TestPasswordConnectorDataNotEmpty(t *testing.T) {
...
@@ -310,3 +312,181 @@ func TestPasswordConnectorDataNotEmpty(t *testing.T) {
require
.
NoError
(
t
,
err
)
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
`{"test": "true"}`
,
string
(
newSess
.
ConnectorData
))
require
.
Equal
(
t
,
`{"test": "true"}`
,
string
(
newSess
.
ConnectorData
))
}
}
func
TestHandlePasswordLoginWithSkipApproval
(
t
*
testing
.
T
)
{
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
defer
cancel
()
connID
:=
"mockPw"
authReqID
:=
"test"
expiry
:=
time
.
Now
()
.
Add
(
100
*
time
.
Second
)
resTypes
:=
[]
string
{
"code"
}
tests
:=
[]
struct
{
name
string
skipApproval
bool
authReq
storage
.
AuthRequest
}{
{
name
:
"Force approval"
,
skipApproval
:
false
,
authReq
:
storage
.
AuthRequest
{
ID
:
authReqID
,
ConnectorID
:
connID
,
RedirectURI
:
"cb"
,
Expiry
:
expiry
,
ResponseTypes
:
resTypes
,
ForceApprovalPrompt
:
true
,
},
},
{
name
:
"Skip approval by server config"
,
skipApproval
:
true
,
authReq
:
storage
.
AuthRequest
{
ID
:
authReqID
,
ConnectorID
:
connID
,
RedirectURI
:
"cb"
,
Expiry
:
expiry
,
ResponseTypes
:
resTypes
,
ForceApprovalPrompt
:
true
,
},
},
{
name
:
"Skip approval by auth request"
,
skipApproval
:
false
,
authReq
:
storage
.
AuthRequest
{
ID
:
authReqID
,
ConnectorID
:
connID
,
RedirectURI
:
"cb"
,
Expiry
:
expiry
,
ResponseTypes
:
resTypes
,
ForceApprovalPrompt
:
false
,
},
},
}
for
_
,
tc
:=
range
tests
{
httpServer
,
s
:=
newTestServer
(
ctx
,
t
,
func
(
c
*
Config
)
{
c
.
SkipApprovalScreen
=
tc
.
skipApproval
c
.
Now
=
time
.
Now
})
defer
httpServer
.
Close
()
sc
:=
storage
.
Connector
{
ID
:
connID
,
Type
:
"mockPassword"
,
Name
:
"MockPassword"
,
ResourceVersion
:
"1"
,
Config
:
[]
byte
(
"{
\"
username
\"
:
\"
foo
\"
,
\"
password
\"
:
\"
password
\"
}"
),
}
if
err
:=
s
.
storage
.
CreateConnector
(
sc
);
err
!=
nil
{
t
.
Fatalf
(
"create connector: %v"
,
err
)
}
if
_
,
err
:=
s
.
OpenConnector
(
sc
);
err
!=
nil
{
t
.
Fatalf
(
"open connector: %v"
,
err
)
}
if
err
:=
s
.
storage
.
CreateAuthRequest
(
tc
.
authReq
);
err
!=
nil
{
t
.
Fatalf
(
"failed to create AuthRequest: %v"
,
err
)
}
rr
:=
httptest
.
NewRecorder
()
path
:=
fmt
.
Sprintf
(
"/auth/%s/login?state=%s&back=&login=foo&password=password"
,
connID
,
authReqID
)
s
.
handlePasswordLogin
(
rr
,
httptest
.
NewRequest
(
"POST"
,
path
,
nil
))
require
.
Equal
(
t
,
303
,
rr
.
Code
)
resp
:=
rr
.
Result
()
cbPath
:=
strings
.
Split
(
resp
.
Header
.
Get
(
"Location"
),
"?"
)[
0
]
if
tc
.
skipApproval
||
!
tc
.
authReq
.
ForceApprovalPrompt
{
require
.
Equal
(
t
,
"/auth/mockPw/cb"
,
cbPath
)
}
else
{
require
.
Equal
(
t
,
"/approval"
,
cbPath
)
}
resp
.
Body
.
Close
()
}
}
func
TestHandleConnectorCallbackWithSkipApproval
(
t
*
testing
.
T
)
{
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
defer
cancel
()
connID
:=
"mock"
authReqID
:=
"test"
expiry
:=
time
.
Now
()
.
Add
(
100
*
time
.
Second
)
resTypes
:=
[]
string
{
"code"
}
tests
:=
[]
struct
{
name
string
skipApproval
bool
authReq
storage
.
AuthRequest
}{
{
name
:
"Force approval"
,
skipApproval
:
false
,
authReq
:
storage
.
AuthRequest
{
ID
:
authReqID
,
ConnectorID
:
connID
,
RedirectURI
:
"cb"
,
Expiry
:
expiry
,
ResponseTypes
:
resTypes
,
ForceApprovalPrompt
:
true
,
},
},
{
name
:
"Skip approval by server config"
,
skipApproval
:
true
,
authReq
:
storage
.
AuthRequest
{
ID
:
authReqID
,
ConnectorID
:
connID
,
RedirectURI
:
"cb"
,
Expiry
:
expiry
,
ResponseTypes
:
resTypes
,
ForceApprovalPrompt
:
true
,
},
},
{
name
:
"Skip approval by auth request"
,
skipApproval
:
false
,
authReq
:
storage
.
AuthRequest
{
ID
:
authReqID
,
ConnectorID
:
connID
,
RedirectURI
:
"cb"
,
Expiry
:
expiry
,
ResponseTypes
:
resTypes
,
ForceApprovalPrompt
:
false
,
},
},
}
for
_
,
tc
:=
range
tests
{
httpServer
,
s
:=
newTestServer
(
ctx
,
t
,
func
(
c
*
Config
)
{
c
.
SkipApprovalScreen
=
tc
.
skipApproval
c
.
Now
=
time
.
Now
})
defer
httpServer
.
Close
()
if
err
:=
s
.
storage
.
CreateAuthRequest
(
tc
.
authReq
);
err
!=
nil
{
t
.
Fatalf
(
"failed to create AuthRequest: %v"
,
err
)
}
rr
:=
httptest
.
NewRecorder
()
path
:=
fmt
.
Sprintf
(
"/callback/%s?state=%s"
,
connID
,
authReqID
)
s
.
handleConnectorCallback
(
rr
,
httptest
.
NewRequest
(
"GET"
,
path
,
nil
))
require
.
Equal
(
t
,
303
,
rr
.
Code
)
resp
:=
rr
.
Result
()
cbPath
:=
strings
.
Split
(
resp
.
Header
.
Get
(
"Location"
),
"?"
)[
0
]
if
tc
.
skipApproval
||
!
tc
.
authReq
.
ForceApprovalPrompt
{
require
.
Equal
(
t
,
"/callback/cb"
,
cbPath
)
}
else
{
require
.
Equal
(
t
,
"/approval"
,
cbPath
)
}
resp
.
Body
.
Close
()
}
}
This diff is collapsed.
Click to expand it.
server/server_test.go
+
1
−
1
View file @
9f70c7da
...
@@ -98,6 +98,7 @@ func newTestServer(ctx context.Context, t *testing.T, updateConfig func(c *Confi
...
@@ -98,6 +98,7 @@ func newTestServer(ctx context.Context, t *testing.T, updateConfig func(c *Confi
Logger
:
logger
,
Logger
:
logger
,
PrometheusRegistry
:
prometheus
.
NewRegistry
(),
PrometheusRegistry
:
prometheus
.
NewRegistry
(),
HealthChecker
:
gosundheit
.
New
(),
HealthChecker
:
gosundheit
.
New
(),
SkipApprovalScreen
:
true
,
// Don't prompt for approval, just immediately redirect with code.
}
}
if
updateConfig
!=
nil
{
if
updateConfig
!=
nil
{
updateConfig
(
&
config
)
updateConfig
(
&
config
)
...
@@ -118,7 +119,6 @@ func newTestServer(ctx context.Context, t *testing.T, updateConfig func(c *Confi
...
@@ -118,7 +119,6 @@ func newTestServer(ctx context.Context, t *testing.T, updateConfig func(c *Confi
if
server
,
err
=
newServer
(
ctx
,
config
,
staticRotationStrategy
(
testKey
));
err
!=
nil
{
if
server
,
err
=
newServer
(
ctx
,
config
,
staticRotationStrategy
(
testKey
));
err
!=
nil
{
t
.
Fatal
(
err
)
t
.
Fatal
(
err
)
}
}
server
.
skipApproval
=
true
// Don't prompt for approval, just immediately redirect with code.
// Default rotation policy
// Default rotation policy
if
server
.
refreshTokenPolicy
==
nil
{
if
server
.
refreshTokenPolicy
==
nil
{
...
...
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