Commit bc3a0e88d9422a1ecdb038f76ef9a1afa7356444
1 parent
f64eb557
Exists in
master
Modificações para adequação dos testes unitários da classe
SecurityContextImpl.
Showing
1 changed file
with
361 additions
and
356 deletions
Show diff stats
impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/implementation/SecurityContextImplTest.java
| 1 | -//package br.gov.frameworkdemoiselle.internal.implementation; | |
| 2 | -// | |
| 3 | -//import static junit.framework.Assert.assertEquals; | |
| 4 | -//import static junit.framework.Assert.assertFalse; | |
| 5 | -//import static junit.framework.Assert.assertNotNull; | |
| 6 | -//import static junit.framework.Assert.assertNull; | |
| 7 | -//import static junit.framework.Assert.assertTrue; | |
| 8 | -//import static junit.framework.Assert.fail; | |
| 9 | -//import static org.easymock.EasyMock.createMock; | |
| 10 | -//import static org.easymock.EasyMock.expect; | |
| 11 | -//import static org.powermock.api.easymock.PowerMock.mockStatic; | |
| 12 | -//import static org.powermock.api.easymock.PowerMock.replay; | |
| 13 | -//import static org.powermock.api.easymock.PowerMock.replayAll; | |
| 14 | -//import static org.powermock.reflect.Whitebox.setInternalState; | |
| 15 | -// | |
| 16 | -//import java.util.Locale; | |
| 17 | -// | |
| 18 | -//import javax.enterprise.inject.spi.BeanManager; | |
| 19 | -// | |
| 20 | -//import org.easymock.EasyMock; | |
| 21 | -//import org.junit.Before; | |
| 22 | -//import org.junit.Test; | |
| 23 | -//import org.junit.runner.RunWith; | |
| 24 | -//import org.powermock.api.easymock.PowerMock; | |
| 25 | -//import org.powermock.core.classloader.annotations.PrepareForTest; | |
| 26 | -//import org.powermock.modules.junit4.PowerMockRunner; | |
| 27 | -// | |
| 28 | -//import br.gov.frameworkdemoiselle.internal.configuration.SecurityConfig; | |
| 29 | -//import br.gov.frameworkdemoiselle.security.Authenticator; | |
| 30 | -//import br.gov.frameworkdemoiselle.security.Authorizer; | |
| 31 | -//import br.gov.frameworkdemoiselle.security.NotLoggedInException; | |
| 32 | -//import br.gov.frameworkdemoiselle.security.User; | |
| 33 | -//import br.gov.frameworkdemoiselle.util.Beans; | |
| 34 | -//import br.gov.frameworkdemoiselle.util.ResourceBundle; | |
| 35 | -// | |
| 36 | -//@RunWith(PowerMockRunner.class) | |
| 37 | -//@PrepareForTest({ CoreBundle.class, Beans.class }) | |
| 38 | -//public class SecurityContextImplTest { | |
| 39 | -// | |
| 40 | -// private SecurityContextImpl context; | |
| 41 | -// | |
| 42 | -// private SecurityConfig config; | |
| 43 | -// | |
| 44 | -// @Before | |
| 45 | -// public void setUp() { | |
| 46 | -// context = new SecurityContextImpl(); | |
| 47 | -// | |
| 48 | -// config = createMock(SecurityConfig.class); | |
| 49 | -// setInternalState(context, "config", config); | |
| 50 | -// } | |
| 51 | -// | |
| 52 | -// @Test | |
| 53 | -// public void testHasPermissionWithSecurityDisabled() { | |
| 54 | -// | |
| 55 | -// expect(config.isEnabled()).andReturn(false); | |
| 56 | -// | |
| 57 | -// replay(config); | |
| 58 | -// | |
| 59 | -// try { | |
| 60 | -// assertTrue(context.hasPermission(null, null)); | |
| 61 | -// } catch (NotLoggedInException e) { | |
| 62 | -// fail(); | |
| 63 | -// } | |
| 64 | -// | |
| 65 | -// } | |
| 66 | -// | |
| 67 | -// @Test | |
| 68 | -// public void testHasPermissionWithSecurityEnabledAndNotLoggedIn() { | |
| 69 | -// Authenticator authenticator = createMock(Authenticator.class); | |
| 70 | -// expect(authenticator.getUser()).andReturn(null).anyTimes(); | |
| 71 | -// | |
| 72 | -// ResourceBundle bundle = new ResourceBundle("demoiselle-core-bundle", Locale.getDefault()); | |
| 73 | -// setInternalState(context, "bundle", bundle); | |
| 74 | -// setInternalState(context, "authenticator", authenticator); | |
| 75 | -// | |
| 76 | -// expect(config.isEnabled()).andReturn(true).anyTimes(); | |
| 77 | -// replay(config, authenticator); | |
| 78 | -// | |
| 79 | -// try { | |
| 80 | -// context.hasPermission(null, null); | |
| 81 | -// fail(); | |
| 82 | -// } catch (NotLoggedInException e) { | |
| 83 | -// assertTrue(e.getMessage().equals(bundle.getString("user-not-authenticated"))); | |
| 84 | -// } | |
| 85 | -// | |
| 86 | -// } | |
| 87 | -// | |
| 88 | -// @Test | |
| 89 | -// public void testHasPermissionWithSecurityEnabledAndLoggedIn() { | |
| 90 | -// expect(config.isEnabled()).andReturn(true).anyTimes(); | |
| 91 | -// replay(config); | |
| 92 | -// | |
| 93 | -// loginSuccessfully(); | |
| 94 | -// | |
| 95 | -// Authorizer authorizer = createMock(Authorizer.class); | |
| 96 | -// expect(authorizer.hasPermission(null, null)).andReturn(true); | |
| 97 | -// | |
| 98 | -// setInternalState(context, "authorizer", authorizer); | |
| 99 | -// | |
| 100 | -// replay(authorizer); | |
| 101 | -// | |
| 102 | -// try { | |
| 103 | -// assertTrue(context.hasPermission(null, null)); | |
| 104 | -// } catch (NotLoggedInException e) { | |
| 105 | -// fail(); | |
| 106 | -// } | |
| 107 | -// | |
| 108 | -// } | |
| 109 | -// | |
| 110 | -// private void loginSuccessfully() { | |
| 111 | -// Authenticator authenticator = createMock(Authenticator.class); | |
| 112 | -// expect(authenticator.authenticate()).andReturn(true); | |
| 113 | -// | |
| 114 | -// BeanManager manager = createMock(BeanManager.class); | |
| 115 | -// mockStatic(Beans.class); | |
| 116 | -// expect(Beans.getBeanManager()).andReturn(manager); | |
| 117 | -// manager.fireEvent(EasyMock.anyObject(Class.class)); | |
| 118 | -// PowerMock.expectLastCall(); | |
| 119 | -// | |
| 120 | -// User user = createMock(User.class); | |
| 121 | -// expect(authenticator.getUser()).andReturn(user).anyTimes(); | |
| 122 | -// | |
| 123 | -// setInternalState(context, "authenticator", authenticator); | |
| 124 | -// | |
| 125 | -// replayAll(authenticator, user, Beans.class, manager); | |
| 126 | -// | |
| 127 | -// context.login(); | |
| 128 | -// assertTrue(context.isLoggedIn()); | |
| 129 | -// } | |
| 130 | -// | |
| 131 | -// @Test | |
| 132 | -// public void testHasRoleWithSecurityDisabled() { | |
| 133 | -// | |
| 134 | -// expect(config.isEnabled()).andReturn(false); | |
| 135 | -// | |
| 136 | -// replay(config); | |
| 137 | -// | |
| 138 | -// try { | |
| 139 | -// assertTrue(context.hasRole(null)); | |
| 140 | -// } catch (NotLoggedInException e) { | |
| 141 | -// fail(); | |
| 142 | -// } | |
| 143 | -// | |
| 144 | -// } | |
| 145 | -// | |
| 146 | -// @Test | |
| 147 | -// public void testHasRoleWithSecurityEnabledAndNotLoggedIn() { | |
| 148 | -// Authenticator authenticator = createMock(Authenticator.class); | |
| 149 | -// expect(authenticator.getUser()).andReturn(null).anyTimes(); | |
| 150 | -// | |
| 151 | -// ResourceBundle bundle = new ResourceBundle("demoiselle-core-bundle", Locale.getDefault()); | |
| 152 | -// setInternalState(context, "bundle", bundle); | |
| 153 | -// expect(config.isEnabled()).andReturn(true).anyTimes(); | |
| 154 | -// | |
| 155 | -// setInternalState(context, "authenticator", authenticator); | |
| 156 | -// | |
| 157 | -// replay(config, authenticator); | |
| 158 | -// | |
| 159 | -// try { | |
| 160 | -// context.hasRole(null); | |
| 161 | -// fail(); | |
| 162 | -// } catch (NotLoggedInException e) { | |
| 163 | -// assertTrue(e.getMessage().equals(bundle.getString("user-not-authenticated"))); | |
| 164 | -// } | |
| 165 | -// | |
| 166 | -// } | |
| 167 | -// | |
| 168 | -// @Test | |
| 169 | -// public void testHasRoleWithSecurityEnabledAndLoggedIn() { | |
| 170 | -// expect(config.isEnabled()).andReturn(true).anyTimes(); | |
| 171 | -// replay(config); | |
| 172 | -// | |
| 173 | -// loginSuccessfully(); | |
| 174 | -// | |
| 175 | -// Authorizer authorizer = createMock(Authorizer.class); | |
| 176 | -// expect(authorizer.hasRole(null)).andReturn(true); | |
| 177 | -// | |
| 178 | -// setInternalState(context, "authorizer", authorizer); | |
| 179 | -// | |
| 180 | -// replay(authorizer); | |
| 181 | -// | |
| 182 | -// try { | |
| 183 | -// assertTrue(context.hasRole(null)); | |
| 184 | -// } catch (NotLoggedInException e) { | |
| 185 | -// fail(); | |
| 186 | -// } | |
| 187 | -// | |
| 188 | -// } | |
| 189 | -// | |
| 190 | -// @Test | |
| 191 | -// public void testIsLoggedInWithSecurityEnabled() { | |
| 192 | -// Authenticator authenticator = createMock(Authenticator.class); | |
| 193 | -// expect(authenticator.getUser()).andReturn(null).anyTimes(); | |
| 194 | -// | |
| 195 | -// expect(config.isEnabled()).andReturn(true).anyTimes(); | |
| 196 | -// | |
| 197 | -// setInternalState(context, "authenticator", authenticator); | |
| 198 | -// | |
| 199 | -// replay(config, authenticator); | |
| 200 | -// | |
| 201 | -// assertFalse(context.isLoggedIn()); | |
| 202 | -// } | |
| 203 | -// | |
| 204 | -// @Test | |
| 205 | -// public void testIsLoggedInWithSecurityDisabled() { | |
| 206 | -// | |
| 207 | -// expect(config.isEnabled()).andReturn(false); | |
| 208 | -// | |
| 209 | -// replay(config); | |
| 210 | -// | |
| 211 | -// assertTrue(context.isLoggedIn()); | |
| 212 | -// | |
| 213 | -// } | |
| 214 | -// | |
| 215 | -// @Test | |
| 216 | -// public void testLoginWithSecurityDisabled() { | |
| 217 | -// | |
| 218 | -// expect(config.isEnabled()).andReturn(false).times(2); | |
| 219 | -// | |
| 220 | -// replay(config); | |
| 221 | -// | |
| 222 | -// context.login(); | |
| 223 | -// | |
| 224 | -// assertTrue(context.isLoggedIn()); | |
| 225 | -// | |
| 226 | -// } | |
| 227 | -// | |
| 228 | -// @Test | |
| 229 | -// public void testLoginWithAuthenticationFail() { | |
| 230 | -// | |
| 231 | -// expect(config.isEnabled()).andReturn(true).anyTimes(); | |
| 232 | -// | |
| 233 | -// Authenticator authenticator = createMock(Authenticator.class); | |
| 234 | -// expect(authenticator.authenticate()).andReturn(false); | |
| 235 | -// expect(authenticator.getUser()).andReturn(null).anyTimes(); | |
| 236 | -// | |
| 237 | -// setInternalState(context, "authenticator", authenticator); | |
| 238 | -// | |
| 239 | -// replayAll(authenticator, config); | |
| 240 | -// | |
| 241 | -// context.login(); | |
| 242 | -// | |
| 243 | -// assertFalse(context.isLoggedIn()); | |
| 244 | -// } | |
| 245 | -// | |
| 246 | -// @Test | |
| 247 | -// public void testLogOutWithSecurityDisabled() { | |
| 248 | -// | |
| 249 | -// expect(config.isEnabled()).andReturn(false).times(2); | |
| 250 | -// | |
| 251 | -// replay(config); | |
| 252 | -// | |
| 253 | -// try { | |
| 254 | -// context.logout(); | |
| 255 | -// assertTrue(context.isLoggedIn()); | |
| 256 | -// } catch (NotLoggedInException e) { | |
| 257 | -// fail(); | |
| 258 | -// } | |
| 259 | -// } | |
| 260 | -// | |
| 261 | -// @Test | |
| 262 | -// public void testLogOutWithoutPreviousLogin() { | |
| 263 | -// Authenticator authenticator = createMock(Authenticator.class); | |
| 264 | -// expect(authenticator.getUser()).andReturn(null).anyTimes(); | |
| 265 | -// | |
| 266 | -// ResourceBundle bundle = new ResourceBundle("demoiselle-core-bundle", Locale.getDefault()); | |
| 267 | -// setInternalState(context, "bundle", bundle); | |
| 268 | -// | |
| 269 | -// expect(config.isEnabled()).andReturn(true).anyTimes(); | |
| 270 | -// | |
| 271 | -// setInternalState(context, "authenticator", authenticator); | |
| 272 | -// | |
| 273 | -// replay(config, authenticator); | |
| 274 | -// | |
| 275 | -// try { | |
| 276 | -// context.logout(); | |
| 277 | -// fail(); | |
| 278 | -// } catch (NotLoggedInException e) { | |
| 279 | -// assertTrue(e.getMessage().equals(bundle.getString("user-not-authenticated"))); | |
| 280 | -// } | |
| 281 | -// } | |
| 282 | -// | |
| 283 | -// @Test | |
| 284 | -// public void testLogOutAfterSuccessfulLogin() { | |
| 285 | -// mockStatic(Beans.class); | |
| 286 | -// | |
| 287 | -// expect(config.isEnabled()).andReturn(true).anyTimes(); | |
| 288 | -// | |
| 289 | -// Authenticator authenticator = createMock(Authenticator.class); | |
| 290 | -// expect(authenticator.authenticate()).andReturn(true); | |
| 291 | -// authenticator.unAuthenticate(); | |
| 292 | -// PowerMock.expectLastCall(); | |
| 293 | -// | |
| 294 | -// User user = createMock(User.class); | |
| 295 | -// expect(authenticator.getUser()).andReturn(user); | |
| 296 | -// expect(authenticator.getUser()).andReturn(null); | |
| 297 | -// | |
| 298 | -// BeanManager manager = createMock(BeanManager.class); | |
| 299 | -// expect(Beans.getBeanManager()).andReturn(manager).times(2); | |
| 300 | -// manager.fireEvent(EasyMock.anyObject(Class.class)); | |
| 301 | -// PowerMock.expectLastCall().times(2); | |
| 302 | -// | |
| 303 | -// setInternalState(context, "authenticator", authenticator); | |
| 304 | -// | |
| 305 | -// replayAll(Beans.class, authenticator, user, manager, config); | |
| 306 | -// | |
| 307 | -// context.login(); | |
| 308 | -// context.logout(); | |
| 309 | -// | |
| 310 | -// assertFalse(context.isLoggedIn()); | |
| 311 | -// } | |
| 312 | -// | |
| 313 | -// @Test | |
| 314 | -// public void testGetUserWhenSecurityIsDisabled() { | |
| 315 | -// Authenticator authenticator = createMock(Authenticator.class); | |
| 316 | -// expect(authenticator.getUser()).andReturn(null).anyTimes(); | |
| 317 | -// | |
| 318 | -// expect(config.isEnabled()).andReturn(false).anyTimes(); | |
| 319 | -// replay(config, authenticator, Beans.class); | |
| 320 | -// | |
| 321 | -// setInternalState(context, "authenticator", authenticator); | |
| 322 | -// | |
| 323 | -// assertNotNull(context.getUser()); | |
| 324 | -// assertNotNull(context.getUser().getId()); | |
| 325 | -// assertNull(context.getUser().getAttribute(null)); | |
| 326 | -// context.getUser().setAttribute(null, null); | |
| 327 | -// } | |
| 328 | -// | |
| 329 | -// @Test | |
| 330 | -// public void testGetUserWhenSecurityIsEnabled() { | |
| 331 | -// Authenticator authenticator = createMock(Authenticator.class); | |
| 332 | -// expect(authenticator.getUser()).andReturn(null).anyTimes(); | |
| 333 | -// | |
| 334 | -// expect(config.isEnabled()).andReturn(true); | |
| 335 | -// replay(config, authenticator, Beans.class); | |
| 336 | -// | |
| 337 | -// setInternalState(context, "authenticator", authenticator); | |
| 338 | -// | |
| 339 | -// assertNull(context.getUser()); | |
| 340 | -// } | |
| 341 | -// | |
| 342 | -// @Test | |
| 343 | -// public void testGetUserWhenSecurityIsEnabledAndUserIsNotNull() { | |
| 344 | -// User user = createMock(User.class); | |
| 345 | -// | |
| 346 | -// Authenticator authenticator = createMock(Authenticator.class); | |
| 347 | -// expect(authenticator.getUser()).andReturn(user).anyTimes(); | |
| 348 | -// | |
| 349 | -// expect(config.isEnabled()).andReturn(true).anyTimes(); | |
| 350 | -// replay(config, user, authenticator, Beans.class); | |
| 351 | -// | |
| 352 | -// setInternalState(context, "authenticator", authenticator); | |
| 353 | -// | |
| 354 | -// assertEquals(context.getUser(), user); | |
| 355 | -// } | |
| 356 | -//} | |
| 1 | +package br.gov.frameworkdemoiselle.internal.implementation; | |
| 2 | + | |
| 3 | +import static junit.framework.Assert.assertEquals; | |
| 4 | +import static junit.framework.Assert.assertFalse; | |
| 5 | +import static junit.framework.Assert.assertNotNull; | |
| 6 | +import static junit.framework.Assert.assertNull; | |
| 7 | +import static junit.framework.Assert.assertTrue; | |
| 8 | +import static junit.framework.Assert.fail; | |
| 9 | +import static org.easymock.EasyMock.createMock; | |
| 10 | +import static org.easymock.EasyMock.expect; | |
| 11 | +import static org.powermock.api.easymock.PowerMock.mockStatic; | |
| 12 | +import static org.powermock.api.easymock.PowerMock.replay; | |
| 13 | +import static org.powermock.api.easymock.PowerMock.replayAll; | |
| 14 | + | |
| 15 | +import java.util.ArrayList; | |
| 16 | +import java.util.List; | |
| 17 | +import java.util.Locale; | |
| 18 | + | |
| 19 | +import static org.powermock.reflect.Whitebox.setInternalState; | |
| 20 | + | |
| 21 | +import javax.enterprise.inject.spi.BeanManager; | |
| 22 | + | |
| 23 | +import org.easymock.EasyMock; | |
| 24 | +import org.junit.Before; | |
| 25 | +import org.junit.Test; | |
| 26 | +import org.junit.runner.RunWith; | |
| 27 | +import org.powermock.api.easymock.PowerMock; | |
| 28 | +import org.powermock.core.classloader.annotations.PrepareForTest; | |
| 29 | +import org.powermock.modules.junit4.PowerMockRunner; | |
| 30 | + | |
| 31 | +import br.gov.frameworkdemoiselle.security.Authenticator; | |
| 32 | +import br.gov.frameworkdemoiselle.security.User; | |
| 33 | +import br.gov.frameworkdemoiselle.security.Authorizer; | |
| 34 | +import br.gov.frameworkdemoiselle.security.NotLoggedInException; | |
| 35 | +import br.gov.frameworkdemoiselle.internal.bootstrap.AuthenticatorBootstrap; | |
| 36 | +import br.gov.frameworkdemoiselle.internal.configuration.SecurityConfigImpl; | |
| 37 | +import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; | |
| 38 | +import br.gov.frameworkdemoiselle.util.Beans; | |
| 39 | +import br.gov.frameworkdemoiselle.util.ResourceBundle; | |
| 40 | + | |
| 41 | +@RunWith(PowerMockRunner.class) | |
| 42 | +@PrepareForTest({ Beans.class, ResourceBundle.class }) | |
| 43 | +public class SecurityContextImplTest { | |
| 44 | + | |
| 45 | + private SecurityContextImpl context; | |
| 46 | + private SecurityConfigImpl config; | |
| 47 | + private ResourceBundle bundle; | |
| 48 | + | |
| 49 | + @Before | |
| 50 | + public void setUpConfig() { | |
| 51 | + context = new SecurityContextImpl(); | |
| 52 | + config = createMock(SecurityConfigImpl.class); | |
| 53 | + | |
| 54 | + mockStatic(Beans.class); | |
| 55 | + expect(Beans.getReference(SecurityConfigImpl.class)).andReturn(config).anyTimes(); | |
| 56 | + } | |
| 57 | + | |
| 58 | + @Test | |
| 59 | + public void testHasPermissionWithSecurityDisabled() { | |
| 60 | + expect(config.isEnabled()).andReturn(false); | |
| 61 | + replayAll(Beans.class,config); | |
| 62 | + | |
| 63 | + try { | |
| 64 | + assertTrue(context.hasPermission(null, null)); | |
| 65 | + } catch (NotLoggedInException e) { | |
| 66 | + fail(); | |
| 67 | + } | |
| 68 | + } | |
| 69 | + | |
| 70 | + public void mockGetAuthenticator() { | |
| 71 | + Class<? extends Authenticator> cache = AuthenticatorImpl.class; | |
| 72 | + List<Class<? extends Authenticator>> cacheList = new ArrayList<Class<? extends Authenticator>>(); | |
| 73 | + cacheList.add(cache); | |
| 74 | + | |
| 75 | + AuthenticatorBootstrap bootstrap = PowerMock.createMock(AuthenticatorBootstrap.class); | |
| 76 | + | |
| 77 | + expect(Beans.getReference(AuthenticatorBootstrap.class)).andReturn(bootstrap).anyTimes(); | |
| 78 | + expect(config.getAuthenticatorClass()).andReturn(null).anyTimes(); | |
| 79 | + expect(bootstrap.getCache()).andReturn(cacheList); | |
| 80 | + expect(Beans.getReference(AuthenticatorImpl.class)).andReturn(new AuthenticatorImpl()); | |
| 81 | + expect(Beans.getReference(Locale.class)).andReturn(Locale.getDefault()).anyTimes(); | |
| 82 | + } | |
| 83 | + | |
| 84 | + @Test | |
| 85 | + public void testHasPermissionWithSecurityEnabledAndNotLoggedIn() { | |
| 86 | + mockGetAuthenticator(); | |
| 87 | + | |
| 88 | + expect(config.isEnabled()).andReturn(true).anyTimes(); | |
| 89 | + replayAll(Beans.class,config); | |
| 90 | + | |
| 91 | + bundle = ResourceBundleProducer.create("demoiselle-core-bundle"); | |
| 92 | + | |
| 93 | + try { | |
| 94 | + context.hasPermission(null, null); | |
| 95 | + fail(); | |
| 96 | + } catch (NotLoggedInException e) { | |
| 97 | + assertTrue(e.getMessage().equals(bundle.getString("user-not-authenticated"))); | |
| 98 | + } | |
| 99 | + } | |
| 100 | + | |
| 101 | + @Test | |
| 102 | + public void testHasPermissionWithSecurityEnabledAndLoggedIn() { | |
| 103 | + expect(config.isEnabled()).andReturn(true).anyTimes(); | |
| 104 | + replay(config); | |
| 105 | + | |
| 106 | + loginSuccessfully(); | |
| 107 | + | |
| 108 | + Authorizer authorizer = createMock(Authorizer.class); | |
| 109 | + expect(authorizer.hasPermission(null, null)).andReturn(true); | |
| 110 | + | |
| 111 | + setInternalState(context, "authorizer", authorizer); | |
| 112 | + | |
| 113 | + replay(authorizer); | |
| 114 | + | |
| 115 | + try { | |
| 116 | + assertTrue(context.hasPermission(null, null)); | |
| 117 | + } catch (NotLoggedInException e) { | |
| 118 | + fail(); | |
| 119 | + } | |
| 120 | + } | |
| 121 | + | |
| 122 | + private void loginSuccessfully() { | |
| 123 | + Authenticator authenticator = createMock(Authenticator.class); | |
| 124 | + expect(authenticator.authenticate()).andReturn(true); | |
| 125 | + | |
| 126 | + BeanManager manager = createMock(BeanManager.class); | |
| 127 | + expect(Beans.getBeanManager()).andReturn(manager); | |
| 128 | + manager.fireEvent(EasyMock.anyObject(Class.class)); | |
| 129 | + PowerMock.expectLastCall(); | |
| 130 | + | |
| 131 | + User user = createMock(User.class); | |
| 132 | + expect(authenticator.getUser()).andReturn(user).anyTimes(); | |
| 133 | + | |
| 134 | + setInternalState(context, "authenticator", authenticator); | |
| 135 | + | |
| 136 | + replayAll(authenticator, user, Beans.class, manager); | |
| 137 | + | |
| 138 | + context.login(); | |
| 139 | + assertTrue(context.isLoggedIn()); | |
| 140 | + } | |
| 141 | + | |
| 142 | + @Test | |
| 143 | + public void testHasRoleWithSecurityDisabled() { | |
| 144 | + expect(config.isEnabled()).andReturn(false); | |
| 145 | + replayAll(Beans.class,config); | |
| 146 | + | |
| 147 | + try { | |
| 148 | + assertTrue(context.hasRole(null)); | |
| 149 | + } catch (NotLoggedInException e) { | |
| 150 | + fail(); | |
| 151 | + } | |
| 152 | + } | |
| 153 | + | |
| 154 | + @Test | |
| 155 | + public void testHasRoleWithSecurityEnabledAndNotLoggedIn() { | |
| 156 | + mockGetAuthenticator(); | |
| 157 | + | |
| 158 | + expect(config.isEnabled()).andReturn(true).anyTimes(); | |
| 159 | + replayAll(Beans.class,config); | |
| 160 | + | |
| 161 | + bundle = ResourceBundleProducer.create("demoiselle-core-bundle"); | |
| 162 | + | |
| 163 | + try { | |
| 164 | + context.hasRole(null); | |
| 165 | + fail(); | |
| 166 | + } catch (NotLoggedInException e) { | |
| 167 | + assertTrue(e.getMessage().equals(bundle.getString("user-not-authenticated"))); | |
| 168 | + } | |
| 169 | + } | |
| 170 | + | |
| 171 | + @Test | |
| 172 | + public void testHasRoleWithSecurityEnabledAndLoggedIn() { | |
| 173 | + expect(config.isEnabled()).andReturn(true).anyTimes(); | |
| 174 | + replay(config); | |
| 175 | + | |
| 176 | + loginSuccessfully(); | |
| 177 | + | |
| 178 | + Authorizer authorizer = createMock(Authorizer.class); | |
| 179 | + expect(authorizer.hasRole(null)).andReturn(true); | |
| 180 | + | |
| 181 | + setInternalState(context, "authorizer", authorizer); | |
| 182 | + | |
| 183 | + replay(authorizer); | |
| 184 | + | |
| 185 | + try { | |
| 186 | + assertTrue(context.hasRole(null)); | |
| 187 | + } catch (NotLoggedInException e) { | |
| 188 | + fail(); | |
| 189 | + } | |
| 190 | + } | |
| 191 | + | |
| 192 | + @Test | |
| 193 | + public void testIsLoggedInWithSecurityEnabled() { | |
| 194 | + Authenticator authenticator = createMock(Authenticator.class); | |
| 195 | + expect(authenticator.getUser()).andReturn(null).anyTimes(); | |
| 196 | + expect(config.isEnabled()).andReturn(true).anyTimes(); | |
| 197 | + | |
| 198 | + setInternalState(context, "authenticator", authenticator); | |
| 199 | + | |
| 200 | + replayAll(config, authenticator); | |
| 201 | + | |
| 202 | + assertFalse(context.isLoggedIn()); | |
| 203 | + } | |
| 204 | + | |
| 205 | + @Test | |
| 206 | + public void testIsLoggedInWithSecurityDisabled() { | |
| 207 | + expect(config.isEnabled()).andReturn(false); | |
| 208 | + replayAll(config,Beans.class); | |
| 209 | + | |
| 210 | + assertTrue(context.isLoggedIn()); | |
| 211 | + } | |
| 212 | + | |
| 213 | + @Test | |
| 214 | + public void testLoginWithSecurityDisabled() { | |
| 215 | + expect(config.isEnabled()).andReturn(false).times(2); | |
| 216 | + replayAll(config,Beans.class); | |
| 217 | + context.login(); | |
| 218 | + | |
| 219 | + assertTrue(context.isLoggedIn()); | |
| 220 | + } | |
| 221 | + | |
| 222 | + @Test | |
| 223 | + public void testLoginWithAuthenticationFail() { | |
| 224 | + Authenticator authenticator = createMock(Authenticator.class); | |
| 225 | + | |
| 226 | + expect(config.isEnabled()).andReturn(true).anyTimes(); | |
| 227 | + expect(authenticator.authenticate()).andReturn(false); | |
| 228 | + expect(authenticator.getUser()).andReturn(null).anyTimes(); | |
| 229 | + | |
| 230 | + setInternalState(context, "authenticator", authenticator); | |
| 231 | + | |
| 232 | + replayAll(authenticator, config); | |
| 233 | + | |
| 234 | + context.login(); | |
| 235 | + assertFalse(context.isLoggedIn()); | |
| 236 | + } | |
| 237 | + | |
| 238 | + @Test | |
| 239 | + public void testLogOutWithSecurityDisabled() { | |
| 240 | + expect(config.isEnabled()).andReturn(false).times(2); | |
| 241 | + | |
| 242 | + replayAll(config,Beans.class); | |
| 243 | + | |
| 244 | + try { | |
| 245 | + context.logout(); | |
| 246 | + assertTrue(context.isLoggedIn()); | |
| 247 | + } catch (NotLoggedInException e) { | |
| 248 | + fail(); | |
| 249 | + } | |
| 250 | + } | |
| 251 | + | |
| 252 | + @Test | |
| 253 | + public void testLogOutWithoutPreviousLogin() { | |
| 254 | + Authenticator authenticator = createMock(Authenticator.class); | |
| 255 | + | |
| 256 | + expect(authenticator.getUser()).andReturn(null).anyTimes(); | |
| 257 | + expect(Beans.getReference(Locale.class)).andReturn(Locale.getDefault()).anyTimes(); | |
| 258 | + expect(config.isEnabled()).andReturn(true).anyTimes(); | |
| 259 | + | |
| 260 | + setInternalState(context, "authenticator", authenticator); | |
| 261 | + | |
| 262 | + replayAll(config, authenticator, Beans.class); | |
| 263 | + | |
| 264 | + bundle = ResourceBundleProducer.create("demoiselle-core-bundle"); | |
| 265 | + | |
| 266 | + try { | |
| 267 | + context.logout(); | |
| 268 | + fail(); | |
| 269 | + } catch (NotLoggedInException e) { | |
| 270 | + assertTrue(e.getMessage().equals(bundle.getString("user-not-authenticated"))); | |
| 271 | + } | |
| 272 | + } | |
| 273 | + | |
| 274 | + @Test | |
| 275 | + public void testLogOutAfterSuccessfulLogin() { | |
| 276 | + expect(config.isEnabled()).andReturn(true).anyTimes(); | |
| 277 | + | |
| 278 | + Authenticator authenticator = createMock(Authenticator.class); | |
| 279 | + expect(authenticator.authenticate()).andReturn(true); | |
| 280 | + authenticator.unAuthenticate(); | |
| 281 | + PowerMock.expectLastCall(); | |
| 282 | + | |
| 283 | + User user = createMock(User.class); | |
| 284 | + expect(authenticator.getUser()).andReturn(user); | |
| 285 | + expect(authenticator.getUser()).andReturn(null); | |
| 286 | + | |
| 287 | + BeanManager manager = createMock(BeanManager.class); | |
| 288 | + expect(Beans.getBeanManager()).andReturn(manager).times(2); | |
| 289 | + manager.fireEvent(EasyMock.anyObject(Class.class)); | |
| 290 | + PowerMock.expectLastCall().times(2); | |
| 291 | + | |
| 292 | + setInternalState(context, "authenticator", authenticator); | |
| 293 | + | |
| 294 | + replayAll(Beans.class, authenticator, user, manager, config); | |
| 295 | + | |
| 296 | + context.login(); | |
| 297 | + context.logout(); | |
| 298 | + | |
| 299 | + assertFalse(context.isLoggedIn()); | |
| 300 | + } | |
| 301 | + | |
| 302 | + @Test | |
| 303 | + public void testGetUserWhenSecurityIsDisabled() { | |
| 304 | + Authenticator authenticator = createMock(Authenticator.class); | |
| 305 | + expect(authenticator.getUser()).andReturn(null).anyTimes(); | |
| 306 | + expect(config.isEnabled()).andReturn(false).anyTimes(); | |
| 307 | + replay(config, authenticator, Beans.class); | |
| 308 | + | |
| 309 | + setInternalState(context, "authenticator", authenticator); | |
| 310 | + | |
| 311 | + assertNotNull(context.getUser()); | |
| 312 | + assertNotNull(context.getUser().getId()); | |
| 313 | + assertNull(context.getUser().getAttribute(null)); | |
| 314 | + context.getUser().setAttribute(null, null); | |
| 315 | + } | |
| 316 | + | |
| 317 | + @Test | |
| 318 | + public void testGetUserWhenSecurityIsEnabled() { | |
| 319 | + Authenticator authenticator = createMock(Authenticator.class); | |
| 320 | + expect(authenticator.getUser()).andReturn(null).anyTimes(); | |
| 321 | + expect(config.isEnabled()).andReturn(true); | |
| 322 | + replay(config, authenticator, Beans.class); | |
| 323 | + | |
| 324 | + setInternalState(context, "authenticator", authenticator); | |
| 325 | + | |
| 326 | + assertNull(context.getUser()); | |
| 327 | + } | |
| 328 | + | |
| 329 | + @Test | |
| 330 | + public void testGetUserWhenSecurityIsEnabledAndUserIsNotNull() { | |
| 331 | + User user = createMock(User.class); | |
| 332 | + | |
| 333 | + Authenticator authenticator = createMock(Authenticator.class); | |
| 334 | + expect(authenticator.getUser()).andReturn(user).anyTimes(); | |
| 335 | + expect(config.isEnabled()).andReturn(true).anyTimes(); | |
| 336 | + replay(config, user, authenticator, Beans.class); | |
| 337 | + | |
| 338 | + setInternalState(context, "authenticator", authenticator); | |
| 339 | + | |
| 340 | + assertEquals(context.getUser(), user); | |
| 341 | + } | |
| 342 | + | |
| 343 | + class AuthenticatorImpl implements Authenticator { | |
| 344 | + | |
| 345 | + private static final long serialVersionUID = 1L; | |
| 346 | + | |
| 347 | + @Override | |
| 348 | + public boolean authenticate() { | |
| 349 | + return false; | |
| 350 | + } | |
| 351 | + | |
| 352 | + @Override | |
| 353 | + public void unAuthenticate() { | |
| 354 | + } | |
| 355 | + | |
| 356 | + @Override | |
| 357 | + public User getUser() { | |
| 358 | + return null; | |
| 359 | + } | |
| 360 | + } | |
| 361 | +} | ... | ... |