diff --git a/core/src/main/java/kst4contest/controller/LoginConfiguration.java b/core/src/main/java/kst4contest/controller/LoginConfiguration.java new file mode 100644 index 00000000..bd8483ca --- /dev/null +++ b/core/src/main/java/kst4contest/controller/LoginConfiguration.java @@ -0,0 +1,53 @@ +package kst4contest.controller; + +import kst4contest.model.ChatPreferences; + +/** + * Tells whether a profile has login credentials an operator entered. + * + *
Two states count as unconfigured. An empty callsign is what every created or duplicated + * profile starts with, because {@link OperatorProfileManagementService} clears the + * credentials on purpose. The bundled template's credentials are what the root profile of a + * fresh installation carries. Neither can log in as the operator, so neither needs a flag of + * its own in {@code preferences.xml}.
+ */ +public final class LoginConfiguration { + + /** Callsign shipped in {@code praktiKSTpreferences.xml}; held equal by a test. */ + public static final String TEMPLATE_CALLSIGN = "DO5AMF"; + + /** Password shipped in {@code praktiKSTpreferences.xml}; held equal by a test. */ + public static final String TEMPLATE_PASSWORD = "changeMe"; + + private LoginConfiguration() { + // Utility class. + } + + /** + * Whether the preferences still hold the bundled template's credentials. + * + *Only the pair counts. The callsign alone belongs to a real operator.
+ * + * @param preferences preferences of the profile to judge + * @return true for an untouched template + */ + public static boolean carriesTemplateCredentials(final ChatPreferences preferences) { + return TEMPLATE_CALLSIGN.equalsIgnoreCase(trimmed(preferences.getStn_loginCallSign())) + && TEMPLATE_PASSWORD.equals(preferences.getStn_loginPassword()); + } + + /** + * Whether the profile has no login an operator entered. + * + * @param preferences preferences of the profile to judge + * @return true when the callsign is empty or the template's credentials are still there + */ + public static boolean isUnconfigured(final ChatPreferences preferences) { + return trimmed(preferences.getStn_loginCallSign()).isEmpty() + || carriesTemplateCredentials(preferences); + } + + private static String trimmed(final String value) { + return value == null ? "" : value.trim(); + } +} diff --git a/core/src/test/java/kst4contest/controller/LoginConfigurationTest.java b/core/src/test/java/kst4contest/controller/LoginConfigurationTest.java new file mode 100644 index 00000000..afbca651 --- /dev/null +++ b/core/src/test/java/kst4contest/controller/LoginConfigurationTest.java @@ -0,0 +1,79 @@ +package kst4contest.controller; + +import kst4contest.model.ChatPreferences; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * When a profile counts as not set up yet. The setup wizard appears on exactly this + * condition, so a wrong answer either nags a working station or leaves a newcomer alone + * with the template's callsign. + */ +class LoginConfigurationTest { + + @TempDir + Path temporaryDirectory; + + private ChatPreferences preferences(String callsign, String password) { + ChatPreferences preferences = new ChatPreferences(temporaryDirectory.resolve("unused.xml")); + preferences.setStn_loginCallSign(callsign); + preferences.setStn_loginPassword(password); + return preferences; + } + + @Test + void anEmptyCallsignIsUnconfigured() { + assertTrue(LoginConfiguration.isUnconfigured(preferences("", "secret"))); + assertTrue(LoginConfiguration.isUnconfigured(preferences(" ", "secret"))); + } + + @Test + void theTemplateCredentialsAreUnconfigured() { + ChatPreferences template = preferences("DO5AMF", "changeMe"); + + assertTrue(LoginConfiguration.carriesTemplateCredentials(template)); + assertTrue(LoginConfiguration.isUnconfigured(template)); + } + + @Test + void theTemplateCallsignWithItsOwnPasswordIsConfigured() { + // The callsign belongs to a real operator; only the pair marks an untouched template. + ChatPreferences owner = preferences("DO5AMF", "a-real-password"); + + assertFalse(LoginConfiguration.carriesTemplateCredentials(owner)); + assertFalse(LoginConfiguration.isUnconfigured(owner)); + } + + @Test + void aRealLoginIsConfigured() { + assertFalse(LoginConfiguration.isUnconfigured(preferences("DL0TEST", "secret"))); + assertFalse(LoginConfiguration.isUnconfigured(preferences("DL0TEST/P", ""))); + } + + @Test + void theConstantsMatchTheBundledTemplate() throws IOException { + Path templateCopy = temporaryDirectory.resolve("template.xml"); + try (InputStream template = ChatPreferences.class + .getResourceAsStream(ChatPreferences.PREFERENCE_RESOURCE)) { + assertNotNull(template, "bundled resource " + ChatPreferences.PREFERENCE_RESOURCE); + Files.copy(template, templateCopy, StandardCopyOption.REPLACE_EXISTING); + } + + ChatPreferences fromTemplate = new ChatPreferences(templateCopy); + assertTrue(fromTemplate.readPreferencesFromXmlFile()); + + assertTrue(LoginConfiguration.carriesTemplateCredentials(fromTemplate), + "the template's credentials changed; a fresh installation would no longer " + + "be recognised as unconfigured"); + } +}