testing library
Helpers for MockMvc tests of a secured web application: make a test
request as a given user, with a JWT, or with a CSRF token, without
going through a real login.
Each helper is a request post-processor: add it to a
MockRequestBuilder with with_. The user helpers put the user in
the request's security context, so the request skips the login and
the access rules see that user. They rely on the chain reading the
request attribute repository, which every chain does unless it sets
its own securityContextRepository. This library also re-exports
withMockUser and its siblings from spring_security_core.
import 'package:spring_boot_starter_security/testing.dart';
await app.mockMvc
.perform(MockRequestBuilder(HttpMethod.get, '/api/admin')
..with_(user('ada').roles(['ADMIN'])))
.andExpect(status: 200);
await app.mockMvc
.perform(MockRequestBuilder(HttpMethod.post, '/api/books')
..with_(user('ada'))
..with_(csrf()))
.andExpect(status: 201);
Spring (Java): spring-security-test's
SecurityMockMvcRequestPostProcessors (user, jwt, csrf, ...).
Classes
- CsrfRequestPostProcessor
- The request post-processor csrf returns.
- JwtRequestPostProcessor
- The request post-processor jwt returns; its methods change the token's claims and authorities.
- UserRequestPostProcessor
- The request post-processor user returns; its methods change the user's roles, authorities or password.
Functions
-
anonymous(
) → RequestPostProcessor -
Makes the request as an anonymous user (
anonymousUserwithROLE_ANONYMOUS), what a visitor without a login is. -
authentication(
Authentication authentication) → RequestPostProcessor -
Makes the request with
authenticationas the current user, for any kind of login, such as a custom token. -
csrf(
) → CsrfRequestPostProcessor -
Makes the request pass the CSRF check as if it carried a valid token;
call
useInvalidToken()on the result to test a refused one. -
httpBasic(
String username, String password) → RequestPostProcessor -
Adds an
Authorization: Basicheader withusernameandpassword, so the real Basic login runs and checks them against the application's users. -
jwt(
) → JwtRequestPostProcessor - Makes the request as if it carried a valid JWT bearer token: no token is decoded or verified.
-
programWideAuthentication(
) → Authentication? -
The authentication outside every scope: who all the code that runs
outside a request (and outside withMockUser) acts as, or
null. -
user(
String username) → UserRequestPostProcessor -
Makes the request as the user
username, already logged in: no password is checked. -
userDetails(
UserDetails user) → RequestPostProcessor -
Makes the request as
user, already logged in, for tests that need your ownUserDetailsclass as the principal. -
withAnonymousUser<
T> (FutureOr< T> body()) → Future<T> -
Runs
bodyas an anonymous user (anonymousUserwithROLE_ANONYMOUS), as the web starter sets for a request without a login, and returns its result. -
withAuthentication<
T> (FutureOr< T> body(), Authentication authentication) → Future<T> -
Runs
bodywithauthenticationas the current user and returns its result. -
withMockUser<
T> (FutureOr< T> body(), {String username = 'user', List<String> roles = const ['USER'], List<String> ? authorities}) → Future<T> -
Runs
bodyas a logged-in user and returns its result. -
withMockUsersAtOnce<
T> (Iterable< String> usernames, FutureOr<T> body(String username), {int rounds = 10, List<String> roles = const ['USER']}) → Future<Map< String, List< >T> > -
Runs
bodyas each ofusernamesat the same time,roundstimes each, and returns what every call gave, by user, in the order of the rounds.