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

     1  package integration_test
     2  
     3  import (
     4  	"net/http"
     5  	"os/exec"
     6  
     7  	"github.com/pf-qiu/concourse/v6/atc"
     8  	"github.com/pf-qiu/concourse/v6/fly/ui"
     9  	"github.com/fatih/color"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  	"github.com/onsi/gomega/gbytes"
    13  	"github.com/onsi/gomega/gexec"
    14  	"github.com/onsi/gomega/ghttp"
    15  	"github.com/tedsuo/rata"
    16  )
    17  
    18  var _ = Describe("Fly CLI", func() {
    19  	Describe("get-team", func() {
    20  		var team atc.Team
    21  
    22  		BeforeEach(func() {
    23  			team = atc.Team{
    24  				ID:   1,
    25  				Name: "myTeam",
    26  				Auth: atc.TeamAuth{
    27  					"owner": map[string][]string{
    28  						"groups": {}, "users": {"local:username"},
    29  					},
    30  				},
    31  			}
    32  		})
    33  
    34  		Context("when not specifying a team name", func() {
    35  			It("fails and says you should give a team name", func() {
    36  				flyCmd := exec.Command(flyPath, "-t", targetName, "get-team")
    37  
    38  				sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    39  				Expect(err).NotTo(HaveOccurred())
    40  
    41  				<-sess.Exited
    42  				Expect(sess.ExitCode()).To(Equal(1))
    43  				Expect(sess.Err).To(gbytes.Say("error: the required flag `" + osFlag("n", "team-name") + "' was not specified"))
    44  			})
    45  		})
    46  
    47  		Context("when specifying a team name", func() {
    48  			var path string
    49  			BeforeEach(func() {
    50  				var err error
    51  				path, err = atc.Routes.CreatePathForRoute(atc.GetTeam, rata.Params{"team_name": "myTeam"})
    52  				Expect(err).NotTo(HaveOccurred())
    53  			})
    54  
    55  			Context("and team is not found", func() {
    56  				JustBeforeEach(func() {
    57  					atcServer.AppendHandlers(
    58  						ghttp.CombineHandlers(
    59  							ghttp.VerifyRequest("GET", path),
    60  							ghttp.RespondWithJSONEncoded(http.StatusNotFound, ""),
    61  						),
    62  					)
    63  				})
    64  
    65  				It("should print team not found error", func() {
    66  					flyCmd := exec.Command(flyPath, "-t", targetName, "get-team", "-n", "myTeam")
    67  
    68  					sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    69  					Expect(err).NotTo(HaveOccurred())
    70  
    71  					<-sess.Exited
    72  					Expect(sess.ExitCode()).To(Equal(1))
    73  				})
    74  			})
    75  
    76  			Context("when atc returns team config", func() {
    77  				BeforeEach(func() {
    78  					atcServer.AppendHandlers(
    79  						ghttp.CombineHandlers(
    80  							ghttp.VerifyRequest("GET", path),
    81  							ghttp.RespondWithJSONEncoded(200, team),
    82  						),
    83  					)
    84  				})
    85  
    86  				It("prints the team config to stdout", func() {
    87  					flyCmd := exec.Command(flyPath, "-t", targetName, "get-team", "-n", "myTeam")
    88  
    89  					sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    90  					Expect(err).NotTo(HaveOccurred())
    91  					Eventually(sess).Should(gexec.Exit(0))
    92  
    93  					Expect(sess.Out).To(PrintTable(ui.Table{
    94  						Headers: ui.TableRow{
    95  							{Contents: "name/role", Color: color.New(color.Bold)},
    96  							{Contents: "users", Color: color.New(color.Bold)},
    97  							{Contents: "groups", Color: color.New(color.Bold)},
    98  						},
    99  						Data: []ui.TableRow{
   100  							{{Contents: "myTeam/owner"}, {Contents: "local:username"}, {Contents: "none"}},
   101  						},
   102  					}))
   103  				})
   104  
   105  				It("produces structured JSON output if requested", func() {
   106  					flyCmd := exec.Command(flyPath, "-t", targetName, "get-team", "-n", "myTeam", "--json")
   107  
   108  					sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   109  					Expect(err).NotTo(HaveOccurred())
   110  					Eventually(sess).Should(gexec.Exit(0))
   111  
   112  					Expect(sess.Out.Contents()).To(MatchJSON(`{"id": 1, "name": "myTeam", "auth": {"owner": {"groups": [], "users": ["local:username"] }}}`))
   113  				})
   114  			})
   115  		})
   116  	})
   117  })