This repository was archived by the owner on Jan 16, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathmain.go
228 lines (199 loc) · 5.86 KB
/
main.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
package parsecli
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/bgentry/heroku-go"
"github.com/facebookgo/clock"
"github.com/facebookgo/ensure"
"github.com/facebookgo/parse"
"github.com/facebookgo/testname"
)
const (
Version = "3.0.5"
CloudDir = "cloud"
HostingDir = "public"
DefaultBaseURL = "https://api.parse.com/1/"
)
var UserAgent = fmt.Sprintf("parse-cli-%s-%s", runtime.GOOS, Version)
type Env struct {
Root string // project root
Server string // parse api server
Type int // project type
ParserEmail string // email associated with developer parse account
ErrorStack bool
Out io.Writer
Err io.Writer
In io.Reader
Exit func(int)
Clock clock.Clock
ParseAPIClient *ParseAPIClient
HerokuAPIClient *heroku.Client
}
type Harness struct {
T testing.TB
Out bytes.Buffer
Err bytes.Buffer
Clock *clock.Mock
Env *Env
remove []string
}
func (h *Harness) MakeEmptyRoot() {
var err error
prefix := fmt.Sprintf("%s-", testname.Get("parse-cli-"))
h.Env.Root, err = ioutil.TempDir("", prefix)
ensure.Nil(h.T, err)
h.remove = append(h.remove, h.Env.Root)
}
func (h *Harness) MakeWithConfig(global string) {
h.Env.Root = makeDirWithConfig(h.T, global)
}
func (h *Harness) Stop() {
for _, p := range h.remove {
os.RemoveAll(p)
}
}
func NewHarness(t testing.TB) *Harness {
te := Harness{
T: t,
Clock: clock.NewMock(),
}
te.Env = &Env{
Out: &te.Out,
Err: &te.Err,
Clock: te.Clock,
ParseAPIClient: &ParseAPIClient{APIClient: &parse.Client{}},
}
return &te
}
// makes a temp directory with the given global config.
func makeDirWithConfig(t testing.TB, global string) string {
dir, err := ioutil.TempDir("", testname.Get("parse-cli-"))
ensure.Nil(t, err)
ensure.Nil(t, os.Mkdir(filepath.Join(dir, "config"), 0755))
ensure.Nil(t, ioutil.WriteFile(
filepath.Join(dir, LegacyConfigFile),
[]byte(global),
os.FileMode(0600),
))
return dir
}
type TransportFunc func(r *http.Request) (*http.Response, error)
func (t TransportFunc) RoundTrip(r *http.Request) (*http.Response, error) {
return t(r)
}
func NewTokenHarness(t testing.TB) *Harness {
h := NewHarness(t)
ht := TransportFunc(func(r *http.Request) (*http.Response, error) {
ensure.DeepEqual(t, r.URL.Path, "/1/accountkey")
ensure.DeepEqual(t, r.Method, "POST")
key := &struct {
AccountKey string `json:"accountKey"`
}{}
ensure.Nil(t, json.NewDecoder(ioutil.NopCloser(r.Body)).Decode(key))
if key.AccountKey != "token" {
return &http.Response{
StatusCode: http.StatusUnauthorized,
Body: ioutil.NopCloser(strings.NewReader(`{"error": "incorrect token"}`)),
}, nil
}
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(strings.NewReader(`{"email": "email"}`)),
}, nil
})
h.Env.ParseAPIClient = &ParseAPIClient{APIClient: &parse.Client{Transport: ht}}
return h
}
func NewAppHarness(t testing.TB) (*Harness, []*App) {
h := NewHarness(t)
apps := []*App{
newTestApp("A"),
newTestApp("B"),
}
res := map[string][]*App{"results": apps}
ht := TransportFunc(func(r *http.Request) (*http.Response, error) {
email := r.Header.Get("X-Parse-Email")
password := r.Header.Get("X-Parse-Password")
token := r.Header.Get("X-Parse-Account-Key")
if !((email == "email" && password == "password") || (token == "token")) {
return &http.Response{
StatusCode: http.StatusUnauthorized,
Body: ioutil.NopCloser(strings.NewReader(`{"error": "incorrect credentials"}`)),
}, nil
}
switch r.URL.Path {
case "/1/apps":
if r.Method == "GET" {
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(strings.NewReader(jsonStr(t, &res))),
}, nil
}
if r.Method != "POST" || r.Body == nil {
return &http.Response{
StatusCode: http.StatusNotFound,
}, errors.New("unknown resource")
}
var params map[string]string
if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil {
return &http.Response{
StatusCode: http.StatusInternalServerError,
}, err
}
details, err := json.Marshal(newTestApp(params["appName"]))
if err != nil {
return &http.Response{
StatusCode: http.StatusInternalServerError,
}, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader(details)),
}, nil
case "/1/apps/an-app":
ensure.DeepEqual(t, r.Method, "GET")
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(strings.NewReader(jsonStr(t, newTestApp("an-app")))),
}, nil
default:
return &http.Response{
StatusCode: http.StatusNotFound,
}, nil
}
})
h.Env.ParseAPIClient = &ParseAPIClient{APIClient: &parse.Client{Transport: ht}}
return h, apps
}
func newTestApp(suffix string) *App {
return &App{
Name: suffix,
DashboardURL: fmt.Sprintf("https://api.example.com/dashboard/%s", suffix),
ApplicationID: fmt.Sprintf("applicationID.%s", suffix),
ClientKey: fmt.Sprintf("clientKey.%s", suffix),
JavaScriptKey: fmt.Sprintf("javaScriptKey.%s", suffix),
WindowsKey: fmt.Sprintf("windowsKey.%s", suffix),
WebhookKey: fmt.Sprintf("webhookKey.%s", suffix),
RestKey: fmt.Sprintf("restKey.%s", suffix),
MasterKey: fmt.Sprintf("masterKey.%s", suffix),
ClientPushEnabled: false,
ClientClassCreationEnabled: true,
RequireRevocableSessions: true,
RevokeSessionOnPasswordChange: true,
}
}
func jsonStr(t testing.TB, v interface{}) string {
b, err := json.Marshal(v)
ensure.Nil(t, err)
return string(b)
}