2020package org .apache .cloudstack ;
2121
2222import java .lang .reflect .Field ;
23+ import java .lang .reflect .Method ;
2324
2425import org .apache .cloudstack .framework .security .keystore .KeystoreDao ;
26+ import org .apache .cloudstack .resourcedetail .UserDetailVO ;
27+ import org .apache .cloudstack .resourcedetail .dao .UserDetailsDao ;
2528import org .apache .cloudstack .saml .SAML2AuthManagerImpl ;
2629import org .apache .cloudstack .saml .SAMLTokenDao ;
2730import org .apache .cloudstack .saml .SAMLTokenVO ;
@@ -50,6 +53,9 @@ public class SAML2AuthManagerImplTest extends TestCase {
5053 @ Mock
5154 private UserDao userDao ;
5255
56+ @ Mock
57+ private UserDetailsDao userDetailsDao ;
58+
5359 @ Mock
5460 DomainManager domainMgr ;
5561
@@ -72,6 +78,10 @@ public void setUp() throws NoSuchFieldException, IllegalAccessException {
7278 userDaoField .setAccessible (true );
7379 userDaoField .set (saml2AuthManager , userDao );
7480
81+ Field userDetailsDaoField = SAML2AuthManagerImpl .class .getDeclaredField ("userDetailsDao" );
82+ userDetailsDaoField .setAccessible (true );
83+ userDetailsDaoField .set (saml2AuthManager , userDetailsDao );
84+
7585 Field domainMgrField = SAML2AuthManagerImpl .class .getDeclaredField ("_domainMgr" );
7686 domainMgrField .setAccessible (true );
7787 domainMgrField .set (saml2AuthManager , domainMgr );
@@ -117,7 +127,57 @@ public void testAuthorizeUser() {
117127 Mockito .verify (userDao , Mockito .atLeastOnce ()).update (Mockito .anyLong (), Mockito .any (user .getClass ()));
118128 }
119129
130+ @ Test
131+ public void testAuthorizeUserStoresPreSamlSourceOnEnable () {
132+ UserVO user = new UserVO (200L );
133+ user .setUsername ("someuser" );
134+ user .setSource (User .Source .LDAP );
135+ Mockito .when (userDao .getUser (Mockito .anyLong ())).thenReturn (user );
136+
137+ saml2AuthManager .authorizeUser (200L , "someID" , true );
138+
139+ Mockito .verify (userDetailsDao ).addDetail (200L , "PreSamlSource" , "LDAP" , false );
140+ assertEquals (User .Source .SAML2 , user .getSource ());
141+ }
142+
143+ @ Test
144+ public void testAuthorizeUserDoesNotRestorePreSamlSourceWhenAlreadyAuthorized () {
145+ UserVO user = new UserVO (200L );
146+ user .setUsername ("someuser" );
147+ user .setSource (User .Source .SAML2 );
148+ Mockito .when (userDao .getUser (Mockito .anyLong ())).thenReturn (user );
149+
150+ saml2AuthManager .authorizeUser (200L , "someID" , true );
151+
152+ Mockito .verify (userDetailsDao , Mockito .never ()).addDetail (Mockito .anyLong (), Mockito .anyString (), Mockito .anyString (), Mockito .anyBoolean ());
153+ }
154+
155+ @ Test
156+ public void testGetPreSamlSourceRestoresStoredSource () throws Exception {
157+ Mockito .when (userDetailsDao .findDetail (200L , "PreSamlSource" )).thenReturn (new UserDetailVO (200L , "PreSamlSource" , "LDAP" ));
158+
159+ assertEquals (User .Source .LDAP , invokeGetPreSamlSource (200L ));
160+ }
161+
162+ @ Test
163+ public void testGetPreSamlSourceDefaultsToUnknownWhenNothingStored () throws Exception {
164+ Mockito .when (userDetailsDao .findDetail (200L , "PreSamlSource" )).thenReturn (null );
165+
166+ assertEquals (User .Source .UNKNOWN , invokeGetPreSamlSource (200L ));
167+ }
168+
169+ @ Test
170+ public void testGetPreSamlSourceDefaultsToUnknownOnGarbageValue () throws Exception {
171+ Mockito .when (userDetailsDao .findDetail (200L , "PreSamlSource" )).thenReturn (new UserDetailVO (200L , "PreSamlSource" , "not-a-real-source" ));
172+
173+ assertEquals (User .Source .UNKNOWN , invokeGetPreSamlSource (200L ));
174+ }
120175
176+ private User .Source invokeGetPreSamlSource (long userId ) throws Exception {
177+ Method method = SAML2AuthManagerImpl .class .getDeclaredMethod ("getPreSamlSource" , long .class );
178+ method .setAccessible (true );
179+ return (User .Source ) method .invoke (saml2AuthManager , userId );
180+ }
121181
122182 @ Test
123183 public void testSaveToken () {
0 commit comments