Implement MessageSourceAware where missing

Closes gh-8951
This commit is contained in:
Arnaud Mergey
2020-10-26 18:48:59 +01:00
committed by Josh Cummings
parent 61550f8a48
commit 2b9efccc50
8 changed files with 132 additions and 16 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2004-2016 the original author or authors.
* Copyright 2004-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -29,6 +29,7 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
@@ -50,8 +51,10 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.willThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
/**
@@ -284,6 +287,22 @@ public class ExceptionTranslationFilterTests {
verifyZeroInteractions(this.mockEntryPoint);
}
@Test
public void setMessageSourceWhenNullThenThrowsException() {
ExceptionTranslationFilter filter = new ExceptionTranslationFilter(this.mockEntryPoint);
assertThatIllegalArgumentException().isThrownBy(() -> filter.setMessageSource(null));
}
@Test
public void setMessageSourceWhenNotNullThenCanGet() {
MessageSource source = mock(MessageSource.class);
ExceptionTranslationFilter filter = new ExceptionTranslationFilter(this.mockEntryPoint);
filter.setMessageSource(source);
String code = "code";
filter.messages.getMessage(code);
verify(source).getMessage(eq(code), any(), any());
}
private AuthenticationEntryPoint mockEntryPoint = (request, response, authException) -> response
.sendRedirect(request.getContextPath() + "/login.jsp");

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,12 +19,16 @@ package org.springframework.security.web.authentication.preauth.x509;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.MessageSource;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.SpringSecurityMessageSource;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* @author Luke Taylor
@@ -36,7 +40,6 @@ public class SubjectDnX509PrincipalExtractorTests {
@Before
public void setUp() {
this.extractor = new SubjectDnX509PrincipalExtractor();
this.extractor.setMessageSource(new SpringSecurityMessageSource());
}
@Test
@@ -71,4 +74,18 @@ public class SubjectDnX509PrincipalExtractorTests {
assertThat(principal).isEqualTo("Duke");
}
@Test
public void setMessageSourceWhenNullThenThrowsException() {
assertThatIllegalArgumentException().isThrownBy(() -> this.extractor.setMessageSource(null));
}
@Test
public void setMessageSourceWhenNotNullThenCanGet() {
MessageSource source = mock(MessageSource.class);
this.extractor.setMessageSource(source);
String code = "code";
this.extractor.messages.getMessage(code);
verify(source).getMessage(eq(code), any(), any());
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@ import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareOnlyThisForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.context.MessageSource;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.security.authentication.AccountStatusUserDetailsChecker;
@@ -44,6 +45,11 @@ import org.springframework.util.StringUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* @author Luke Taylor
@@ -411,6 +417,22 @@ public class AbstractRememberMeServicesTests {
assertThat(cookie.getDomain()).isEqualTo("spring.io");
}
@Test
public void setMessageSourceWhenNullThenThrowsException() {
MockRememberMeServices services = new MockRememberMeServices();
assertThatIllegalArgumentException().isThrownBy(() -> services.setMessageSource(null));
}
@Test
public void setMessageSourceWhenNotNullThenCanGet() {
MessageSource source = mock(MessageSource.class);
MockRememberMeServices services = new MockRememberMeServices();
services.setMessageSource(source);
String code = "code";
services.messages.getMessage(code);
verify(source).getMessage(eq(code), any(), any());
}
private Cookie[] createLoginCookie(String cookieToken) {
MockRememberMeServices services = new MockRememberMeServices(this.uds);
Cookie cookie = new Cookie(AbstractRememberMeServices.SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY,