mirror of
https://github.com/imgfloat/server.git
synced 2026-02-05 03:39:26 +00:00
Add env test
This commit is contained in:
@@ -0,0 +1,31 @@
|
|||||||
|
package com.imgfloat.app.config;
|
||||||
|
|
||||||
|
import jakarta.annotation.PostConstruct;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class TwitchCredentialsValidator {
|
||||||
|
private final String clientId;
|
||||||
|
private final String clientSecret;
|
||||||
|
|
||||||
|
public TwitchCredentialsValidator(
|
||||||
|
@Value("${spring.security.oauth2.client.registration.twitch.client-id}") String clientId,
|
||||||
|
@Value("${spring.security.oauth2.client.registration.twitch.client-secret}") String clientSecret) {
|
||||||
|
this.clientId = clientId;
|
||||||
|
this.clientSecret = clientSecret;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
void validate() {
|
||||||
|
ensurePresent(clientId, "TWITCH_CLIENT_ID");
|
||||||
|
ensurePresent(clientSecret, "TWITCH_CLIENT_SECRET");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ensurePresent(String value, String name) {
|
||||||
|
if (!StringUtils.hasText(value) || "changeme".equalsIgnoreCase(value.trim())) {
|
||||||
|
throw new IllegalStateException(name + " must be set in the environment or .env file");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,6 +7,8 @@ server:
|
|||||||
key-store-type: ${SSL_KEYSTORE_TYPE:PKCS12}
|
key-store-type: ${SSL_KEYSTORE_TYPE:PKCS12}
|
||||||
|
|
||||||
spring:
|
spring:
|
||||||
|
config:
|
||||||
|
import: optional:file:.env[.properties]
|
||||||
application:
|
application:
|
||||||
name: imgfloat
|
name: imgfloat
|
||||||
thymeleaf:
|
thymeleaf:
|
||||||
@@ -16,8 +18,8 @@ spring:
|
|||||||
client:
|
client:
|
||||||
registration:
|
registration:
|
||||||
twitch:
|
twitch:
|
||||||
client-id: ${TWITCH_CLIENT_ID:changeme}
|
client-id: ${TWITCH_CLIENT_ID}
|
||||||
client-secret: ${TWITCH_CLIENT_SECRET:changeme}
|
client-secret: ${TWITCH_CLIENT_SECRET}
|
||||||
redirect-uri: "{baseUrl}/login/oauth2/code/twitch"
|
redirect-uri: "{baseUrl}/login/oauth2/code/twitch"
|
||||||
authorization-grant-type: authorization_code
|
authorization-grant-type: authorization_code
|
||||||
scope: ["user:read:email"]
|
scope: ["user:read:email"]
|
||||||
|
|||||||
@@ -19,7 +19,10 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
|
|||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest(properties = {
|
||||||
|
"spring.security.oauth2.client.registration.twitch.client-id=test-client-id",
|
||||||
|
"spring.security.oauth2.client.registration.twitch.client-secret=test-client-secret"
|
||||||
|
})
|
||||||
@AutoConfigureMockMvc
|
@AutoConfigureMockMvc
|
||||||
class ChannelApiIntegrationTest {
|
class ChannelApiIntegrationTest {
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package com.imgfloat.app;
|
||||||
|
|
||||||
|
import com.imgfloat.app.config.TwitchCredentialsValidator;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||||
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
|
|
||||||
|
class TwitchEnvironmentValidationTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void failsToStartWhenTwitchCredentialsMissing() {
|
||||||
|
assertThatThrownBy(() -> new SpringApplicationBuilder(ImgfloatApplication.class)
|
||||||
|
.properties("server.port=0")
|
||||||
|
.run())
|
||||||
|
.hasRootCauseInstanceOf(IllegalArgumentException.class)
|
||||||
|
.hasRootCauseMessage("Could not resolve placeholder 'TWITCH_CLIENT_ID' in value \"${TWITCH_CLIENT_ID}\"");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void loadsCredentialsFromDotEnvFile() {
|
||||||
|
ConfigurableApplicationContext context = null;
|
||||||
|
try {
|
||||||
|
context = new SpringApplicationBuilder(ImgfloatApplication.class)
|
||||||
|
.properties(
|
||||||
|
"server.port=0",
|
||||||
|
"spring.config.import=optional:file:src/test/resources/valid.env[.properties]")
|
||||||
|
.run();
|
||||||
|
ConfigurableApplicationContext finalContext = context;
|
||||||
|
assertThatCode(() -> finalContext.getBean(TwitchCredentialsValidator.class))
|
||||||
|
.doesNotThrowAnyException();
|
||||||
|
} finally {
|
||||||
|
if (context != null) {
|
||||||
|
context.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
2
src/test/resources/valid.env
Normal file
2
src/test/resources/valid.env
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
TWITCH_CLIENT_ID=test-client-id
|
||||||
|
TWITCH_CLIENT_SECRET=test-client-secret
|
||||||
Reference in New Issue
Block a user