mirror of
https://github.com/fabioformosa/quartz-manager.git
synced 2025-12-28 05:03:15 +09:00
#77 added a validation test on the in-memory user props and renamed the username field
This commit is contained in:
@@ -6,7 +6,7 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Slf4j
|
||||
@ConditionalOnClass(name = {"it.fabioformosa.quartzmanager.security.WebSecurityConfigJWT"})
|
||||
@ConditionalOnClass(name = {"it.fabioformosa.quartzmanager.api.security.QuartzManagerSecurityConfig"})
|
||||
@Configuration
|
||||
public class SecurityDiscoverConfig {
|
||||
|
||||
|
||||
@@ -4,21 +4,34 @@ import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Validated
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "quartz-manager.security.accounts.in-memory")
|
||||
@Getter @Setter
|
||||
public class InMemoryAccountProperties {
|
||||
private boolean enabled;
|
||||
private boolean enabled = true;
|
||||
|
||||
@Valid
|
||||
@NotNull
|
||||
@NotEmpty
|
||||
private List<User> users;
|
||||
|
||||
@Getter @Setter
|
||||
public static class User {
|
||||
private String name;
|
||||
@NotBlank
|
||||
private String username;
|
||||
@NotBlank
|
||||
private String password;
|
||||
@NotEmpty
|
||||
private List<String> roles = new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||
"quartz-manager.security.jwt.cookie-strategy.enabled=true",
|
||||
"quartz-manager.security.jwt.cookie-strategy.cookie=AUTH-TOKEN",
|
||||
"quartz-manager.security.accounts.in-memory.enabled=true",
|
||||
"quartz-manager.security.accounts.in-memory.users[0].name=foo",
|
||||
"quartz-manager.security.accounts.in-memory.users[0].username=foo",
|
||||
"quartz-manager.security.accounts.in-memory.users[0].password=bar",
|
||||
"quartz-manager.security.accounts.in-memory.users[0].roles[0]=admin",
|
||||
})
|
||||
|
||||
@@ -21,7 +21,7 @@ import org.springframework.test.context.TestPropertySource;
|
||||
"quartz-manager.security.jwt.cookie-strategy.enabled=true",
|
||||
"quartz-manager.security.jwt.cookie-strategy.cookie=AUTH-TOKEN",
|
||||
"quartz-manager.security.accounts.in-memory.enabled=true",
|
||||
"quartz-manager.security.accounts.in-memory.users[0].name=foo",
|
||||
"quartz-manager.security.accounts.in-memory.users[0].username=foo",
|
||||
"quartz-manager.security.accounts.in-memory.users[0].password=bar",
|
||||
"quartz-manager.security.accounts.in-memory.users[0].roles[0]=admin",
|
||||
})
|
||||
|
||||
@@ -12,7 +12,7 @@ import org.springframework.test.context.TestPropertySource;
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@TestPropertySource(properties = {
|
||||
"quartz-manager.security.accounts.in-memory.enabled=true",
|
||||
"quartz-manager.security.accounts.in-memory.users[0].name=foo",
|
||||
"quartz-manager.security.accounts.in-memory.users[0].username=foo",
|
||||
"quartz-manager.security.accounts.in-memory.users[0].password=bar",
|
||||
"quartz-manager.security.accounts.in-memory.users[0].roles[0]=admin",
|
||||
})
|
||||
|
||||
@@ -20,7 +20,7 @@ import org.springframework.test.context.TestPropertySource;
|
||||
"quartz-manager.security.jwt.header-strategy.header=Authorization",
|
||||
"quartz-manager.security.jwt.cookie-strategy.enabled=false",
|
||||
"quartz-manager.security.accounts.in-memory.enabled=true",
|
||||
"quartz-manager.security.accounts.in-memory.users[0].name=foo",
|
||||
"quartz-manager.security.accounts.in-memory.users[0].username=foo",
|
||||
"quartz-manager.security.accounts.in-memory.users[0].password=bar",
|
||||
"quartz-manager.security.accounts.in-memory.users[0].roles[0]=admin",
|
||||
})
|
||||
|
||||
@@ -19,7 +19,7 @@ import org.springframework.test.context.TestPropertySource;
|
||||
"quartz-manager.security.jwt.header-strategy.header=Authorization",
|
||||
"quartz-manager.security.jwt.cookie-strategy.enabled=false",
|
||||
"quartz-manager.security.accounts.in-memory.enabled=true",
|
||||
"quartz-manager.security.accounts.in-memory.users[0].name=foo",
|
||||
"quartz-manager.security.accounts.in-memory.users[0].username=foo",
|
||||
"quartz-manager.security.accounts.in-memory.users[0].password=bar",
|
||||
"quartz-manager.security.accounts.in-memory.users[0].roles[0]=admin",
|
||||
})
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
package it.fabioformosa.quartzmanager.api.security.properties;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.springframework.boot.context.properties.bind.BindResult;
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
|
||||
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource;
|
||||
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class InMemoryUsersValidationControllerTest {
|
||||
|
||||
private static Validator propertyValidator;
|
||||
|
||||
static Stream<Arguments> notValidInMemoryProps = Stream.of(
|
||||
Arguments.of(
|
||||
Map.of("quartz-manager.security.accounts.in-memory.users[0].password", "bar"),
|
||||
Map.of("quartz-manager.security.accounts.in-memory.users[0].roles[0]", "admin")),
|
||||
Arguments.of(
|
||||
Map.of("quartz-manager.security.accounts.in-memory.users[0].username", "foo"),
|
||||
Map.of("quartz-manager.security.accounts.in-memory.users[0].roles[0]", "admin")),
|
||||
Arguments.of(
|
||||
Map.of("quartz-manager.security.accounts.in-memory.users[0].username", "foo"),
|
||||
Map.of("quartz-manager.security.accounts.in-memory.users[0].password", "bar"))
|
||||
);
|
||||
|
||||
|
||||
@BeforeAll
|
||||
public static void setup() {
|
||||
propertyValidator = Validation.buildDefaultValidatorFactory().getValidator();
|
||||
}
|
||||
|
||||
static Stream<Arguments> getNotValidInMemoryProps(){
|
||||
return notValidInMemoryProps;
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("it.fabioformosa.quartzmanager.api.security.properties.InMemoryUsersValidationControllerTest#getNotValidInMemoryProps")
|
||||
void givenAMissingUsername_whenThePropertyValidationIsApplied_thenShouldRaiseValidationError(Map<String, String> properties) throws Exception {
|
||||
ConfigurationPropertySource source = new MapConfigurationPropertySource(properties);
|
||||
|
||||
Binder binder = new Binder(source);
|
||||
BindResult<InMemoryAccountProperties> result = binder.bind("quartz-manager.security.accounts.in-memory", InMemoryAccountProperties.class);
|
||||
|
||||
Assertions.assertThat(result.isBound()).isTrue();
|
||||
|
||||
InMemoryAccountProperties inMemoryAccountProperties = result.get();
|
||||
Assertions.assertThat(propertyValidator.validate(inMemoryAccountProperties).size()).isGreaterThan(0);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenAllInMemoryPropsAreSet_whenThePropertyValidationIsApplied_thenShouldRaiseValidationError() throws Exception {
|
||||
Map<String, String> properties = new HashMap<>();
|
||||
properties.put("quartz-manager.security.accounts.in-memory.users[0].username", "foo");
|
||||
properties.put("quartz-manager.security.accounts.in-memory.users[0].password", "bar");
|
||||
properties.put("quartz-manager.security.accounts.in-memory.users[0].roles[0]", "admin");
|
||||
|
||||
ConfigurationPropertySource source = new MapConfigurationPropertySource(properties);
|
||||
|
||||
Binder binder = new Binder(source);
|
||||
BindResult<InMemoryAccountProperties> result = binder.bind("quartz-manager.security.accounts.in-memory", InMemoryAccountProperties.class);
|
||||
|
||||
Assertions.assertThat(result.isBound()).isTrue();
|
||||
|
||||
InMemoryAccountProperties inMemoryAccountProperties = result.get();
|
||||
Assertions.assertThat(propertyValidator.validate(inMemoryAccountProperties).size()).isEqualTo(0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,7 +8,7 @@ quartz-manager:
|
||||
in-memory:
|
||||
enabled: true
|
||||
users:
|
||||
- name: admin
|
||||
- username: admin
|
||||
password: admin
|
||||
roles:
|
||||
- ADMIN
|
||||
|
||||
Reference in New Issue
Block a user