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

     1  package integration_test
     2  
     3  import (
     4  	"os/exec"
     5  
     6  	"github.com/pf-qiu/concourse/v6/fly/ui"
     7  	"github.com/fatih/color"
     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("Fly CLI", func() {
    16  	Describe("userinfo", func() {
    17  		var (
    18  			flyCmd *exec.Cmd
    19  		)
    20  
    21  		BeforeEach(func() {
    22  			flyCmd = exec.Command(flyPath, "-t", targetName, "userinfo")
    23  		})
    24  
    25  		Context("when userinfo is returned from the API", func() {
    26  			BeforeEach(func() {
    27  				atcServer.AppendHandlers(
    28  					ghttp.CombineHandlers(
    29  						ghttp.VerifyRequest("GET", "/api/v1/user"),
    30  						ghttp.RespondWithJSONEncoded(200, map[string]interface{}{
    31  							"sub":       "zero",
    32  							"name":      "test",
    33  							"user_id":   "test_id",
    34  							"user_name": "test_user",
    35  							"email":     "test_email",
    36  							"is_admin":  false,
    37  							"is_system": false,
    38  							"teams": map[string][]string{
    39  								"other_team": {"owner"},
    40  								"test_team":  {"owner", "viewer"},
    41  							},
    42  						}),
    43  					),
    44  				)
    45  			})
    46  
    47  			It("shows username", func() {
    48  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    49  				Expect(err).NotTo(HaveOccurred())
    50  
    51  				Eventually(sess).Should(gexec.Exit(0))
    52  				Expect(sess.Out).To(PrintTable(ui.Table{
    53  					Headers: ui.TableRow{
    54  						{Contents: "username", Color: color.New(color.Bold)},
    55  						{Contents: "team/role", Color: color.New(color.Bold)},
    56  					},
    57  					Data: []ui.TableRow{
    58  						{{Contents: "test_user"}, {Contents: "other_team/owner,test_team/owner,test_team/viewer"}},
    59  					},
    60  				}))
    61  			})
    62  
    63  			Context("when --json is given", func() {
    64  				BeforeEach(func() {
    65  					flyCmd.Args = append(flyCmd.Args, "--json")
    66  				})
    67  
    68  				It("prints response in json as stdout", func() {
    69  					sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    70  					Expect(err).NotTo(HaveOccurred())
    71  
    72  					Eventually(sess).Should(gexec.Exit(0))
    73  					Expect(sess.Out.Contents()).To(MatchJSON(`{
    74  							"sub":       "zero",
    75  							"name":      "test",
    76  							"user_id":   "test_id",
    77  							"user_name": "test_user",
    78  							"email":     "test_email",
    79  							"is_admin":  false,
    80  							"is_system": false,
    81  							"teams": {
    82  								"other_team": ["owner"],
    83  								"test_team": ["owner", "viewer"]
    84  							}
    85  					}`))
    86  				})
    87  			})
    88  		})
    89  
    90  		Context("and the api returns an internal server error", func() {
    91  			BeforeEach(func() {
    92  				atcServer.AppendHandlers(
    93  					ghttp.CombineHandlers(
    94  						ghttp.VerifyRequest("GET", "/api/v1/user"),
    95  						ghttp.RespondWith(500, ""),
    96  					),
    97  				)
    98  			})
    99  
   100  			It("writes an error message to stderr", func() {
   101  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   102  				Expect(err).NotTo(HaveOccurred())
   103  
   104  				Eventually(sess).Should(gexec.Exit(1))
   105  				Eventually(sess.Err).Should(gbytes.Say("Unexpected Response"))
   106  			})
   107  		})
   108  	})
   109  })