-
Notifications
You must be signed in to change notification settings - Fork 8
/
client_test.go
83 lines (73 loc) · 2.25 KB
/
client_test.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package scalingo
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/golang-jwt/jwt/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewClient(t *testing.T) {
t.Run("static token generator should be used if present", func(t *testing.T) {
ctx := context.Background()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
auth := r.Header.Get("Authorization")
require.NotEmpty(t, auth)
split := strings.Split(auth, " ")
require.Len(t, split, 2)
assert.Equal(t, "static-token", split[1])
w.WriteHeader(200)
_, err := w.Write([]byte(`{"apps": []}`))
require.NoError(t, err)
}))
defer server.Close()
client, err := New(ctx, ClientConfig{
APIEndpoint: server.URL,
StaticTokenGenerator: NewStaticTokenGenerator("static-token"),
})
require.NoError(t, err)
_, err = client.AppsList(ctx)
require.NoError(t, err)
})
t.Run("it should exchange the API token for a JWT", func(t *testing.T) {
ctx := context.Background()
claims := &jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(time.Now().Add(5 * time.Minute)),
}
jwtToken := jwt.NewWithClaims(jwt.SigningMethodNone, claims)
jwt, err := jwtToken.SignedString(jwt.UnsafeAllowNoneSignatureType)
require.NoError(t, err)
authserver := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.URL.Path, "exchange") {
_, password, ok := r.BasicAuth()
require.True(t, ok)
assert.Equal(t, "api-token", password)
w.WriteHeader(200)
_, err := w.Write([]byte(fmt.Sprintf(`{"token": "%v"}`, jwt)))
require.NoError(t, err)
}
if strings.Contains(r.URL.Path, "self") {
auth := r.Header.Get("Authorization")
require.NotEmpty(t, auth)
split := strings.Split(auth, " ")
require.Len(t, split, 2)
assert.Equal(t, jwt, split[1])
w.WriteHeader(200)
_, err := w.Write([]byte(`{"user": {}}`))
require.NoError(t, err)
}
}))
defer authserver.Close()
client, err := New(ctx, ClientConfig{
AuthEndpoint: authserver.URL,
APIToken: "api-token",
})
require.NoError(t, err)
_, err = client.Self(ctx)
require.NoError(t, err)
})
}