Exemple #1
0
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")
}
Exemple #2
0
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")
}
Exemple #3
0
func TestUnsuccessfullyLoggingInWithoutInteractivity(t *testing.T) {
	configRepo := testhelpers.FakeConfigRepository{}
	configRepo.Delete()
	config, _ := configRepo.Get()

	ui := new(testhelpers.FakeUI)

	callLogin(
		[]string{
			"*****@*****.**",
			"bar",
		},
		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.Contains(t, ui.Outputs[3], "Error authenticating")
	assert.Equal(t, len(ui.Outputs), 4)
}
Exemple #4
0
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)
}
Exemple #5
0
func testSuccessfulLogin(t *testing.T, args []string, inputs []string) (ui *testhelpers.FakeUI) {
	configRepo := testhelpers.FakeConfigRepository{}
	configRepo.Delete()
	config, _ := configRepo.Get()

	ui = new(testhelpers.FakeUI)
	ui.Inputs = inputs
	auth := &testhelpers.FakeAuthenticator{
		AccessToken:  "my_access_token",
		RefreshToken: "my_refresh_token",
		ConfigRepo:   configRepo,
	}
	callLogin(
		args,
		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.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, "password")

	return
}
Exemple #6
0
func TestRefreshingTheToken(t *testing.T) {
	ccServer := httptest.NewTLSServer(http.HandlerFunc(refreshTokenApiEndpoint))
	defer ccServer.Close()

	authServer := httptest.NewTLSServer(http.HandlerFunc(refreshTokenAuthEndpoint))
	defer authServer.Close()

	configRepo := testhelpers.FakeConfigRepository{}
	configRepo.Delete()
	config, err := configRepo.Get()
	assert.NoError(t, err)

	config.AuthorizationEndpoint = authServer.URL
	config.Target = ccServer.URL
	config.AccessToken = "bearer initial-access-token"
	config.RefreshToken = "initial-refresh-token"

	auth := NewUAAAuthenticator(configRepo)
	client := NewApiClient(auth)

	request, err := NewRequest("GET", config.Target+"/v2/foo", config.AccessToken, nil)
	assert.NoError(t, err)
	_, err = client.PerformRequest(request)
	assert.NoError(t, err)

	savedConfig := testhelpers.SavedConfiguration
	assert.Equal(t, savedConfig.AccessToken, "bearer new-access-token")
	assert.Equal(t, savedConfig.RefreshToken, "new-refresh-token")
}
Exemple #7
0
func createAuthenticator(t *testing.T, apiServer *httptest.Server, authServer *httptest.Server) (configuration.ConfigurationRepository, api.Authenticator) {
	configRepo := testhelpers.FakeConfigRepository{}
	configRepo.Delete()
	config, err := configRepo.Get()
	assert.NoError(t, err)

	config.AuthorizationEndpoint = authServer.URL
	config.Target = apiServer.URL
	config.AccessToken = "bearer initial-access-token"
	config.RefreshToken = "initial-refresh-token"

	authGateway := NewUAAAuthGateway()
	authenticator := api.NewUAAAuthenticator(authGateway, configRepo)

	return configRepo, authenticator
}
Exemple #8
0
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)
}
Exemple #9
0
func TestServerErrorLoggingIn(t *testing.T) {
	ts := httptest.NewTLSServer(http.HandlerFunc(errorLoginEndpoint))
	defer ts.Close()

	configRepo := testhelpers.FakeConfigRepository{}
	configRepo.Delete()
	config, err := configRepo.Get()
	assert.NoError(t, err)
	config.AuthorizationEndpoint = ts.URL
	config.AccessToken = ""

	auth := NewUAAAuthenticator(configRepo)
	err = auth.Authenticate("*****@*****.**", "bar")
	assert.Error(t, err)
	assert.Equal(t, err.Error(), "Server error, status code: 500, error code: 0, message: ")
	savedConfig := testhelpers.SavedConfiguration
	assert.Empty(t, savedConfig.AccessToken)
}
Exemple #10
0
func TestUnsuccessfullyLoggingIn(t *testing.T) {
	ts := httptest.NewTLSServer(http.HandlerFunc(unsuccessfulLoginEndpoint))
	defer ts.Close()

	configRepo := testhelpers.FakeConfigRepository{}
	configRepo.Delete()
	config, err := configRepo.Get()
	assert.NoError(t, err)
	config.AuthorizationEndpoint = ts.URL
	config.AccessToken = ""

	auth := NewUAAAuthenticator(configRepo)
	err = auth.Authenticate("*****@*****.**", "oops wrong pass")
	assert.Error(t, err)
	assert.Equal(t, err.Error(), "Password in incorrect, please try again.")
	savedConfig := testhelpers.SavedConfiguration
	assert.Empty(t, savedConfig.AccessToken)
}
Exemple #11
0
func TestSuccessfullyLoggingIn(t *testing.T) {
	ts := httptest.NewTLSServer(http.HandlerFunc(successfulLoginEndpoint))
	defer ts.Close()

	configRepo := testhelpers.FakeConfigRepository{}
	configRepo.Delete()
	config, err := configRepo.Get()
	assert.NoError(t, err)
	config.AuthorizationEndpoint = ts.URL
	config.AccessToken = ""

	auth := NewUAAAuthenticator(configRepo)
	err = auth.Authenticate("*****@*****.**", "bar")

	savedConfig := testhelpers.SavedConfiguration
	assert.Equal(t, savedConfig.AccessToken, "BEARER my_access_token")
	assert.Equal(t, savedConfig.RefreshToken, "my_refresh_token")
}
Exemple #12
0
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)
}