|
| 1 | +// Copyright 2025 The go-github AUTHORS. All rights reserved. |
| 2 | +// |
| 3 | +// Use of this source code is governed by a BSD-style |
| 4 | +// license that can be found in the LICENSE file. |
| 5 | + |
| 6 | +package github |
| 7 | + |
| 8 | +import ( |
| 9 | + "net/http" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/google/go-cmp/cmp" |
| 13 | +) |
| 14 | + |
| 15 | +func TestSCIMEnterpriseGroups_Marshal(t *testing.T) { |
| 16 | + t.Parallel() |
| 17 | + testJSONMarshal(t, &SCIMEnterpriseGroups{}, "{}") |
| 18 | + |
| 19 | + u := &SCIMEnterpriseGroups{ |
| 20 | + Schemas: []string{SCIMSchemasURINamespacesListResponse}, |
| 21 | + TotalResults: Ptr(1), |
| 22 | + ItemsPerPage: Ptr(1), |
| 23 | + StartIndex: Ptr(1), |
| 24 | + Resources: []*SCIMEnterpriseGroupAttributes{{ |
| 25 | + DisplayName: Ptr("gn1"), |
| 26 | + Members: []*SCIMEnterpriseDisplayReference{{ |
| 27 | + Value: "idm1", |
| 28 | + Ref: "https://api.github.com/scim/v2/enterprises/ee/Users/idm1", |
| 29 | + Display: Ptr("m1"), |
| 30 | + }}, |
| 31 | + Schemas: []string{SCIMSchemasURINamespacesGroups}, |
| 32 | + ExternalID: Ptr("eidgn1"), |
| 33 | + ID: Ptr("idgn1"), |
| 34 | + Meta: &SCIMEnterpriseMeta{ |
| 35 | + ResourceType: "Group", |
| 36 | + Created: &Timestamp{referenceTime}, |
| 37 | + LastModified: &Timestamp{referenceTime}, |
| 38 | + Location: Ptr("https://api.github.com/scim/v2/enterprises/ee/Groups/idgn1"), |
| 39 | + }, |
| 40 | + }}, |
| 41 | + } |
| 42 | + |
| 43 | + want := `{ |
| 44 | + "schemas": ["` + SCIMSchemasURINamespacesListResponse + `"], |
| 45 | + "totalResults": 1, |
| 46 | + "itemsPerPage": 1, |
| 47 | + "startIndex": 1, |
| 48 | + "Resources": [{ |
| 49 | + "schemas": ["` + SCIMSchemasURINamespacesGroups + `"], |
| 50 | + "id": "idgn1", |
| 51 | + "externalId": "eidgn1", |
| 52 | + "displayName": "gn1", |
| 53 | + "meta": { |
| 54 | + "resourceType": "Group", |
| 55 | + "created": ` + referenceTimeStr + `, |
| 56 | + "lastModified": ` + referenceTimeStr + `, |
| 57 | + "location": "https://api.github.com/scim/v2/enterprises/ee/Groups/idgn1" |
| 58 | + }, |
| 59 | + "members": [{ |
| 60 | + "value": "idm1", |
| 61 | + "$ref": "https://api.github.com/scim/v2/enterprises/ee/Users/idm1", |
| 62 | + "display": "m1" |
| 63 | + }] |
| 64 | + }] |
| 65 | + }` |
| 66 | + |
| 67 | + testJSONMarshal(t, u, want) |
| 68 | +} |
| 69 | + |
| 70 | +func TestSCIMEnterpriseGroupAttributes_Marshal(t *testing.T) { |
| 71 | + t.Parallel() |
| 72 | + testJSONMarshal(t, &SCIMEnterpriseGroupAttributes{}, "{}") |
| 73 | + |
| 74 | + u := &SCIMEnterpriseGroupAttributes{ |
| 75 | + DisplayName: Ptr("dn"), |
| 76 | + Members: []*SCIMEnterpriseDisplayReference{{ |
| 77 | + Value: "v", |
| 78 | + Ref: "r", |
| 79 | + Display: Ptr("d"), |
| 80 | + }}, |
| 81 | + ExternalID: Ptr("eid"), |
| 82 | + ID: Ptr("id"), |
| 83 | + Schemas: []string{"s1"}, |
| 84 | + Meta: &SCIMEnterpriseMeta{ |
| 85 | + ResourceType: "rt", |
| 86 | + Created: &Timestamp{referenceTime}, |
| 87 | + LastModified: &Timestamp{referenceTime}, |
| 88 | + Location: Ptr("l"), |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + want := `{ |
| 93 | + "schemas": ["s1"], |
| 94 | + "externalId": "eid", |
| 95 | + "displayName": "dn", |
| 96 | + "members" : [{ |
| 97 | + "value": "v", |
| 98 | + "$ref": "r", |
| 99 | + "display": "d" |
| 100 | + }], |
| 101 | + "id": "id", |
| 102 | + "meta": { |
| 103 | + "resourceType": "rt", |
| 104 | + "created": ` + referenceTimeStr + `, |
| 105 | + "lastModified": ` + referenceTimeStr + `, |
| 106 | + "location": "l" |
| 107 | + } |
| 108 | + }` |
| 109 | + |
| 110 | + testJSONMarshal(t, u, want) |
| 111 | +} |
| 112 | + |
| 113 | +func TestEnterpriseService_ListProvisionedSCIMEnterpriseGroups(t *testing.T) { |
| 114 | + t.Parallel() |
| 115 | + client, mux, _ := setup(t) |
| 116 | + |
| 117 | + mux.HandleFunc("/scim/v2/enterprises/ee/Groups", func(w http.ResponseWriter, r *http.Request) { |
| 118 | + testMethod(t, r, "GET") |
| 119 | + testFormValues(t, r, values{ |
| 120 | + "startIndex": "1", |
| 121 | + "excludedAttributes": "members,meta", |
| 122 | + "count": "3", |
| 123 | + "filter": `externalId eq "914a"`, |
| 124 | + }) |
| 125 | + w.WriteHeader(http.StatusOK) |
| 126 | + _, _ = w.Write([]byte(`{ |
| 127 | + "schemas": ["` + SCIMSchemasURINamespacesListResponse + `"], |
| 128 | + "totalResults": 1, |
| 129 | + "itemsPerPage": 1, |
| 130 | + "startIndex": 1, |
| 131 | + "Resources": [{ |
| 132 | + "schemas": ["` + SCIMSchemasURINamespacesGroups + `"], |
| 133 | + "id": "914a", |
| 134 | + "externalId": "de88", |
| 135 | + "displayName": "gn1", |
| 136 | + "meta": { |
| 137 | + "resourceType": "Group", |
| 138 | + "created": ` + referenceTimeStr + `, |
| 139 | + "lastModified": ` + referenceTimeStr + `, |
| 140 | + "location": "https://api.github.com/scim/v2/enterprises/ee/Groups/914a" |
| 141 | + }, |
| 142 | + "members": [{ |
| 143 | + "value": "e7f9", |
| 144 | + "$ref": "https://api.github.com/scim/v2/enterprises/ee/Users/e7f9", |
| 145 | + "display": "d1" |
| 146 | + }] |
| 147 | + }] |
| 148 | + }`)) |
| 149 | + }) |
| 150 | + |
| 151 | + ctx := t.Context() |
| 152 | + opts := &ListProvisionedSCIMGroupsEnterpriseOptions{ |
| 153 | + StartIndex: 1, |
| 154 | + ExcludedAttributes: "members,meta", |
| 155 | + Count: 3, |
| 156 | + Filter: `externalId eq "914a"`, |
| 157 | + } |
| 158 | + groups, _, err := client.Enterprise.ListProvisionedSCIMGroups(ctx, "ee", opts) |
| 159 | + if err != nil { |
| 160 | + t.Errorf("Enterprise.ListProvisionedSCIMGroups returned error: %v", err) |
| 161 | + } |
| 162 | + |
| 163 | + want := SCIMEnterpriseGroups{ |
| 164 | + Schemas: []string{SCIMSchemasURINamespacesListResponse}, |
| 165 | + TotalResults: Ptr(1), |
| 166 | + ItemsPerPage: Ptr(1), |
| 167 | + StartIndex: Ptr(1), |
| 168 | + Resources: []*SCIMEnterpriseGroupAttributes{{ |
| 169 | + ID: Ptr("914a"), |
| 170 | + Meta: &SCIMEnterpriseMeta{ |
| 171 | + ResourceType: "Group", |
| 172 | + Created: &Timestamp{referenceTime}, |
| 173 | + LastModified: &Timestamp{referenceTime}, |
| 174 | + Location: Ptr("https://api.github.com/scim/v2/enterprises/ee/Groups/914a"), |
| 175 | + }, |
| 176 | + DisplayName: Ptr("gn1"), |
| 177 | + Schemas: []string{SCIMSchemasURINamespacesGroups}, |
| 178 | + ExternalID: Ptr("de88"), |
| 179 | + Members: []*SCIMEnterpriseDisplayReference{{ |
| 180 | + Value: "e7f9", |
| 181 | + Ref: "https://api.github.com/scim/v2/enterprises/ee/Users/e7f9", |
| 182 | + Display: Ptr("d1"), |
| 183 | + }}, |
| 184 | + }}, |
| 185 | + } |
| 186 | + |
| 187 | + if diff := cmp.Diff(want, *groups); diff != "" { |
| 188 | + t.Errorf("Enterprise.ListProvisionedSCIMGroups diff mismatch (-want +got):\n%v", diff) |
| 189 | + } |
| 190 | + |
| 191 | + const methodName = "ListProvisionedSCIMGroups" |
| 192 | + testBadOptions(t, methodName, func() (err error) { |
| 193 | + _, _, err = client.Enterprise.ListProvisionedSCIMGroups(ctx, "\n", opts) |
| 194 | + return err |
| 195 | + }) |
| 196 | + |
| 197 | + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 198 | + _, r, err := client.Enterprise.ListProvisionedSCIMGroups(ctx, "o", opts) |
| 199 | + return r, err |
| 200 | + }) |
| 201 | +} |
0 commit comments