github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/integration/isolated/auth_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/integration/helpers"
     5  
     6  	. "github.com/onsi/ginkgo"
     7  	. "github.com/onsi/gomega"
     8  	. "github.com/onsi/gomega/gbytes"
     9  	. "github.com/onsi/gomega/gexec"
    10  )
    11  
    12  var _ = Describe("auth command", func() {
    13  	Context("Help", func() {
    14  		It("displays the help information", func() {
    15  			session := helpers.CF("auth", "--help")
    16  			Eventually(session).Should(Say("NAME:"))
    17  			Eventually(session).Should(Say("auth - Authenticate non-interactively\n\n"))
    18  
    19  			Eventually(session).Should(Say("USAGE:"))
    20  			Eventually(session).Should(Say("cf auth USERNAME PASSWORD\n"))
    21  			Eventually(session).Should(Say("cf auth CLIENT_ID CLIENT_SECRET --client-credentials\n\n"))
    22  
    23  			Eventually(session).Should(Say("WARNING:"))
    24  			Eventually(session).Should(Say("Providing your password as a command line option is highly discouraged"))
    25  			Eventually(session).Should(Say("Your password may be visible to others and may be recorded in your shell history\n\n"))
    26  
    27  			Eventually(session).Should(Say("EXAMPLES:"))
    28  			Eventually(session).Should(Say("cf auth name@example\\.com \"my password\" \\(use quotes for passwords with a space\\)"))
    29  			Eventually(session).Should(Say("cf auth name@example\\.com \\\"\\\\\"password\\\\\"\\\" \\(escape quotes if used in password\\)\n\n"))
    30  
    31  			Eventually(session).Should(Say("SEE ALSO:"))
    32  			Eventually(session).Should(Say("api, login, target"))
    33  
    34  			Eventually(session).Should(Exit(0))
    35  		})
    36  	})
    37  
    38  	Context("when no arguments are provided", func() {
    39  		It("errors-out with the help information", func() {
    40  			session := helpers.CF("auth")
    41  			Eventually(session.Err).Should(Say("Incorrect Usage: the required arguments `USERNAME` and `PASSWORD` were not provided\n\n"))
    42  			Eventually(session).Should(Say("NAME:"))
    43  
    44  			Eventually(session).Should(Exit(1))
    45  		})
    46  	})
    47  
    48  	Context("when only a username is provided", func() {
    49  		It("errors-out with a password required error and the help information", func() {
    50  			session := helpers.CF("auth", "some-user")
    51  			Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `PASSWORD` was not provided\n\n"))
    52  			Eventually(session).Should(Say("NAME:"))
    53  
    54  			Eventually(session).Should(Exit(1))
    55  		})
    56  	})
    57  
    58  	Context("when too many arguments are provided", func() {
    59  		It("displays an 'unknown flag' error message", func() {
    60  			session := helpers.CF("auth", "some-username", "some-password", "-a", "api.bosh-lite.com")
    61  
    62  			Eventually(session.Err).Should(Say("Incorrect Usage: unknown flag `a'"))
    63  			Eventually(session).Should(Say("NAME:"))
    64  
    65  			Eventually(session).Should(Exit(1))
    66  		})
    67  	})
    68  
    69  	Context("when the API endpoint is not set", func() {
    70  		BeforeEach(func() {
    71  			helpers.UnsetAPI()
    72  		})
    73  
    74  		It("displays an error message", func() {
    75  			session := helpers.CF("auth", "some-username", "some-password")
    76  
    77  			Eventually(session).Should(Say("FAILED"))
    78  			Eventually(session.Err).Should(Say("No API endpoint set\\. Use 'cf login' or 'cf api' to target an endpoint\\."))
    79  
    80  			Eventually(session).Should(Exit(1))
    81  		})
    82  	})
    83  
    84  	Context("when no flags are set (logging in with password grant type)", func() {
    85  		Context("when the user provides an invalid username/password combo", func() {
    86  			BeforeEach(func() {
    87  				helpers.LoginCF()
    88  				helpers.TargetOrgAndSpace(ReadOnlyOrg, ReadOnlySpace)
    89  			})
    90  
    91  			It("clears the cached tokens and target info, then displays an error message", func() {
    92  				session := helpers.CF("auth", "some-username", "some-password")
    93  
    94  				Eventually(session).Should(Say("API endpoint: %s", helpers.GetAPI()))
    95  				Eventually(session).Should(Say("Authenticating\\.\\.\\."))
    96  				Eventually(session).Should(Say("FAILED"))
    97  				Eventually(session.Err).Should(Say("Credentials were rejected, please try again\\."))
    98  				Eventually(session).Should(Exit(1))
    99  
   100  				// Verify that the user is not logged-in
   101  				targetSession1 := helpers.CF("target")
   102  				Eventually(targetSession1.Err).Should(Say("Not logged in\\. Use 'cf login' to log in\\."))
   103  				Eventually(targetSession1).Should(Say("FAILED"))
   104  				Eventually(targetSession1).Should(Exit(1))
   105  
   106  				// Verify that neither org nor space is targeted
   107  				helpers.LoginCF()
   108  				targetSession2 := helpers.CF("target")
   109  				Eventually(targetSession2).Should(Say("No org or space targeted, use 'cf target -o ORG -s SPACE'"))
   110  				Eventually(targetSession2).Should(Exit(0))
   111  			})
   112  		})
   113  
   114  		Context("when the username and password are valid", func() {
   115  			It("authenticates the user", func() {
   116  				username, password := helpers.GetCredentials()
   117  				session := helpers.CF("auth", username, password)
   118  
   119  				Eventually(session).Should(Say("API endpoint: %s", helpers.GetAPI()))
   120  				Eventually(session).Should(Say("Authenticating\\.\\.\\."))
   121  				Eventually(session).Should(Say("OK"))
   122  				Eventually(session).Should(Say("Use 'cf target' to view or set your target org and space"))
   123  
   124  				Eventually(session).Should(Exit(0))
   125  			})
   126  		})
   127  	})
   128  
   129  	Context("when the 'client-credentials' flag is set", func() {
   130  		Context("when the user provides an invalid client id/secret combo", func() {
   131  			BeforeEach(func() {
   132  				helpers.LoginCF()
   133  				helpers.TargetOrgAndSpace(ReadOnlyOrg, ReadOnlySpace)
   134  			})
   135  
   136  			It("clears the cached tokens and target info, then displays an error message", func() {
   137  				session := helpers.CF("auth", "some-client-id", "some-client-secret", "--client-credentials")
   138  
   139  				Eventually(session).Should(Say("API endpoint: %s", helpers.GetAPI()))
   140  				Eventually(session).Should(Say("Authenticating\\.\\.\\."))
   141  				Eventually(session).Should(Say("FAILED"))
   142  				Eventually(session.Err).Should(Say("Credentials were rejected, please try again\\."))
   143  				Eventually(session).Should(Exit(1))
   144  
   145  				// Verify that the user is not logged-in
   146  				targetSession1 := helpers.CF("target")
   147  				Eventually(targetSession1.Err).Should(Say("Not logged in\\. Use 'cf login' to log in\\."))
   148  				Eventually(targetSession1).Should(Say("FAILED"))
   149  				Eventually(targetSession1).Should(Exit(1))
   150  
   151  				// Verify that neither org nor space is targeted
   152  				helpers.LoginCF()
   153  				targetSession2 := helpers.CF("target")
   154  				Eventually(targetSession2).Should(Say("No org or space targeted, use 'cf target -o ORG -s SPACE'"))
   155  				Eventually(targetSession2).Should(Exit(0))
   156  			})
   157  		})
   158  
   159  		Context("when the client id and client secret are valid", func() {
   160  			It("authenticates the user", func() {
   161  				clientID, clientSecret := helpers.SkipIfClientCredentialsNotSet()
   162  				session := helpers.CF("auth", clientID, clientSecret, "--client-credentials")
   163  
   164  				Eventually(session).Should(Say("API endpoint: %s", helpers.GetAPI()))
   165  				Eventually(session).Should(Say("Authenticating\\.\\.\\."))
   166  				Eventually(session).Should(Say("OK"))
   167  				Eventually(session).Should(Say("Use 'cf target' to view or set your target org and space"))
   168  
   169  				Eventually(session).Should(Exit(0))
   170  			})
   171  		})
   172  	})
   173  
   174  	Context("when a user authenticates with valid client credentials", func() {
   175  		BeforeEach(func() {
   176  			clientID, clientSecret := helpers.SkipIfClientCredentialsNotSet()
   177  			session := helpers.CF("auth", clientID, clientSecret, "--client-credentials")
   178  			Eventually(session).Should(Exit(0))
   179  		})
   180  
   181  		Context("when a different user authenticates with valid password credentials", func() {
   182  			It("should fail authentication and displays an error informing the user they need to log out", func() {
   183  				username, password := helpers.GetCredentials()
   184  				session := helpers.CF("auth", username, password)
   185  
   186  				Eventually(session).Should(Say("FAILED"))
   187  				Eventually(session.Err).Should(Say("Service account currently logged in\\. Use 'cf logout' to log out service account and try again\\."))
   188  				Eventually(session).Should(Exit(1))
   189  			})
   190  		})
   191  	})
   192  })