Skip to content

Commit 9c57bd8

Browse files
Ted YoungMike Gehard
authored andcommitted
when a user is not logged in, do not invite to use target -o or -s
Signed-off-by: Mike Gehard <mgehard@pivotallabs.com>
1 parent 116cb8e commit 9c57bd8

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

src/cf/commands/target.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ func (t Target) Run(c *cli.Context) {
4343

4444
if argsCount == 0 && orgName == "" && spaceName == "" {
4545
t.ui.ShowConfiguration(t.config)
46+
47+
if !t.config.IsLoggedIn() {
48+
return
49+
}
4650
if !t.config.HasOrganization() {
4751
t.ui.Say("No org targeted. Use 'cf target -o' to target an org.")
4852
}
@@ -59,7 +63,9 @@ func (t Target) Run(c *cli.Context) {
5963

6064
if orgName != "" {
6165
t.setOrganization(orgName)
62-
t.ui.Say("No space targeted. Use 'cf target -s' to target a space.")
66+
if t.config.IsLoggedIn() {
67+
t.ui.Say("No space targeted. Use 'cf target -s' to target a space.")
68+
}
6369
return
6470
}
6571

@@ -120,7 +126,7 @@ func (t *Target) saveTarget(target string, info *InfoResponse) (err error) {
120126

121127
func (t Target) setOrganization(orgName string) {
122128
if !t.config.IsLoggedIn() {
123-
t.ui.Failed("You must be logged in to set an organization.", nil)
129+
t.ui.Failed("You must be logged in to set an organization. Use 'cf login'.", nil)
124130
return
125131
}
126132

@@ -137,7 +143,7 @@ func (t Target) setOrganization(orgName string) {
137143

138144
func (t Target) setSpace(spaceName string) {
139145
if !t.config.IsLoggedIn() {
140-
t.ui.Failed("You must be logged in to set a space.", nil)
146+
t.ui.Failed("You must be logged in to set a space. Use 'cf login'.", nil)
141147
return
142148
}
143149

src/cf/commands/target_test.go

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,67 @@ import (
1313
"testing"
1414
)
1515

16-
func TestTargetWithoutArgument(t *testing.T) {
16+
func TestTargetWithoutArgumentAndLoggedIn(t *testing.T) {
1717
orgRepo := &testhelpers.FakeOrgRepository{}
1818
spaceRepo := &testhelpers.FakeSpaceRepository{}
1919
configRepo := &testhelpers.FakeConfigRepository{}
2020
config := configRepo.Login()
2121
config.Target = "https://api.run.pivotal.io"
2222
fakeUI := callTarget([]string{}, configRepo, orgRepo, spaceRepo)
2323

24+
assert.Equal(t, len(fakeUI.Outputs), 4)
2425
assert.Contains(t, fakeUI.Outputs[0], "https://api.run.pivotal.io")
26+
assert.Contains(t, fakeUI.Outputs[1], "user: ")
27+
assert.Contains(t, fakeUI.Outputs[2], "No org targeted")
28+
assert.Contains(t, fakeUI.Outputs[3], "No space targeted")
29+
}
30+
31+
func TestTargetWithoutArgumentsAndNotLoggedIn(t *testing.T) {
32+
orgRepo := &testhelpers.FakeOrgRepository{}
33+
spaceRepo := &testhelpers.FakeSpaceRepository{}
34+
configRepo := &testhelpers.FakeConfigRepository{}
35+
err := configRepo.ClearSession()
36+
assert.NoError(t, err)
37+
config, err := configRepo.Get()
38+
assert.NoError(t, err)
39+
config.Target = "https://api.run.pivotal.io"
40+
41+
fakeUI := callTarget([]string{}, configRepo, orgRepo, spaceRepo)
42+
assert.Equal(t, len(fakeUI.Outputs), 2)
43+
assert.Contains(t, fakeUI.Outputs[0], "https://api.run.pivotal.io")
44+
assert.Contains(t, fakeUI.Outputs[1], "Logged out.")
45+
}
46+
47+
func TestTargetWithOrganizationFlagAndNotLoggedIn(t *testing.T) {
48+
orgRepo := &testhelpers.FakeOrgRepository{}
49+
spaceRepo := &testhelpers.FakeSpaceRepository{}
50+
configRepo := &testhelpers.FakeConfigRepository{}
51+
err := configRepo.ClearSession()
52+
assert.NoError(t, err)
53+
config, err := configRepo.Get()
54+
assert.NoError(t, err)
55+
config.Target = "https://api.run.pivotal.io"
56+
57+
fakeUI := callTarget([]string{"-o", "my-organization"}, configRepo, orgRepo, spaceRepo)
58+
assert.Equal(t, len(fakeUI.Outputs), 2)
59+
assert.Contains(t, fakeUI.Outputs[0], "FAILED")
60+
assert.Contains(t, fakeUI.Outputs[1], "You must be logged in to set an organization. Use 'cf login'.")
61+
}
62+
63+
func TestTargetWithSpaceFlagAndNotLoggedIn(t *testing.T) {
64+
orgRepo := &testhelpers.FakeOrgRepository{}
65+
spaceRepo := &testhelpers.FakeSpaceRepository{}
66+
configRepo := &testhelpers.FakeConfigRepository{}
67+
err := configRepo.ClearSession()
68+
assert.NoError(t, err)
69+
config, err := configRepo.Get()
70+
assert.NoError(t, err)
71+
config.Target = "https://api.run.pivotal.io"
72+
73+
fakeUI := callTarget([]string{"-s", "my-space"}, configRepo, orgRepo, spaceRepo)
74+
assert.Equal(t, len(fakeUI.Outputs), 2)
75+
assert.Contains(t, fakeUI.Outputs[0], "FAILED")
76+
assert.Contains(t, fakeUI.Outputs[1], "You must be logged in to set a space. Use 'cf login'.")
2577
}
2678

2779
// With target argument

0 commit comments

Comments
 (0)