func TestSuccessfullyLoggingInWithUsernameAsArgument(t *testing.T) { configRepo := testhelpers.FakeConfigRepository{} configRepo.Delete() config, _ := configRepo.Get() ui := new(testhelpers.FakeUI) ui.Inputs = []string{"bar"} auth := &testhelpers.FakeAuthenticator{ AccessToken: "my_access_token", RefreshToken: "my_refresh_token", ConfigRepo: configRepo, } callLogin( []string{"*****@*****.**"}, ui, configRepo, &testhelpers.FakeOrgRepository{}, &testhelpers.FakeSpaceRepository{}, auth, ) savedConfig := testhelpers.SavedConfiguration assert.Contains(t, ui.Outputs[0], config.Target) assert.Contains(t, ui.Outputs[2], "OK") assert.Contains(t, ui.Prompts[0], "Password") assert.Equal(t, savedConfig.AccessToken, "my_access_token") assert.Equal(t, savedConfig.RefreshToken, "my_refresh_token") assert.Equal(t, auth.Email, "*****@*****.**") assert.Equal(t, auth.Password, "bar") }
func TestUnsuccessfullyLoggingIn(t *testing.T) { configRepo := testhelpers.FakeConfigRepository{} configRepo.Delete() config, _ := configRepo.Get() ui := new(testhelpers.FakeUI) ui.Inputs = []string{ "*****@*****.**", "bar", "bar", "bar", "bar", } callLogin( []string{}, ui, configRepo, &testhelpers.FakeOrgRepository{}, &testhelpers.FakeSpaceRepository{}, &testhelpers.FakeAuthenticator{AuthError: true, ConfigRepo: configRepo}, ) assert.Contains(t, ui.Outputs[0], config.Target) assert.Equal(t, ui.Outputs[1], "Authenticating...") assert.Equal(t, ui.Outputs[2], "FAILED") assert.Equal(t, ui.Outputs[5], "Authenticating...") assert.Equal(t, ui.Outputs[6], "FAILED") assert.Equal(t, ui.Outputs[9], "Authenticating...") assert.Equal(t, ui.Outputs[10], "FAILED") }
func TestLoggingInWithOneOrgAndNoSpace(t *testing.T) { configRepo := testhelpers.FakeConfigRepository{} configRepo.Delete() config, _ := configRepo.Get() ui := new(testhelpers.FakeUI) ui.Inputs = []string{"*****@*****.**", "bar"} orgs := []cf.Organization{ cf.Organization{"FirstOrg", "org-1-guid"}, } spaces := []cf.Space{} callLogin( []string{}, ui, configRepo, &testhelpers.FakeOrgRepository{Organizations: orgs}, &testhelpers.FakeSpaceRepository{Spaces: spaces}, &testhelpers.FakeAuthenticator{ConfigRepo: configRepo}, ) assert.Contains(t, ui.Outputs[0], config.Target) assert.Contains(t, ui.Prompts[0], "Username") assert.Contains(t, ui.Prompts[1], "Password") assert.Contains(t, ui.Outputs[2], "OK") assert.Contains(t, ui.Outputs[5], "API endpoint:") assert.Contains(t, ui.Outputs[7], "FirstOrg") assert.Contains(t, ui.Outputs[8], "No spaces found") savedConfig := testhelpers.SavedConfiguration assert.Equal(t, orgs[0], savedConfig.Organization) assert.Equal(t, cf.Space{}, savedConfig.Space) }
func TestSpaceRequirement(t *testing.T) { ui := new(testhelpers.FakeUI) config := &configuration.Configuration{ Organization: cf.Organization{ Name: "my-org", Guid: "my-org-guid", }, Space: cf.Space{ Name: "my-space", Guid: "my-space-guid", }, } req := NewTargetedSpaceRequirement(ui, config) success := req.Execute() assert.True(t, success) config.Space = cf.Space{} req = NewTargetedSpaceRequirement(ui, config) success = req.Execute() assert.False(t, success) assert.Contains(t, ui.Outputs[0], "FAILED") assert.Contains(t, ui.Outputs[1], "No space targeted") ui.ClearOutputs() config.Organization = cf.Organization{} req = NewTargetedSpaceRequirement(ui, config) success = req.Execute() assert.False(t, success) assert.Contains(t, ui.Outputs[0], "FAILED") assert.Contains(t, ui.Outputs[1], "No org and space targeted") }
func TestLoggingInWithMultipleOrgsAndSpaces(t *testing.T) { configRepo := testhelpers.FakeConfigRepository{} configRepo.Delete() config, _ := configRepo.Get() ui := new(testhelpers.FakeUI) ui.Inputs = []string{"*****@*****.**", "bar", "2", "1"} orgs := []cf.Organization{ cf.Organization{"FirstOrg", "org-1-guid"}, cf.Organization{"SecondOrg", "org-2-guid"}, } spaces := []cf.Space{ cf.Space{Name: "FirstSpace", Guid: "space-1-guid"}, cf.Space{Name: "SecondSpace", Guid: "space-2-guid"}, } callLogin( []string{}, ui, configRepo, &testhelpers.FakeOrgRepository{Organizations: orgs}, &testhelpers.FakeSpaceRepository{Spaces: spaces}, &testhelpers.FakeAuthenticator{ConfigRepo: configRepo}, ) assert.Contains(t, ui.Outputs[0], config.Target) assert.Contains(t, ui.Prompts[0], "Username") assert.Contains(t, ui.Prompts[1], "Password") assert.Contains(t, ui.Outputs[2], "OK") assert.Contains(t, ui.Outputs[3], "FirstOrg") assert.Contains(t, ui.Outputs[4], "SecondOrg") assert.Contains(t, ui.Prompts[2], "Organization") assert.Contains(t, ui.Outputs[5], "SecondOrg") assert.Contains(t, ui.Outputs[7], "FirstSpace") assert.Contains(t, ui.Outputs[8], "SecondSpace") assert.Contains(t, ui.Prompts[3], "Space") assert.Contains(t, ui.Outputs[9], "FirstSpace") savedConfig := testhelpers.SavedConfiguration assert.Equal(t, orgs[1], savedConfig.Organization) assert.Equal(t, spaces[0], savedConfig.Space) }
func TestWhenUserPicksInvalidOrgNumberAndSpaceNumber(t *testing.T) { configRepo := testhelpers.FakeConfigRepository{} configRepo.Delete() orgs := []cf.Organization{ cf.Organization{"Org1", "org-1-guid"}, cf.Organization{"Org2", "org-2-guid"}, } spaces := []cf.Space{ cf.Space{Name: "FirstSpace", Guid: "space-1-guid"}, cf.Space{Name: "SecondSpace", Guid: "space-2-guid"}, } ui := new(testhelpers.FakeUI) ui.Inputs = []string{"*****@*****.**", "bar", "3", "2", "3", "1"} callLogin( []string{}, ui, configRepo, &testhelpers.FakeOrgRepository{Organizations: orgs}, &testhelpers.FakeSpaceRepository{Spaces: spaces}, &testhelpers.FakeAuthenticator{ConfigRepo: configRepo}, ) assert.Contains(t, ui.Prompts[2], "Organization") assert.Contains(t, ui.Outputs[5], "FAILED") assert.Contains(t, ui.Prompts[3], "Organization") assert.Contains(t, ui.Outputs[9], "Targeting org") assert.Contains(t, ui.Prompts[4], "Space") assert.Contains(t, ui.Outputs[13], "FAILED") assert.Contains(t, ui.Prompts[5], "Space") assert.Contains(t, ui.Outputs[17], "Targeting space") savedConfig := testhelpers.SavedConfiguration assert.Equal(t, orgs[1], savedConfig.Organization) assert.Equal(t, spaces[0], savedConfig.Space) }