Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
Fix model for get, create and update group variable
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
yogeshlonkar committed Dec 8, 2024
1 parent 0cb05aa commit 5146c24
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 13 deletions.
2 changes: 1 addition & 1 deletion group_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
72 changes: 60 additions & 12 deletions group_variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand All @@ -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,
},
}

Expand All @@ -59,15 +59,15 @@ 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"}})
if err != nil {
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)
}
Expand All @@ -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{
Expand All @@ -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)
}
Expand Down Expand Up @@ -128,15 +156,35 @@ 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{})
if err != nil {
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)
}
Expand Down

0 comments on commit 5146c24

Please sign in to comment.