-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatch_test.go
178 lines (172 loc) · 5.25 KB
/
patch_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
package ma_test
import (
"encoding/json"
"io"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/bzimmer/ma"
)
func decode(a *assert.Assertions, r io.Reader) map[string]any {
data := make(map[string]any)
dec := json.NewDecoder(r)
a.NoError(dec.Decode(&data))
return data
}
func newPatchTestMux(a *assert.Assertions) http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/image/GH8UQ9-0", func(_ http.ResponseWriter, _ *http.Request) {
a.Fail("should not be called")
})
mux.HandleFunc("/image/B2fHSt7-0", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "testdata/image_B2fHSt7-0.json")
})
mux.HandleFunc("/image/B2fHSt7-1", func(w http.ResponseWriter, r *http.Request) {
data := decode(a, r.Body)
a.Contains(data, "Latitude")
http.ServeFile(w, r, "testdata/image_B2fHSt7-0.json")
})
mux.HandleFunc("/image/B2fHSt7-2", func(_ http.ResponseWriter, _ *http.Request) {
a.Fail("should not be called")
})
mux.HandleFunc("/image/B2fHSt7-3", func(w http.ResponseWriter, r *http.Request) {
data := decode(a, r.Body)
a.Contains(data, "KeywordArray")
a.Empty(data["KeywordArray"])
http.ServeFile(w, r, "testdata/image_B2fHSt7-0.json")
})
mux.HandleFunc("/image/B2fHSt7-4", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNotFound)
enc := json.NewEncoder(w)
a.NoError(enc.Encode(map[string]any{
"Response": map[string]any{
"Uri": "/api/v2/image/B2fHSt7-4?_pretty=true",
"Locator": "Image",
"LocatorType": "Object",
"UriDescription": "Image by key",
"EndpointType": "Image",
},
"Code": 404,
"Message": "Not Found",
}))
})
mux.HandleFunc("/album/RM4BL3", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNotFound)
enc := json.NewEncoder(w)
a.NoError(enc.Encode(map[string]any{
"Response": map[string]any{
"Uri": "/api/v2/album/RM4BL3?_pretty=true",
"Locator": "Album",
"LocatorType": "Object",
"UriDescription": "Album by key",
"EndpointType": "Album",
},
"Code": 404,
"Message": "Not Found",
}))
})
mux.HandleFunc("/album/RM4BL2", func(w http.ResponseWriter, r *http.Request) {
data := decode(a, r.Body)
a.Contains(data, "Name")
a.Contains(data, "UrlName")
http.ServeFile(w, r, "testdata/album_RM4BL2.json")
})
mux.HandleFunc("/album/RM4BLQ", func(w http.ResponseWriter, r *http.Request) {
data := decode(a, r.Body)
a.Contains(data, "Name")
a.Contains(data, "UrlName")
a.Equal("Foo-Bar", data["UrlName"])
http.ServeFile(w, r, "testdata/album_RM4BL2.json")
})
return mux
}
func TestPatch(t *testing.T) {
for _, tt := range []harness{
{
name: "dry run",
args: []string{"patch", "image", "--dryrun", "--keyword", "foo", "GH8UQ9-0"},
counters: map[string]int{"patch.image.dryrun": 1},
},
{
name: "keyword",
args: []string{"patch", "image", "--keyword", "foo", "B2fHSt7-0"},
counters: map[string]int{"patch.image": 1},
},
{
name: "latitude",
args: []string{"patch", "image", "--latitude", "48.4321", "B2fHSt7-1"},
counters: map[string]int{"patch.image": 1},
},
{
name: "no patches image",
args: []string{"patch", "image", "B2fHSt7-2"},
},
{
name: "no patches album",
args: []string{"patch", "album", "RM4BLQ"},
},
{
name: "empty keywords",
args: []string{"patch", "image", "--dryrun", "--keyword", "", "B2fHSt7-3"},
counters: map[string]int{"patch.image.dryrun": 1},
},
{
name: "album",
args: []string{"patch", "album",
"--name", "2021-07-04 Fourth of July", "--urlname", "2021-07-04-Fourth-of-July", "RM4BL2"},
counters: map[string]int{"patch.album": 1},
},
{
name: "album with auto url naming",
args: []string{"patch", "album", "--name", "foo bar", "--auto", "RM4BLQ"},
counters: map[string]int{"patch.album": 1},
},
{
name: "invalid url name",
args: []string{"patch", "album", "--urlname", "this-is-invalid", "RM4BL2"},
err: ma.ErrInvalidURLName.Error(),
},
{
name: "more than one album key",
args: []string{"patch", "album", "RM4BL2", "XM4BL2"},
err: "expected only one albumKey argument",
},
{
name: "no album keys",
args: []string{"patch", "album"},
err: "expected one albumKey argument",
},
{
name: "both urlname and auto urlname",
args: []string{"patch", "album", "--urlname", "Foo-Bar", "--auto", "XM4BL2"},
err: "only one of `auto` or `urlname` may be specified",
},
{
name: "invalid urlname",
args: []string{"patch", "album", "--name", "bar baz", "--urlname", "foo bar", "XM4BL2"},
err: "node url name must start with a number or capital letter",
},
{
name: "auto urlname without name",
args: []string{"patch", "album", "--auto", "XM4BL2"},
err: "cannot specify `auto` without `name`",
},
{
name: "patch album 404",
args: []string{"patch", "album", "--name", "bar foo", "RM4BL3"},
err: http.StatusText(http.StatusNotFound),
},
{
name: "patch image 404",
args: []string{"patch", "image", "--title", "something bar foo", "B2fHSt7-4"},
err: http.StatusText(http.StatusNotFound),
},
} {
tt := tt
t.Run(tt.name, func(t *testing.T) {
a := assert.New(t)
mux := newPatchTestMux(a)
run(t, &tt, mux, ma.CommandPatch)
})
}
}