Skip to content

Commit

Permalink
when a user is not logged in, do not invite to use target -o or -s
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Gehard <[email protected]>
  • Loading branch information
Ted Young authored and Mike Gehard committed Sep 6, 2013
1 parent 116cb8e commit 9c57bd8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/cf/commands/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func (t Target) Run(c *cli.Context) {

if argsCount == 0 && orgName == "" && spaceName == "" {
t.ui.ShowConfiguration(t.config)

if !t.config.IsLoggedIn() {
return
}
if !t.config.HasOrganization() {
t.ui.Say("No org targeted. Use 'cf target -o' to target an org.")
}
Expand All @@ -59,7 +63,9 @@ func (t Target) Run(c *cli.Context) {

if orgName != "" {
t.setOrganization(orgName)
t.ui.Say("No space targeted. Use 'cf target -s' to target a space.")
if t.config.IsLoggedIn() {
t.ui.Say("No space targeted. Use 'cf target -s' to target a space.")
}
return
}

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

func (t Target) setOrganization(orgName string) {
if !t.config.IsLoggedIn() {
t.ui.Failed("You must be logged in to set an organization.", nil)
t.ui.Failed("You must be logged in to set an organization. Use 'cf login'.", nil)
return
}

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

func (t Target) setSpace(spaceName string) {
if !t.config.IsLoggedIn() {
t.ui.Failed("You must be logged in to set a space.", nil)
t.ui.Failed("You must be logged in to set a space. Use 'cf login'.", nil)
return
}

Expand Down
54 changes: 53 additions & 1 deletion src/cf/commands/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,67 @@ import (
"testing"
)

func TestTargetWithoutArgument(t *testing.T) {
func TestTargetWithoutArgumentAndLoggedIn(t *testing.T) {
orgRepo := &testhelpers.FakeOrgRepository{}
spaceRepo := &testhelpers.FakeSpaceRepository{}
configRepo := &testhelpers.FakeConfigRepository{}
config := configRepo.Login()
config.Target = "https://api.run.pivotal.io"
fakeUI := callTarget([]string{}, configRepo, orgRepo, spaceRepo)

assert.Equal(t, len(fakeUI.Outputs), 4)
assert.Contains(t, fakeUI.Outputs[0], "https://api.run.pivotal.io")
assert.Contains(t, fakeUI.Outputs[1], "user: ")
assert.Contains(t, fakeUI.Outputs[2], "No org targeted")
assert.Contains(t, fakeUI.Outputs[3], "No space targeted")
}

func TestTargetWithoutArgumentsAndNotLoggedIn(t *testing.T) {
orgRepo := &testhelpers.FakeOrgRepository{}
spaceRepo := &testhelpers.FakeSpaceRepository{}
configRepo := &testhelpers.FakeConfigRepository{}
err := configRepo.ClearSession()
assert.NoError(t, err)
config, err := configRepo.Get()
assert.NoError(t, err)
config.Target = "https://api.run.pivotal.io"

fakeUI := callTarget([]string{}, configRepo, orgRepo, spaceRepo)
assert.Equal(t, len(fakeUI.Outputs), 2)
assert.Contains(t, fakeUI.Outputs[0], "https://api.run.pivotal.io")
assert.Contains(t, fakeUI.Outputs[1], "Logged out.")
}

func TestTargetWithOrganizationFlagAndNotLoggedIn(t *testing.T) {
orgRepo := &testhelpers.FakeOrgRepository{}
spaceRepo := &testhelpers.FakeSpaceRepository{}
configRepo := &testhelpers.FakeConfigRepository{}
err := configRepo.ClearSession()
assert.NoError(t, err)
config, err := configRepo.Get()
assert.NoError(t, err)
config.Target = "https://api.run.pivotal.io"

fakeUI := callTarget([]string{"-o", "my-organization"}, configRepo, orgRepo, spaceRepo)
assert.Equal(t, len(fakeUI.Outputs), 2)
assert.Contains(t, fakeUI.Outputs[0], "FAILED")
assert.Contains(t, fakeUI.Outputs[1], "You must be logged in to set an organization. Use 'cf login'.")
}

func TestTargetWithSpaceFlagAndNotLoggedIn(t *testing.T) {
orgRepo := &testhelpers.FakeOrgRepository{}
spaceRepo := &testhelpers.FakeSpaceRepository{}
configRepo := &testhelpers.FakeConfigRepository{}
err := configRepo.ClearSession()
assert.NoError(t, err)
config, err := configRepo.Get()
assert.NoError(t, err)
config.Target = "https://api.run.pivotal.io"

fakeUI := callTarget([]string{"-s", "my-space"}, configRepo, orgRepo, spaceRepo)
assert.Equal(t, len(fakeUI.Outputs), 2)
assert.Contains(t, fakeUI.Outputs[0], "FAILED")
assert.Contains(t, fakeUI.Outputs[1], "You must be logged in to set a space. Use 'cf login'.")
}

// With target argument
Expand Down

0 comments on commit 9c57bd8

Please sign in to comment.