-
Notifications
You must be signed in to change notification settings - Fork 25
/
mobile_upload_test.go
215 lines (194 loc) · 7.53 KB
/
mobile_upload_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
package main
import (
"strings"
"testing"
"github.com/rainforestapp/rainforest-cli/rainforest"
"github.com/urfave/cli"
)
const (
testMobileAppPath = "test/testing.zip"
)
type fakeMobileUploadAPI struct {
getPresignedPOST func(fileExt string, siteID int, environmentID int, appSlot int) (*rainforest.RFPresignedPostData, error)
uploadToS3 func(postData *rainforest.RFPresignedPostData, filePath string) error
setSiteEnvironmentURL func(siteID int, environmentID int, appSlot int, newURL string) error
}
func (f fakeMobileUploadAPI) GetPresignedPOST(fileExt string, siteID int, environmentID int, appSlot int) (*rainforest.RFPresignedPostData, error) {
if f.getPresignedPOST != nil {
return f.getPresignedPOST(fileExt, siteID, environmentID, appSlot)
}
return nil, nil
}
func (f fakeMobileUploadAPI) UploadToS3(postData *rainforest.RFPresignedPostData, filePath string) error {
if f.uploadToS3 != nil {
return f.uploadToS3(postData, filePath)
}
return nil
}
func (f fakeMobileUploadAPI) UpdateURL(siteID int, environmentID int, appSlot int, newURL string) error {
if f.setSiteEnvironmentURL != nil {
return f.setSiteEnvironmentURL(siteID, environmentID, appSlot, newURL)
}
return nil
}
func TestUploadMobileApp(t *testing.T) {
siteID := 123
environmentID := 456
appSlot := 2
callCount := make(map[string]int)
f := fakeMobileUploadAPI{
getPresignedPOST: func(fileExt string, siteID int, environmentID int, appSlot int) (*rainforest.RFPresignedPostData, error) {
callCount["getPresignedPOST"] = callCount["getPresignedPOST"] + 1
return &rainforest.RFPresignedPostData{}, nil
},
uploadToS3: func(postData *rainforest.RFPresignedPostData, filePath string) error {
callCount["uploadToS3"] = callCount["uploadToS3"] + 1
return nil
},
setSiteEnvironmentURL: func(siteID int, environmentID int, appSlot int, newURL string) error {
callCount["setSiteEnvironmentURL"] = callCount["setSiteEnvironmentURL"] + 1
return nil
},
}
err := uploadMobileApp(f, testMobileAppPath, siteID, environmentID, appSlot)
if err != nil {
t.Errorf("Unexpected error: %v", err.Error())
}
if expected := 1; callCount["uploadToS3"] != expected {
t.Errorf("api.uploadToS3 called invalid number of times: %v, expected %v", callCount["uploadToS3"], expected)
}
if expected := 1; callCount["getPresignedPOST"] != expected {
t.Errorf("api.getPresignedPOST called invalid number of times: %v, expected %v", callCount["getPresignedPOST"], expected)
}
if expected := 1; callCount["setSiteEnvironmentURL"] != expected {
t.Errorf("api.setSiteEnvironmentURL called invalid number of times: %v, expected %v", callCount["setSiteEnvironmentURL"], expected)
}
}
func TestMobileAppUpload(t *testing.T) {
siteID := "123"
environmentID := "456"
appSlot := "2"
callCount := make(map[string]int)
f := fakeMobileUploadAPI{
getPresignedPOST: func(fileExt string, siteID int, environmentID int, appSlot int) (*rainforest.RFPresignedPostData, error) {
callCount["getPresignedPOST"] = callCount["getPresignedPOST"] + 1
return &rainforest.RFPresignedPostData{}, nil
},
uploadToS3: func(postData *rainforest.RFPresignedPostData, filePath string) error {
callCount["uploadToS3"] = callCount["uploadToS3"] + 1
return nil
},
setSiteEnvironmentURL: func(siteID int, environmentID int, appSlot int, newURL string) error {
callCount["setSiteEnvironmentURL"] = callCount["setSiteEnvironmentURL"] + 1
return nil
},
}
fakeContext := newFakeContext(map[string]interface{}{
"site-id": siteID,
"environment-id": environmentID,
"app-slot": appSlot,
}, cli.Args{testMobileAppPath})
err := mobileAppUpload(fakeContext, f)
if err != nil {
t.Errorf("Unexpected error: %v", err.Error())
}
if expected := 1; callCount["uploadToS3"] != expected {
t.Errorf("api.uploadToS3 called invalid number of times: %v, expected %v", callCount["uploadToS3"], expected)
}
if expected := 1; callCount["getPresignedPOST"] != expected {
t.Errorf("api.getPresignedPOST called invalid number of times: %v, expected %v", callCount["getPresignedPOST"], expected)
}
if expected := 1; callCount["setSiteEnvironmentURL"] != expected {
t.Errorf("api.setSiteEnvironmentURL called invalid number of times: %v, expected %v", callCount["setSiteEnvironmentURL"], expected)
}
// Bad extension
fakeContext = newFakeContext(map[string]interface{}{
"site-id": siteID,
"environment-id": environmentID,
}, cli.Args{"./file.exe"})
err = mobileAppUpload(fakeContext, f)
if _, ok := err.(*cli.ExitError); !ok &&
!strings.Contains(err.Error(), "Invalid file extension") {
t.Errorf("Not erroring on invalid extension.")
}
// missing envorionment-id flag
fakeContext = newFakeContext(map[string]interface{}{
"site-id": siteID,
}, cli.Args{"./file.zip"})
err = mobileAppUpload(fakeContext, f)
if _, ok := err.(*cli.ExitError); !ok &&
!strings.Contains(err.Error(), "environment-id flag required") {
t.Errorf("Not erroring on missing environment-id.")
}
// non-int envorionment-id flag
fakeContext = newFakeContext(map[string]interface{}{
"site-id": siteID,
"envorionment-id": "test",
}, cli.Args{"./file.zip"})
err = mobileAppUpload(fakeContext, f)
if _, ok := err.(*cli.ExitError); !ok &&
!strings.Contains(err.Error(), "environment-id must be an integer") {
t.Errorf("envorionment-id must be an integer.")
}
// missing site-id flag
fakeContext = newFakeContext(map[string]interface{}{
"environment-id": environmentID,
}, cli.Args{"./file.zip"})
err = mobileAppUpload(fakeContext, f)
if _, ok := err.(*cli.ExitError); !ok &&
!strings.Contains(err.Error(), "site-id flag required") {
t.Errorf("Not erroring on missing site-id.")
}
// non-int site-id flag
fakeContext = newFakeContext(map[string]interface{}{
"site-id": "test",
"environment-id": environmentID,
}, cli.Args{"./file.zip"})
err = mobileAppUpload(fakeContext, f)
if _, ok := err.(*cli.ExitError); !ok &&
!strings.Contains(err.Error(), "site-id must be an integer") {
t.Errorf("Not erroring on missing site-id.")
}
// app-slot flag is set to a non-int
fakeContext = newFakeContext(map[string]interface{}{
"site-id": siteID,
"environment-id": environmentID,
"app-slot": "test",
}, cli.Args{"./file.zip"})
err = mobileAppUpload(fakeContext, f)
if _, ok := err.(*cli.ExitError); !ok &&
!strings.Contains(err.Error(), "app-slot must be an integer (1 to 100)") {
t.Errorf("Not erroring on invalid app-slot.")
}
// app-slot flag is set to a non 1-100 int
fakeContext = newFakeContext(map[string]interface{}{
"site-id": siteID,
"environment-id": environmentID,
"app-slot": 101,
}, cli.Args{"./file.zip"})
err = mobileAppUpload(fakeContext, f)
if _, ok := err.(*cli.ExitError); !ok &&
!strings.Contains(err.Error(), "app-slot must be an integer (1 to 100)") {
t.Errorf("Not erroring on invalid app-slot.")
}
// missing filepath args
fakeContext = newFakeContext(map[string]interface{}{
"site-id": siteID,
"environment-id": environmentID,
}, cli.Args{""})
err = mobileAppUpload(fakeContext, f)
if _, ok := err.(*cli.ExitError); !ok &&
!strings.Contains(err.Error(), "Mobile app filepath not specified") {
t.Errorf("Should have errored on missing filepath")
}
// bad filepath
fakeContext = newFakeContext(map[string]interface{}{
"site-id": siteID,
"environment-id": environmentID,
}, cli.Args{"./bad_file_name.zip"})
err = mobileAppUpload(fakeContext, f)
if _, ok := err.(*cli.ExitError); !ok &&
!strings.Contains(err.Error(), "no such file or directory") {
t.Errorf("Should have errored on missing filepath")
}
}