spring-projects / spring-projects/spring-security

Consider Adding Testing Support for Building Authentications

Open
#8,750 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

in: test type: enhancement
Dominant language
Java
Stars
9.6k
Forks
6.3k
Avg merge
2d 11h
Merged PRs (30d)
52

Description

SecurityMockMvcRequestPostProcessors is a class that simplifies creating RequestPostProcessors for the purposes of testing. In large part, each RequestPostProcessor is responsible for creating an Authentication and populating the SecurityContextHolder.

The builders that SecurityMockMvcRequestPostProcessors exposes are actually quite convenient to use since they provide reasonable testing defaults. For example, oidcLogin() simplifies creating an OAuth2AuthenticationToken by defaulting certain claims and authorities:

RequestPostProcessor requestPostProcessor = oidcLogin().idToken(id -> id
        .subject("test-subject"));
this.mvc.perform(get("/").with(requestPostProcessor));

As noted in a related ticket, these builders are designed to support MockMvc testing; however, since many of them are largely responsible for creating an Authentication, such behavior could reasonably be extracted out into a separate class:

public final class TestAuthentications {
    public static OidcLoginAuthenticationBuilder oidcLogin() {
        return new OidcLoginAuthenticationBuilder();
    }

    // ...

    public static class OidcLoginAuthenticationBuilder {
        // ... similar support as OidcLoginRequestPostProcessor

        public Authentication build() {
            return new OAuth2AuthenticationToken(...);
        }
    }
}

By doing so, testers could easily build the appropriate Authentication for service-level testing, where MockMvc is not being used:

@Test 
public void testMyService() {
    Authentication authentication = oidcLogin().idToken(id -> id
            .subject("test-subject")).build();
    SecurityContextHolder.getContext().setAuthentication(authentication);

    // ... rest of test
}

The nice thing about this is that exposing a builder is a great deal more powerful than Spring Security's annotation support that's used in simpler authentication scenarios.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading SecurityMockMvcRequestPostProcessors and the related ticket #8459 comment. Compare the existing authentication-building behavior with the proposed TestAuthentications API and determine which builders need service-level support. Done means the scope and API for building Authentication independently of MockMvc are defined and covered by appropriate tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
security, testing
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.