1818
1919import org .apache .cloudstack .acl .ControlledEntity ;
2020import org .apache .cloudstack .acl .SecurityChecker ;
21+ import org .apache .cloudstack .backup .BackupOfferingVO ;
22+ import org .apache .cloudstack .backup .dao .BackupOfferingDetailsDao ;
23+ import org .junit .Assert ;
2124import org .junit .Test ;
2225import org .junit .runner .RunWith ;
2326import org .mockito .InjectMocks ;
3538import com .cloud .user .dao .AccountDao ;
3639import com .cloud .utils .Ternary ;
3740
41+ import java .util .Collections ;
42+
3843@ RunWith (MockitoJUnitRunner .class )
3944public class DomainCheckerTest {
4045
@@ -46,6 +51,8 @@ public class DomainCheckerTest {
4651 DomainDao _domainDao ;
4752 @ Mock
4853 ProjectManager _projectMgr ;
54+ @ Mock
55+ BackupOfferingDetailsDao backupOfferingDetailsDao ;
4956
5057 @ Spy
5158 @ InjectMocks
@@ -163,4 +170,44 @@ public void testProjectOwnerCannotAccess() {
163170 domainChecker .validateCallerHasAccessToEntityOwner (caller , entity , SecurityChecker .AccessType .ListEntry );
164171 }
165172
173+ @ Test
174+ public void testBackupOfferingAccessRootAdmin () {
175+ Account rootAdmin = Mockito .mock (Account .class );
176+ Mockito .when (rootAdmin .getId ()).thenReturn (1L );
177+ BackupOfferingVO backupOfferingVO = Mockito .mock (BackupOfferingVO .class );
178+ Mockito .when (_accountService .isRootAdmin (rootAdmin .getId ())).thenReturn (true );
179+
180+ boolean hasAccess = domainChecker .checkAccess (rootAdmin , backupOfferingVO );
181+ Assert .assertTrue (hasAccess );
182+ }
183+
184+ @ Test
185+ public void testBackupOfferingAccessDomainAdmin () {
186+ Account domainAdmin = Mockito .mock (Account .class );
187+ Mockito .when (domainAdmin .getId ()).thenReturn (2L );
188+ BackupOfferingVO backupOfferingVO = Mockito .mock (BackupOfferingVO .class );
189+ AccountVO owner = Mockito .mock (AccountVO .class );
190+ Mockito .when (_accountService .isDomainAdmin (domainAdmin .getId ())).thenReturn (true );
191+ Mockito .when (domainAdmin .getDomainId ()).thenReturn (10L );
192+ Mockito .when (owner .getDomainId ()).thenReturn (101L );
193+ Mockito .when (_domainDao .isChildDomain (100L , 10L )).thenReturn (true );
194+ Mockito .when (backupOfferingDetailsDao .findDomainIds (backupOfferingVO .getId ())).thenReturn (Collections .singletonList (100L ));
195+
196+ boolean hasAccess = domainChecker .checkAccess (domainAdmin , backupOfferingVO );
197+ Assert .assertTrue (hasAccess );
198+ }
199+
200+ @ Test
201+ public void testBackupOfferingAccessNoAccess () {
202+ Account normalUser = Mockito .mock (Account .class );
203+ Mockito .when (normalUser .getId ()).thenReturn (3L );
204+ BackupOfferingVO backupOfferingVO = Mockito .mock (BackupOfferingVO .class );
205+ Mockito .when (_accountService .isRootAdmin (normalUser .getId ())).thenReturn (false );
206+ Mockito .when (_accountService .isDomainAdmin (normalUser .getId ())).thenReturn (false );
207+ Mockito .when (backupOfferingDetailsDao .findDomainIds (backupOfferingVO .getId ())).thenReturn (Collections .singletonList (100L ));
208+
209+ boolean hasAccess = domainChecker .checkAccess (normalUser , backupOfferingVO );
210+ Assert .assertFalse (hasAccess );
211+ }
212+
166213}
0 commit comments