|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/hookdeck/hookdeck-cli/pkg/hookdeck" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | +) |
| 9 | + |
| 10 | +func TestProfile_ApplyValidateAPIKeyResponse(t *testing.T) { |
| 11 | + t.Run("nil response is no-op", func(t *testing.T) { |
| 12 | + p := &Profile{ProjectId: "keep", GuestURL: "https://guest"} |
| 13 | + p.ApplyValidateAPIKeyResponse(nil, true) |
| 14 | + require.Equal(t, "keep", p.ProjectId) |
| 15 | + require.Equal(t, "https://guest", p.GuestURL) |
| 16 | + }) |
| 17 | + |
| 18 | + t.Run("sets project fields and clears guest when requested", func(t *testing.T) { |
| 19 | + p := &Profile{GuestURL: "https://guest"} |
| 20 | + p.ApplyValidateAPIKeyResponse(&hookdeck.ValidateAPIKeyResponse{ |
| 21 | + ProjectID: "team_1", |
| 22 | + ProjectMode: "inbound", |
| 23 | + }, true) |
| 24 | + require.Equal(t, "team_1", p.ProjectId) |
| 25 | + require.Equal(t, "inbound", p.ProjectMode) |
| 26 | + require.Equal(t, ProjectTypeGateway, p.ProjectType) |
| 27 | + require.Empty(t, p.GuestURL) |
| 28 | + }) |
| 29 | + |
| 30 | + t.Run("preserves guest URL when clearGuestURL is false", func(t *testing.T) { |
| 31 | + p := &Profile{GuestURL: "https://guest.example/x"} |
| 32 | + p.ApplyValidateAPIKeyResponse(&hookdeck.ValidateAPIKeyResponse{ |
| 33 | + ProjectID: "team_2", |
| 34 | + ProjectMode: "console", |
| 35 | + }, false) |
| 36 | + require.Equal(t, "team_2", p.ProjectId) |
| 37 | + require.Equal(t, ProjectTypeConsole, p.ProjectType) |
| 38 | + require.Equal(t, "https://guest.example/x", p.GuestURL) |
| 39 | + }) |
| 40 | +} |
| 41 | + |
| 42 | +func TestProfile_ApplyPollAPIKeyResponse(t *testing.T) { |
| 43 | + t.Run("nil response is no-op", func(t *testing.T) { |
| 44 | + p := &Profile{APIKey: "k", ProjectId: "p"} |
| 45 | + p.ApplyPollAPIKeyResponse(nil, "") |
| 46 | + require.Equal(t, "k", p.APIKey) |
| 47 | + require.Equal(t, "p", p.ProjectId) |
| 48 | + }) |
| 49 | + |
| 50 | + t.Run("sets credentials and guest URL", func(t *testing.T) { |
| 51 | + p := &Profile{} |
| 52 | + p.ApplyPollAPIKeyResponse(&hookdeck.PollAPIKeyResponse{ |
| 53 | + APIKey: "key_from_poll", |
| 54 | + ProjectID: "team_p", |
| 55 | + ProjectMode: "inbound", |
| 56 | + }, "https://guest") |
| 57 | + require.Equal(t, "key_from_poll", p.APIKey) |
| 58 | + require.Equal(t, "team_p", p.ProjectId) |
| 59 | + require.Equal(t, ProjectTypeGateway, p.ProjectType) |
| 60 | + require.Equal(t, "https://guest", p.GuestURL) |
| 61 | + }) |
| 62 | + |
| 63 | + t.Run("clears-style guest with empty string", func(t *testing.T) { |
| 64 | + p := &Profile{GuestURL: "old"} |
| 65 | + p.ApplyPollAPIKeyResponse(&hookdeck.PollAPIKeyResponse{ |
| 66 | + APIKey: "k123456789012", |
| 67 | + ProjectID: "t", |
| 68 | + ProjectMode: "inbound", |
| 69 | + }, "") |
| 70 | + require.Empty(t, p.GuestURL) |
| 71 | + }) |
| 72 | +} |
| 73 | + |
| 74 | +func TestProfile_ApplyCIClient(t *testing.T) { |
| 75 | + p := &Profile{} |
| 76 | + p.ApplyCIClient(hookdeck.CIClient{ |
| 77 | + APIKey: "ci_key_123456", |
| 78 | + ProjectID: "team_ci", |
| 79 | + ProjectMode: "inbound", |
| 80 | + }) |
| 81 | + require.Equal(t, "ci_key_123456", p.APIKey) |
| 82 | + require.Equal(t, "team_ci", p.ProjectId) |
| 83 | + require.Equal(t, ProjectTypeGateway, p.ProjectType) |
| 84 | + require.Empty(t, p.GuestURL) |
| 85 | +} |
0 commit comments