from app_new.yeastar.models import YeastarConfig


def test_admin_can_save_encrypted_yeastar_credentials(app, authenticated_client):
    response = authenticated_client.post(
        '/yeastar/config',
        data={
            'base_url': 'https://pbx.example.test',
            'username': 'maintenance',
            'password': 'a-strong-test-password',
        },
    )
    assert response.status_code == 302

    with app.app_context():
        config = YeastarConfig.get_config()
        assert config.base_url == 'https://pbx.example.test'
        assert config.get_username() == 'maintenance'
        assert config.get_password() == 'a-strong-test-password'
        assert b'maintenance' not in config.username_encrypted
        assert b'a-strong-test-password' not in config.password_encrypted


def test_yeastar_url_requires_http_scheme(app, authenticated_client):
    response = authenticated_client.post(
        '/yeastar/config',
        data={
            'base_url': 'pbx.example.test',
            'username': 'maintenance',
            'password': 'a-strong-test-password',
        },
        follow_redirects=True,
    )
    assert response.status_code == 200
    assert b'doit commencer par https:// ou http://' in response.data
