From 5146c247bffcd03c040e2928d960488c451a7f02 Mon Sep 17 00:00:00 2001 From: Yogesh Date: Sun, 8 Dec 2024 18:04:46 +0100 Subject: [PATCH] Fix model for get, create and update group variable GitLab has inconsistent naming of the filed for hidden variables. It seems for keeping UI consistent, GitLab chose to introduce different naming for the same field. This could have been easy handled in UI by setting two variables on API request instead of introducing combined field for UI. I have added 2 test cases for create and update project API as per the gitlab.com API that I also manually tested. --- group_variables.go | 2 +- group_variables_test.go | 72 ++++++++++++++++++++++++++++++++++------- 2 files changed, 61 insertions(+), 13 deletions(-) diff --git a/group_variables.go b/group_variables.go index b3e7bdfe6..7978e594e 100644 --- a/group_variables.go +++ b/group_variables.go @@ -41,7 +41,7 @@ type GroupVariable struct { VariableType VariableTypeValue `json:"variable_type"` Protected bool `json:"protected"` Masked bool `json:"masked"` - MaskedAndHidden bool `json:"masked_and_hidden"` + Hidden bool `json:"hidden"` Raw bool `json:"raw"` EnvironmentScope string `json:"environment_scope"` Description string `json:"description"` diff --git a/group_variables_test.go b/group_variables_test.go index 153317b45..0c0de84bb 100644 --- a/group_variables_test.go +++ b/group_variables_test.go @@ -29,7 +29,7 @@ func TestListGroupVariabless(t *testing.T) { mux.HandleFunc("/api/v4/groups/1/variables", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, http.MethodGet) - fmt.Fprint(w, `[{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true,"masked_and_hidden": true}]`) + fmt.Fprint(w, `[{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true,"hidden": true}]`) }) variables, _, err := client.GroupVariables.ListVariables(1, &ListGroupVariablesOptions{}) @@ -39,11 +39,11 @@ func TestListGroupVariabless(t *testing.T) { want := []*GroupVariable{ { - Key: "TEST_VARIABLE_1", - Value: "test1", - Protected: false, - Masked: true, - MaskedAndHidden: true, + Key: "TEST_VARIABLE_1", + Value: "test1", + Protected: false, + Masked: true, + Hidden: true, }, } @@ -59,7 +59,7 @@ func TestGetGroupVariable(t *testing.T) { func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, http.MethodGet) testParams(t, r, "filter%5Benvironment_scope%5D=prod") - fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true,"masked_and_hidden": true}`) + fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true,"hidden": false}`) }) variable, _, err := client.GroupVariables.GetVariable(1, "TEST_VARIABLE_1", &GetGroupVariableOptions{Filter: &VariableFilter{EnvironmentScope: "prod"}}) @@ -67,7 +67,7 @@ func TestGetGroupVariable(t *testing.T) { t.Errorf("GroupVariables.GetVariable returned error: %v", err) } - want := &GroupVariable{Key: "TEST_VARIABLE_1", Value: "test1", Protected: false, Masked: true, MaskedAndHidden: true} + want := &GroupVariable{Key: "TEST_VARIABLE_1", Value: "test1", Protected: false, Masked: true, Hidden: false} if !reflect.DeepEqual(want, variable) { t.Errorf("GroupVariables.GetVariable returned %+v, want %+v", variable, want) } @@ -79,7 +79,35 @@ func TestCreateGroupVariable(t *testing.T) { mux.HandleFunc("/api/v4/groups/1/variables", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, http.MethodPost) - fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true,"masked_and_hidden": true}`) + fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","value":"test1","protected": false,"masked": true,"hidden": false}`) + }) + + opt := &CreateGroupVariableOptions{ + Key: Ptr("TEST_VARIABLE_1"), + Value: Ptr("test1"), + Protected: Ptr(false), + Masked: Ptr(true), + MaskedAndHidden: Ptr(false), + } + + variable, _, err := client.GroupVariables.CreateVariable(1, opt, nil) + if err != nil { + t.Errorf("GroupVariables.CreateVariable returned error: %v", err) + } + + want := &GroupVariable{Key: "TEST_VARIABLE_1", Value: "test1", Protected: false, Masked: true, Hidden: false} + if !reflect.DeepEqual(want, variable) { + t.Errorf("GroupVariables.CreateVariable returned %+v, want %+v", variable, want) + } +} + +func TestCreateGroupVariable_MaskedAndHidden(t *testing.T) { + mux, client := setup(t) + + mux.HandleFunc("/api/v4/groups/1/variables", + func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodPost) + fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","protected": false,"masked": true,"hidden": true}`) }) opt := &CreateGroupVariableOptions{ @@ -95,7 +123,7 @@ func TestCreateGroupVariable(t *testing.T) { t.Errorf("GroupVariables.CreateVariable returned error: %v", err) } - want := &GroupVariable{Key: "TEST_VARIABLE_1", Value: "test1", Protected: false, Masked: true, MaskedAndHidden: true} + want := &GroupVariable{Key: "TEST_VARIABLE_1", Protected: false, Masked: true, Hidden: true} if !reflect.DeepEqual(want, variable) { t.Errorf("GroupVariables.CreateVariable returned %+v, want %+v", variable, want) } @@ -128,7 +156,27 @@ func TestUpdateGroupVariable(t *testing.T) { mux.HandleFunc("/api/v4/groups/1/variables/TEST_VARIABLE_1", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, http.MethodPut) - fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true}`) + fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true,"hidden": false}`) + }) + + variable, _, err := client.GroupVariables.UpdateVariable(1, "TEST_VARIABLE_1", &UpdateGroupVariableOptions{}) + if err != nil { + t.Errorf("GroupVariables.UpdateVariable returned error: %v", err) + } + + want := &GroupVariable{Key: "TEST_VARIABLE_1", Value: "test1", Protected: false, Masked: true, Hidden: false} + if !reflect.DeepEqual(want, variable) { + t.Errorf("Groups.UpdatedGroup returned %+v, want %+v", variable, want) + } +} + +func TestUpdateGroupVariable_MaskedAndHidden(t *testing.T) { + mux, client := setup(t) + + mux.HandleFunc("/api/v4/groups/1/variables/TEST_VARIABLE_1", + func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodPut) + fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","protected": false,"masked": true,"hidden": true}`) }) variable, _, err := client.GroupVariables.UpdateVariable(1, "TEST_VARIABLE_1", &UpdateGroupVariableOptions{}) @@ -136,7 +184,7 @@ func TestUpdateGroupVariable(t *testing.T) { t.Errorf("GroupVariables.UpdateVariable returned error: %v", err) } - want := &GroupVariable{Key: "TEST_VARIABLE_1", Value: "test1", Protected: false, Masked: true} + want := &GroupVariable{Key: "TEST_VARIABLE_1", Protected: false, Masked: true, Hidden: true} if !reflect.DeepEqual(want, variable) { t.Errorf("Groups.UpdatedGroup returned %+v, want %+v", variable, want) }