|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.firebase.auth; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertFalse; |
| 21 | +import static org.junit.Assert.assertNotNull; |
| 22 | +import static org.junit.Assert.assertNull; |
| 23 | +import static org.junit.Assert.assertTrue; |
| 24 | + |
| 25 | +import com.google.api.client.googleapis.util.Utils; |
| 26 | +import com.google.api.client.json.JsonFactory; |
| 27 | +import com.google.common.collect.ImmutableList; |
| 28 | +import com.google.common.collect.ImmutableMap; |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Map; |
| 32 | +import org.junit.Test; |
| 33 | + |
| 34 | +public class SamlProviderConfigTest { |
| 35 | + |
| 36 | + private static final JsonFactory jsonFactory = Utils.getDefaultJsonFactory(); |
| 37 | + |
| 38 | + private static final String SAML_JSON_STRING = |
| 39 | + ("{" |
| 40 | + + " 'name': 'projects/projectId/inboundSamlConfigs/saml.provider-id'," |
| 41 | + + " 'displayName': 'DISPLAY_NAME'," |
| 42 | + + " 'enabled': true," |
| 43 | + + " 'idpConfig': {" |
| 44 | + + " 'idpEntityId': 'IDP_ENTITY_ID'," |
| 45 | + + " 'ssoUrl': 'https://example.com/login'," |
| 46 | + + " 'idpCertificates': [" |
| 47 | + + " { 'x509Certificate': 'certificate1' }," |
| 48 | + + " { 'x509Certificate': 'certificate2' }" |
| 49 | + + " ]" |
| 50 | + + " }," |
| 51 | + + " 'spConfig': {" |
| 52 | + + " 'spEntityId': 'RP_ENTITY_ID'," |
| 53 | + + " 'callbackUri': 'https://projectId.firebaseapp.com/__/auth/handler'" |
| 54 | + + " }" |
| 55 | + + "}").replace("'", "\""); |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testJsonDeserialization() throws IOException { |
| 59 | + SamlProviderConfig config = jsonFactory.fromString(SAML_JSON_STRING, SamlProviderConfig.class); |
| 60 | + |
| 61 | + assertEquals("saml.provider-id", config.getProviderId()); |
| 62 | + assertEquals("DISPLAY_NAME", config.getDisplayName()); |
| 63 | + assertTrue(config.isEnabled()); |
| 64 | + assertEquals("IDP_ENTITY_ID", config.getIdpEntityId()); |
| 65 | + assertEquals("https://example.com/login", config.getSsoUrl()); |
| 66 | + assertEquals(ImmutableList.of("certificate1", "certificate2"), config.getX509Certificates()); |
| 67 | + assertEquals("RP_ENTITY_ID", config.getRpEntityId()); |
| 68 | + assertEquals("https://projectId.firebaseapp.com/__/auth/handler", config.getCallbackUrl()); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testCreateRequest() throws IOException { |
| 73 | + SamlProviderConfig.CreateRequest createRequest = |
| 74 | + new SamlProviderConfig.CreateRequest() |
| 75 | + .setProviderId("saml.provider-id") |
| 76 | + .setDisplayName("DISPLAY_NAME") |
| 77 | + .setEnabled(false) |
| 78 | + .setIdpEntityId("IDP_ENTITY_ID") |
| 79 | + .setSsoUrl("https://example.com/login") |
| 80 | + .addX509Certificate("certificate1") |
| 81 | + .addX509Certificate("certificate2") |
| 82 | + .setRpEntityId("RP_ENTITY_ID") |
| 83 | + .setCallbackUrl("https://projectId.firebaseapp.com/__/auth/handler"); |
| 84 | + |
| 85 | + assertEquals("saml.provider-id", createRequest.getProviderId()); |
| 86 | + Map<String,Object> properties = createRequest.getProperties(); |
| 87 | + assertEquals(4, properties.size()); |
| 88 | + assertEquals("DISPLAY_NAME", (String) properties.get("displayName")); |
| 89 | + assertFalse((boolean) properties.get("enabled")); |
| 90 | + |
| 91 | + Map<String, Object> idpConfig = (Map<String, Object>) properties.get("idpConfig"); |
| 92 | + assertNotNull(idpConfig); |
| 93 | + assertEquals(3, idpConfig.size()); |
| 94 | + assertEquals("IDP_ENTITY_ID", idpConfig.get("idpEntityId")); |
| 95 | + assertEquals("https://example.com/login", idpConfig.get("ssoUrl")); |
| 96 | + List<Object> idpCertificates = (List<Object>) idpConfig.get("idpCertificates"); |
| 97 | + assertNotNull(idpCertificates); |
| 98 | + assertEquals(2, idpCertificates.size()); |
| 99 | + assertEquals(ImmutableMap.of("x509Certificate", "certificate1"), idpCertificates.get(0)); |
| 100 | + assertEquals(ImmutableMap.of("x509Certificate", "certificate2"), idpCertificates.get(1)); |
| 101 | + |
| 102 | + Map<String, Object> spConfig = (Map<String, Object>) properties.get("spConfig"); |
| 103 | + assertNotNull(spConfig); |
| 104 | + assertEquals(2, spConfig.size()); |
| 105 | + assertEquals("RP_ENTITY_ID", spConfig.get("spEntityId")); |
| 106 | + assertEquals("https://projectId.firebaseapp.com/__/auth/handler", spConfig.get("callbackUri")); |
| 107 | + } |
| 108 | + |
| 109 | + @Test(expected = IllegalArgumentException.class) |
| 110 | + public void testCreateRequestMissingProviderId() { |
| 111 | + new SamlProviderConfig.CreateRequest().setProviderId(null); |
| 112 | + } |
| 113 | + |
| 114 | + @Test(expected = IllegalArgumentException.class) |
| 115 | + public void testCreateRequestInvalidProviderId() { |
| 116 | + new SamlProviderConfig.CreateRequest().setProviderId("oidc.provider-id"); |
| 117 | + } |
| 118 | + |
| 119 | + @Test(expected = IllegalArgumentException.class) |
| 120 | + public void testCreateRequestMissingDisplayName() { |
| 121 | + new SamlProviderConfig.CreateRequest().setDisplayName(null); |
| 122 | + } |
| 123 | + |
| 124 | + @Test(expected = IllegalArgumentException.class) |
| 125 | + public void testCreateRequestMissingIdpEntityId() { |
| 126 | + new SamlProviderConfig.CreateRequest().setIdpEntityId(null); |
| 127 | + } |
| 128 | + |
| 129 | + @Test(expected = IllegalArgumentException.class) |
| 130 | + public void testCreateRequestMissingSsoUrl() { |
| 131 | + new SamlProviderConfig.CreateRequest().setSsoUrl(null); |
| 132 | + } |
| 133 | + |
| 134 | + @Test(expected = IllegalArgumentException.class) |
| 135 | + public void testCreateRequestInvalidSsoUrl() { |
| 136 | + new SamlProviderConfig.CreateRequest().setSsoUrl("not a valid url"); |
| 137 | + } |
| 138 | + |
| 139 | + @Test(expected = IllegalArgumentException.class) |
| 140 | + public void testCreateRequestMissingX509Certificate() { |
| 141 | + new SamlProviderConfig.CreateRequest().addX509Certificate(null); |
| 142 | + } |
| 143 | + |
| 144 | + @Test(expected = IllegalArgumentException.class) |
| 145 | + public void testCreateRequestMissingRpEntityId() { |
| 146 | + new SamlProviderConfig.CreateRequest().setRpEntityId(null); |
| 147 | + } |
| 148 | + |
| 149 | + @Test(expected = IllegalArgumentException.class) |
| 150 | + public void testCreateRequestMissingCallbackUrl() { |
| 151 | + new SamlProviderConfig.CreateRequest().setCallbackUrl(null); |
| 152 | + } |
| 153 | + |
| 154 | + @Test(expected = IllegalArgumentException.class) |
| 155 | + public void testCreateRequestInvalidCallbackUrl() { |
| 156 | + new SamlProviderConfig.CreateRequest().setCallbackUrl("not a valid url"); |
| 157 | + } |
| 158 | +} |
0 commit comments