3
0

Use the right auth tokens for handshake pairing in tests

This commit is contained in:
Denis Arh 2020-10-09 09:27:47 +02:00 committed by Peter Grlica
parent bdab3b5ef5
commit bcdb66cb0e
2 changed files with 17 additions and 1 deletions

View File

@ -148,6 +148,9 @@ func TestSuccessfulNodePairing(t *testing.T) {
init: func(ctx context.Context, n *types.Node, authToken string) error {
h.apiInit().
//Debug().
// make sure we do not use test auth-token for authentication but
// we do it with pairing token
Intercept(helpers.ReqHeaderAuthBearer(nil)).
Post(fmt.Sprintf("/nodes/%d/handshake", n.SharedNodeID)).
FormData("pairToken", n.PairToken).
FormData("authToken", authToken).
@ -181,6 +184,9 @@ func TestSuccessfulNodePairing(t *testing.T) {
complete: func(ctx context.Context, n *types.Node, authToken string) error {
h.apiInit().
//Debug().
// make sure we do not use test auth-token but
// one provided to us in the initial handshake step
Intercept(helpers.ReqHeaderRawAuthBearer(n.AuthToken)).
Post(fmt.Sprintf("/nodes/%d/handshake-complete", n.SharedNodeID)).
FormData("authToken", authToken).
Expect(t).

View File

@ -19,6 +19,16 @@ func BindAuthMiddleware(r chi.Router) {
func ReqHeaderAuthBearer(user *types.User) apitest.Intercept {
return func(req *http.Request) {
req.Header.Set("Authorization", "Bearer "+auth.DefaultJwtHandler.Encode(user))
if user == nil {
req.Header.Del("Authorization")
} else {
req.Header.Set("Authorization", "Bearer "+auth.DefaultJwtHandler.Encode(user))
}
}
}
func ReqHeaderRawAuthBearer(token string) apitest.Intercept {
return func(req *http.Request) {
req.Header.Set("Authorization", "Bearer "+token)
}
}