1//
2// Copyright 2021, Sander van Harmelen
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//     http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17package gitlab
18
19import (
20	"fmt"
21	"log"
22	"net/http"
23	"reflect"
24	"testing"
25)
26
27func TestCreateLabel(t *testing.T) {
28	mux, server, client := setup(t)
29	defer teardown(server)
30
31	mux.HandleFunc("/api/v4/projects/1/labels", func(w http.ResponseWriter, r *http.Request) {
32		testMethod(t, r, http.MethodPost)
33		fmt.Fprint(w, `{"id":1, "name": "My Label", "color" : "#11FF22"}`)
34	})
35
36	// Create new label
37	l := &CreateLabelOptions{
38		Name:  String("My Label"),
39		Color: String("#11FF22"),
40	}
41	label, _, err := client.Labels.CreateLabel("1", l)
42
43	if err != nil {
44		log.Fatal(err)
45	}
46	want := &Label{ID: 1, Name: "My Label", Color: "#11FF22"}
47	if !reflect.DeepEqual(want, label) {
48		t.Errorf("Labels.CreateLabel returned %+v, want %+v", label, want)
49	}
50}
51
52func TestDeleteLabel(t *testing.T) {
53	mux, server, client := setup(t)
54	defer teardown(server)
55
56	mux.HandleFunc("/api/v4/projects/1/labels", func(w http.ResponseWriter, r *http.Request) {
57		testMethod(t, r, http.MethodDelete)
58	})
59
60	// Delete label
61	label := &DeleteLabelOptions{
62		Name: String("My Label"),
63	}
64
65	_, err := client.Labels.DeleteLabel("1", label)
66	if err != nil {
67		log.Fatal(err)
68	}
69}
70
71func TestUpdateLabel(t *testing.T) {
72	mux, server, client := setup(t)
73	defer teardown(server)
74
75	mux.HandleFunc("/api/v4/projects/1/labels", func(w http.ResponseWriter, r *http.Request) {
76		testMethod(t, r, http.MethodPut)
77		fmt.Fprint(w, `{"id":1, "name": "New Label", "color" : "#11FF23" , "description":"This is updated label"}`)
78	})
79
80	// Update label
81	l := &UpdateLabelOptions{
82		Name:        String("My Label"),
83		NewName:     String("New Label"),
84		Color:       String("#11FF23"),
85		Description: String("This is updated label"),
86	}
87
88	label, resp, err := client.Labels.UpdateLabel("1", l)
89
90	if resp == nil {
91		log.Fatal(err)
92	}
93	if err != nil {
94		log.Fatal(err)
95	}
96
97	want := &Label{ID: 1, Name: "New Label", Color: "#11FF23", Description: "This is updated label"}
98
99	if !reflect.DeepEqual(want, label) {
100		t.Errorf("Labels.UpdateLabel returned %+v, want %+v", label, want)
101	}
102}
103
104func TestSubscribeToLabel(t *testing.T) {
105	mux, server, client := setup(t)
106	defer teardown(server)
107
108	mux.HandleFunc("/api/v4/projects/1/labels/5/subscribe", func(w http.ResponseWriter, r *http.Request) {
109		testMethod(t, r, http.MethodPost)
110		fmt.Fprint(w, `{  "id" : 5, "name" : "bug", "color" : "#d9534f", "description": "Bug reported by user", "open_issues_count": 1, "closed_issues_count": 0, "open_merge_requests_count": 1, "subscribed": true,"priority": null}`)
111	})
112
113	label, _, err := client.Labels.SubscribeToLabel("1", "5")
114	if err != nil {
115		log.Fatal(err)
116	}
117	want := &Label{ID: 5, Name: "bug", Color: "#d9534f", Description: "Bug reported by user", OpenIssuesCount: 1, ClosedIssuesCount: 0, OpenMergeRequestsCount: 1, Subscribed: true}
118	if !reflect.DeepEqual(want, label) {
119		t.Errorf("Labels.SubscribeToLabel returned %+v, want %+v", label, want)
120	}
121}
122
123func TestUnsubscribeFromLabel(t *testing.T) {
124	mux, server, client := setup(t)
125	defer teardown(server)
126
127	mux.HandleFunc("/api/v4/projects/1/labels/5/unsubscribe", func(w http.ResponseWriter, r *http.Request) {
128		testMethod(t, r, http.MethodPost)
129	})
130
131	_, err := client.Labels.UnsubscribeFromLabel("1", "5")
132	if err != nil {
133		log.Fatal(err)
134	}
135}
136
137func TestListLabels(t *testing.T) {
138	mux, server, client := setup(t)
139	defer teardown(server)
140
141	mux.HandleFunc("/api/v4/projects/1/labels", func(w http.ResponseWriter, r *http.Request) {
142		testMethod(t, r, http.MethodGet)
143		fmt.Fprint(w, `[{  "id" : 5, "name" : "bug", "color" : "#d9534f", "description": "Bug reported by user", "open_issues_count": 1, "closed_issues_count": 0, "open_merge_requests_count": 1, "subscribed": true,"priority": null}]`)
144	})
145
146	o := &ListLabelsOptions{
147		ListOptions: ListOptions{
148			Page:    1,
149			PerPage: 10,
150		},
151	}
152	label, _, err := client.Labels.ListLabels("1", o)
153	if err != nil {
154		t.Log(err.Error() == "invalid ID type 1.1, the ID must be an int or a string")
155	}
156	want := []*Label{{ID: 5, Name: "bug", Color: "#d9534f", Description: "Bug reported by user", OpenIssuesCount: 1, ClosedIssuesCount: 0, OpenMergeRequestsCount: 1, Subscribed: true}}
157	if !reflect.DeepEqual(want, label) {
158		t.Errorf("Labels.ListLabels returned %+v, want %+v", label, want)
159	}
160}
161
162func TestGetLabel(t *testing.T) {
163	mux, server, client := setup(t)
164	defer teardown(server)
165
166	mux.HandleFunc("/api/v4/projects/1/labels/5", func(w http.ResponseWriter, r *http.Request) {
167		testMethod(t, r, http.MethodGet)
168		fmt.Fprint(w, `{  "id" : 5, "name" : "bug", "color" : "#d9534f", "description": "Bug reported by user", "open_issues_count": 1, "closed_issues_count": 0, "open_merge_requests_count": 1, "subscribed": true,"priority": null}`)
169	})
170
171	label, _, err := client.Labels.GetLabel("1", 5)
172	if err != nil {
173		t.Log(err)
174	}
175	want := &Label{ID: 5, Name: "bug", Color: "#d9534f", Description: "Bug reported by user", OpenIssuesCount: 1, ClosedIssuesCount: 0, OpenMergeRequestsCount: 1, Subscribed: true}
176	if !reflect.DeepEqual(want, label) {
177		t.Errorf("Labels.GetLabel returned %+v, want %+v", label, want)
178	}
179}
180