package com.writeoff.security;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class PasswordCodecServiceTest {

    private final PasswordCodecService passwordCodecService = new PasswordCodecService();

    @Test
    void shouldEncodePasswordWithSaltAndMatch() {
        String rawPassword = "Abcd1234!";

        String first = passwordCodecService.encode(rawPassword);
        String second = passwordCodecService.encode(rawPassword);

        assertTrue(passwordCodecService.isEncoded(first));
        assertTrue(passwordCodecService.matches(rawPassword, first));
        assertTrue(passwordCodecService.matches(rawPassword, second));
        assertNotEquals(first, second);
        assertFalse(passwordCodecService.matches("wrong-password", first));
    }

    @Test
    void shouldRemainCompatibleWithLegacyPlaintextPassword() {
        assertTrue(passwordCodecService.matches("123456", "123456"));
        assertFalse(passwordCodecService.isEncoded("123456"));
    }
}
