<commit_before>package coreconfig_test

import (
	"io/ioutil"
	"os"
	"path/filepath"

	"code.cloudfoundry.org/cli/cf/configuration"
	"code.cloudfoundry.org/cli/cf/configuration/configurationfakes"
	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
	"code.cloudfoundry.org/cli/cf/models"
	"code.cloudfoundry.org/cli/version"
	"github.com/blang/semver"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Configuration Repository", func() {
	var (
		config    coreconfig.Repository
		persistor *configurationfakes.FakePersistor
	)

	BeforeEach(func() {
		persistor = new(configurationfakes.FakePersistor)
		persistor.ExistsReturns(true)
		config = coreconfig.NewRepositoryFromPersistor(persistor, func(err error) { panic(err) })
	})

	It("is threadsafe", func() {
		performSaveCh := make(chan struct{})
		beginSaveCh := make(chan struct{})
		finishSaveCh := make(chan struct{})
		finishReadCh := make(chan struct{})

		persistor.SaveStub = func(configuration.DataInterface) error {
			close(beginSaveCh)
			<-performSaveCh
			close(finishSaveCh)

			return nil
		}

		go func() {
			config.SetAPIEndpoint("foo")
		}()

		<-beginSaveCh

		go func() {
			config.APIEndpoint()
			close(finishReadCh)
		}()

		Consistently(finishSaveCh).ShouldNot(BeClosed())
		Consistently(finishReadCh).ShouldNot(BeClosed())

		performSaveCh <- struct{}{}

		Eventually(finishReadCh).Should(BeClosed())
	})

	It("has acccessor methods for all config fields", func() {
		config.SetAPIEndpoint("http://api.the-endpoint")
		Expect(config.APIEndpoint()).To(Equal("http://api.the-endpoint"))

		config.SetAPIVersion("3")
		Expect(config.APIVersion()).To(Equal("3"))

		config.SetAuthenticationEndpoint("http://auth.the-endpoint")
		Expect(config.AuthenticationEndpoint()).To(Equal("http://auth.the-endpoint"))

		config.SetUaaEndpoint("http://uaa.the-endpoint")
		Expect(config.UaaEndpoint()).To(Equal("http://uaa.the-endpoint"))

		config.SetAccessToken("the-token")
		Expect(config.AccessToken()).To(Equal("the-token"))

		config.SetUAAOAuthClient("cf-oauth-client-id")
		Expect(config.UAAOAuthClient()).To(Equal("cf-oauth-client-id"))

		config.SetUAAOAuthClientSecret("cf-oauth-client-secret")
		Expect(config.UAAOAuthClientSecret()).To(Equal("cf-oauth-client-secret"))

		config.SetSSHOAuthClient("oauth-client-id")
		Expect(config.SSHOAuthClient()).To(Equal("oauth-client-id"))

		config.SetDopplerEndpoint("doppler.the-endpoint")
		Expect(config.DopplerEndpoint()).To(Equal("doppler.the-endpoint"))

		config.SetRefreshToken("the-token")
		Expect(config.RefreshToken()).To(Equal("the-token"))

		organization := models.OrganizationFields{Name: "the-org"}
		config.SetOrganizationFields(organization)
		Expect(config.OrganizationFields()).To(Equal(organization))

		space := models.SpaceFields{Name: "the-space"}
		config.SetSpaceFields(space)
		Expect(config.SpaceFields()).To(Equal(space))

		config.SetSSLDisabled(false)
		Expect(config.IsSSLDisabled()).To(BeFalse())

		config.SetLocale("en_US")
		Expect(config.Locale()).To(Equal("en_US"))

		config.SetPluginRepo(models.PluginRepo{Name: "repo", URL: "nowhere.com"})
		Expect(config.PluginRepos()[0].Name).To(Equal("repo"))
		Expect(config.PluginRepos()[0].URL).To(Equal("nowhere.com"))

		s, _ := semver.Make("3.1")
		Expect(config.IsMinAPIVersion(s)).To(Equal(false))

		config.SetMinCLIVersion("6.5.0")
		Expect(config.MinCLIVersion()).To(Equal("6.5.0"))

		config.SetMinRecommendedCLIVersion("6.9.0")
		Expect(config.MinRecommendedCLIVersion()).To(Equal("6.9.0"))
	})

	Describe("APIEndpoint", func() {
		It("sanitizes the target URL", func() {
			config.SetAPIEndpoint("http://api.the-endpoint/")
			Expect(config.APIEndpoint()).To(Equal("http://api.the-endpoint"))
		})
	})

	Describe("HasAPIEndpoint", func() {
		Context("when both endpoint and version are set", func() {
			BeforeEach(func() {
				config.SetAPIEndpoint("http://example.org")
				config.SetAPIVersion("192.168.127.12")
			})

			It("returns true", func() {
				Expect(config.HasAPIEndpoint()).To(BeTrue())
			})
		})

		Context("when endpoint is not set", func() {
			BeforeEach(func() {
				config.SetAPIVersion("192.168.127.12")
			})

			It("returns false", func() {
				Expect(config.HasAPIEndpoint()).To(BeFalse())
			})
		})

		Context("when version is not set", func() {
			BeforeEach(func() {
				config.SetAPIEndpoint("http://example.org")
			})

			It("returns false", func() {
				Expect(config.HasAPIEndpoint()).To(BeFalse())
			})
		})
	})

	Describe("UserGUID", func() {
		Context("with a valid access token", func() {
			BeforeEach(func() {
				config.SetAccessToken("<KEY>")
			})

			It("returns the guid", func() {
				Expect(config.UserGUID()).To(Equal("772dda3f-669f-4276-b2bd-90486abe1f6f"))
			})
		})

		Context("with an invalid access token", func() {
			BeforeEach(func() {
				config.SetAccessToken("bearer <KEY>")
			})

			It("returns an empty string", func() {
				Expect(config.UserGUID()).To(BeEmpty())
			})
		})
	})

	Describe("UserEmail", func() {
		Context("with a valid access token", func() {
			BeforeEach(func() {
				config.SetAccessToken("<KEY>")
			})

			It("returns the email", func() {
				Expect(config.UserEmail()).To(Equal("<EMAIL>"))
			})
		})

		Context("with an invalid access token", func() {
			BeforeEach(func() {
				config.SetAccessToken("bearer <KEY>")
			})

			It("returns an empty string", func() {
				Expect(config.UserEmail()).To(BeEmpty())
			})
		})<commit_msg>Backfill test for legacy config username<commit_after>package coreconfig_test

import (
	"io/ioutil"
	"os"
	"path/filepath"

	"code.cloudfoundry.org/cli/cf/configuration"
	"code.cloudfoundry.org/cli/cf/configuration/configurationfakes"
	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
	"code.cloudfoundry.org/cli/cf/models"
	"code.cloudfoundry.org/cli/version"
	"github.com/blang/semver"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Configuration Repository", func() {

	const (
		AccessTokenForHumanUsers = "<KEY>"
		InvalidAccessToken       = "<KEY>"
	)

	var (
		config    coreconfig.Repository
		persistor *configurationfakes.FakePersistor
	)

	BeforeEach(func() {
		persistor = new(configurationfakes.FakePersistor)
		persistor.ExistsReturns(true)
		config = coreconfig.NewRepositoryFromPersistor(persistor, func(err error) { panic(err) })
	})

	It("is threadsafe", func() {
		performSaveCh := make(chan struct{})
		beginSaveCh := make(chan struct{})
		finishSaveCh := make(chan struct{})
		finishReadCh := make(chan struct{})

		persistor.SaveStub = func(configuration.DataInterface) error {
			close(beginSaveCh)
			<-performSaveCh
			close(finishSaveCh)

			return nil
		}

		go func() {
			config.SetAPIEndpoint("foo")
		}()

		<-beginSaveCh

		go func() {
			config.APIEndpoint()
			close(finishReadCh)
		}()

		Consistently(finishSaveCh).ShouldNot(BeClosed())
		Consistently(finishReadCh).ShouldNot(BeClosed())

		performSaveCh <- struct{}{}

		Eventually(finishReadCh).Should(BeClosed())
	})

	It("has acccessor methods for all config fields", func() {
		config.SetAPIEndpoint("http://api.the-endpoint")
		Expect(config.APIEndpoint()).To(Equal("http://api.the-endpoint"))

		config.SetAPIVersion("3")
		Expect(config.APIVersion()).To(Equal("3"))

		config.SetAuthenticationEndpoint("http://auth.the-endpoint")
		Expect(config.AuthenticationEndpoint()).To(Equal("http://auth.the-endpoint"))

		config.SetUaaEndpoint("http://uaa.the-endpoint")
		Expect(config.UaaEndpoint()).To(Equal("http://uaa.the-endpoint"))

		config.SetAccessToken("the-token")
		Expect(config.AccessToken()).To(Equal("the-token"))

		config.SetUAAOAuthClient("cf-oauth-client-id")
		Expect(config.UAAOAuthClient()).To(Equal("cf-oauth-client-id"))

		config.SetUAAOAuthClientSecret("cf-oauth-client-secret")
		Expect(config.UAAOAuthClientSecret()).To(Equal("cf-oauth-client-secret"))

		config.SetSSHOAuthClient("oauth-client-id")
		Expect(config.SSHOAuthClient()).To(Equal("oauth-client-id"))

		config.SetDopplerEndpoint("doppler.the-endpoint")
		Expect(config.DopplerEndpoint()).To(Equal("doppler.the-endpoint"))

		config.SetRefreshToken("the-token")
		Expect(config.RefreshToken()).To(Equal("the-token"))

		organization := models.OrganizationFields{Name: "the-org"}
		config.SetOrganizationFields(organization)
		Expect(config.OrganizationFields()).To(Equal(organization))

		space := models.SpaceFields{Name: "the-space"}
		config.SetSpaceFields(space)
		Expect(config.SpaceFields()).To(Equal(space))

		config.SetSSLDisabled(false)
		Expect(config.IsSSLDisabled()).To(BeFalse())

		config.SetLocale("en_US")
		Expect(config.Locale()).To(Equal("en_US"))

		config.SetPluginRepo(models.PluginRepo{Name: "repo", URL: "nowhere.com"})
		Expect(config.PluginRepos()[0].Name).To(Equal("repo"))
		Expect(config.PluginRepos()[0].URL).To(Equal("nowhere.com"))

		s, _ := semver.Make("3.1")
		Expect(config.IsMinAPIVersion(s)).To(Equal(false))

		config.SetMinCLIVersion("6.5.0")
		Expect(config.MinCLIVersion()).To(Equal("6.5.0"))

		config.SetMinRecommendedCLIVersion("6.9.0")
		Expect(config.MinRecommendedCLIVersion()).To(Equal("6.9.0"))
	})

	Describe("APIEndpoint", func() {
		It("sanitizes the target URL", func() {
			config.SetAPIEndpoint("http://api.the-endpoint/")
			Expect(config.APIEndpoint()).To(Equal("http://api.the-endpoint"))
		})
	})

	Describe("HasAPIEndpoint", func() {
		Context("when both endpoint and version are set", func() {
			BeforeEach(func() {
				config.SetAPIEndpoint("http://example.org")
				config.SetAPIVersion("192.168.127.12")
			})

			It("returns true", func() {
				Expect(config.HasAPIEndpoint()).To(BeTrue())
			})
		})

		Context("when endpoint is not set", func() {
			BeforeEach(func() {
				config.SetAPIVersion("192.168.127.12")
			})

			It("returns false", func() {
				Expect(config.HasAPIEndpoint()).To(BeFalse())
			})
		})

		Context("when version is not set", func() {
			BeforeEach(func() {
				config.SetAPIEndpoint("http://example.org")
			})

			It("returns false", func() {
				Expect(config.HasAPIEndpoint()).To(BeFalse())
			})
		})
	})

	Describe("UserGUID", func() {
		Context("with a valid access token", func() {
			BeforeEach(func() {
				config.SetAccessToken(AccessTokenForHumanUsers)
			})

			It("returns the guid", func() {
				Expect(config.UserGUID()).To(Equal("9519be3e-44d9-40d0-ab9a-f4ace11df159"))
			})
		})

		Context("with an invalid access token", func() {
			BeforeEach(func() {
				config.SetAccessToken(InvalidAccessToken)
			})

			It("returns an empty string", func() {
				Expect(config.UserGUID()).To(BeEmpty())
			})
		})
	})

	Describe("Username", func() {
		Context("with a valid user access token", func() {
			BeforeEach(func() {
				config.SetAccessToken(AccessTokenForHumanUsers)
			})

			It("returns the username", func() {
				Expect(config.Username()).To(Equal("admin"))
			})
		})

		Context("with an invalid access token", func() {
			BeforeEach(func() {
				config.SetAccessToken(InvalidAccessToken)
			})

			It("returns an empty string", func() {
				Expect(config.Username()).To(BeEmpty())
			})
		})
	})

	Describe("UserEmail", func() {
		Context("with a valid access token", func() {
			BeforeEach(func() {
				config.SetAccessToken(AccessTokenForHumanUsers)
			})

			It("returns the email", func() {
				Expect(config.UserEmail()).To(Equal("admin"))
			})
		})

		Context("with an invalid access token", func() {
			BeforeEach(func() {
				config.SetAccessToken(InvalidAccessToken)
			})

			It("returns an empty string", func() {
				Expect(config.UserEmail()).To(BeEmpty())
			})
		})
{'fraction_non_alphanumeric': 0.17840497151734852, 'fraction_numerical': 0.01009839461418954, 'mean_word_length': 4.756085444610035, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '131561', 'n_tokens_mistral': 4437, 'n_tokens_neox': 3859, 'n_words': 504}