package com.cameleer.server.app.security;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

/**
 * Unit tests for {@link BootstrapTokenValidator}.
 * No Spring context needed — implementation constructed directly.
 */
class BootstrapTokenValidatorTest {

    @Test
    void validate_correctToken_returnsTrue() {
        SecurityProperties props = new SecurityProperties();
        props.setBootstrapToken("my-secret-token");
        BootstrapTokenValidator validator = new BootstrapTokenValidator(props);

        assertTrue(validator.validate("my-secret-token"));
    }

    @Test
    void validate_wrongToken_returnsFalse() {
        SecurityProperties props = new SecurityProperties();
        props.setBootstrapToken("my-secret-token");
        BootstrapTokenValidator validator = new BootstrapTokenValidator(props);

        assertFalse(validator.validate("wrong-token"));
    }

    @Test
    void validate_previousToken_returnsTrueWhenSet() {
        SecurityProperties props = new SecurityProperties();
        props.setBootstrapToken("new-token");
        props.setBootstrapTokenPrevious("old-token");
        BootstrapTokenValidator validator = new BootstrapTokenValidator(props);

        assertTrue(validator.validate("old-token"), "Previous token should be accepted during rotation");
    }

    @Test
    void validate_nullToken_returnsFalse() {
        SecurityProperties props = new SecurityProperties();
        props.setBootstrapToken("my-secret-token");
        BootstrapTokenValidator validator = new BootstrapTokenValidator(props);

        assertFalse(validator.validate(null));
    }

    @Test
    void validate_blankToken_returnsFalse() {
        SecurityProperties props = new SecurityProperties();
        props.setBootstrapToken("my-secret-token");
        BootstrapTokenValidator validator = new BootstrapTokenValidator(props);

        assertFalse(validator.validate(""));
        assertFalse(validator.validate("   "));
    }

    @Test
    void validate_previousTokenNotSet_onlyCurrentAccepted() {
        SecurityProperties props = new SecurityProperties();
        props.setBootstrapToken("current-token");
        // bootstrapTokenPrevious is null by default
        BootstrapTokenValidator validator = new BootstrapTokenValidator(props);

        assertTrue(validator.validate("current-token"));
        assertFalse(validator.validate("some-old-token"));
    }
}
