github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/actor/v2action/auth_test.go (about)

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