-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathrun_test.go
More file actions
161 lines (144 loc) · 4.14 KB
/
run_test.go
File metadata and controls
161 lines (144 loc) · 4.14 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
156
157
158
159
160
161
package infra
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"os"
"reflect"
"testing"
"time"
"github.com/dependabot/cli/internal/server"
"github.com/dependabot/cli/internal/model"
)
func Test_checkCredAccess(t *testing.T) {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal("Failed to create listener: ", err.Error())
}
testServer := &http.Server{
ReadHeaderTimeout: time.Second,
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-OAuth-Scopes", "repo, write:packages")
_, _ = w.Write([]byte("SUCCESS"))
}),
}
go func() {
_ = testServer.Serve(l)
}()
t.Cleanup(func() {
testServer.Shutdown(context.Background())
l.Close()
})
addr := fmt.Sprintf("http://127.0.0.1:%v", l.Addr().(*net.TCPAddr).Port)
t.Run("returns error if the credential has write access", func(t *testing.T) {
defaultApiEndpoint = addr
credentials := []model.Credential{{
"token": "ghp_fake",
}}
err := checkCredAccess(context.Background(), nil, credentials)
if !errors.Is(err, ErrWriteAccess) {
t.Error("unexpected error", err)
}
})
t.Run("it works with GitHub Enterprise", func(t *testing.T) {
defaultApiEndpoint = "http://example.com" // ensure it's not used
credentials := []model.Credential{{
"token": "ghp_fake",
}}
job := &model.Job{Source: model.Source{APIEndpoint: &addr}}
err := checkCredAccess(context.Background(), job, credentials)
if !errors.Is(err, ErrWriteAccess) {
t.Error("unexpected error", err)
}
})
}
func Test_expandEnvironmentVariables(t *testing.T) {
t.Run("injects environment variables", func(t *testing.T) {
os.Setenv("ENV1", "value1")
os.Setenv("ENV2", "value2")
api := &server.API{}
params := &RunParams{
Creds: []model.Credential{{
"type": "test",
"url": "url",
"username": "$ENV1",
"pass": "$ENV2",
}},
}
expandEnvironmentVariables(api, params)
if params.Creds[0]["username"] != "value1" {
t.Error("expected username to be injected", params.Creds[0]["username"])
}
if params.Creds[0]["pass"] != "value2" {
t.Error("expected pass to be injected", params.Creds[0]["pass"])
}
if api.Actual.Input.Credentials[0]["username"] != "$ENV1" {
t.Error("expected username NOT to be injected", api.Actual.Input.Credentials[0]["username"])
}
if api.Actual.Input.Credentials[0]["pass"] != "$ENV2" {
t.Error("expected pass NOT to be injected", api.Actual.Input.Credentials[0]["pass"])
}
})
}
func Test_generateIgnoreConditions(t *testing.T) {
const (
outputFileName = "test_output"
dependencyName = "dep1"
version = "1.0.0"
)
t.Run("generates ignore conditions", func(t *testing.T) {
runParams := &RunParams{
Output: outputFileName,
}
v := "1.0.0"
actual := &model.SmokeTest{
Output: []model.Output{{
Type: "create_pull_request",
Expect: model.UpdateWrapper{Data: model.CreatePullRequest{
Dependencies: []model.Dependency{{
Name: dependencyName,
Version: &v,
}},
}},
}},
}
if err := generateIgnoreConditions(runParams, actual); err != nil {
t.Fatal(err)
}
if len(actual.Input.Job.IgnoreConditions) != 1 {
t.Error("expected 1 ignore condition to be generated, got", len(actual.Input.Job.IgnoreConditions))
}
ignore := actual.Input.Job.IgnoreConditions[0]
if reflect.DeepEqual(ignore, &model.Condition{
DependencyName: dependencyName,
Source: outputFileName,
VersionRequirement: ">" + version,
}) {
t.Error("unexpected ignore condition", ignore)
}
})
t.Run("handles removed dependency", func(t *testing.T) {
runParams := &RunParams{
Output: outputFileName,
}
actual := &model.SmokeTest{
Output: []model.Output{{
Type: "create_pull_request",
Expect: model.UpdateWrapper{Data: model.CreatePullRequest{
Dependencies: []model.Dependency{{
Name: dependencyName,
Removed: true,
}},
}},
}},
}
if err := generateIgnoreConditions(runParams, actual); err != nil {
t.Fatal(err)
}
if len(actual.Input.Job.IgnoreConditions) != 0 {
t.Error("expected 0 ignore condition to be generated, got", len(actual.Input.Job.IgnoreConditions))
}
})
}