1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
//
// Copyright 2021, Sander van Harmelen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package gitlab
import (
"fmt"
"net/http"
"reflect"
"testing"
)
func TestListGroupVariabless(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.MethodGet)
fmt.Fprint(w, `[{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true}]`)
})
variables, _, err := client.GroupVariables.ListVariables(1, &ListGroupVariablesOptions{})
if err != nil {
t.Errorf("GroupVariables.ListVariables returned error: %v", err)
}
want := []*GroupVariable{
{
Key: "TEST_VARIABLE_1",
Value: "test1",
Protected: false,
Masked: true,
},
}
if !reflect.DeepEqual(want, variables) {
t.Errorf("GroupVariables.ListVariablesreturned %+v, want %+v", variables, want)
}
}
func TestGetGroupVariable(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.MethodGet)
fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true}`)
})
variable, _, err := client.GroupVariables.GetVariable(1, "TEST_VARIABLE_1")
if err != nil {
t.Errorf("GroupVariables.GetVariable returned error: %v", err)
}
want := &GroupVariable{Key: "TEST_VARIABLE_1", Value: "test1", Protected: false, Masked: true}
if !reflect.DeepEqual(want, variable) {
t.Errorf("GroupVariables.GetVariable returned %+v, want %+v", variable, want)
}
}
func TestCreateGroupVariable(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","value": "test1","protected": false,"masked": true}`)
})
opt := &CreateGroupVariableOptions{
Key: String("TEST_VARIABLE_1"),
Value: String("test1"),
Protected: Bool(false),
Masked: Bool(true),
}
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}
if !reflect.DeepEqual(want, variable) {
t.Errorf("GroupVariables.CreateVariable returned %+v, want %+v", variable, want)
}
}
func TestDeleteGroupVariable(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.MethodDelete)
w.WriteHeader(http.StatusAccepted)
})
resp, err := client.GroupVariables.RemoveVariable(1, "TEST_VARIABLE_1")
if err != nil {
t.Errorf("GroupVariables.RemoveVariable returned error: %v", err)
}
want := http.StatusAccepted
got := resp.StatusCode
if got != want {
t.Errorf("GroupVariables.RemoveVariable returned %d, want %d", got, want)
}
}
func TestUpdateGroupVariable(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","value": "test1","protected": false,"masked": 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}
if !reflect.DeepEqual(want, variable) {
t.Errorf("Groups.UpdatedGroup returned %+v, want %+v", variable, want)
}
}
|