|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.cloudstack; |
| 21 | + |
| 22 | +import com.cloud.user.DomainManager; |
| 23 | +import com.cloud.user.User; |
| 24 | +import com.cloud.user.UserVO; |
| 25 | +import com.cloud.user.dao.UserDao; |
| 26 | +import junit.framework.TestCase; |
| 27 | +import org.apache.cloudstack.framework.security.keystore.KeystoreDao; |
| 28 | +import org.apache.cloudstack.saml.SAML2AuthManagerImpl; |
| 29 | +import org.apache.cloudstack.saml.SAMLTokenDao; |
| 30 | +import org.apache.cloudstack.saml.SAMLTokenVO; |
| 31 | +import org.junit.Before; |
| 32 | +import org.junit.Test; |
| 33 | +import org.junit.runner.RunWith; |
| 34 | +import org.mockito.Mock; |
| 35 | +import org.mockito.Mockito; |
| 36 | +import org.mockito.runners.MockitoJUnitRunner; |
| 37 | + |
| 38 | +import java.lang.reflect.Field; |
| 39 | + |
| 40 | +@RunWith(MockitoJUnitRunner.class) |
| 41 | +public class SAML2AuthManagerImplTest extends TestCase { |
| 42 | + @Mock |
| 43 | + private KeystoreDao ksDao; |
| 44 | + |
| 45 | + @Mock |
| 46 | + private SAMLTokenDao samlTokenDao; |
| 47 | + |
| 48 | + @Mock |
| 49 | + private UserDao userDao; |
| 50 | + |
| 51 | + @Mock |
| 52 | + DomainManager domainMgr; |
| 53 | + |
| 54 | + SAML2AuthManagerImpl saml2AuthManager; |
| 55 | + |
| 56 | + @Override |
| 57 | + @Before |
| 58 | + public void setUp() throws NoSuchFieldException, IllegalAccessException { |
| 59 | + saml2AuthManager = Mockito.spy(new SAML2AuthManagerImpl()); |
| 60 | + |
| 61 | + Field ksDaoField = SAML2AuthManagerImpl.class.getDeclaredField("_ksDao"); |
| 62 | + ksDaoField.setAccessible(true); |
| 63 | + ksDaoField.set(saml2AuthManager, ksDao); |
| 64 | + |
| 65 | + Field samlTokenDaoField = SAML2AuthManagerImpl.class.getDeclaredField("_samlTokenDao"); |
| 66 | + samlTokenDaoField.setAccessible(true); |
| 67 | + samlTokenDaoField.set(saml2AuthManager, samlTokenDao); |
| 68 | + |
| 69 | + Field userDaoField = SAML2AuthManagerImpl.class.getDeclaredField("_userDao"); |
| 70 | + userDaoField.setAccessible(true); |
| 71 | + userDaoField.set(saml2AuthManager, userDao); |
| 72 | + |
| 73 | + Field domainMgrField = SAML2AuthManagerImpl.class.getDeclaredField("_domainMgr"); |
| 74 | + domainMgrField.setAccessible(true); |
| 75 | + domainMgrField.set(saml2AuthManager, domainMgr); |
| 76 | + |
| 77 | + // enable the plugin |
| 78 | + Mockito.doReturn(true).when(saml2AuthManager).isSAMLPluginEnabled(); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testIsUserAuthorized() { |
| 83 | + final String entityID = "some IDP ID"; |
| 84 | + |
| 85 | + // Test unauthorized user |
| 86 | + UserVO user = new UserVO(200L); |
| 87 | + user.setUsername("someuser"); |
| 88 | + user.setSource(User.Source.UNKNOWN); |
| 89 | + user.setExternalEntity(entityID); |
| 90 | + Mockito.when(userDao.getUser(Mockito.anyLong())).thenReturn(user); |
| 91 | + assertFalse(saml2AuthManager.isUserAuthorized(user.getId(), "someID")); |
| 92 | + |
| 93 | + // Test authorized user with wrong IDP |
| 94 | + user.setSource(User.Source.SAML2); |
| 95 | + Mockito.when(userDao.getUser(Mockito.anyLong())).thenReturn(user); |
| 96 | + assertFalse(saml2AuthManager.isUserAuthorized(user.getId(), "someID")); |
| 97 | + |
| 98 | + // Test authorized user with wrong IDP |
| 99 | + user.setSource(User.Source.SAML2); |
| 100 | + Mockito.when(userDao.getUser(Mockito.anyLong())).thenReturn(user); |
| 101 | + assertTrue(saml2AuthManager.isUserAuthorized(user.getId(), entityID)); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void testAuthorizeUser() { |
| 106 | + // Test invalid user |
| 107 | + Mockito.when(userDao.getUser(Mockito.anyLong())).thenReturn(null); |
| 108 | + assertFalse(saml2AuthManager.authorizeUser(1L, "someID", true)); |
| 109 | + |
| 110 | + // Test valid user |
| 111 | + UserVO user = new UserVO(200L); |
| 112 | + user.setUsername("someuser"); |
| 113 | + Mockito.when(userDao.getUser(Mockito.anyLong())).thenReturn(user); |
| 114 | + assertTrue(saml2AuthManager.authorizeUser(1L, "someID", true)); |
| 115 | + Mockito.verify(userDao, Mockito.atLeastOnce()).update(Mockito.anyLong(), Mockito.any(user.getClass())); |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + @Test |
| 121 | + public void testSaveToken() { |
| 122 | + // duplicate token test |
| 123 | + Mockito.when(samlTokenDao.findByUuid(Mockito.anyString())).thenReturn(new SAMLTokenVO()); |
| 124 | + saml2AuthManager.saveToken("someAuthnID", null, "https://idp.bhaisaab.org/profile/shibboleth"); |
| 125 | + Mockito.verify(samlTokenDao, Mockito.times(0)).persist(Mockito.any(SAMLTokenVO.class)); |
| 126 | + |
| 127 | + // valid test |
| 128 | + Mockito.when(samlTokenDao.findByUuid(Mockito.anyString())).thenReturn(null); |
| 129 | + saml2AuthManager.saveToken("someAuthnID", null, "https://idp.bhaisaab.org/profile/shibboleth"); |
| 130 | + Mockito.verify(samlTokenDao, Mockito.times(1)).persist(Mockito.any(SAMLTokenVO.class)); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void testGetToken() { |
| 135 | + SAMLTokenVO randomToken = new SAMLTokenVO("uuid", 1L, "someIDPDI"); |
| 136 | + Mockito.when(samlTokenDao.findByUuid(Mockito.anyString())).thenReturn(randomToken); |
| 137 | + assertEquals(saml2AuthManager.getToken("someAuthnID"), randomToken); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void testExpireToken() { |
| 142 | + saml2AuthManager.expireTokens(); |
| 143 | + Mockito.verify(samlTokenDao, Mockito.atLeast(1)).expireTokens(); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void testPluginEnabled() { |
| 148 | + assertTrue(saml2AuthManager.isSAMLPluginEnabled()); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + public void testPluginComponentName() { |
| 153 | + assertEquals(saml2AuthManager.getConfigComponentName(), "SAML2-PLUGIN"); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void testGetCommands() { |
| 158 | + // Plugin enabled |
| 159 | + assertTrue(saml2AuthManager.getCommands().size() > 0); |
| 160 | + assertTrue(saml2AuthManager.getAuthCommands().size() > 0); |
| 161 | + |
| 162 | + // Plugin disabled |
| 163 | + Mockito.doReturn(false).when(saml2AuthManager).isSAMLPluginEnabled(); |
| 164 | + assertTrue(saml2AuthManager.getCommands().size() == 0); |
| 165 | + assertTrue(saml2AuthManager.getAuthCommands().size() == 0); |
| 166 | + // Re-enable the plugin |
| 167 | + Mockito.doReturn(true).when(saml2AuthManager).isSAMLPluginEnabled(); |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + public void testConfigKeys() { |
| 172 | + assertTrue(saml2AuthManager.getConfigKeys().length > 0); |
| 173 | + } |
| 174 | +} |
0 commit comments