github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/fly/integration/status_test.go (about)

     1  package integration_test
     2  
     3  import (
     4  	"net/http"
     5  	"os/exec"
     6  
     7  	"github.com/pf-qiu/concourse/v6/fly/rc"
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  	"github.com/onsi/gomega/gbytes"
    11  	"github.com/onsi/gomega/gexec"
    12  	"github.com/onsi/gomega/ghttp"
    13  )
    14  
    15  var _ = Describe("status Command", func() {
    16  	var (
    17  		flyCmd *exec.Cmd
    18  	)
    19  
    20  	BeforeEach(func() {
    21  		createFlyRc(rc.Targets{
    22  			"with-token": {
    23  				API:      atcServer.URL(),
    24  				TeamName: "test",
    25  				Token:    &rc.TargetToken{Type: "Bearer", Value: validAccessToken(date(2020, 1, 1))},
    26  			},
    27  			"without-token": {
    28  				API:      "https://example.com/another-test",
    29  				TeamName: "test",
    30  				Token:    &rc.TargetToken{},
    31  			},
    32  		})
    33  	})
    34  
    35  	Context("status with no target name", func() {
    36  		var (
    37  			flyCmd *exec.Cmd
    38  		)
    39  		BeforeEach(func() {
    40  			flyCmd = exec.Command(flyPath, "status")
    41  		})
    42  
    43  		It("instructs the user to specify --target", func() {
    44  			sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    45  			Expect(err).NotTo(HaveOccurred())
    46  
    47  			<-sess.Exited
    48  			Expect(sess.ExitCode()).To(Equal(1))
    49  
    50  			Expect(sess.Err).To(gbytes.Say(`no target specified. specify the target with -t`))
    51  		})
    52  	})
    53  
    54  	Context("status with target name", func() {
    55  		Context("when target is saved with valid token", func() {
    56  			BeforeEach(func() {
    57  				atcServer.Reset()
    58  				atcServer.AppendHandlers(
    59  					ghttp.CombineHandlers(
    60  						ghttp.VerifyRequest("GET", "/api/v1/user"),
    61  						ghttp.RespondWithJSONEncoded(200, map[string]interface{}{"team": "test"}),
    62  					),
    63  				)
    64  			})
    65  
    66  			It("the command succeeds", func() {
    67  				flyCmd = exec.Command(flyPath, "-t", "with-token", "status")
    68  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    69  				Expect(err).NotTo(HaveOccurred())
    70  
    71  				<-sess.Exited
    72  				Expect(sess.ExitCode()).To(Equal(0))
    73  
    74  				Expect(sess.Out).To(gbytes.Say(`logged in successfully`))
    75  			})
    76  		})
    77  
    78  		Context("when target is saved with a token that is rejected by the server", func() {
    79  			BeforeEach(func() {
    80  				atcServer.Reset()
    81  				atcServer.AppendHandlers(
    82  					ghttp.CombineHandlers(
    83  						ghttp.VerifyRequest("GET", "/api/v1/user"),
    84  						ghttp.RespondWith(http.StatusUnauthorized, nil),
    85  					),
    86  				)
    87  			})
    88  
    89  			It("the command fails", func() {
    90  				flyCmd = exec.Command(flyPath, "-t", "with-token", "status")
    91  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    92  				Expect(err).NotTo(HaveOccurred())
    93  
    94  				<-sess.Exited
    95  				Expect(sess.ExitCode()).To(Equal(1))
    96  
    97  				Expect(sess.Err).To(gbytes.Say(`please login again`))
    98  				Expect(sess.Err).To(gbytes.Say(`token validation failed with error: not authorized`))
    99  			})
   100  		})
   101  
   102  		Context("when target is saved with invalid token", func() {
   103  			It("the command fails", func() {
   104  				flyCmd = exec.Command(flyPath, "-t", "without-token", "status")
   105  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   106  				Expect(err).NotTo(HaveOccurred())
   107  
   108  				<-sess.Exited
   109  				Expect(sess.ExitCode()).To(Equal(1))
   110  
   111  				Expect(sess.Err).To(gbytes.Say(`logged out`))
   112  			})
   113  		})
   114  	})
   115  })