1
1
package cli_test
2
2
3
3
import (
4
+ "os"
4
5
"testing"
5
6
6
7
"github.com/stretchr/testify/assert"
7
8
"github.com/stretchr/testify/require"
8
9
9
10
"github.com/coder/coder/cli/clitest"
11
+ "github.com/coder/coder/cli/config"
10
12
"github.com/coder/coder/coderd/coderdtest"
11
13
"github.com/coder/coder/pty/ptytest"
12
14
)
13
15
14
16
func TestLogout (t * testing.T ) {
15
17
t .Parallel ()
18
+ t .Run ("Logout" , func (t * testing.T ) {
19
+ t .Parallel ()
20
+
21
+ pty := ptytest .New (t )
22
+ config := login (t , pty )
23
+
24
+ // ensure session files exist
25
+ require .FileExists (t , string (config .URL ()))
26
+ require .FileExists (t , string (config .Session ()))
27
+
28
+ logoutChan := make (chan struct {})
29
+ logout , _ := clitest .New (t , "logout" , "--global-config" , string (config ))
30
+ logout .SetIn (pty .Input ())
31
+ logout .SetOut (pty .Output ())
32
+
33
+ go func () {
34
+ defer close (logoutChan )
35
+ err := logout .Execute ()
36
+ assert .NoError (t , err )
37
+ assert .NoFileExists (t , string (config .URL ()))
38
+ assert .NoFileExists (t , string (config .Session ()))
39
+ }()
40
+
41
+ pty .ExpectMatch ("Are you sure you want to logout?" )
42
+ pty .WriteLine ("yes" )
43
+ pty .ExpectMatch ("Successfully logged out" )
44
+ <- logoutChan
45
+ })
46
+ t .Run ("SkipPrompt" , func (t * testing.T ) {
47
+ t .Parallel ()
48
+
49
+ pty := ptytest .New (t )
50
+ config := login (t , pty )
51
+
52
+ // ensure session files exist
53
+ require .FileExists (t , string (config .URL ()))
54
+ require .FileExists (t , string (config .Session ()))
55
+
56
+ logoutChan := make (chan struct {})
57
+ logout , _ := clitest .New (t , "logout" , "--global-config" , string (config ), "-y" )
58
+ logout .SetIn (pty .Input ())
59
+ logout .SetOut (pty .Output ())
60
+
61
+ go func () {
62
+ defer close (logoutChan )
63
+ err := logout .Execute ()
64
+ assert .NoError (t , err )
65
+ assert .NoFileExists (t , string (config .URL ()))
66
+ assert .NoFileExists (t , string (config .Session ()))
67
+ }()
68
+
69
+ pty .ExpectMatch ("Successfully logged out" )
70
+ <- logoutChan
71
+ })
72
+ t .Run ("NoURLFile" , func (t * testing.T ) {
73
+ t .Parallel ()
74
+
75
+ pty := ptytest .New (t )
76
+ config := login (t , pty )
77
+
78
+ // ensure session files exist
79
+ require .FileExists (t , string (config .URL ()))
80
+ require .FileExists (t , string (config .Session ()))
81
+
82
+ err := os .Remove (string (config .URL ()))
83
+ require .NoError (t , err )
84
+
85
+ logoutChan := make (chan struct {})
86
+ logout , _ := clitest .New (t , "logout" , "--global-config" , string (config ))
87
+
88
+ logout .SetIn (pty .Input ())
89
+ logout .SetOut (pty .Output ())
90
+
91
+ go func () {
92
+ defer close (logoutChan )
93
+ err := logout .Execute ()
94
+ assert .NoError (t , err )
95
+ assert .NoFileExists (t , string (config .URL ()))
96
+ assert .NoFileExists (t , string (config .Session ()))
97
+ }()
98
+
99
+ pty .ExpectMatch ("Are you sure you want to logout?" )
100
+ pty .WriteLine ("yes" )
101
+ pty .ExpectMatch ("You are not logged in. Try logging in using 'coder login <url>'." )
102
+ <- logoutChan
103
+ })
104
+ t .Run ("NoSessionFile" , func (t * testing.T ) {
105
+ t .Parallel ()
106
+
107
+ pty := ptytest .New (t )
108
+ config := login (t , pty )
109
+
110
+ // ensure session files exist
111
+ require .FileExists (t , string (config .URL ()))
112
+ require .FileExists (t , string (config .Session ()))
113
+
114
+ err := os .Remove (string (config .Session ()))
115
+ require .NoError (t , err )
116
+
117
+ logoutChan := make (chan struct {})
118
+ logout , _ := clitest .New (t , "logout" , "--global-config" , string (config ))
119
+
120
+ logout .SetIn (pty .Input ())
121
+ logout .SetOut (pty .Output ())
122
+
123
+ go func () {
124
+ defer close (logoutChan )
125
+ err = logout .Execute ()
126
+ assert .NoError (t , err )
127
+ assert .NoFileExists (t , string (config .URL ()))
128
+ assert .NoFileExists (t , string (config .Session ()))
129
+ }()
130
+
131
+ pty .ExpectMatch ("Are you sure you want to logout?" )
132
+ pty .WriteLine ("yes" )
133
+ pty .ExpectMatch ("You are not logged in. Try logging in using 'coder login <url>'." )
134
+ <- logoutChan
135
+ })
136
+ }
137
+
138
+ func login (t * testing.T , pty * ptytest.PTY ) config.Root {
139
+ t .Helper ()
16
140
17
- // login
18
141
client := coderdtest .New (t , nil )
19
142
coderdtest .CreateFirstUser (t , client )
20
143
21
144
doneChan := make (chan struct {})
22
- root , config := clitest .New (t , "login" , "--force-tty" , client .URL .String (), "--no-open" )
23
- pty := ptytest .New (t )
145
+ root , cfg := clitest .New (t , "login" , "--force-tty" , client .URL .String (), "--no-open" )
24
146
root .SetIn (pty .Input ())
25
147
root .SetOut (pty .Output ())
26
148
go func () {
@@ -34,13 +156,5 @@ func TestLogout(t *testing.T) {
34
156
pty .ExpectMatch ("Welcome to Coder" )
35
157
<- doneChan
36
158
37
- // ensure session files exist
38
- require .FileExists (t , string (config .URL ()))
39
- require .FileExists (t , string (config .Session ()))
40
-
41
- logout , _ := clitest .New (t , "logout" , "--global-config" , string (config ))
42
- err := logout .Execute ()
43
- require .NoError (t , err )
44
- require .NoFileExists (t , string (config .URL ()))
45
- require .NoFileExists (t , string (config .Session ()))
159
+ return cfg
46
160
}
0 commit comments