Skip to content

Commit 3c67830

Browse files
Add empty-keys validation, --use-automations=false tests
Reject empty JSON arrays for --keys on bulk-update and bulk-create before hitting the API. Add unit tests for --use-automations=false path and empty keys validation.
1 parent 87c549f commit 3c67830

2 files changed

Lines changed: 107 additions & 0 deletions

File tree

cmd/key.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"encoding/json"
5+
"errors"
56

67
"github.com/lokalise/go-lokalise-api/v4"
78
"github.com/spf13/cobra"
@@ -161,6 +162,9 @@ var keyBulkUpdateCmd = &cobra.Command{
161162
if err := json.Unmarshal([]byte(bulkUpdateKeys), &keys); err != nil {
162163
return err
163164
}
165+
if len(keys) == 0 {
166+
return errors.New("--keys must contain at least one key object")
167+
}
164168

165169
resp, err := Api.Keys().BulkUpdate(
166170
projectId,
@@ -183,6 +187,9 @@ var keyBulkCreateCmd = &cobra.Command{
183187
if err := json.Unmarshal([]byte(bulkCreateKeys), &keys); err != nil {
184188
return err
185189
}
190+
if len(keys) == 0 {
191+
return errors.New("--keys must contain at least one key object")
192+
}
186193

187194
resp, err := Api.Keys().Create(
188195
projectId,

cmd/key_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,74 @@ func TestKeyBulkCreate(t *testing.T) {
162162
}
163163
}
164164

165+
func TestKeyBulkUpdate_UseAutomationsFalse(t *testing.T) {
166+
client, mux, _, teardown := setup()
167+
defer teardown()
168+
169+
mux.HandleFunc(
170+
fmt.Sprintf("/api2/projects/%s/keys", testProjectID),
171+
func(w http.ResponseWriter, r *http.Request) {
172+
w.Header().Set("Content-Type", "application/json")
173+
testMethod(t, r, "PUT")
174+
testHeader(t, r, "X-Api-Token", testApiToken)
175+
176+
data := `{"keys":[{"key_id":123,"description":"Updated"}],"use_automations":false}`
177+
req := new(bytes.Buffer)
178+
_ = json.Compact(req, []byte(data))
179+
testBody(t, r, req.String())
180+
181+
_, _ = fmt.Fprint(w, `{
182+
"project_id": "`+testProjectID+`",
183+
"keys": [{"key_id": 123}]
184+
}`)
185+
})
186+
187+
keysJSON := `[{"key_id":123,"description":"Updated"}]`
188+
args := []string{"key", "bulk-update", "--keys=" + keysJSON, "--project-id=" + testProjectID, "--use-automations=false"}
189+
rootCmd.SetArgs(args)
190+
keyBulkUpdateCmd.PreRun = func(cmd *cobra.Command, args []string) {
191+
Api = client
192+
}
193+
194+
if err := rootCmd.Execute(); err != nil {
195+
t.Errorf("Expected no error, got %v", err)
196+
}
197+
}
198+
199+
func TestKeyBulkCreate_UseAutomationsFalse(t *testing.T) {
200+
client, mux, _, teardown := setup()
201+
defer teardown()
202+
203+
mux.HandleFunc(
204+
fmt.Sprintf("/api2/projects/%s/keys", testProjectID),
205+
func(w http.ResponseWriter, r *http.Request) {
206+
w.Header().Set("Content-Type", "application/json")
207+
testMethod(t, r, "POST")
208+
testHeader(t, r, "X-Api-Token", testApiToken)
209+
210+
data := `{"keys":[{"key_name":"welcome","platforms":["web"],"tags":[]}],"use_automations":false}`
211+
req := new(bytes.Buffer)
212+
_ = json.Compact(req, []byte(data))
213+
testBody(t, r, req.String())
214+
215+
_, _ = fmt.Fprint(w, `{
216+
"project_id": "`+testProjectID+`",
217+
"keys": [{"key_id": 789}]
218+
}`)
219+
})
220+
221+
keysJSON := `[{"key_name":"welcome","platforms":["web"],"tags":[]}]`
222+
args := []string{"key", "bulk-create", "--keys=" + keysJSON, "--project-id=" + testProjectID, "--use-automations=false"}
223+
rootCmd.SetArgs(args)
224+
keyBulkCreateCmd.PreRun = func(cmd *cobra.Command, args []string) {
225+
Api = client
226+
}
227+
228+
if err := rootCmd.Execute(); err != nil {
229+
t.Errorf("Expected no error, got %v", err)
230+
}
231+
}
232+
165233
func TestKeyBulkUpdate_InvalidJSON(t *testing.T) {
166234
client, _, _, teardown := setup()
167235
defer teardown()
@@ -193,3 +261,35 @@ func TestKeyBulkCreate_InvalidJSON(t *testing.T) {
193261
t.Error("Expected error for invalid JSON, got nil")
194262
}
195263
}
264+
265+
func TestKeyBulkUpdate_EmptyKeys(t *testing.T) {
266+
client, _, _, teardown := setup()
267+
defer teardown()
268+
269+
args := []string{"key", "bulk-update", "--keys=[]", "--project-id=" + testProjectID}
270+
rootCmd.SetArgs(args)
271+
keyBulkUpdateCmd.PreRun = func(cmd *cobra.Command, args []string) {
272+
Api = client
273+
}
274+
275+
err := rootCmd.Execute()
276+
if err == nil {
277+
t.Error("Expected error for empty keys, got nil")
278+
}
279+
}
280+
281+
func TestKeyBulkCreate_EmptyKeys(t *testing.T) {
282+
client, _, _, teardown := setup()
283+
defer teardown()
284+
285+
args := []string{"key", "bulk-create", "--keys=[]", "--project-id=" + testProjectID}
286+
rootCmd.SetArgs(args)
287+
keyBulkCreateCmd.PreRun = func(cmd *cobra.Command, args []string) {
288+
Api = client
289+
}
290+
291+
err := rootCmd.Execute()
292+
if err == nil {
293+
t.Error("Expected error for empty keys, got nil")
294+
}
295+
}

0 commit comments

Comments
 (0)