forked from parse-community/parse-cli
/
login_cmd_test.go
153 lines (124 loc) · 3.16 KB
/
login_cmd_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
package main
import (
"io/ioutil"
"regexp"
"strings"
"testing"
"github.com/facebookgo/ensure"
)
func TestPopulateCreds(t *testing.T) {
t.Parallel()
h := newHarness(t)
defer h.Stop()
l := &loginCmd{}
h.env.In = strings.NewReader("email\npassword\n")
ensure.Nil(t, l.populateCreds(h.env))
ensure.DeepEqual(t, l.credentials.email, "email")
ensure.DeepEqual(t, l.credentials.password, "password")
}
func TestGetTokenCredentials(t *testing.T) {
t.Parallel()
h := newHarness(t)
defer h.Stop()
l := &loginCmd{}
h.env.Server = "http://api.example.com/1/"
l.tokenReader = strings.NewReader(
`machine api.example.com
login email
password token
`,
)
credentials, err := l.getTokenCredentials(h.env)
ensure.Nil(t, err)
ensure.DeepEqual(t, credentials.email, "email")
ensure.DeepEqual(t, credentials.token, "token")
h.env.Server = "http://api.parse.com"
credentials, err = l.getTokenCredentials(h.env)
ensure.Err(t, err, regexp.MustCompile("could not find token for"))
}
func TestAuthUserWithToken(t *testing.T) {
t.Parallel()
h, _ := newAppHarness(t)
defer h.Stop()
l := &loginCmd{}
h.env.Server = "http://api.example.org/1/"
l.tokenReader = strings.NewReader(
`machine api.example.org
login email
password token
`,
)
ensure.Nil(t, l.authUserWithToken(h.env))
}
func TestUpdatedNetrcContent(t *testing.T) {
t.Parallel()
h := newHarness(t)
defer h.Stop()
l := &loginCmd{}
h.env.Server = "https://api.example.com/1/"
updated, err := l.updatedNetrcContent(h.env,
strings.NewReader(
`machine api.example.com
login email
password token0
machine api.example.org
login email
password token
`,
),
&credentials{email: "email", token: "token"},
)
ensure.Nil(t, err)
ensure.DeepEqual(t,
string(updated),
`machine api.example.com
login email
password token
machine api.example.org
login email
password token
`,
)
h.env.Server = "https://api.example.org/1/"
updated, err = l.updatedNetrcContent(h.env,
strings.NewReader(
`machine api.example.com
login email
password token
`,
),
&credentials{email: "email", token: "token"},
)
ensure.Nil(t, err)
ensure.DeepEqual(t,
string(updated),
`machine api.example.com
login email
password token
machine api.example.org
login email
password token`,
)
}
func TestLogin(t *testing.T) {
t.Parallel()
h, _ := newAppHarness(t)
defer h.Stop()
l := &loginCmd{tokenReader: strings.NewReader("")}
h.env.In = ioutil.NopCloser(strings.NewReader("n\nemail\ntoken\n"))
ensure.Nil(t, l.run(h.env))
ensure.DeepEqual(t,
h.Out.String(),
`Please enter the email id you used to register with Parse
and an account key if you already generated it.
If you do not have an account key or would like to generate a new one,
please type: "y" to open the browser or "n" to continue: Email: Account Key: Successfully stored credentials.
`)
h.env.In = ioutil.NopCloser(strings.NewReader("n\nemail\ninvalid\n"))
ensure.Err(t, l.run(h.env), regexp.MustCompile("Please try again"))
ensure.DeepEqual(t,
h.Err.String(),
`Sorry, we do not have a user with this email and account key.
Please follow instructions at https://www.parse.com/account/keys to generate a new account key.
`)
}