11package command
22
33import (
4- "bytes"
54 "context"
65 "io/ioutil"
76 "net/http/httptest"
@@ -34,7 +33,7 @@ func TestLogin(t *testing.T) {
3433 ts := httptest .NewServer (tfeserver .Handler )
3534 defer ts .Close ()
3635
37- loginTestCase := func (test func (t * testing.T , c * LoginCommand , ui * cli.MockUi , inp func ( string ) )) func (t * testing.T ) {
36+ loginTestCase := func (test func (t * testing.T , c * LoginCommand , ui * cli.MockUi )) func (t * testing.T ) {
3837 return func (t * testing.T ) {
3938 t .Helper ()
4039 workDir , err := ioutil .TempDir ("" , "terraform-test-command-login" )
@@ -48,15 +47,15 @@ func TestLogin(t *testing.T) {
4847 ctx , cancel := context .WithCancel (context .Background ())
4948 defer cancel ()
5049
51- ui := cli .NewMockUi ()
50+ // Do not use the NewMockUi initializer here, as we want to delay
51+ // the call to init until after setting up the input mocks
52+ ui := new (cli.MockUi )
53+
5254 browserLauncher := webbrowser .NewMockLauncher (ctx )
5355 creds := cliconfig .EmptyCredentialsSourceForTests (filepath .Join (workDir , "credentials.tfrc.json" ))
5456 svcs := disco .NewWithCredentialsSource (creds )
5557 svcs .SetUserAgent (httpclient .TerraformUserAgent (version .String ()))
5658
57- inputBuf := & bytes.Buffer {}
58- ui .InputReader = inputBuf
59-
6059 svcs .ForceHostServices (svchost .Hostname ("app.terraform.io" ), map [string ]interface {}{
6160 "login.v1" : map [string ]interface {}{
6261 // On app.terraform.io we use password-based authorization.
@@ -96,16 +95,16 @@ func TestLogin(t *testing.T) {
9695 },
9796 }
9897
99- test (t , c , ui , func (data string ) {
100- t .Helper ()
101- inputBuf .WriteString (data )
102- })
98+ test (t , c , ui )
10399 }
104100 }
105101
106- t .Run ("defaulting to app.terraform.io with password flow" , loginTestCase (func (t * testing.T , c * LoginCommand , ui * cli.MockUi , inp func (string )) {
107- // Enter "yes" at the consent prompt, then a username and then a password.
108- inp ("yes\n foo\n bar\n " )
102+ t .Run ("defaulting to app.terraform.io with password flow" , loginTestCase (func (t * testing.T , c * LoginCommand , ui * cli.MockUi ) {
103+ defer testInputMap (t , map [string ]string {
104+ "approve" : "yes" ,
105+ "username" : "foo" ,
106+ "password" : "bar" ,
107+ })()
109108 status := c .Run (nil )
110109 if status != 0 {
111110 t .Fatalf ("unexpected error code %d\n stderr:\n %s" , status , ui .ErrorWriter .String ())
@@ -121,9 +120,11 @@ func TestLogin(t *testing.T) {
121120 }
122121 }))
123122
124- t .Run ("example.com with authorization code flow" , loginTestCase (func (t * testing.T , c * LoginCommand , ui * cli.MockUi , inp func ( string ) ) {
123+ t .Run ("example.com with authorization code flow" , loginTestCase (func (t * testing.T , c * LoginCommand , ui * cli.MockUi ) {
125124 // Enter "yes" at the consent prompt.
126- inp ("yes\n " )
125+ defer testInputMap (t , map [string ]string {
126+ "approve" : "yes" ,
127+ })()
127128 status := c .Run ([]string {"example.com" })
128129 if status != 0 {
129130 t .Fatalf ("unexpected error code %d\n stderr:\n %s" , status , ui .ErrorWriter .String ())
@@ -139,10 +140,13 @@ func TestLogin(t *testing.T) {
139140 }
140141 }))
141142
142- t .Run ("TFE host without login support" , loginTestCase (func (t * testing.T , c * LoginCommand , ui * cli.MockUi , inp func ( string ) ) {
143+ t .Run ("TFE host without login support" , loginTestCase (func (t * testing.T , c * LoginCommand , ui * cli.MockUi ) {
143144 // Enter "yes" at the consent prompt, then paste a token with some
144145 // accidental whitespace.
145- inp ("yes\n good-token \n " )
146+ defer testInputMap (t , map [string ]string {
147+ "approve" : "yes" ,
148+ "token" : " good-token " ,
149+ })()
146150 status := c .Run ([]string {"tfe.acme.com" })
147151 if status != 0 {
148152 t .Fatalf ("unexpected error code %d\n stderr:\n %s" , status , ui .ErrorWriter .String ())
@@ -158,9 +162,12 @@ func TestLogin(t *testing.T) {
158162 }
159163 }))
160164
161- t .Run ("TFE host without login support, incorrectly pasted token" , loginTestCase (func (t * testing.T , c * LoginCommand , ui * cli.MockUi , inp func ( string ) ) {
165+ t .Run ("TFE host without login support, incorrectly pasted token" , loginTestCase (func (t * testing.T , c * LoginCommand , ui * cli.MockUi ) {
162166 // Enter "yes" at the consent prompt, then paste an invalid token.
163- inp ("yes\n good-tok\n " )
167+ defer testInputMap (t , map [string ]string {
168+ "approve" : "yes" ,
169+ "token" : "good-tok" ,
170+ })()
164171 status := c .Run ([]string {"tfe.acme.com" })
165172 if status != 1 {
166173 t .Fatalf ("unexpected error code %d\n stderr:\n %s" , status , ui .ErrorWriter .String ())
@@ -176,7 +183,7 @@ func TestLogin(t *testing.T) {
176183 }
177184 }))
178185
179- t .Run ("host without login or TFE API support" , loginTestCase (func (t * testing.T , c * LoginCommand , ui * cli.MockUi , inp func ( string ) ) {
186+ t .Run ("host without login or TFE API support" , loginTestCase (func (t * testing.T , c * LoginCommand , ui * cli.MockUi ) {
180187 status := c .Run ([]string {"unsupported.example.net" })
181188 if status == 0 {
182189 t .Fatalf ("successful exit; want error" )
@@ -186,4 +193,34 @@ func TestLogin(t *testing.T) {
186193 t .Fatalf ("missing expected error message\n want: %s\n full output:\n %s" , want , got )
187194 }
188195 }))
196+
197+ t .Run ("answering no cancels" , loginTestCase (func (t * testing.T , c * LoginCommand , ui * cli.MockUi ) {
198+ // Enter "no" at the consent prompt
199+ defer testInputMap (t , map [string ]string {
200+ "approve" : "no" ,
201+ })()
202+ status := c .Run (nil )
203+ if status != 1 {
204+ t .Fatalf ("unexpected error code %d\n stderr:\n %s" , status , ui .ErrorWriter .String ())
205+ }
206+
207+ if got , want := ui .ErrorWriter .String (), "Login cancelled" ; ! strings .Contains (got , want ) {
208+ t .Fatalf ("missing expected error message\n want: %s\n full output:\n %s" , want , got )
209+ }
210+ }))
211+
212+ t .Run ("answering y cancels" , loginTestCase (func (t * testing.T , c * LoginCommand , ui * cli.MockUi ) {
213+ // Enter "y" at the consent prompt
214+ defer testInputMap (t , map [string ]string {
215+ "approve" : "y" ,
216+ })()
217+ status := c .Run (nil )
218+ if status != 1 {
219+ t .Fatalf ("unexpected error code %d\n stderr:\n %s" , status , ui .ErrorWriter .String ())
220+ }
221+
222+ if got , want := ui .ErrorWriter .String (), "Login cancelled" ; ! strings .Contains (got , want ) {
223+ t .Fatalf ("missing expected error message\n want: %s\n full output:\n %s" , want , got )
224+ }
225+ }))
189226}
0 commit comments