github.com/arunkumar7540/cli@v6.45.0+incompatible/actor/v2action/auth_test.go (about)

     1  package v2action_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	. "code.cloudfoundry.org/cli/actor/v2action"
     8  	"code.cloudfoundry.org/cli/actor/v2action/v2actionfakes"
     9  	"code.cloudfoundry.org/cli/api/uaa/constant"
    10  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("Auth Actions", func() {
    16  	var (
    17  		actor         *Actor
    18  		fakeUAAClient *v2actionfakes.FakeUAAClient
    19  		fakeConfig    *v2actionfakes.FakeConfig
    20  	)
    21  
    22  	BeforeEach(func() {
    23  		fakeUAAClient = new(v2actionfakes.FakeUAAClient)
    24  		fakeConfig = new(v2actionfakes.FakeConfig)
    25  		actor = NewActor(nil, fakeUAAClient, fakeConfig)
    26  	})
    27  
    28  	Describe("Authenticate", func() {
    29  		var (
    30  			grantType constant.GrantType
    31  			actualErr error
    32  		)
    33  
    34  		JustBeforeEach(func() {
    35  			actualErr = actor.Authenticate("some-username", "some-password", "uaa", grantType)
    36  		})
    37  
    38  		It("unsets org and space targeting", func() {
    39  			Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(1))
    40  		})
    41  
    42  		When("no API errors occur", func() {
    43  			BeforeEach(func() {
    44  				fakeUAAClient.AuthenticateReturns(
    45  					"some-access-token",
    46  					"some-refresh-token",
    47  					nil,
    48  				)
    49  			})
    50  
    51  			It("sets the auth and refresh tokens in the config", func() {
    52  				Expect(fakeConfig.SetTokenInformationCallCount()).To(Equal(1))
    53  				accessToken, refreshToken, sshOAuthClient := fakeConfig.SetTokenInformationArgsForCall(0)
    54  				Expect(accessToken).To(Equal("bearer some-access-token"))
    55  				Expect(refreshToken).To(Equal("some-refresh-token"))
    56  				Expect(sshOAuthClient).To(BeEmpty())
    57  			})
    58  
    59  			When("the grant type is a password grant", func() {
    60  				BeforeEach(func() {
    61  					grantType = constant.GrantTypePassword
    62  				})
    63  
    64  				It("authenticates the user and returns access and refresh tokens", func() {
    65  					Expect(actualErr).NotTo(HaveOccurred())
    66  
    67  					Expect(fakeUAAClient.AuthenticateCallCount()).To(Equal(1))
    68  					creds, origin, passedGrantType := fakeUAAClient.AuthenticateArgsForCall(0)
    69  					Expect(creds).To(Equal(map[string]string{
    70  						"username": "some-username",
    71  						"password": "some-password",
    72  					}))
    73  					Expect(origin).To(Equal("uaa"))
    74  					Expect(passedGrantType).To(Equal(constant.GrantTypePassword))
    75  
    76  					Expect(fakeConfig.SetUAAGrantTypeCallCount()).To(Equal(1))
    77  					Expect(fakeConfig.SetUAAGrantTypeArgsForCall(0)).To(Equal(""))
    78  				})
    79  
    80  				When("a previous user authenticated with a client grant type", func() {
    81  					BeforeEach(func() {
    82  						fakeConfig.UAAGrantTypeReturns("client_credentials")
    83  					})
    84  
    85  					It("returns a PasswordGrantTypeLogoutRequiredError", func() {
    86  						Expect(actualErr).To(MatchError(actionerror.PasswordGrantTypeLogoutRequiredError{}))
    87  						Expect(fakeConfig.UAAGrantTypeCallCount()).To(Equal(1))
    88  					})
    89  				})
    90  			})
    91  
    92  			When("the grant type is client credentials", func() {
    93  				BeforeEach(func() {
    94  					grantType = constant.GrantTypeClientCredentials
    95  				})
    96  
    97  				It("stores the grant type and the client id", func() {
    98  					Expect(fakeConfig.SetUAAClientCredentialsCallCount()).To(Equal(1))
    99  					clientID, clientSecret := fakeConfig.SetUAAClientCredentialsArgsForCall(0)
   100  					Expect(clientID).To(Equal("some-username"))
   101  					Expect(clientSecret).To(BeEmpty())
   102  					Expect(fakeConfig.SetUAAGrantTypeCallCount()).To(Equal(1))
   103  					Expect(fakeConfig.SetUAAGrantTypeArgsForCall(0)).To(Equal(string(constant.GrantTypeClientCredentials)))
   104  				})
   105  
   106  				It("authenticates the user and returns access and refresh tokens", func() {
   107  					Expect(actualErr).NotTo(HaveOccurred())
   108  
   109  					Expect(fakeUAAClient.AuthenticateCallCount()).To(Equal(1))
   110  					creds, origin, passedGrantType := fakeUAAClient.AuthenticateArgsForCall(0)
   111  					Expect(creds).To(Equal(map[string]string{
   112  						"client_id":     "some-username",
   113  						"client_secret": "some-password",
   114  					}))
   115  					Expect(origin).To(Equal("uaa"))
   116  					Expect(passedGrantType).To(Equal(constant.GrantTypeClientCredentials))
   117  
   118  					Expect(fakeConfig.SetUAAGrantTypeCallCount()).To(Equal(1))
   119  					Expect(fakeConfig.SetUAAGrantTypeArgsForCall(0)).To(BeEquivalentTo(constant.GrantTypeClientCredentials))
   120  				})
   121  			})
   122  		})
   123  
   124  		When("an API error occurs", func() {
   125  			var expectedErr error
   126  
   127  			BeforeEach(func() {
   128  				expectedErr = errors.New("some error")
   129  				fakeUAAClient.AuthenticateReturns(
   130  					"",
   131  					"",
   132  					expectedErr,
   133  				)
   134  			})
   135  
   136  			It("returns the error", func() {
   137  				Expect(actualErr).To(MatchError(expectedErr))
   138  
   139  				Expect(fakeConfig.SetTokenInformationCallCount()).To(Equal(1))
   140  				accessToken, refreshToken, sshOAuthClient := fakeConfig.SetTokenInformationArgsForCall(0)
   141  				Expect(accessToken).To(BeEmpty())
   142  				Expect(refreshToken).To(BeEmpty())
   143  				Expect(sshOAuthClient).To(BeEmpty())
   144  
   145  				Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(1))
   146  			})
   147  		})
   148  	})
   149  
   150  	Describe("GetLoginPrompts", func() {
   151  		When("getting login prompts info from UAA", func() {
   152  			var (
   153  				prompts map[string]coreconfig.AuthPrompt
   154  			)
   155  
   156  			BeforeEach(func() {
   157  				fakeUAAClient.LoginPromptsReturns(map[string][]string{
   158  					"username": {"text", "Email"},
   159  					"pin":      {"password", "PIN Number"},
   160  				})
   161  				prompts = actor.GetLoginPrompts()
   162  			})
   163  
   164  			It("gets the login prompts", func() {
   165  				Expect(prompts).To(Equal(map[string]coreconfig.AuthPrompt{
   166  					"username": {
   167  						DisplayName: "Email",
   168  						Type:        coreconfig.AuthPromptTypeText,
   169  					},
   170  					"pin": {
   171  						DisplayName: "PIN Number",
   172  						Type:        coreconfig.AuthPromptTypePassword,
   173  					},
   174  				}))
   175  			})
   176  		})
   177  	})
   178  })