One of the possibilities is to have one Spec for checking the actual login behavior (for example, LoginSpec ), which is completely written out as it is now. For other specifications that need to be logged in before performing the actual test, you can abstract the whole process of logging in to the method in LoginPage . Like now, singIn .
When you have many specifications that need to be logged in before they can begin testing the features that they intend to test, re-entering the browser through the browser can take a lot of time.
An alternative would be to create a specific controller that boots only in dev / test environments and that offers a login action. Therefore, instead of going through all the steps (go to the page, enter a name, enter a password, ...), you can just go to URL /my-app/testLogin/auth?username=username .
Below is an example of how we do this in our Grails + Spring security configuration. We also combine other utilities in this controller that are used to configure several specifications, otherwise it would take a few clicks in the browser, for example. change the interface language.
// Example TestLoginController when using the Spring Security plugin class TestLoginController { def auth = { String userName, String startPage = 'dashboard' -> // Block the dev login functionality in production environments // Can also be done with filter, ... Environment.executeForCurrentEnvironment { production { render(status: HttpServletResponse.SC_NOT_FOUND) return } } def endUser = getYourEndUserDataByUsername() if (endUser) { // Logout existing user new SecurityContextLogoutHandler().logout(request, null, null) // Authenticate the user UserDetails userDetails = new User(endUser) def authenticationToken = new UsernamePasswordAuthenticationToken(userDetails, userDetails.password, userDetails.authorities) SecurityContextHolder.context.setAuthentication(authenticationToken) // Bind the security context to the (new) session session.SPRING_SECURITY_CONTEXT = SecurityContextHolder.context redirect(action: "index", controller: startPage) } }
Ruben
source share