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 pathjssdk_test.go
178 lines (150 loc) · 4.57 KB
/
jssdk_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 parsecmd
import (
"io/ioutil"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"github.com/ParsePlatform/parse-cli/parsecli"
"github.com/facebookgo/ensure"
"github.com/facebookgo/parse"
)
var defaultParseConfig = &parsecli.ParseConfig{
ProjectConfig: &parsecli.ProjectConfig{
Type: parsecli.LegacyParseFormat,
Parse: &parsecli.ParseProjectConfig{},
},
}
func newJsSdkHarness(t testing.TB) *parsecli.Harness {
h := parsecli.NewHarness(t)
ht := parsecli.TransportFunc(func(r *http.Request) (*http.Response, error) {
ensure.DeepEqual(t, r.URL.Path, "/1/jsVersions")
rows := jsSDKVersion{JS: []string{"1.2.8", "1.2.9", "1.2.10", "1.2.11", "0.2.0"}}
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(strings.NewReader(jsonStr(t, rows))),
}, nil
})
h.Env.ParseAPIClient = &parsecli.ParseAPIClient{APIClient: &parse.Client{Transport: ht}}
return h
}
func newJsSdkHarnessError(t testing.TB) *parsecli.Harness {
h := parsecli.NewHarness(t)
ht := parsecli.TransportFunc(func(r *http.Request) (*http.Response, error) {
ensure.DeepEqual(t, r.URL.Path, "/1/jsVersions")
return &http.Response{
StatusCode: http.StatusExpectationFailed,
Body: ioutil.NopCloser(strings.NewReader(`{"error":"something is wrong"}`)),
}, nil
})
h.Env.ParseAPIClient = &parsecli.ParseAPIClient{APIClient: &parse.Client{Transport: ht}}
return h
}
func newJsSdkHarnessWithConfig(t testing.TB) (*parsecli.Harness, *parsecli.Context) {
h := newJsSdkHarness(t)
h.MakeEmptyRoot()
ensure.Nil(t, parsecli.CloneSampleCloudCode(h.Env, true))
h.Out.Reset()
c, err := parsecli.ConfigFromDir(h.Env.Root)
ensure.Nil(t, err)
config, ok := (c).(*parsecli.ParseConfig)
ensure.True(t, ok)
return h, &parsecli.Context{Config: config}
}
func TestGetAllJSVersions(t *testing.T) {
t.Parallel()
h := newJsSdkHarness(t)
defer h.Stop()
j := jsSDKCmd{}
versions, err := j.getAllJSSdks(h.Env)
ensure.Nil(t, err)
ensure.DeepEqual(t, versions, []string{"1.2.11", "1.2.10", "1.2.9", "1.2.8", "0.2.0"})
}
func TestGetAllJSVersionsError(t *testing.T) {
t.Parallel()
h := newJsSdkHarnessError(t)
defer h.Stop()
j := jsSDKCmd{}
_, err := j.getAllJSSdks(h.Env)
ensure.Err(t, err, regexp.MustCompile(`something is wrong`))
}
func TestPrintVersions(t *testing.T) {
t.Parallel()
h, c := newJsSdkHarnessWithConfig(t)
defer h.Stop()
j := jsSDKCmd{}
ensure.Nil(t, j.printVersions(h.Env, c))
ensure.DeepEqual(t, h.Out.String(),
` 1.2.11
1.2.10
1.2.9
1.2.8
0.2.0
`)
}
func TestSetVersionInvalid(t *testing.T) {
t.Parallel()
h, c := newJsSdkHarnessWithConfig(t)
defer h.Stop()
j := jsSDKCmd{newVersion: "1.2.12"}
ensure.Err(t, j.setVersion(h.Env, c), regexp.MustCompile("Invalid SDK version selected"))
}
func TestSetVersionNoneSelected(t *testing.T) {
t.Parallel()
h := parsecli.NewHarness(t)
defer h.Stop()
c := &parsecli.Context{Config: defaultParseConfig}
var j jsSDKCmd
c.Config.GetProjectConfig().Parse.JSSDK = "1.2.1"
ensure.Nil(t, j.getVersion(h.Env, c))
ensure.DeepEqual(t, "Current JavaScript SDK version is 1.2.1\n",
h.Out.String())
c.Config.GetProjectConfig().Parse.JSSDK = ""
ensure.Err(t, j.getVersion(h.Env, c),
regexp.MustCompile("JavaScript SDK version not set for this project."))
}
func TestSetValidVersion(t *testing.T) {
t.Parallel()
h, c := newJsSdkHarnessWithConfig(t)
defer h.Stop()
j := jsSDKCmd{newVersion: "1.2.11"}
ensure.Nil(t, j.setVersion(h.Env, c))
ensure.DeepEqual(t, h.Out.String(), "Current JavaScript SDK version is 1.2.11\n")
content, err := ioutil.ReadFile(filepath.Join(h.Env.Root, parsecli.ParseProject))
ensure.Nil(t, err)
ensure.DeepEqual(t, string(content), `{
"project_type": 1,
"parse": {
"jssdk": "1.2.11"
}
}`)
}
// NOTE: testing for legacy config format
func newLegacyJsSdkHarnessWithConfig(t testing.TB) (*parsecli.Harness, *parsecli.Context) {
h := newJsSdkHarness(t)
h.MakeEmptyRoot()
ensure.Nil(t, os.Mkdir(filepath.Join(h.Env.Root, parsecli.ConfigDir), 0755))
path := filepath.Join(h.Env.Root, parsecli.LegacyConfigFile)
ensure.Nil(t, ioutil.WriteFile(path,
[]byte(`{
"global": {
"parseVersion" : "1.2.9"
}
}`),
0600))
c, err := parsecli.ConfigFromDir(h.Env.Root)
ensure.Nil(t, err)
config, ok := (c).(*parsecli.ParseConfig)
ensure.True(t, ok)
return h, &parsecli.Context{Config: config}
}
func TestLegacySetValidVersion(t *testing.T) {
t.Parallel()
h, c := newLegacyJsSdkHarnessWithConfig(t)
defer h.Stop()
j := jsSDKCmd{newVersion: "1.2.11"}
ensure.Nil(t, j.setVersion(h.Env, c))
ensure.DeepEqual(t, h.Out.String(), "Current JavaScript SDK version is 1.2.11\n")
}