-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprobation_test.go
284 lines (250 loc) · 9.06 KB
/
probation_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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package main
import (
"testing"
"os"
"path/filepath"
"fmt"
"strings"
"errors"
)
func mockProbationVersion(reg, project, asset, version string) error {
project_dir := filepath.Join(reg, project)
err := os.Mkdir(project_dir, 0755)
if err != nil {
return fmt.Errorf("failed to create a project directory; %w", err)
}
err = os.WriteFile(
filepath.Join(project_dir, permissionsFileName),
[]byte(`{ "owners": [], "uploaders": [] }`),
0644,
)
if err != nil {
return fmt.Errorf("failed to create some mock permissions; %w", err)
}
asset_dir := filepath.Join(project_dir, asset)
err = os.Mkdir(asset_dir, 0755)
if err != nil {
return fmt.Errorf("failed to create an asset directory; %w", err)
}
version_dir := filepath.Join(asset_dir, version)
err = os.Mkdir(version_dir, 0755)
if err != nil {
return fmt.Errorf("failed to create a version directory; %w", err)
}
err = os.WriteFile(
filepath.Join(version_dir, summaryFileName),
[]byte(`
{
"upload_user_id": "cynthia",
"upload_start": "2020-02-02T02:02:02Z",
"upload_finish": "2020-02-02T02:02:20Z",
"on_probation": true
}`),
0644,
)
if err != nil {
return fmt.Errorf("failed to create a mock summary; %w", err)
}
contents := "ARGLEFARGLE"
err = os.WriteFile(filepath.Join(version_dir, "random"), []byte(contents), 0644)
if err != nil {
return fmt.Errorf("failed to create some mock files; %w", err)
}
err = os.WriteFile(filepath.Join(project_dir, usageFileName), []byte(fmt.Sprintf(`{ "total": %d }`, len(contents))), 0644)
if err != nil {
return fmt.Errorf("failed to create mock usage; %w", err)
}
return nil
}
func TestApproveProbationHandler(t *testing.T) {
reg, err := constructMockRegistry()
if err != nil {
t.Fatalf("failed to create the registry; %v", err)
}
project := "dawn"
asset := "sinnoh"
version := "foo"
err = mockProbationVersion(reg, project, asset, version)
if err != nil {
t.Fatalf("failed to create a mock version; %v", err)
}
reqpath, err := dumpRequest("approve_probation", fmt.Sprintf(`{ "project": "%s", "asset": "%s", "version": "%s" }`, project, asset, version))
if err != nil {
t.Fatalf("failed to dump a request type; %v", err)
}
// Lack of authorization fails.
globals := newGlobalConfiguration(reg)
err = approveProbationHandler(reqpath, &globals)
if err == nil || !strings.Contains(err.Error(), "not authorized") {
t.Fatalf("failed to approve probation; %v", err)
}
self, err := identifyUser(reg)
if err != nil {
t.Fatalf("failed to identify self; %v", err)
}
globals.Administrators = append(globals.Administrators, self)
err = approveProbationHandler(reqpath, &globals)
if err != nil {
t.Fatalf("failed to approve probation; %v", err)
}
summ, err := readSummary(filepath.Join(reg, project, asset, version))
if err != nil {
t.Fatalf("failed to read the summary; %v", err)
}
if summ.OnProbation != nil {
t.Fatal("version should not be on probation after approval")
}
latest, err := readLatest(filepath.Join(reg, project, asset))
if err != nil {
t.Fatalf("failed to read the latest; %v", err)
}
if latest.Version != version {
t.Fatal("latest version should be updated after approval")
}
// Checking that logs are created.
logs, err := readAllLogs(reg)
if err != nil {
t.Fatalf("failed to read the logs; %v", err)
}
if len(logs) != 1 {
t.Fatalf("expected exactly one entry in the log directory")
}
if logs[0].Type != "add-version" ||
logs[0].Project == nil || *(logs[0].Project) != project ||
logs[0].Asset == nil || *(logs[0].Asset) != asset ||
logs[0].Version == nil || *(logs[0].Version) != version ||
logs[0].Latest == nil || !*(logs[0].Latest) {
t.Fatalf("unexpected contents for first log in %q", reg)
}
// Repeated approval attempts fail.
err = approveProbationHandler(reqpath, &globals)
if err == nil || !strings.Contains(err.Error(), "not on probation") {
t.Fatal("expected failure for non-probational version")
}
}
func TestApproveProbationHandlerNotLatest(t *testing.T) {
for _, other_latest := range []bool{ true, false } {
reg, err := constructMockRegistry()
if err != nil {
t.Fatalf("failed to create the registry; %v", err)
}
project := "dawn"
asset := "sinnoh"
version := "foo"
err = mockProbationVersion(reg, project, asset, version)
if err != nil {
t.Fatalf("failed to create a mock version; %v", err)
}
// Mocking up another version that was either earlier or later.
version2 := "bar"
project_dir := filepath.Join(reg, project)
asset_dir := filepath.Join(project_dir, asset)
version_dir := filepath.Join(asset_dir, version2)
err = os.MkdirAll(version_dir, 0755)
if err != nil {
t.Fatalf("failed to create a new version directory; %v", err)
}
var new_time string
if other_latest {
new_time = "2999-02-02T02:02:02Z"
} else {
new_time = "1999-02-02T02:02:02Z"
}
err = os.WriteFile(
filepath.Join(version_dir, summaryFileName),
[]byte(fmt.Sprintf(`
{
"upload_user_id": "cynthia",
"upload_start": "2020-02-02T02:02:02Z",
"upload_finish": "%s",
"on_probation": true
}`, new_time)),
0644,
)
if err != nil {
t.Fatalf("failed to create a mock summary; %v", err)
}
err = os.WriteFile(filepath.Join(asset_dir, latestFileName), []byte(fmt.Sprintf(`{ "version": "%s" }`, version2)), 0644)
if err != nil {
t.Fatalf("failed to write the latest file; %v", err)
}
// Running the approval.
reqpath, err := dumpRequest("approve_probation", fmt.Sprintf(`{ "project": "%s", "asset": "%s", "version": "%s" }`, project, asset, version))
if err != nil {
t.Fatalf("failed to dump a request type; %v", err)
}
self, err := identifyUser(reg)
if err != nil {
t.Fatalf("failed to identify self; %v", err)
}
globals := newGlobalConfiguration(reg)
globals.Administrators = append(globals.Administrators, self)
err = approveProbationHandler(reqpath, &globals)
if err != nil {
t.Fatalf("failed to approve probation; %v", err)
}
// Checking that we are (or not) the latest version.
latest, err := readLatest(filepath.Join(reg, project, asset))
if err != nil {
t.Fatalf("failed to read the latest; %v", err)
}
if other_latest {
if latest.Version != version2 {
t.Fatal("latest version should not be updated after approval")
}
} else {
if latest.Version != version {
t.Fatal("latest version should be updated after approval")
}
}
// Checking that a suitable log is created.
logs, err := readAllLogs(reg)
if err != nil {
t.Fatalf("failed to read the logs; %v", err)
}
if len(logs) != 1 {
t.Fatalf("expected exactly one entry in the log directory")
}
if logs[0].Latest == nil || *(logs[0].Latest) == other_latest {
t.Fatalf("unexpected latest flag after probation approval for first log in %q", reg)
}
}
}
func TestRejectProbationHandler(t *testing.T) {
reg, err := constructMockRegistry()
if err != nil {
t.Fatalf("failed to create the registry; %v", err)
}
globals := newGlobalConfiguration(reg)
project := "dawn"
asset := "sinnoh"
version := "foo"
err = mockProbationVersion(reg, project, asset, version)
if err != nil {
t.Fatalf("failed to create a mock version; %v", err)
}
reqpath, err := dumpRequest("reject_probation", fmt.Sprintf(`{ "project": "%s", "asset": "%s", "version": "%s" }`, project, asset, version))
if err != nil {
t.Fatalf("failed to dump a request type; %v", err)
}
self, err := identifyUser(reg)
if err != nil {
t.Fatalf("failed to identify self; %v", err)
}
globals.Administrators = append(globals.Administrators, self)
err = rejectProbationHandler(reqpath, &globals)
if err != nil {
t.Fatalf("failed to reject probation; %v", err)
}
project_dir := filepath.Join(reg, project)
if _, err := os.Stat(filepath.Join(project_dir, asset, version)); err == nil || !errors.Is(err, os.ErrNotExist) {
t.Fatal("failed to delete the probational directory")
}
usage, err := readUsage(project_dir)
if err != nil {
t.Fatalf("failed to read the project usage; %v", err)
}
if usage.Total != 0 {
t.Fatalf("expected the project usage to be zero, not %d", usage.Total)
}
}