|
| 1 | +package tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + . "github.com/onsi/ginkgo/v2" // nolint |
| 11 | + . "github.com/onsi/gomega" // nolint |
| 12 | + . "github.com/onsi/gomega/ghttp" // nolint |
| 13 | + |
| 14 | + . "github.com/openshift-online/ocm-sdk-go/testing" // nolint |
| 15 | +) |
| 16 | + |
| 17 | +var _ = Describe("List users", func() { |
| 18 | + var ctx context.Context |
| 19 | + |
| 20 | + var ssoServer *Server |
| 21 | + var apiServer *Server |
| 22 | + var config string |
| 23 | + |
| 24 | + var subscriptionInfo string = `{ |
| 25 | + "items": [ |
| 26 | + { |
| 27 | + "kind": "Subscription", |
| 28 | + "cluster_id": "my-cluster", |
| 29 | + "id": "subsID" |
| 30 | + } |
| 31 | + ] |
| 32 | + }` |
| 33 | + |
| 34 | + var clusterInfo string = `{ |
| 35 | + "kind": "ClusterList", |
| 36 | + "total": 1, |
| 37 | + "items": [ |
| 38 | + { |
| 39 | + "kind": "Cluster", |
| 40 | + "id": "my-cluster", |
| 41 | + "subscription": { "id": "subsID" }, |
| 42 | + "state": "ready" |
| 43 | + } |
| 44 | + ] |
| 45 | + }` |
| 46 | + |
| 47 | + var groupsInfo string = `{ |
| 48 | + "kind":"GroupList", |
| 49 | + "href":"/api/clusters_mgmt/v1/clusters/my-cluster/groups", |
| 50 | + "page":1, |
| 51 | + "size":2, |
| 52 | + "total":2, |
| 53 | + "items": [ |
| 54 | + { |
| 55 | + "kind":"Group", |
| 56 | + "id":"dedicated-admins", |
| 57 | + "users": { |
| 58 | + "kind":"UserList", |
| 59 | + "items": [ |
| 60 | + { |
| 61 | + "kind":"User", |
| 62 | + "id":"ddddddddddddddddddddddddddddddddddddddddddd" |
| 63 | + }, |
| 64 | + { |
| 65 | + "kind":"User", |
| 66 | + "id":"ssssss" |
| 67 | + } |
| 68 | + ] |
| 69 | + } |
| 70 | + }, |
| 71 | + { |
| 72 | + "kind":"Group", |
| 73 | + "id":"cluster-admins", |
| 74 | + "users": { |
| 75 | + "kind":"UserList", |
| 76 | + "items": [ |
| 77 | + { |
| 78 | + "kind":"User", |
| 79 | + "id":"ddddddddddddddddddddddddddddddddddddddddddd" |
| 80 | + }, |
| 81 | + { |
| 82 | + "kind":"User", |
| 83 | + "id":"ssssssssss" |
| 84 | + } |
| 85 | + ] |
| 86 | + } |
| 87 | + } |
| 88 | + ] |
| 89 | + }` |
| 90 | + |
| 91 | + BeforeEach(func() { |
| 92 | + // Create a context: |
| 93 | + ctx = context.Background() |
| 94 | + |
| 95 | + // Create the servers: |
| 96 | + ssoServer = MakeTCPServer() |
| 97 | + apiServer = MakeTCPServer() |
| 98 | + |
| 99 | + // Create the token: |
| 100 | + accessToken := MakeTokenString("Bearer", 15*time.Minute) |
| 101 | + |
| 102 | + // Prepare the server: |
| 103 | + ssoServer.AppendHandlers( |
| 104 | + RespondWithAccessToken(accessToken), |
| 105 | + ) |
| 106 | + |
| 107 | + // Login: |
| 108 | + result := NewCommand(). |
| 109 | + Args( |
| 110 | + "login", |
| 111 | + "--client-id", "my-client", |
| 112 | + "--client-secret", "my-secret", |
| 113 | + "--token-url", ssoServer.URL(), |
| 114 | + "--url", apiServer.URL(), |
| 115 | + ). |
| 116 | + Run(ctx) |
| 117 | + Expect(result.ExitCode()).To(BeZero()) |
| 118 | + config = result.ConfigString() |
| 119 | + }) |
| 120 | + |
| 121 | + AfterEach(func() { |
| 122 | + // Close the servers: |
| 123 | + ssoServer.Close() |
| 124 | + apiServer.Close() |
| 125 | + }) |
| 126 | + |
| 127 | + It("Prints GROUP and USER columns aligned correctly", func() { |
| 128 | + // Prepare the server: |
| 129 | + apiServer.AppendHandlers( |
| 130 | + RespondWithJSON(http.StatusOK, subscriptionInfo), |
| 131 | + RespondWithJSON(http.StatusOK, clusterInfo), |
| 132 | + RespondWithJSON(http.StatusOK, groupsInfo), |
| 133 | + ) |
| 134 | + |
| 135 | + result := NewCommand(). |
| 136 | + ConfigString(config). |
| 137 | + Args("list", "users", "--cluster", "my-cluster"). |
| 138 | + Run(ctx) |
| 139 | + |
| 140 | + lines := result.OutLines() |
| 141 | + header := lines[0] |
| 142 | + |
| 143 | + Expect(result.ExitCode()).To(BeZero()) |
| 144 | + Expect(lines).To(HaveLen(5)) |
| 145 | + Expect(header).To(MatchRegexp(`^GROUP\s+USER$`)) |
| 146 | + |
| 147 | + // Skip header and read content |
| 148 | + userHeaderCol := strings.Index(header, "USER") |
| 149 | + for i := 1; i < len(lines); i++ { |
| 150 | + line := lines[i] |
| 151 | + |
| 152 | + // Split into logical columns to get the actual username value |
| 153 | + fields := strings.Fields(line) |
| 154 | + Expect(fields).To(HaveLen(2)) |
| 155 | + |
| 156 | + userCol := strings.Index(line, fields[1]) |
| 157 | + Expect(userCol).To(Equal(userHeaderCol), fmt.Sprintf("USER column in line %d is not aligned with header", i)) |
| 158 | + } |
| 159 | + }) |
| 160 | + |
| 161 | +}) |
0 commit comments