|
| 1 | +package federatedsettingsidentityprovider_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion" |
| 8 | + "github.com/mongodb/terraform-provider-mongodbatlas/internal/service/federatedsettingsidentityprovider" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "go.mongodb.org/atlas-sdk/v20231115004/admin" |
| 11 | +) |
| 12 | + |
| 13 | +var ( |
| 14 | + groupID = "group_id" |
| 15 | + organizationID = "organization_id" |
| 16 | + role = "role" |
| 17 | + emailAddress = "emailAddress" |
| 18 | + federationSettingsID = "federationSettingsId" |
| 19 | + firstName = "firstName" |
| 20 | + lastName = "lastName" |
| 21 | + userID = "userId" |
| 22 | + roleAssignmentsID = "roleAssignmentsID" |
| 23 | + externalGroupName = "externalGroupName" |
| 24 | + notBefore = time.Now() |
| 25 | + notAfter = notBefore.Add(time.Hour * time.Duration(1)) |
| 26 | + fileName = "fileName" |
| 27 | + domainAllowList = []string{"domainAllowList"} |
| 28 | + domainRestrictionEnabled = true |
| 29 | + identityProviderID = "identityProviderId" |
| 30 | + postAuthRoleGrants = []string{"postAuthRoleGrants"} |
| 31 | + acsURL = "acsURL" |
| 32 | + associatedDomains = []string{"associatedDomains"} |
| 33 | + audienceURI = "audienceURI" |
| 34 | + displayName = "displayName" |
| 35 | + issuerURI = "issuerURI" |
| 36 | + oktaIdpID = "oktaIdpID" |
| 37 | + requestBinding = "requestBinding" |
| 38 | + responseSignatureAlgorithm = "responseSignatureAlgorithm" |
| 39 | + ssoDebugEnabled = true |
| 40 | + ssoURL = "ssoUrl" |
| 41 | + status = "ACTIVE" |
| 42 | + protocol = "SAML" |
| 43 | + |
| 44 | + roleAssignments = []admin.RoleAssignment{ |
| 45 | + { |
| 46 | + GroupId: &groupID, |
| 47 | + OrgId: &organizationID, |
| 48 | + Role: &role, |
| 49 | + }, |
| 50 | + } |
| 51 | + flattenedRoleAssignments = []map[string]any{ |
| 52 | + { |
| 53 | + "group_id": &groupID, |
| 54 | + "org_id": &organizationID, |
| 55 | + "role": &role, |
| 56 | + }, |
| 57 | + } |
| 58 | + pemCertificates = []admin.X509Certificate{ |
| 59 | + { |
| 60 | + NotAfter: ¬After, |
| 61 | + NotBefore: ¬Before, |
| 62 | + }, |
| 63 | + } |
| 64 | + flattenedPemCertificates = []map[string]any{ |
| 65 | + { |
| 66 | + "not_after": conversion.TimePtrToStringPtr(¬After), |
| 67 | + "not_before": conversion.TimePtrToStringPtr(¬Before), |
| 68 | + }, |
| 69 | + } |
| 70 | + federationRoleMapping = []admin.AuthFederationRoleMapping{ |
| 71 | + { |
| 72 | + ExternalGroupName: externalGroupName, |
| 73 | + Id: &roleAssignmentsID, |
| 74 | + RoleAssignments: &roleAssignments, |
| 75 | + }, |
| 76 | + } |
| 77 | + flattenedFederationRoleMapping = []map[string]any{ |
| 78 | + { |
| 79 | + "external_group_name": externalGroupName, |
| 80 | + "id": &roleAssignmentsID, |
| 81 | + "role_assignments": flattenedRoleAssignments, |
| 82 | + }, |
| 83 | + } |
| 84 | + federatedUser = []admin.FederatedUser{ |
| 85 | + { |
| 86 | + EmailAddress: emailAddress, |
| 87 | + FederationSettingsId: federationSettingsID, |
| 88 | + FirstName: firstName, |
| 89 | + LastName: lastName, |
| 90 | + UserId: &userID, |
| 91 | + }, |
| 92 | + } |
| 93 | + flattenedFederatedUser = []map[string]any{ |
| 94 | + { |
| 95 | + "email_address": emailAddress, |
| 96 | + "federation_settings_id": federationSettingsID, |
| 97 | + "first_name": firstName, |
| 98 | + "last_name": lastName, |
| 99 | + "user_id": &userID, |
| 100 | + }, |
| 101 | + } |
| 102 | + associatedOrgs = []admin.ConnectedOrgConfig{ |
| 103 | + { |
| 104 | + DomainAllowList: &domainAllowList, |
| 105 | + DomainRestrictionEnabled: domainRestrictionEnabled, |
| 106 | + IdentityProviderId: identityProviderID, |
| 107 | + OrgId: organizationID, |
| 108 | + PostAuthRoleGrants: &postAuthRoleGrants, |
| 109 | + RoleMappings: &federationRoleMapping, |
| 110 | + UserConflicts: nil, |
| 111 | + }, |
| 112 | + } |
| 113 | + flattenedAssociatedOrgs = []map[string]any{ |
| 114 | + { |
| 115 | + "domain_allow_list": &domainAllowList, |
| 116 | + "domain_restriction_enabled": domainRestrictionEnabled, |
| 117 | + "identity_provider_id": identityProviderID, |
| 118 | + "org_id": organizationID, |
| 119 | + "post_auth_role_grants": &postAuthRoleGrants, |
| 120 | + "role_mappings": flattenedFederationRoleMapping, |
| 121 | + "user_conflicts": nil, |
| 122 | + }, |
| 123 | + } |
| 124 | + pemFileInfo = admin.PemFileInfo{ |
| 125 | + FileName: &fileName, |
| 126 | + Certificates: &pemCertificates, |
| 127 | + } |
| 128 | + flattenedPemFileInfo = []map[string]any{ |
| 129 | + { |
| 130 | + "certificates": flattenedPemCertificates, |
| 131 | + "file_name": &fileName, |
| 132 | + }, |
| 133 | + } |
| 134 | +) |
| 135 | + |
| 136 | +func TestFlattenRoleAssignments(t *testing.T) { |
| 137 | + testCases := []struct { |
| 138 | + name string |
| 139 | + input []admin.RoleAssignment |
| 140 | + output []map[string]any |
| 141 | + }{ |
| 142 | + { |
| 143 | + name: "Non empty FlattenRoleAssignments", |
| 144 | + input: roleAssignments, |
| 145 | + output: flattenedRoleAssignments, |
| 146 | + }, |
| 147 | + { |
| 148 | + name: "Empty FlattenRoleAssignments", |
| 149 | + input: []admin.RoleAssignment{}, |
| 150 | + output: nil, |
| 151 | + }, |
| 152 | + } |
| 153 | + for _, tc := range testCases { |
| 154 | + t.Run(tc.name, func(t *testing.T) { |
| 155 | + resultModel := federatedsettingsidentityprovider.FlattenRoleAssignments(tc.input) |
| 156 | + assert.Equal(t, tc.output, resultModel) |
| 157 | + }) |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +func TestFlattenFederatedUser(t *testing.T) { |
| 162 | + testCases := []struct { |
| 163 | + name string |
| 164 | + input []admin.FederatedUser |
| 165 | + output []map[string]any |
| 166 | + }{ |
| 167 | + { |
| 168 | + name: "Non empty FlattenFederatedUser", |
| 169 | + input: federatedUser, |
| 170 | + output: flattenedFederatedUser, |
| 171 | + }, |
| 172 | + { |
| 173 | + name: "Empty FlattenFederatedUser", |
| 174 | + input: []admin.FederatedUser{}, |
| 175 | + output: nil, |
| 176 | + }, |
| 177 | + } |
| 178 | + for _, tc := range testCases { |
| 179 | + t.Run(tc.name, func(t *testing.T) { |
| 180 | + resultModel := federatedsettingsidentityprovider.FlattenFederatedUser(tc.input) |
| 181 | + assert.Equal(t, tc.output, resultModel) |
| 182 | + }) |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +func TestFlattenAuthFederationRoleMapping(t *testing.T) { |
| 187 | + testCases := []struct { |
| 188 | + name string |
| 189 | + input []admin.AuthFederationRoleMapping |
| 190 | + output []map[string]any |
| 191 | + }{ |
| 192 | + { |
| 193 | + name: "Non empty FlattenAuthFederationRoleMapping", |
| 194 | + input: federationRoleMapping, |
| 195 | + output: flattenedFederationRoleMapping, |
| 196 | + }, |
| 197 | + { |
| 198 | + name: "Empty FlattenAuthFederationRoleMapping", |
| 199 | + input: []admin.AuthFederationRoleMapping{}, |
| 200 | + output: nil, |
| 201 | + }, |
| 202 | + } |
| 203 | + for _, tc := range testCases { |
| 204 | + t.Run(tc.name, func(t *testing.T) { |
| 205 | + resultModel := federatedsettingsidentityprovider.FlattenAuthFederationRoleMapping(tc.input) |
| 206 | + assert.Equal(t, tc.output, resultModel) |
| 207 | + }) |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +func TestFlattenFederatedSettingsCertificates(t *testing.T) { |
| 212 | + testCases := []struct { |
| 213 | + name string |
| 214 | + input []admin.X509Certificate |
| 215 | + output []map[string]any |
| 216 | + }{ |
| 217 | + { |
| 218 | + name: "Non empty FlattenFederatedSettingsCertificates", |
| 219 | + input: pemCertificates, |
| 220 | + output: flattenedPemCertificates, |
| 221 | + }, |
| 222 | + { |
| 223 | + name: "Empty FlattenFederatedSettingsCertificates", |
| 224 | + input: []admin.X509Certificate{}, |
| 225 | + output: nil, |
| 226 | + }, |
| 227 | + } |
| 228 | + for _, tc := range testCases { |
| 229 | + t.Run(tc.name, func(t *testing.T) { |
| 230 | + resultModel := federatedsettingsidentityprovider.FlattenFederatedSettingsCertificates(tc.input) |
| 231 | + assert.Equal(t, tc.output, resultModel) |
| 232 | + }) |
| 233 | + } |
| 234 | +} |
| 235 | + |
| 236 | +func TestFlattenPemFileInfo(t *testing.T) { |
| 237 | + testCases := []struct { |
| 238 | + name string |
| 239 | + input admin.PemFileInfo |
| 240 | + output []map[string]any |
| 241 | + }{ |
| 242 | + { |
| 243 | + name: "Non empty FlattenPemFileInfo", |
| 244 | + input: pemFileInfo, |
| 245 | + output: flattenedPemFileInfo, |
| 246 | + }, |
| 247 | + { |
| 248 | + name: "Empty FlattenPemFileInfo", |
| 249 | + input: admin.PemFileInfo{}, |
| 250 | + output: nil, |
| 251 | + }, |
| 252 | + } |
| 253 | + for _, tc := range testCases { |
| 254 | + t.Run(tc.name, func(t *testing.T) { |
| 255 | + resultModel := federatedsettingsidentityprovider.FlattenPemFileInfo(tc.input) |
| 256 | + assert.Equal(t, tc.output, resultModel) |
| 257 | + }) |
| 258 | + } |
| 259 | +} |
| 260 | + |
| 261 | +func TestFlattenAssociatedOrgs(t *testing.T) { |
| 262 | + testCases := []struct { |
| 263 | + name string |
| 264 | + input []admin.ConnectedOrgConfig |
| 265 | + output []map[string]any |
| 266 | + }{ |
| 267 | + { |
| 268 | + name: "Non empty FlattenAssociatedOrgs without UserConflics", |
| 269 | + input: associatedOrgs, |
| 270 | + output: flattenedAssociatedOrgs, |
| 271 | + }, |
| 272 | + { |
| 273 | + name: "Non empty FlattenAssociatedOrgs with UserConflics", |
| 274 | + input: []admin.ConnectedOrgConfig{ |
| 275 | + { |
| 276 | + DomainAllowList: &domainAllowList, |
| 277 | + DomainRestrictionEnabled: domainRestrictionEnabled, |
| 278 | + IdentityProviderId: identityProviderID, |
| 279 | + OrgId: organizationID, |
| 280 | + PostAuthRoleGrants: &postAuthRoleGrants, |
| 281 | + RoleMappings: &federationRoleMapping, |
| 282 | + UserConflicts: &federatedUser, |
| 283 | + }, |
| 284 | + }, |
| 285 | + output: []map[string]any{ |
| 286 | + { |
| 287 | + "domain_allow_list": &domainAllowList, |
| 288 | + "domain_restriction_enabled": domainRestrictionEnabled, |
| 289 | + "identity_provider_id": identityProviderID, |
| 290 | + "org_id": organizationID, |
| 291 | + "post_auth_role_grants": &postAuthRoleGrants, |
| 292 | + "role_mappings": flattenedFederationRoleMapping, |
| 293 | + "user_conflicts": flattenedFederatedUser, |
| 294 | + }, |
| 295 | + }, |
| 296 | + }, |
| 297 | + { |
| 298 | + name: "Empty FlattenAssociatedOrgs", |
| 299 | + input: []admin.ConnectedOrgConfig{}, |
| 300 | + output: nil, |
| 301 | + }, |
| 302 | + } |
| 303 | + for _, tc := range testCases { |
| 304 | + t.Run(tc.name, func(t *testing.T) { |
| 305 | + resultModel := federatedsettingsidentityprovider.FlattenAssociatedOrgs(tc.input) |
| 306 | + assert.Equal(t, tc.output, resultModel) |
| 307 | + }) |
| 308 | + } |
| 309 | +} |
| 310 | + |
| 311 | +func TestFlattenFederatedSettingsIdentityProvider(t *testing.T) { |
| 312 | + testCases := []struct { |
| 313 | + name string |
| 314 | + input []admin.FederationIdentityProvider |
| 315 | + output []map[string]any |
| 316 | + }{ |
| 317 | + { |
| 318 | + name: "Non empty FlattenFederatedSettingsIdentityProvider", |
| 319 | + input: []admin.FederationIdentityProvider{ |
| 320 | + { |
| 321 | + AcsUrl: &acsURL, |
| 322 | + AssociatedDomains: &associatedDomains, |
| 323 | + AssociatedOrgs: &associatedOrgs, |
| 324 | + AudienceUri: &audienceURI, |
| 325 | + DisplayName: &displayName, |
| 326 | + IssuerUri: &issuerURI, |
| 327 | + OktaIdpId: oktaIdpID, |
| 328 | + PemFileInfo: &pemFileInfo, |
| 329 | + RequestBinding: &requestBinding, |
| 330 | + ResponseSignatureAlgorithm: &responseSignatureAlgorithm, |
| 331 | + SsoDebugEnabled: &ssoDebugEnabled, |
| 332 | + SsoUrl: &ssoURL, |
| 333 | + Status: &status, |
| 334 | + Id: identityProviderID, |
| 335 | + Protocol: &protocol, |
| 336 | + }, |
| 337 | + }, |
| 338 | + output: []map[string]any{ |
| 339 | + { |
| 340 | + "acs_url": &acsURL, |
| 341 | + "associated_domains": &associatedDomains, |
| 342 | + "associated_orgs": flattenedAssociatedOrgs, |
| 343 | + "audience_uri": &audienceURI, |
| 344 | + "display_name": &displayName, |
| 345 | + "issuer_uri": &issuerURI, |
| 346 | + "okta_idp_id": oktaIdpID, |
| 347 | + "pem_file_info": flattenedPemFileInfo, |
| 348 | + "request_binding": &requestBinding, |
| 349 | + "response_signature_algorithm": &responseSignatureAlgorithm, |
| 350 | + "sso_debug_enabled": &ssoDebugEnabled, |
| 351 | + "sso_url": &ssoURL, |
| 352 | + "status": &status, |
| 353 | + "idp_id": identityProviderID, |
| 354 | + "protocol": &protocol, |
| 355 | + }, |
| 356 | + }, |
| 357 | + }, |
| 358 | + { |
| 359 | + name: "Empty FlattenFederatedSettingsIdentityProvider", |
| 360 | + input: []admin.FederationIdentityProvider{}, |
| 361 | + output: nil, |
| 362 | + }, |
| 363 | + } |
| 364 | + for _, tc := range testCases { |
| 365 | + t.Run(tc.name, func(t *testing.T) { |
| 366 | + resultModel := federatedsettingsidentityprovider.FlattenFederatedSettingsIdentityProvider(tc.input) |
| 367 | + assert.Equal(t, tc.output, resultModel) |
| 368 | + }) |
| 369 | + } |
| 370 | +} |
0 commit comments