|
1 | 1 | package add |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "errors" |
4 | 5 | "testing" |
5 | 6 |
|
6 | 7 | "github.com/google/shlex" |
7 | 8 | "github.com/stretchr/testify/assert" |
8 | 9 | "github.com/stretchr/testify/require" |
9 | 10 |
|
| 11 | + "github.com/algolia/algoliasearch-client-go/v4/algolia/search" |
10 | 12 | "github.com/algolia/cli/pkg/cmdutil" |
11 | 13 | "github.com/algolia/cli/pkg/config" |
12 | 14 | "github.com/algolia/cli/pkg/iostreams" |
@@ -97,8 +99,68 @@ func TestNewAddCmd(t *testing.T) { |
97 | 99 |
|
98 | 100 | assert.Equal(t, tt.wantsOpts.Profile.Name, opts.Profile.Name) |
99 | 101 | assert.Equal(t, tt.wantsOpts.Profile.ApplicationID, opts.Profile.ApplicationID) |
100 | | - assert.Equal(t, tt.wantsOpts.Profile.AdminAPIKey, opts.Profile.AdminAPIKey) |
| 102 | + assert.Equal(t, tt.wantsOpts.Profile.APIKey, opts.Profile.APIKey) |
101 | 103 | assert.Equal(t, tt.wantsOpts.Profile.Default, opts.Profile.Default) |
102 | 104 | }) |
103 | 105 | } |
104 | 106 | } |
| 107 | + |
| 108 | +type stubAPIKeyInspector struct { |
| 109 | + listErr error |
| 110 | + |
| 111 | + getResp *search.GetApiKeyResponse |
| 112 | + getErr error |
| 113 | + getCalled bool |
| 114 | +} |
| 115 | + |
| 116 | +func (s *stubAPIKeyInspector) ListAPIKeys(opts ...search.RequestOption) (*search.ListApiKeysResponse, error) { |
| 117 | + return &search.ListApiKeysResponse{}, s.listErr |
| 118 | +} |
| 119 | + |
| 120 | +func (s *stubAPIKeyInspector) GetAPIKey(r search.ApiGetApiKeyRequest, opts ...search.RequestOption) (*search.GetApiKeyResponse, error) { |
| 121 | + s.getCalled = true |
| 122 | + return s.getResp, s.getErr |
| 123 | +} |
| 124 | + |
| 125 | +func (s *stubAPIKeyInspector) NewAPIGetAPIKeyRequest(key string) search.ApiGetApiKeyRequest { |
| 126 | + return search.ApiGetApiKeyRequest{} |
| 127 | +} |
| 128 | + |
| 129 | +func TestInspectAPIKey_AdminKeySkipsGetApiKey(t *testing.T) { |
| 130 | + stub := &stubAPIKeyInspector{ |
| 131 | + listErr: nil, // admin keys can list API keys |
| 132 | + getErr: errors.New("should not be called"), |
| 133 | + } |
| 134 | + |
| 135 | + isAdmin, acls, err := inspectAPIKey(stub, "my-admin-key") |
| 136 | + require.NoError(t, err) |
| 137 | + assert.True(t, isAdmin) |
| 138 | + assert.Nil(t, acls) |
| 139 | + assert.False(t, stub.getCalled) |
| 140 | +} |
| 141 | + |
| 142 | +func TestInspectAPIKey_NonAdminKeyReturnsACLs(t *testing.T) { |
| 143 | + stub := &stubAPIKeyInspector{ |
| 144 | + listErr: errors.New("API error [403] forbidden"), // non-admin keys cannot list API keys |
| 145 | + getResp: &search.GetApiKeyResponse{ |
| 146 | + Acl: []search.Acl{search.ACL_SEARCH, search.ACL_ADD_OBJECT}, |
| 147 | + }, |
| 148 | + } |
| 149 | + |
| 150 | + isAdmin, acls, err := inspectAPIKey(stub, "my-write-key") |
| 151 | + require.NoError(t, err) |
| 152 | + assert.False(t, isAdmin) |
| 153 | + assert.Equal(t, []string{"search", "addObject"}, acls) |
| 154 | + assert.True(t, stub.getCalled) |
| 155 | +} |
| 156 | + |
| 157 | +func TestInspectAPIKey_InvalidCredentials(t *testing.T) { |
| 158 | + stub := &stubAPIKeyInspector{ |
| 159 | + listErr: errors.New("API error [403] invalid"), // fall back to GetApiKey |
| 160 | + getErr: errors.New("API error [403] invalid"), |
| 161 | + } |
| 162 | + |
| 163 | + _, _, err := inspectAPIKey(stub, "bad-key") |
| 164 | + require.Error(t, err) |
| 165 | + assert.Equal(t, "invalid application credentials", err.Error()) |
| 166 | +} |
0 commit comments