|
6 | 6 | "net/url" |
7 | 7 | "testing" |
8 | 8 |
|
| 9 | + "github.com/dash0hq/dash0-api-client-go/profiles" |
9 | 10 | "github.com/stretchr/testify/assert" |
10 | 11 | "github.com/stretchr/testify/require" |
11 | 12 | ) |
@@ -52,3 +53,44 @@ func TestRevoke_NoOpsOnEmptyArgs(t *testing.T) { |
52 | 53 | assert.True(t, Revoke(RevokeRequest{ClientID: "client-abc-123", RefreshToken: "dash0_rt_test"}), "empty apiURL should no-op") |
53 | 54 | assert.True(t, Revoke(RevokeRequest{APIURL: "https://api.example.com", ClientID: "client-abc-123"}), "empty refreshToken should no-op") |
54 | 55 | } |
| 56 | + |
| 57 | +func TestRevoke_OmitsEmptyClientID(t *testing.T) { |
| 58 | + t.Setenv("DASH0_CONFIG_DIR", t.TempDir()) |
| 59 | + |
| 60 | + var gotForm url.Values |
| 61 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 62 | + require.NoError(t, r.ParseForm()) |
| 63 | + gotForm = r.Form |
| 64 | + w.WriteHeader(http.StatusOK) |
| 65 | + })) |
| 66 | + defer server.Close() |
| 67 | + |
| 68 | + assert.True(t, Revoke(RevokeRequest{APIURL: server.URL, RefreshToken: "dash0_rt_test"})) |
| 69 | + require.NotNil(t, gotForm) |
| 70 | + _, present := gotForm["client_id"] |
| 71 | + assert.False(t, present, "empty client_id must be omitted, not sent as an empty parameter") |
| 72 | +} |
| 73 | + |
| 74 | +func TestRevoke_FallsBackToDCRCache(t *testing.T) { |
| 75 | + dir := t.TempDir() |
| 76 | + t.Setenv("DASH0_CONFIG_DIR", dir) |
| 77 | + |
| 78 | + var gotForm url.Values |
| 79 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 80 | + require.NoError(t, r.ParseForm()) |
| 81 | + gotForm = r.Form |
| 82 | + w.WriteHeader(http.StatusOK) |
| 83 | + })) |
| 84 | + defer server.Close() |
| 85 | + |
| 86 | + store, err := profiles.NewOAuthClientStore() |
| 87 | + require.NoError(t, err) |
| 88 | + require.NoError(t, store.Put(server.URL, profiles.OAuthClientRecord{ |
| 89 | + ClientID: "cached-from-dcr", |
| 90 | + RedirectURI: "http://localhost/cb", |
| 91 | + })) |
| 92 | + |
| 93 | + assert.True(t, Revoke(RevokeRequest{APIURL: server.URL, RefreshToken: "dash0_rt_test"})) |
| 94 | + require.NotNil(t, gotForm) |
| 95 | + assert.Equal(t, "cached-from-dcr", gotForm.Get("client_id")) |
| 96 | +} |
0 commit comments