-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwhoami_plugin_test.go
More file actions
153 lines (128 loc) · 4.99 KB
/
Copy pathwhoami_plugin_test.go
File metadata and controls
153 lines (128 loc) · 4.99 KB
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_test
import (
"strings"
"code.cloudfoundry.org/cli/plugin/models"
"code.cloudfoundry.org/cli/plugin/pluginfakes"
io_helpers "code.cloudfoundry.org/cli/util/testhelpers/io"
. "github.com/jtuchscherer/whoami-plugin"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("WhoamiPlugin", func() {
Describe(".Run", func() {
var fakeCliConnection *pluginfakes.FakeCliConnection
var whoamiCmd *WhoamiCmd
var outputChan chan []string
BeforeEach(func() {
outputChan = make(chan []string)
fakeCliConnection = &pluginfakes.FakeCliConnection{}
fakeCliConnection.UsernameReturns("user@user.com", nil)
whoamiCmd = &WhoamiCmd{}
})
Context("when the user has an API set", func() {
BeforeEach(func() {
fakeCliConnection.HasAPIEndpointReturns(true, nil)
fakeCliConnection.ApiEndpointReturns("http://run.pivotal.io", nil)
})
Context("and the user is logged in", func() {
BeforeEach(func() {
fakeCliConnection.IsLoggedInReturns(true, nil)
fakeCliConnection.UsernameReturns("user@user.com", nil)
})
Context("and there is a username", func() {
BeforeEach(func() {
fakeCliConnection.UsernameReturns("user@user.com", nil)
})
It("displays the username", func(done Done) {
defer close(done)
go invokeCmd(outputChan, whoamiCmd, fakeCliConnection)
var output []string
Eventually(outputChan, 2).Should(Receive(&output))
outputString := strings.Join(output, "")
Expect(outputString).To(ContainSubstring("You are logged in"))
Expect(outputString).To(ContainSubstring("user@user.com"))
Expect(outputString).To(ContainSubstring("http://run.pivotal.io"))
})
Context("and the user has a space and org", func() {
BeforeEach(func() {
fakeCliConnection.UsernameReturns("user@user.com", nil)
fakeCliConnection.HasSpaceReturns(true, nil)
fakeCliConnection.HasOrganizationReturns(true, nil)
fakeCliConnection.GetCurrentOrgReturns(plugin_models.Organization{OrganizationFields: plugin_models.OrganizationFields{Name: "testOrg"}}, nil)
fakeCliConnection.GetCurrentSpaceReturns(plugin_models.Space{SpaceFields: plugin_models.SpaceFields{Name: "testSpace"}}, nil)
})
It("shows the org and space", func(done Done) {
defer close(done)
go invokeCmd(outputChan, whoamiCmd, fakeCliConnection)
var output []string
Eventually(outputChan, 2).Should(Receive(&output))
outputString := strings.Join(output, "")
Expect(outputString).To(ContainSubstring("You are targeting"))
Expect(outputString).To(ContainSubstring("testOrg"))
Expect(outputString).To(ContainSubstring("testSpace"))
})
})
Context("and the user has no space", func() {
BeforeEach(func() {
fakeCliConnection.HasSpaceReturns(false, nil)
})
It("shows no information about target", func(done Done) {
defer close(done)
go invokeCmd(outputChan, whoamiCmd, fakeCliConnection)
var output []string
Eventually(outputChan, 2).Should(Receive(&output))
outputString := strings.Join(output, "")
Expect(outputString).ToNot(ContainSubstring("You are targeting"))
})
})
Context("and the user has no org", func() {
BeforeEach(func() {
fakeCliConnection.HasOrganizationReturns(false, nil)
})
It("shows no information about target", func(done Done) {
defer close(done)
go invokeCmd(outputChan, whoamiCmd, fakeCliConnection)
var output []string
Eventually(outputChan, 2).Should(Receive(&output))
outputString := strings.Join(output, "")
Expect(outputString).ToNot(ContainSubstring("You are targeting"))
})
})
})
Context("and there is no username", func() {
BeforeEach(func() {
fakeCliConnection.UsernameReturns("", nil)
})
It("prints out a helpful error message", func(done Done) {
defer GinkgoRecover()
defer close(done)
go invokeCmd(outputChan, whoamiCmd, fakeCliConnection)
var output []string
Eventually(outputChan, 2).Should(Receive(&output))
outputString := strings.Join(output, "")
Expect(outputString).To(ContainSubstring("you are logged in, but your username is empty"))
})
})
})
Context("and the user is not logged in", func() {
BeforeEach(func() {
fakeCliConnection.IsLoggedInReturns(false, nil)
})
It("prints out a helpful error message", func(done Done) {
defer GinkgoRecover()
defer close(done)
go invokeCmd(outputChan, whoamiCmd, fakeCliConnection)
var output []string
Eventually(outputChan, 2).Should(Receive(&output))
outputString := strings.Join(output, "")
Expect(outputString).To(ContainSubstring("Nobody is logged in"))
})
})
})
})
})
func invokeCmd(outputChan chan []string, whoamiCmd *WhoamiCmd, fakeCliConnection *pluginfakes.FakeCliConnection) {
outputChan <- io_helpers.CaptureOutput(func() {
whoamiCmd.Run(fakeCliConnection, []string{"whoami"})
})
}