-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathclient_test.go
More file actions
155 lines (131 loc) · 3.85 KB
/
client_test.go
File metadata and controls
155 lines (131 loc) · 3.85 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package codacyclient
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"codacy/cli-v2/domain"
"github.com/stretchr/testify/assert"
)
func TestGetRequest_Success(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"data": "ok"}`))
}))
defer ts.Close()
initFlags := domain.InitFlags{ApiToken: "dummy"}
resp, err := getRequest(ts.URL, initFlags.ApiToken)
assert.NoError(t, err)
assert.Contains(t, string(resp), "ok")
}
func TestGetRequest_Failure(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
defer ts.Close()
initFlags := domain.InitFlags{ApiToken: "dummy"}
_, err := getRequest(ts.URL, initFlags.ApiToken)
assert.Error(t, err)
}
func TestGetPageAndGetAllPages(t *testing.T) {
type testItem struct{ Value int }
serverPages := []struct {
data []testItem
cursor string
}{
{[]testItem{{Value: 1}, {Value: 2}}, "next"},
{[]testItem{{Value: 3}}, ""},
}
calls := 0
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
resp := map[string]interface{}{
"data": serverPages[calls].data,
"pagination": map[string]interface{}{"cursor": serverPages[calls].cursor},
}
calls++
json.NewEncoder(w).Encode(resp)
}))
defer ts.Close()
initFlags := domain.InitFlags{ApiToken: "dummy"}
parser := func(body []byte) ([]testItem, string, error) {
var objmap map[string]json.RawMessage
if err := json.Unmarshal(body, &objmap); err != nil {
return nil, "", err
}
var items []testItem
if err := json.Unmarshal(objmap["data"], &items); err != nil {
return nil, "", err
}
var pagination struct {
Cursor string `json:"cursor"`
}
if objmap["pagination"] != nil {
_ = json.Unmarshal(objmap["pagination"], &pagination)
}
return items, pagination.Cursor, nil
}
// Test GetPage
calls = 0
items, cursor, err := GetPage[testItem](ts.URL, initFlags, parser)
assert.NoError(t, err)
assert.Len(t, items, 2)
assert.Equal(t, "next", cursor)
// Test getAllPages
calls = 0
allItems, err := getAllPages[testItem](ts.URL, initFlags, parser)
assert.NoError(t, err)
assert.Len(t, allItems, 3)
}
func TestGetDefaultToolPatternsConfig_Empty(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
resp := map[string]interface{}{
"data": []interface{}{},
"pagination": map[string]interface{}{"cursor": ""},
}
json.NewEncoder(w).Encode(resp)
}))
defer ts.Close()
CodacyApiBase = ts.URL
initFlags := domain.InitFlags{ApiToken: "dummy"}
patterns, err := GetDefaultToolPatternsConfigWithCodacyAPIBase(CodacyApiBase, initFlags, "tool-uuid", true)
assert.NoError(t, err)
assert.Empty(t, patterns)
}
func TestGetDefaultToolPatternsConfig_WithNonRecommended(t *testing.T) {
config := []domain.PatternDefinition{
{
Id: "internal_id_1",
Enabled: true,
},
{
Id: "internal_id_2",
Enabled: false,
},
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
resp := map[string]interface{}{
"data": config,
"pagination": map[string]interface{}{"cursor": ""},
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(resp)
}))
defer ts.Close()
expected := []domain.PatternConfiguration{
{
Enabled: true,
PatternDefinition: domain.PatternDefinition{
Id: "internal_id_1",
Enabled: true,
},
},
}
CodacyApiBase = ts.URL
initFlags := domain.InitFlags{ApiToken: "dummy"}
patterns, err := GetDefaultToolPatternsConfigWithCodacyAPIBase(CodacyApiBase, initFlags, "tool-uuid", true)
fmt.Println(len(patterns))
assert.NoError(t, err)
assert.Equal(t, expected, patterns)
}