Skip to content

Commit

Permalink
Add tests to ensure that the properties are same in the config and co…
Browse files Browse the repository at this point in the history
…re config file
  • Loading branch information
prateek3255 committed Feb 21, 2024
1 parent 366296e commit 58abb6e
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 4 deletions.
4 changes: 2 additions & 2 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ core_config_version: 0
# password_reset_token_lifetime:


# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: 86400000 (1 day)) long value. Time in milliseconds for how long an
# (DIFFERENT_ACROSS_TENANTS | OPTIONAL | Default: 86400000 (1 day)) long value. Time in milliseconds for how long an
# email verification token / link is valid for.
# email_verification_token_lifetime:

Expand Down Expand Up @@ -139,7 +139,7 @@ core_config_version: 0

# (OPTIONAL | Default: null). This is used when deploying the core in SuperTokens SaaS infrastructure. If set, limits
# what database information is shown to / modifiable by the dev when they query the core to get the information about
# their tenants. It only exposes that information when this key is used instead of the regular api_keys config.
# their tenants. It only exposes that information when this key is used instead of the regular api_keys config.
# supertokens_saas_secret:

# (DIFFERENT_ACROSS_APPS | OPTIONAL | Default: null). This is used when the core needs to assume a specific CDI version
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/supertokens/config/CoreConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ public class CoreConfig {

@ConfigYamlOnly
@JsonProperty
@ConfigDescription("The port on which the SuperTokens service will run. (Default: 3567)")
@ConfigDescription("The port at which SuperTokens service runs. (Default: 3567)")
private int port = 3567;

@ConfigYamlOnly
@JsonProperty
@ConfigDescription("The host on which the SuperTokens service will run. Values here can be localhost, example.com, 0.0.0.0 or any IP address associated with your machine. (Default: localhost)")
@ConfigDescription("The host on which SuperTokens service runs. Values here can be localhost, example.com, 0.0.0.0 or any IP address associated with your machine. (Default: localhost)")
private String host = "localhost";

@ConfigYamlOnly
Expand Down
90 changes: 90 additions & 0 deletions src/test/java/io/supertokens/test/CoreConfigListAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

package io.supertokens.test;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import io.supertokens.ProcessState;
import io.supertokens.config.CoreConfig;
import io.supertokens.config.annotations.ConfigDescription;
import io.supertokens.httpRequest.HttpRequest;
import org.junit.AfterClass;
import org.junit.Before;
Expand All @@ -28,6 +31,14 @@

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.FileReader;
import java.io.BufferedReader;
import java.lang.reflect.Field;

public class CoreConfigListAPITest {
@Rule
public TestRule watchman = Utils.getOnFailure();
Expand Down Expand Up @@ -59,6 +70,8 @@ public void testRetreivingConfigProperties() throws Exception {
for (int i = 0; i < result.size(); i++) {
JsonObject config = result.get(i).getAsJsonObject();
assertTrue(config.get("name").getAsJsonPrimitive().isString());
// Ensure that the name is not present in the protected configs
assertTrue(!Arrays.asList(CoreConfig.PROTECTED_CONFIGS).contains(config.get("name").getAsString()));
assertTrue(config.get("description").getAsJsonPrimitive().isString());
assertTrue(config.get("isDifferentAcrossTenants").getAsJsonPrimitive().isBoolean());
assertTrue(config.get("type").getAsJsonPrimitive().isString());
Expand All @@ -70,4 +83,81 @@ public void testRetreivingConfigProperties() throws Exception {
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
}

@Test
public void testMatchConfigPropertiesDescription() throws Exception {
String[] args = { "../" };

TestingProcessManager.TestingProcess process = TestingProcessManager.start(args);
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

try (BufferedReader reader = new BufferedReader(new FileReader("./config.yaml"))) {
// Get the content of the file as string
String content = reader.lines().collect(Collectors.joining(System.lineSeparator()));
// Find the line that contains 'core_config_version', and then split
// the file after that line
String allProperties = content.split("core_config_version:\\s*\\d+\n\n")[1];

// Split by all the other allProperties string by new line
String[] properties = allProperties.split("\n\n");
// This will contain the description of each property from the yaml file
Map<String, String> propertyDescriptions = new HashMap<String, String>();

for (int i = 0; i < properties.length; i++) {
String possibleProperty = properties[i].trim();
String[] lines = possibleProperty.split("\n");
// This ensures that it is a property with a description as a comment
// at the top
if (lines[lines.length - 1].endsWith(":")) {
String propertyKeyString = lines[lines.length - 1];
// Remove the comment "# " from the start
String propertyKey = propertyKeyString.substring(2, propertyKeyString.length() - 1);
String propertyDescription = "";
// Remove the comment "# " from the start and merge all the lines to form the
// description
for (int j = 0; j < lines.length - 1; j++) {
propertyDescription = propertyDescription + " " + lines[j].substring(2);
}
propertyDescription = propertyDescription.trim();

propertyDescriptions.put(propertyKey, propertyDescription);
}
}

for (String fieldId : CoreConfig.getValidFields()) {
// access_token_signing_key_update_interval is an alias for
// access_token_dynamic_signing_key_update_interval,
// we don't have a description for core_config_version
// and webserver_https_enabled is not present in the config.yaml file
// so we skip these properties.
if (fieldId.equals("access_token_signing_key_update_interval")
|| fieldId.equals("core_config_version")
|| fieldId.equals("webserver_https_enabled")) {
continue;
}

Field field = CoreConfig.class.getDeclaredField(fieldId);

// Skip fields that are not annotated with JsonProperty
if (!field.isAnnotationPresent(JsonProperty.class)) {
continue;
}

String descriptionInConfig = field.getAnnotation(ConfigDescription.class).value();
String descriptionInYaml = propertyDescriptions.get(fieldId);
// Remove the default value from config, since we add default value at the end
// config description
descriptionInConfig = descriptionInConfig.replaceAll("\\s\\[Default:.*|\\s\\(Default:.*", "").trim();
// Remove period from end if present, since not all descriptions in
// config.yaml have that
descriptionInConfig = descriptionInConfig.replaceAll("\\.$", "").trim();

// Assert that description in yaml contains the description in config
assertTrue(descriptionInYaml.contains(descriptionInConfig));
}
}

process.kill();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
}

}

0 comments on commit 58abb6e

Please sign in to comment.