|
| 1 | +/** |
| 2 | + * ContainerProxy |
| 3 | + * |
| 4 | + * Copyright (C) 2016-2021 Open Analytics |
| 5 | + * |
| 6 | + * =========================================================================== |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the Apache License as published by |
| 10 | + * The Apache Software Foundation, either version 2 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * Apache License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the Apache License |
| 19 | + * along with this program. If not, see <http://www.apache.org/licenses/> |
| 20 | + */ |
| 21 | +package eu.openanalytics.containerproxy.test.unit; |
| 22 | + |
| 23 | +import eu.openanalytics.containerproxy.ContainerProxyApplication; |
| 24 | +import eu.openanalytics.containerproxy.auth.impl.OpenIDAuthenticationBackend; |
| 25 | +import eu.openanalytics.containerproxy.test.proxy.TestProxyService; |
| 26 | +import net.minidev.json.JSONArray; |
| 27 | +import org.apache.logging.log4j.LogManager; |
| 28 | +import org.apache.logging.log4j.Logger; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | +import org.junit.runner.RunWith; |
| 31 | +import org.springframework.boot.test.context.SpringBootTest; |
| 32 | +import org.springframework.test.context.ActiveProfiles; |
| 33 | +import org.springframework.test.context.junit4.SpringRunner; |
| 34 | + |
| 35 | +import java.util.ArrayList; |
| 36 | +import java.util.Arrays; |
| 37 | +import java.util.List; |
| 38 | + |
| 39 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 40 | + |
| 41 | + |
| 42 | +@SpringBootTest(classes= {TestProxyService.TestConfiguration.class, ContainerProxyApplication.class}) |
| 43 | +@RunWith(SpringRunner.class) |
| 44 | +@ActiveProfiles("test") |
| 45 | +public class TestOpenIdParseClaimRoles { |
| 46 | + |
| 47 | + private final Logger logger = LogManager.getLogger(OpenIDAuthenticationBackend.class); |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testParseProperJsonArray() { |
| 51 | + JSONArray claimValue = new JSONArray(); |
| 52 | + claimValue.add("operators"); |
| 53 | + claimValue.add("default-roles-master"); |
| 54 | + claimValue.add("uma_authorization"); |
| 55 | + claimValue.add("offline_access"); |
| 56 | + |
| 57 | + List<String> result = OpenIDAuthenticationBackend.parseRolesClaim(logger, "realm_access_roles", claimValue); |
| 58 | + |
| 59 | + assertEquals(Arrays.asList("operators","default-roles-master","uma_authorization","offline_access"), result); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testParseList() { |
| 64 | + List<String> claimValue = new ArrayList<>(); |
| 65 | + claimValue.add("operators"); |
| 66 | + claimValue.add("default-roles-master"); |
| 67 | + claimValue.add("uma_authorization"); |
| 68 | + claimValue.add("offline_access"); |
| 69 | + |
| 70 | + List<String> result = OpenIDAuthenticationBackend.parseRolesClaim(logger, "realm_access_roles", claimValue); |
| 71 | + |
| 72 | + assertEquals(Arrays.asList("operators","default-roles-master","uma_authorization","offline_access"), result); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testParseProperJsonArrayAsString() { |
| 77 | + String claimValue = "[\"operators\",\"default-roles-master\",\"uma_authorization\",\"offline_access\"]"; |
| 78 | + |
| 79 | + List<String> result = OpenIDAuthenticationBackend.parseRolesClaim(logger, "realm_access_roles", claimValue); |
| 80 | + |
| 81 | + assertEquals(Arrays.asList("operators","default-roles-master","uma_authorization","offline_access"), result); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testParseNonStandardJsonArrayAsString() { |
| 86 | + String claimValue = "[operators,default-roles-master,uma_authorization,offline_access]"; |
| 87 | + |
| 88 | + List<String> result = OpenIDAuthenticationBackend.parseRolesClaim(logger, "realm_access_roles", claimValue); |
| 89 | + |
| 90 | + assertEquals(Arrays.asList("operators","default-roles-master","uma_authorization","offline_access"), result); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void testParseNonStandardJsonArrayAsString2() { |
| 95 | + String claimValue = "[operators, default-roles-master,uma_authorization, offline_access ]"; |
| 96 | + |
| 97 | + List<String> result = OpenIDAuthenticationBackend.parseRolesClaim(logger, "realm_access_roles", claimValue); |
| 98 | + |
| 99 | + assertEquals(Arrays.asList("operators","default-roles-master","uma_authorization","offline_access"), result); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testParseNonStandardJsonArrayAsString3() { |
| 104 | + String claimValue = "[operators, default-roles-master,uma_authorization, \"offline_access\"]"; |
| 105 | + |
| 106 | + List<String> result = OpenIDAuthenticationBackend.parseRolesClaim(logger, "realm_access_roles", claimValue); |
| 107 | + |
| 108 | + assertEquals(Arrays.asList("operators","default-roles-master","uma_authorization","offline_access"), result); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testParseNonStandardJsonArrayAsString4() { |
| 113 | + String claimValue = "[operators, default-roles-master,uma_authorization, 'offline_access']"; |
| 114 | + |
| 115 | + List<String> result = OpenIDAuthenticationBackend.parseRolesClaim(logger, "realm_access_roles", claimValue); |
| 116 | + |
| 117 | + assertEquals(Arrays.asList("operators","default-roles-master","uma_authorization","offline_access"), result); |
| 118 | + } |
| 119 | + |
| 120 | +} |
0 commit comments