Perform JUnit 5 clean up tasks

- For CookieCsrfTokenRepositoryTests and
CookieServerCsrfTokenRepositoryTests

Issue gh-12086
This commit is contained in:
Alex Montoya 2022-11-17 21:46:02 -08:00 committed by Josh Cummings
parent b79ba89eeb
commit 8717b7544a
No known key found for this signature in database
GPG Key ID: A306A51F43B8E5A5
2 changed files with 46 additions and 47 deletions

View File

@ -33,7 +33,7 @@ import static org.springframework.security.web.csrf.CsrfTokenAssert.assertThatCs
* @author Alex Montoya
* @since 4.1
*/
public class CookieCsrfTokenRepositoryTests {
class CookieCsrfTokenRepositoryTests {
CookieCsrfTokenRepository repository;
@ -50,7 +50,7 @@ public class CookieCsrfTokenRepositoryTests {
}
@Test
public void generateToken() {
void generateToken() {
CsrfToken generateToken = this.repository.generateToken(this.request);
assertThat(generateToken).isNotNull();
assertThat(generateToken.getHeaderName()).isEqualTo(CookieCsrfTokenRepository.DEFAULT_CSRF_HEADER_NAME);
@ -59,7 +59,7 @@ public class CookieCsrfTokenRepositoryTests {
}
@Test
public void generateTokenCustom() {
void generateTokenCustom() {
String headerName = "headerName";
String parameterName = "paramName";
this.repository.setHeaderName(headerName);
@ -72,7 +72,7 @@ public class CookieCsrfTokenRepositoryTests {
}
@Test
public void saveToken() {
void saveToken() {
CsrfToken token = this.repository.generateToken(this.request);
this.repository.saveToken(token, this.request, this.response);
Cookie tokenCookie = this.response.getCookie(CookieCsrfTokenRepository.DEFAULT_CSRF_COOKIE_NAME);
@ -81,11 +81,11 @@ public class CookieCsrfTokenRepositoryTests {
assertThat(tokenCookie.getPath()).isEqualTo(this.request.getContextPath());
assertThat(tokenCookie.getSecure()).isEqualTo(this.request.isSecure());
assertThat(tokenCookie.getValue()).isEqualTo(token.getToken());
assertThat(tokenCookie.isHttpOnly()).isEqualTo(true);
assertThat(tokenCookie.isHttpOnly()).isTrue();
}
@Test
public void saveTokenSecure() {
void saveTokenSecure() {
this.request.setSecure(true);
CsrfToken token = this.repository.generateToken(this.request);
this.repository.saveToken(token, this.request, this.response);
@ -94,7 +94,7 @@ public class CookieCsrfTokenRepositoryTests {
}
@Test
public void saveTokenSecureFlagTrue() {
void saveTokenSecureFlagTrue() {
this.request.setSecure(false);
this.repository.setSecure(Boolean.TRUE);
CsrfToken token = this.repository.generateToken(this.request);
@ -146,7 +146,7 @@ public class CookieCsrfTokenRepositoryTests {
}
@Test
public void saveTokenHttpOnlyTrue() {
void saveTokenHttpOnlyTrue() {
this.repository.setCookieHttpOnly(true);
CsrfToken token = this.repository.generateToken(this.request);
this.repository.saveToken(token, this.request, this.response);
@ -191,7 +191,7 @@ public class CookieCsrfTokenRepositoryTests {
}
@Test
public void saveTokenCustomPath() {
void saveTokenCustomPath() {
String customPath = "/custompath";
this.repository.setCookiePath(customPath);
CsrfToken token = this.repository.generateToken(this.request);
@ -201,7 +201,7 @@ public class CookieCsrfTokenRepositoryTests {
}
@Test
public void saveTokenEmptyCustomPath() {
void saveTokenEmptyCustomPath() {
String customPath = "";
this.repository.setCookiePath(customPath);
CsrfToken token = this.repository.generateToken(this.request);
@ -211,7 +211,7 @@ public class CookieCsrfTokenRepositoryTests {
}
@Test
public void saveTokenNullCustomPath() {
void saveTokenNullCustomPath() {
String customPath = null;
this.repository.setCookiePath(customPath);
CsrfToken token = this.repository.generateToken(this.request);
@ -221,7 +221,7 @@ public class CookieCsrfTokenRepositoryTests {
}
@Test
public void saveTokenWithCookieDomain() {
void saveTokenWithCookieDomain() {
String domainName = "example.com";
this.repository.setCookieDomain(domainName);
CsrfToken token = this.repository.generateToken(this.request);
@ -241,7 +241,7 @@ public class CookieCsrfTokenRepositoryTests {
}
@Test
public void saveTokenWithCookieMaxAge() {
void saveTokenWithCookieMaxAge() {
int maxAge = 1200;
this.repository.setCookieMaxAge(maxAge);
CsrfToken token = this.repository.generateToken(this.request);
@ -291,24 +291,24 @@ public class CookieCsrfTokenRepositoryTests {
}
@Test
public void loadTokenNoCookiesNull() {
void loadTokenNoCookiesNull() {
assertThat(this.repository.loadToken(this.request)).isNull();
}
@Test
public void loadTokenCookieIncorrectNameNull() {
void loadTokenCookieIncorrectNameNull() {
this.request.setCookies(new Cookie("other", "name"));
assertThat(this.repository.loadToken(this.request)).isNull();
}
@Test
public void loadTokenCookieValueEmptyString() {
void loadTokenCookieValueEmptyString() {
this.request.setCookies(new Cookie(CookieCsrfTokenRepository.DEFAULT_CSRF_COOKIE_NAME, ""));
assertThat(this.repository.loadToken(this.request)).isNull();
}
@Test
public void loadToken() {
void loadToken() {
CsrfToken generateToken = this.repository.generateToken(this.request);
this.request
.setCookies(new Cookie(CookieCsrfTokenRepository.DEFAULT_CSRF_COOKIE_NAME, generateToken.getToken()));
@ -320,7 +320,7 @@ public class CookieCsrfTokenRepositoryTests {
}
@Test
public void loadTokenCustom() {
void loadTokenCustom() {
String cookieName = "cookieName";
String value = "value";
String headerName = "headerName";
@ -337,7 +337,7 @@ public class CookieCsrfTokenRepositoryTests {
}
@Test
public void loadDeferredTokenWhenDoesNotExistThenGeneratedAndSaved() {
void loadDeferredTokenWhenDoesNotExistThenGeneratedAndSaved() {
DeferredCsrfToken deferredCsrfToken = this.repository.loadDeferredToken(this.request, this.response);
CsrfToken csrfToken = deferredCsrfToken.get();
assertThat(csrfToken).isNotNull();
@ -353,7 +353,7 @@ public class CookieCsrfTokenRepositoryTests {
}
@Test
public void loadDeferredTokenWhenExistsAndNullSavedThenGeneratedAndSaved() {
void loadDeferredTokenWhenExistsAndNullSavedThenGeneratedAndSaved() {
CsrfToken generatedToken = this.repository.generateToken(this.request);
this.request
.setCookies(new Cookie(CookieCsrfTokenRepository.DEFAULT_CSRF_COOKIE_NAME, generatedToken.getToken()));
@ -366,7 +366,7 @@ public class CookieCsrfTokenRepositoryTests {
}
@Test
public void loadDeferredTokenWhenExistsAndNullSavedAndNonNullSavedThenLoaded() {
void loadDeferredTokenWhenExistsAndNullSavedAndNonNullSavedThenLoaded() {
CsrfToken generatedToken = this.repository.generateToken(this.request);
this.request
.setCookies(new Cookie(CookieCsrfTokenRepository.DEFAULT_CSRF_COOKIE_NAME, generatedToken.getToken()));
@ -379,7 +379,7 @@ public class CookieCsrfTokenRepositoryTests {
}
@Test
public void loadDeferredTokenWhenExistsThenLoaded() {
void loadDeferredTokenWhenExistsThenLoaded() {
CsrfToken generatedToken = this.repository.generateToken(this.request);
this.request
.setCookies(new Cookie(CookieCsrfTokenRepository.DEFAULT_CSRF_COOKIE_NAME, generatedToken.getToken()));
@ -412,23 +412,22 @@ public class CookieCsrfTokenRepositoryTests {
}
@Test
public void setCookieNameNullIllegalArgumentException() {
void setCookieNameNullIllegalArgumentException() {
assertThatIllegalArgumentException().isThrownBy(() -> this.repository.setCookieName(null));
}
@Test
public void setParameterNameNullIllegalArgumentException() {
void setParameterNameNullIllegalArgumentException() {
assertThatIllegalArgumentException().isThrownBy(() -> this.repository.setParameterName(null));
}
@Test
public void setHeaderNameNullIllegalArgumentException() {
void setHeaderNameNullIllegalArgumentException() {
assertThatIllegalArgumentException().isThrownBy(() -> this.repository.setHeaderName(null));
}
@Test
public void setCookieMaxAgeZeroIllegalArgumentException() {
void setCookieMaxAgeZeroIllegalArgumentException() {
assertThatIllegalArgumentException().isThrownBy(() -> this.repository.setCookieMaxAge(0));
}
}

View File

@ -39,7 +39,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Alex Montoya
* @since 5.1
*/
public class CookieServerCsrfTokenRepositoryTests {
class CookieServerCsrfTokenRepositoryTests {
private CookieServerCsrfTokenRepository csrfTokenRepository;
@ -72,54 +72,54 @@ public class CookieServerCsrfTokenRepositoryTests {
}
@Test
public void generateTokenWhenDefaultThenDefaults() {
void generateTokenWhenDefaultThenDefaults() {
generateTokenAndAssertExpectedValues();
}
@Test
public void generateTokenWhenCustomHeaderThenCustomHeader() {
void generateTokenWhenCustomHeaderThenCustomHeader() {
setExpectedHeaderName("someHeader");
generateTokenAndAssertExpectedValues();
}
@Test
public void generateTokenWhenCustomParameterThenCustomParameter() {
void generateTokenWhenCustomParameterThenCustomParameter() {
setExpectedParameterName("someParam");
generateTokenAndAssertExpectedValues();
}
@Test
public void generateTokenWhenCustomHeaderAndParameterThenCustomHeaderAndParameter() {
void generateTokenWhenCustomHeaderAndParameterThenCustomHeaderAndParameter() {
setExpectedHeaderName("someHeader");
setExpectedParameterName("someParam");
generateTokenAndAssertExpectedValues();
}
@Test
public void saveTokenWhenNoSubscriptionThenNotWritten() {
void saveTokenWhenNoSubscriptionThenNotWritten() {
MockServerWebExchange exchange = MockServerWebExchange.from(this.request);
this.csrfTokenRepository.saveToken(exchange, createToken());
assertThat(exchange.getResponse().getCookies().getFirst(this.expectedCookieName)).isNull();
}
@Test
public void saveTokenWhenDefaultThenDefaults() {
void saveTokenWhenDefaultThenDefaults() {
saveAndAssertExpectedValues(createToken());
}
@Test
public void saveTokenWhenNullThenDeletes() {
void saveTokenWhenNullThenDeletes() {
saveAndAssertExpectedValues(null);
}
@Test
public void saveTokenWhenHttpOnlyFalseThenHttpOnlyFalse() {
void saveTokenWhenHttpOnlyFalseThenHttpOnlyFalse() {
setExpectedHttpOnly(false);
saveAndAssertExpectedValues(createToken());
}
@Test
public void saveTokenWhenCookieMaxAgeThenCookieMaxAge() {
void saveTokenWhenCookieMaxAgeThenCookieMaxAge() {
setExpectedCookieMaxAge(3600);
saveAndAssertExpectedValues(createToken());
}
@ -131,7 +131,7 @@ public class CookieServerCsrfTokenRepositoryTests {
}
@Test
public void saveTokenWhenCustomPropertiesThenCustomProperties() {
void saveTokenWhenCustomPropertiesThenCustomProperties() {
setExpectedDomain("spring.io");
setExpectedCookieName("csrfCookie");
setExpectedPath("/some/path");
@ -188,7 +188,7 @@ public class CookieServerCsrfTokenRepositoryTests {
}
@Test
public void saveTokenWhenSslInfoNullThenNotSecure() {
void saveTokenWhenSslInfoNullThenNotSecure() {
MockServerWebExchange exchange = MockServerWebExchange.from(this.request);
this.csrfTokenRepository.saveToken(exchange, createToken()).block();
ResponseCookie cookie = exchange.getResponse().getCookies().getFirst(this.expectedCookieName);
@ -197,7 +197,7 @@ public class CookieServerCsrfTokenRepositoryTests {
}
@Test
public void saveTokenWhenSecureFlagTrueThenSecure() {
void saveTokenWhenSecureFlagTrueThenSecure() {
MockServerWebExchange exchange = MockServerWebExchange.from(this.request);
this.csrfTokenRepository.setSecure(true);
this.csrfTokenRepository.saveToken(exchange, createToken()).block();
@ -217,7 +217,7 @@ public class CookieServerCsrfTokenRepositoryTests {
}
@Test
public void saveTokenWhenSecureFlagFalseThenNotSecure() {
void saveTokenWhenSecureFlagFalseThenNotSecure() {
MockServerWebExchange exchange = MockServerWebExchange.from(this.request);
this.csrfTokenRepository.setSecure(false);
this.csrfTokenRepository.saveToken(exchange, createToken()).block();
@ -237,7 +237,7 @@ public class CookieServerCsrfTokenRepositoryTests {
}
@Test
public void saveTokenWhenSecureFlagFalseAndSslInfoThenNotSecure() {
void saveTokenWhenSecureFlagFalseAndSslInfoThenNotSecure() {
MockServerWebExchange exchange = MockServerWebExchange.from(this.request);
this.request.sslInfo(new MockSslInfo());
this.csrfTokenRepository.setSecure(false);
@ -259,12 +259,12 @@ public class CookieServerCsrfTokenRepositoryTests {
}
@Test
public void loadTokenWhenCookieExistThenTokenFound() {
void loadTokenWhenCookieExistThenTokenFound() {
loadAndAssertExpectedValues();
}
@Test
public void loadTokenWhenCustomThenTokenFound() {
void loadTokenWhenCustomThenTokenFound() {
setExpectedParameterName("paramName");
setExpectedHeaderName("headerName");
setExpectedCookieName("csrfCookie");
@ -272,20 +272,20 @@ public class CookieServerCsrfTokenRepositoryTests {
}
@Test
public void loadTokenWhenNoCookiesThenNullToken() {
void loadTokenWhenNoCookiesThenNullToken() {
MockServerWebExchange exchange = MockServerWebExchange.from(this.request);
CsrfToken csrfToken = this.csrfTokenRepository.loadToken(exchange).block();
assertThat(csrfToken).isNull();
}
@Test
public void loadTokenWhenCookieExistsWithNoValue() {
void loadTokenWhenCookieExistsWithNoValue() {
setExpectedCookieValue("");
loadAndAssertExpectedValues();
}
@Test
public void loadTokenWhenCookieExistsWithNullValue() {
void loadTokenWhenCookieExistsWithNullValue() {
setExpectedCookieValue(null);
loadAndAssertExpectedValues();
}