github.com/chenbh/concourse/v6@v6.4.2/fly/integration/teams_test.go (about) 1 package integration_test 2 3 import ( 4 "os/exec" 5 6 "github.com/chenbh/concourse/v6/atc" 7 "github.com/chenbh/concourse/v6/fly/ui" 8 "github.com/fatih/color" 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 "github.com/onsi/gomega/gbytes" 12 "github.com/onsi/gomega/gexec" 13 "github.com/onsi/gomega/ghttp" 14 ) 15 16 var _ = Describe("Fly CLI", func() { 17 Describe("teams", func() { 18 var ( 19 flyCmd *exec.Cmd 20 ) 21 22 BeforeEach(func() { 23 flyCmd = exec.Command(flyPath, "-t", targetName, "teams") 24 }) 25 26 Context("when teams are returned from the API", func() { 27 BeforeEach(func() { 28 atcServer.AppendHandlers( 29 ghttp.CombineHandlers( 30 ghttp.VerifyRequest("GET", "/api/v1/teams"), 31 ghttp.RespondWithJSONEncoded(200, []atc.Team{ 32 { 33 ID: 1, 34 Name: "main", 35 Auth: atc.TeamAuth{ 36 "owner": map[string][]string{ 37 "groups": []string{}, 38 "users": []string{}, 39 }, 40 }, 41 }, 42 { 43 ID: 2, 44 Name: "a-team", 45 Auth: atc.TeamAuth{ 46 "owner": map[string][]string{ 47 "groups": []string{"github:github-org"}, 48 "users": []string{}, 49 }, 50 }, 51 }, 52 { 53 ID: 3, 54 Name: "b-team", 55 Auth: atc.TeamAuth{ 56 "member": map[string][]string{ 57 "groups": []string{}, 58 "users": []string{"github:github-user"}, 59 }, 60 }, 61 }, 62 { 63 ID: 4, 64 Name: "c-team", 65 Auth: atc.TeamAuth{ 66 "owner": map[string][]string{ 67 "users": []string{"github:github-user"}, 68 "groups": []string{"github:github-org"}, 69 }, 70 "member": map[string][]string{ 71 "users": []string{"github:github-user"}, 72 "groups": []string{"github:github-org"}, 73 }, 74 "viewer": map[string][]string{ 75 "users": []string{"github:github-user"}, 76 "groups": []string{"github:github-org"}, 77 }, 78 }, 79 }, 80 }), 81 ), 82 ) 83 }) 84 85 It("lists them to the user, ordered by name", func() { 86 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 87 Expect(err).NotTo(HaveOccurred()) 88 89 Eventually(sess).Should(gexec.Exit(0)) 90 Expect(sess.Out).To(PrintTable(ui.Table{ 91 Headers: ui.TableRow{ 92 {Contents: "name", Color: color.New(color.Bold)}, 93 }, 94 Data: []ui.TableRow{ 95 {{Contents: "a-team"}}, 96 {{Contents: "b-team"}}, 97 {{Contents: "c-team"}}, 98 {{Contents: "main"}}, 99 }, 100 })) 101 }) 102 103 Context("when --json is given", func() { 104 BeforeEach(func() { 105 flyCmd.Args = append(flyCmd.Args, "--json") 106 }) 107 108 It("prints response in json as stdout", func() { 109 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 110 Expect(err).NotTo(HaveOccurred()) 111 112 Eventually(sess).Should(gexec.Exit(0)) 113 Expect(sess.Out.Contents()).To(MatchJSON(`[ 114 { 115 "id": 1, 116 "name": "main", 117 "auth": { "owner":{"groups":[], "users":[]}} 118 }, 119 { 120 "id": 2, 121 "name": "a-team", 122 "auth": { 123 "owner": { 124 "groups": ["github:github-org"], 125 "users": [] 126 } 127 } 128 }, 129 { 130 "id": 3, 131 "name": "b-team", 132 "auth": { 133 "member": { 134 "users": ["github:github-user"], 135 "groups": [] 136 } 137 } 138 }, 139 { 140 "id": 4, 141 "name": "c-team", 142 "auth": { 143 "owner": { 144 "groups":["github:github-org"], 145 "users":["github:github-user"] 146 }, 147 "member": { 148 "groups":["github:github-org"], 149 "users":["github:github-user"] 150 }, 151 "viewer": { 152 "groups":["github:github-org"], 153 "users":["github:github-user"] 154 } 155 } 156 } 157 ]`)) 158 }) 159 }) 160 161 Context("when the details flag is specified", func() { 162 BeforeEach(func() { 163 flyCmd.Args = append(flyCmd.Args, "--details") 164 }) 165 166 It("lists them to the user, ordered by name", func() { 167 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 168 Expect(err).NotTo(HaveOccurred()) 169 170 Eventually(sess).Should(gexec.Exit(0)) 171 Expect(sess.Out).To(PrintTable(ui.Table{ 172 Data: []ui.TableRow{ 173 {{Contents: "a-team/owner"}, {Contents: "none"}, {Contents: "github:github-org"}}, 174 {{Contents: "b-team/member"}, {Contents: "github:github-user"}, {Contents: "none"}}, 175 {{Contents: "c-team/member"}, {Contents: "github:github-user"}, {Contents: "github:github-org"}}, 176 {{Contents: "c-team/owner"}, {Contents: "github:github-user"}, {Contents: "github:github-org"}}, 177 {{Contents: "c-team/viewer"}, {Contents: "github:github-user"}, {Contents: "github:github-org"}}, 178 {{Contents: "main/owner"}, {Contents: "all"}, {Contents: "none"}}, 179 }, 180 })) 181 }) 182 }) 183 }) 184 185 Context("and the api returns an internal server error", func() { 186 BeforeEach(func() { 187 atcServer.AppendHandlers( 188 ghttp.CombineHandlers( 189 ghttp.VerifyRequest("GET", "/api/v1/teams"), 190 ghttp.RespondWith(500, ""), 191 ), 192 ) 193 }) 194 195 It("writes an error message to stderr", func() { 196 sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter) 197 Expect(err).NotTo(HaveOccurred()) 198 199 Eventually(sess).Should(gexec.Exit(1)) 200 Eventually(sess.Err).Should(gbytes.Say("Unexpected Response")) 201 }) 202 }) 203 }) 204 })