package tech.easyflow.auth.config;

import org.junit.Assert;
import org.junit.Test;

public class DevLoginGuardTest {

    @Test
    public void shouldAcceptConfiguredAdminAccount() {
        DevLoginGuard guard = new DevLoginGuard(createProperties());
        Assert.assertTrue(guard.isAllowedAccount("admin"));
        Assert.assertFalse(guard.isAllowedAccount("guest"));
        Assert.assertFalse(guard.isAllowedAccount(null));
    }

    @Test
    public void shouldRecognizeLoopbackAddresses() {
        DevLoginGuard guard = new DevLoginGuard(createProperties());
        Assert.assertTrue(guard.isLoopbackAddress("127.0.0.1"));
        Assert.assertTrue(guard.isLoopbackAddress("::1"));
        Assert.assertFalse(guard.isLoopbackAddress("192.168.1.10"));
        Assert.assertFalse(guard.isLoopbackAddress("not-an-ip"));
    }

    private LoginProperties createProperties() {
        LoginProperties properties = new LoginProperties();
        LoginProperties.DevBypassProperties devBypass = new LoginProperties.DevBypassProperties();
        devBypass.setEnabled(true);
        devBypass.setAccount("admin");
        devBypass.setLoopbackOnly(true);
        properties.setDevBypass(devBypass);
        return properties;
    }
}
