-
Notifications
You must be signed in to change notification settings - Fork 254
/
Copy pathenvironments_test.go
109 lines (94 loc) · 2.54 KB
/
environments_test.go
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
package gitlab
import (
"fmt"
"net/http"
"strconv"
"testing"
"github.com/mvisonneau/gitlab-ci-pipelines-exporter/pkg/schemas"
"github.com/stretchr/testify/assert"
)
func TestGetProjectEnvironments(t *testing.T) {
mux, server, c := getMockedClient()
defer server.Close()
mux.HandleFunc(fmt.Sprintf("/api/v4/projects/foo/environments"),
func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "GET", r.Method)
assert.Equal(t, []string{"100"}, r.URL.Query()["per_page"])
currentPage, err := strconv.Atoi(r.URL.Query()["page"][0])
assert.NoError(t, err)
w.Header().Add("X-Total-Pages", "2")
w.Header().Add("X-Page", strconv.Itoa(currentPage))
w.Header().Add("X-Next-Page", strconv.Itoa(currentPage+1))
if currentPage == 1 {
fmt.Fprint(w, `[{"name":"main"},{"id":1337,"name":"dev"}]`)
return
}
fmt.Fprint(w, `[]`)
})
mux.HandleFunc(fmt.Sprintf("/api/v4/projects/0/environments"),
func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
})
envs, err := c.GetProjectEnvironments("foo", "^dev")
assert.NoError(t, err)
assert.Equal(t, map[int]string{1337: "dev"}, envs)
// Test invalid project
_, err = c.GetProjectEnvironments("0", "")
assert.Error(t, err)
// Test invalid regexp
_, err = c.GetProjectEnvironments("1", "[")
assert.Error(t, err)
}
func TestGetEnvironment(t *testing.T) {
mux, server, c := getMockedClient()
defer server.Close()
mux.HandleFunc("/api/v4/projects/foo/environments/1",
func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.Method, "GET")
fmt.Fprint(w, `
{
"id": 1,
"name": "foo",
"external_url": "https://foo.example.com",
"state": "available",
"last_deployment": {
"id": 2,
"iid": 30,
"ref": "bar",
"created_at": "2019-03-25T18:55:13.252Z",
"deployable": {
"status": "success",
"tag": false,
"duration": 21623.13423,
"user": {
"public_email": "foo@bar.com"
},
"commit": {
"short_id": "416d8ea1"
}
}
}
}`)
})
e, err := c.GetEnvironment("foo", 1)
assert.NoError(t, err)
assert.NotNil(t, e)
expectedEnv := schemas.Environment{
ProjectName: "foo",
ID: 1,
Name: "foo",
ExternalURL: "https://foo.example.com",
Available: true,
LatestDeployment: schemas.Deployment{
ID: 2,
RefKind: schemas.RefKindBranch,
RefName: "bar",
AuthorEmail: "foo@bar.com",
Timestamp: 1553540113,
DurationSeconds: 21623.13423,
CommitShortID: "416d8ea1",
Status: "success",
},
}
assert.Equal(t, expectedEnv, e)
}