-
Notifications
You must be signed in to change notification settings - Fork 57
Add bulk key operations: bulk-create, bulk-update, bulk-delete #212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lechuckcaptain
wants to merge
4
commits into
lokalise:main
Choose a base branch
from
lechuckcaptain:feature/bulk-key-operations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
84d7289
Add bulk-create, bulk-update, and bulk-delete key subcommands
lechuckcaptain 87c549f
refactor: decouple flag state, use int64 for key IDs, add body assert…
lechuckcaptain 3c67830
Add empty-keys validation, --use-automations=false tests
lechuckcaptain 2b9a549
Add empty key-ids validation for bulk-delete, assert error messages i…
lechuckcaptain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "testing" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func TestKeyBulkDelete(t *testing.T) { | ||
| client, mux, _, teardown := setup() | ||
| defer teardown() | ||
|
|
||
| mux.HandleFunc( | ||
| fmt.Sprintf("/api2/projects/%s/keys", testProjectID), | ||
| func(w http.ResponseWriter, r *http.Request) { | ||
| w.Header().Set("Content-Type", "application/json") | ||
| testMethod(t, r, "DELETE") | ||
| testHeader(t, r, "X-Api-Token", testApiToken) | ||
|
|
||
| data := `{"keys":[123,456]}` | ||
| req := new(bytes.Buffer) | ||
| _ = json.Compact(req, []byte(data)) | ||
| testBody(t, r, req.String()) | ||
|
|
||
| _, _ = fmt.Fprint(w, `{ | ||
| "project_id": "`+testProjectID+`", | ||
| "keys_removed": true, | ||
| "keys_locked": 0 | ||
| }`) | ||
| }) | ||
|
|
||
| args := []string{"key", "bulk-delete", "--key-ids=123,456", "--project-id=" + testProjectID} | ||
| rootCmd.SetArgs(args) | ||
| keyBulkDeleteCmd.PreRun = func(cmd *cobra.Command, args []string) { | ||
| Api = client | ||
| } | ||
|
|
||
| if err := rootCmd.Execute(); err != nil { | ||
| t.Errorf("Expected no error, got %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestKeyBulkUpdate(t *testing.T) { | ||
| client, mux, _, teardown := setup() | ||
| defer teardown() | ||
|
|
||
| mux.HandleFunc( | ||
| fmt.Sprintf("/api2/projects/%s/keys", testProjectID), | ||
| func(w http.ResponseWriter, r *http.Request) { | ||
| w.Header().Set("Content-Type", "application/json") | ||
| testMethod(t, r, "PUT") | ||
| testHeader(t, r, "X-Api-Token", testApiToken) | ||
|
|
||
| _, _ = fmt.Fprint(w, `{ | ||
| "project_id": "`+testProjectID+`", | ||
| "keys": [ | ||
| { | ||
| "key_id": 123, | ||
| "key_name": { | ||
| "ios": "updated_key", | ||
| "android": "updated_key", | ||
| "web": "updated_key", | ||
| "other": "updated_key" | ||
| }, | ||
| "platforms": ["web"], | ||
| "filenames": {}, | ||
| "description": "Updated description", | ||
| "tags": ["updated"], | ||
| "comments": [], | ||
| "screenshots": [], | ||
| "translations": [] | ||
| } | ||
| ] | ||
| }`) | ||
| }) | ||
|
|
||
| keysJSON := `[{"key_id":123,"description":"Updated description","tags":["updated"]}]` | ||
| args := []string{"key", "bulk-update", "--keys=" + keysJSON, "--project-id=" + testProjectID} | ||
| rootCmd.SetArgs(args) | ||
| keyBulkUpdateCmd.PreRun = func(cmd *cobra.Command, args []string) { | ||
| Api = client | ||
| } | ||
|
|
||
| if err := rootCmd.Execute(); err != nil { | ||
| t.Errorf("Expected no error, got %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestKeyBulkCreate(t *testing.T) { | ||
| client, mux, _, teardown := setup() | ||
| defer teardown() | ||
|
|
||
| mux.HandleFunc( | ||
| fmt.Sprintf("/api2/projects/%s/keys", testProjectID), | ||
| func(w http.ResponseWriter, r *http.Request) { | ||
| w.Header().Set("Content-Type", "application/json") | ||
| testMethod(t, r, "POST") | ||
| testHeader(t, r, "X-Api-Token", testApiToken) | ||
|
|
||
| _, _ = fmt.Fprint(w, `{ | ||
| "project_id": "`+testProjectID+`", | ||
| "keys": [ | ||
| { | ||
| "key_id": 789, | ||
| "key_name": { | ||
| "ios": "welcome", | ||
| "android": "welcome", | ||
| "web": "welcome", | ||
| "other": "welcome" | ||
| }, | ||
| "platforms": ["web"], | ||
| "filenames": {}, | ||
| "description": "", | ||
| "tags": [], | ||
| "comments": [], | ||
| "screenshots": [], | ||
| "translations": [] | ||
| }, | ||
| { | ||
| "key_id": 790, | ||
| "key_name": { | ||
| "ios": "goodbye", | ||
| "android": "goodbye", | ||
| "web": "goodbye", | ||
| "other": "goodbye" | ||
| }, | ||
| "platforms": ["ios", "android"], | ||
| "filenames": {}, | ||
| "description": "", | ||
| "tags": ["v2"], | ||
| "comments": [], | ||
| "screenshots": [], | ||
| "translations": [] | ||
| } | ||
| ] | ||
| }`) | ||
| }) | ||
|
|
||
| keysJSON := `[{"key_name":"welcome","platforms":["web"],"tags":[]},{"key_name":"goodbye","platforms":["ios","android"],"tags":["v2"]}]` | ||
| args := []string{"key", "bulk-create", "--keys=" + keysJSON, "--project-id=" + testProjectID} | ||
| rootCmd.SetArgs(args) | ||
| keyBulkCreateCmd.PreRun = func(cmd *cobra.Command, args []string) { | ||
| Api = client | ||
| } | ||
|
|
||
| if err := rootCmd.Execute(); err != nil { | ||
| t.Errorf("Expected no error, got %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestKeyBulkUpdate_InvalidJSON(t *testing.T) { | ||
| client, _, _, teardown := setup() | ||
| defer teardown() | ||
|
|
||
| args := []string{"key", "bulk-update", "--keys=not-json", "--project-id=" + testProjectID} | ||
| rootCmd.SetArgs(args) | ||
| keyBulkUpdateCmd.PreRun = func(cmd *cobra.Command, args []string) { | ||
| Api = client | ||
| } | ||
|
|
||
| err := rootCmd.Execute() | ||
| if err == nil { | ||
| t.Error("Expected error for invalid JSON, got nil") | ||
| } | ||
| } | ||
|
|
||
| func TestKeyBulkCreate_InvalidJSON(t *testing.T) { | ||
| client, _, _, teardown := setup() | ||
| defer teardown() | ||
|
|
||
| args := []string{"key", "bulk-create", "--keys=not-json", "--project-id=" + testProjectID} | ||
| rootCmd.SetArgs(args) | ||
| keyBulkCreateCmd.PreRun = func(cmd *cobra.Command, args []string) { | ||
| Api = client | ||
| } | ||
|
|
||
| err := rootCmd.Execute() | ||
| if err == nil { | ||
| t.Error("Expected error for invalid JSON, got nil") | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.