Child pages
  • Geomajas FAQ
Skip to end of metadata
Go to start of metadata

Security in unit test

To use backend security in a unit test:

  • add before/after to create/destroy context
  • (optionally) autowire SecurityContext if you need it in your test or just for checking
  • (optionally) force reload of application context if you have interference with a previous test that has not cleaned up the context correctly
@ContextConfiguration(locations = {"/org/geomajas/spring/geomajasContext.xml", <others>....})
@TestExecutionListeners(listeners = {ReloadContextTestExecutionListener.class,
		DependencyInjectionTestExecutionListener.class})  // optional, forces reload of application context
@ReloadContext // optional, forces reload of application context
public class MyTest {
 
	@Autowired
	private SecurityContext securityContext; // optional autowired security context
 
	@Autowired
	private org.geomajas.security.SecurityManager securityManager;
 
	@Before
	public void login() {
		// assure security context is set
		securityManager.createSecurityContext(null);
	}
 
	// alternative for login()
	/*@Before 
	public void loginWithToken() {
		// alternatively log in with a token of a known user
		String token = userRegisteredBySpecificProcess();
		securityManager.createSecurityContext(token);
	}*/
	@After
	public void clearSecurityContext() {
		// clear the security context (logout)
		ThreadScopeContextHolder.clear();
	}
}

  • No labels

1 Comment

  1. In Deskmanager, register user based on a Profile:

    	@Autowired
    	private SecurityService securityService;
     
    	/**
    	* return token
    	*/
    	public String registerUser(Profile profile) {
    		return ((DeskmanagerSecurityService) securityService).registerRole(RetrieveRolesRequest.MANAGER_ID, profile);
    	}