-
Notifications
You must be signed in to change notification settings - Fork 8
/
auth_mock.go
36 lines (30 loc) · 921 Bytes
/
auth_mock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package scalingo
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"time"
"github.com/golang-jwt/jwt/v4"
"github.com/golang/mock/gomock"
httpclient "github.com/Scalingo/go-scalingo/v7/http"
"github.com/Scalingo/go-scalingo/v7/http/httpmock"
)
func MockAuth(ctrl *gomock.Controller) *httpmock.MockClient {
mock := httpmock.NewMockClient(ctrl)
mock.EXPECT().Do(gomock.Any(), gomock.Any()).DoAndReturn(func(_ context.Context, _ *httpclient.APIRequest) (*http.Response, error) {
claims := &jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(time.Now().Add(5 * time.Minute)),
}
jwtToken := jwt.NewWithClaims(jwt.SigningMethodNone, claims)
jwt, err := jwtToken.SignedString(jwt.UnsafeAllowNoneSignatureType)
if err != nil {
return nil, err
}
return &http.Response{
Body: io.NopCloser(bytes.NewBuffer([]byte(fmt.Sprintf(`{"token": "%v"}`, jwt)))),
}, nil
}).AnyTimes()
return mock
}