-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathselfheal_test.go
More file actions
129 lines (110 loc) · 3.94 KB
/
Copy pathselfheal_test.go
File metadata and controls
129 lines (110 loc) · 3.94 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package cmd
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/customerio/cli/internal/client"
)
// newClientFor builds a client pointed at srv with a pre-set access token so
// Do() skips the OAuth exchange (the bogus JWT gets a far-future expiry).
func newClientFor(url, saToken string) *client.Client {
return client.New(client.Config{
BaseURL: url,
ServiceAccountToken: saToken,
AccessToken: "test-jwt",
})
}
func TestMaybePromoteSandboxToken_PromotesWhenLive(t *testing.T) {
t.Setenv("HOME", t.TempDir())
var gotPath, gotMethod string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath, gotMethod = r.URL.Path, r.Method
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"token":"sa_live_promoted999","id":1,"name":"live-bootstrap"}`))
}))
defer srv.Close()
saToken := "sa_sandbox_bootstrap123"
if err := client.WriteCredentials(&client.Credentials{
ServiceAccountToken: saToken,
AccountID: "42",
Region: "us",
}); err != nil {
t.Fatal(err)
}
c := newClientFor(srv.URL, saToken)
maybePromoteSandboxToken(context.Background(), c, saToken, false)
if gotMethod != http.MethodPost || gotPath != "/v1/accounts/42/promote_sandbox_token" {
t.Fatalf("unexpected request: %s %s", gotMethod, gotPath)
}
if c.ServiceAccountToken() != "sa_live_promoted999" {
t.Fatalf("in-memory client token not swapped: %q", c.ServiceAccountToken())
}
creds, err := client.ReadCredentials()
if err != nil {
t.Fatal(err)
}
if creds.ServiceAccountToken != "sa_live_promoted999" {
t.Fatalf("stored token not swapped: %q", creds.ServiceAccountToken)
}
if creds.AccessToken != "" {
t.Fatal("cached access token should be cleared after promotion")
}
}
func TestMaybePromoteSandboxToken_Throttled(t *testing.T) {
t.Setenv("HOME", t.TempDir())
called := false
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { called = true }))
defer srv.Close()
saToken := "sa_sandbox_x"
if err := client.WriteCredentials(&client.Credentials{
ServiceAccountToken: saToken,
AccountID: "1",
SandboxPromoteCheckedAt: time.Now(),
}); err != nil {
t.Fatal(err)
}
maybePromoteSandboxToken(context.Background(), newClientFor(srv.URL, saToken), saToken, false)
if called {
t.Fatal("should not probe promote within the throttle window")
}
}
func TestMaybePromoteSandboxToken_SkipsLiveAndReadOnly(t *testing.T) {
called := false
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { called = true }))
defer srv.Close()
// Live token: nothing to promote.
maybePromoteSandboxToken(context.Background(), newClientFor(srv.URL, "sa_live_x"), "sa_live_x", false)
// Sandbox token but read-only: the POST would be blocked, so skip.
maybePromoteSandboxToken(context.Background(), newClientFor(srv.URL, "sa_sandbox_x"), "sa_sandbox_x", true)
if called {
t.Fatal("should not call promote for a live token or in read-only mode")
}
}
func TestMaybePromoteSandboxToken_SwallowsStillSandbox(t *testing.T) {
t.Setenv("HOME", t.TempDir())
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusForbidden)
_, _ = w.Write([]byte(`{"errors":[{"detail":"still in sandbox"}]}`))
}))
defer srv.Close()
saToken := "sa_sandbox_y"
if err := client.WriteCredentials(&client.Credentials{
ServiceAccountToken: saToken,
AccountID: "7",
}); err != nil {
t.Fatal(err)
}
maybePromoteSandboxToken(context.Background(), newClientFor(srv.URL, saToken), saToken, false)
creds, err := client.ReadCredentials()
if err != nil {
t.Fatal(err)
}
if creds.ServiceAccountToken != saToken {
t.Fatalf("token must be unchanged on 403, got %q", creds.ServiceAccountToken)
}
if creds.SandboxPromoteCheckedAt.IsZero() {
t.Fatal("throttle timestamp should be recorded after a failed attempt")
}
}