|
| 1 | +// Copyright IBM Corp. 2018, 2026 |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package tfe |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +func TestAdminSettings_SCIM_Read(t *testing.T) { |
| 16 | + skipUnlessEnterprise(t) |
| 17 | + client := testClient(t) |
| 18 | + ctx := context.Background() |
| 19 | + |
| 20 | + t.Run("read scim settings with default values", func(t *testing.T) { |
| 21 | + scimSettings, err := client.Admin.Settings.SCIM.Read(ctx) |
| 22 | + require.NoError(t, err) |
| 23 | + |
| 24 | + assert.Equal(t, "scim", scimSettings.ID) |
| 25 | + assert.False(t, scimSettings.Enabled) |
| 26 | + assert.False(t, scimSettings.Paused) |
| 27 | + assert.Empty(t, scimSettings.SiteAdminGroupSCIMID) |
| 28 | + assert.Empty(t, scimSettings.SiteAdminGroupDisplayName) |
| 29 | + }) |
| 30 | +} |
| 31 | + |
| 32 | +func TestAdminSettings_SCIM_Update(t *testing.T) { |
| 33 | + skipUnlessEnterprise(t) |
| 34 | + client := testClient(t) |
| 35 | + ctx := context.Background() |
| 36 | + |
| 37 | + enableSAML(ctx, t, client, true) |
| 38 | + defer enableSAML(ctx, t, client, false) |
| 39 | + |
| 40 | + scimClient := client.Admin.Settings.SCIM |
| 41 | + |
| 42 | + t.Run("enable scim settings", func(t *testing.T) { |
| 43 | + err := setSAMLProviderType(ctx, t, client, true) |
| 44 | + require.NoErrorf(t, err, "failed to set SAML provider type") |
| 45 | + defer cleanupSCIMSettings(ctx, t, client) |
| 46 | + |
| 47 | + scimSettings, err := scimClient.Update(ctx, AdminSCIMSettingUpdateOptions{Enabled: Bool(true)}) |
| 48 | + require.NoError(t, err) |
| 49 | + |
| 50 | + assert.True(t, scimSettings.Enabled) |
| 51 | + }) |
| 52 | + |
| 53 | + t.Run("pause scim settings", func(t *testing.T) { |
| 54 | + err := setSAMLProviderType(ctx, t, client, true) |
| 55 | + require.NoErrorf(t, err, "failed to set SAML provider type") |
| 56 | + defer cleanupSCIMSettings(ctx, t, client) |
| 57 | + |
| 58 | + _, err = scimClient.Update(ctx, AdminSCIMSettingUpdateOptions{ |
| 59 | + Enabled: Bool(true), |
| 60 | + }) |
| 61 | + require.NoError(t, err) |
| 62 | + |
| 63 | + testCases := []struct { |
| 64 | + name string |
| 65 | + paused bool |
| 66 | + }{ |
| 67 | + {"pause scim provisioning", true}, |
| 68 | + {"unpause scim provisioning", false}, |
| 69 | + } |
| 70 | + |
| 71 | + for _, tc := range testCases { |
| 72 | + t.Run(tc.name, func(t *testing.T) { |
| 73 | + _, err := scimClient.Update(ctx, AdminSCIMSettingUpdateOptions{Paused: &tc.paused}) |
| 74 | + require.NoError(t, err) |
| 75 | + scimSettings, err := scimClient.Read(ctx) |
| 76 | + require.NoError(t, err) |
| 77 | + assert.Equal(t, tc.paused, scimSettings.Paused) |
| 78 | + }) |
| 79 | + } |
| 80 | + }) |
| 81 | + |
| 82 | + t.Run("update site admin group scim id", func(t *testing.T) { |
| 83 | + err := setSAMLProviderType(ctx, t, client, true) |
| 84 | + require.NoErrorf(t, err, "failed to set SAML provider type") |
| 85 | + defer cleanupSCIMSettings(ctx, t, client) |
| 86 | + |
| 87 | + _, err = scimClient.Update(ctx, AdminSCIMSettingUpdateOptions{Enabled: Bool(true)}) |
| 88 | + require.NoError(t, err) |
| 89 | + |
| 90 | + scimToken := generateSCIMToken(ctx, t, client) |
| 91 | + scimGroupID := createSCIMGroup(ctx, t, client, "foo", scimToken) |
| 92 | + |
| 93 | + testCases := []struct { |
| 94 | + name string |
| 95 | + scimGroupID string |
| 96 | + raiseError bool |
| 97 | + }{ |
| 98 | + {"link scim group to site admin role", scimGroupID, false}, |
| 99 | + {"trying to link non-existent group - should raise error", "this-group-doesn't-exist", true}, |
| 100 | + } |
| 101 | + |
| 102 | + for _, tc := range testCases { |
| 103 | + t.Run(tc.name, func(t *testing.T) { |
| 104 | + _, err := scimClient.Update(ctx, AdminSCIMSettingUpdateOptions{SiteAdminGroupSCIMID: &tc.scimGroupID}) |
| 105 | + if tc.raiseError { |
| 106 | + require.Error(t, err) |
| 107 | + return |
| 108 | + } |
| 109 | + require.NoError(t, err) |
| 110 | + scimSettings, err := scimClient.Read(ctx) |
| 111 | + require.NoError(t, err) |
| 112 | + assert.Equal(t, tc.scimGroupID, scimSettings.SiteAdminGroupSCIMID) |
| 113 | + assert.Equal(t, "foo", scimSettings.SiteAdminGroupDisplayName) |
| 114 | + }) |
| 115 | + } |
| 116 | + }) |
| 117 | +} |
| 118 | + |
| 119 | +func TestAdminSettings_SCIM_Delete(t *testing.T) { |
| 120 | + skipUnlessEnterprise(t) |
| 121 | + client := testClient(t) |
| 122 | + ctx := context.Background() |
| 123 | + |
| 124 | + enableSAML(ctx, t, client, true) |
| 125 | + defer enableSAML(ctx, t, client, false) |
| 126 | + |
| 127 | + scimClient := client.Admin.Settings.SCIM |
| 128 | + |
| 129 | + t.Run("disable scim settings", func(t *testing.T) { |
| 130 | + err := setSAMLProviderType(ctx, t, client, true) |
| 131 | + require.NoErrorf(t, err, "failed to set SAML provider type") |
| 132 | + defer cleanupSCIMSettings(ctx, t, client) |
| 133 | + |
| 134 | + testCases := []struct { |
| 135 | + name string |
| 136 | + isScimEnabled bool |
| 137 | + }{ |
| 138 | + {"disable scim provisioning when it's already enabled", true}, |
| 139 | + {"disable scim provisioning when it's already disabled - should not raise error", false}, |
| 140 | + } |
| 141 | + |
| 142 | + for _, tc := range testCases { |
| 143 | + t.Run(tc.name, func(t *testing.T) { |
| 144 | + if tc.isScimEnabled { |
| 145 | + _, err := scimClient.Update(ctx, AdminSCIMSettingUpdateOptions{Enabled: Bool(true)}) |
| 146 | + require.NoError(t, err) |
| 147 | + } |
| 148 | + |
| 149 | + err := scimClient.Delete(ctx) |
| 150 | + require.NoError(t, err) |
| 151 | + |
| 152 | + scimSettings, err := scimClient.Read(ctx) |
| 153 | + require.NoError(t, err) |
| 154 | + assert.False(t, scimSettings.Enabled) |
| 155 | + }) |
| 156 | + } |
| 157 | + }) |
| 158 | +} |
| 159 | + |
| 160 | +// cleanup scim settings by disabling scim provisioning and setting saml provider type to unknown. |
| 161 | +func cleanupSCIMSettings(ctx context.Context, t *testing.T, client *Client) { |
| 162 | + t.Helper() |
| 163 | + scimSettings, err := client.Admin.Settings.SCIM.Read(ctx) |
| 164 | + if err == nil && scimSettings.Enabled { |
| 165 | + err = client.Admin.Settings.SCIM.Delete(ctx) |
| 166 | + require.NoErrorf(t, err, "failed to disable SCIM provisioning") |
| 167 | + } |
| 168 | + |
| 169 | + err = setSAMLProviderType(ctx, t, client, false) |
| 170 | + require.NoErrorf(t, err, "failed to set SAML provider type") |
| 171 | +} |
| 172 | + |
| 173 | +// generate a SCIM token for testing |
| 174 | +func generateSCIMToken(ctx context.Context, t *testing.T, client *Client) string { |
| 175 | + t.Helper() |
| 176 | + // TFE requires a minimum of 30 days for SCIM token expiration |
| 177 | + expiredAt := time.Now().Add(30 * 24 * time.Hour) |
| 178 | + |
| 179 | + options := struct { |
| 180 | + Description *string `jsonapi:"attr,description"` |
| 181 | + ExpiredAt *time.Time `jsonapi:"attr,expired-at,iso8601"` |
| 182 | + }{ |
| 183 | + Description: String("test-scim-token"), |
| 184 | + ExpiredAt: &expiredAt, |
| 185 | + } |
| 186 | + req, err := client.NewRequest("POST", "admin/scim-tokens", &options) |
| 187 | + require.NoError(t, err) |
| 188 | + |
| 189 | + var res struct { |
| 190 | + Token string `jsonapi:"attr,token"` |
| 191 | + } |
| 192 | + err = req.Do(ctx, &res) |
| 193 | + require.NoError(t, err) |
| 194 | + |
| 195 | + return res.Token |
| 196 | +} |
0 commit comments