import pytest

from splent_framework.db import db
from splent_io.splent_feature_auth.models import User
from splent_io.splent_feature_profile.models import UserProfile
from splent_framework.helpers.test_helpers_auth import login, logout


@pytest.fixture(scope="function")
def test_client(test_client):
    """
    Extends the test_client fixture to add additional specific data for module testing.
    for module testing (por example, new users)
    """
    with test_client.application.app_context():
        user_test = User(email="user@example.com", password="test1234")
        db.session.add(user_test)
        db.session.commit()

        profile = UserProfile(
            user_id=user_test.id, name="Name", surname="Surname"
        )
        db.session.add(profile)
        db.session.commit()

    yield test_client


def test_edit_profile_page_get(test_client):
    """
    Tests access to the profile editing page via a GET request.
    """
    login_response = login(test_client, "user@example.com", "test1234")
    assert login_response.status_code == 200, "Login was unsuccessful."

    response = test_client.get("/profile/edit")
    assert (
        response.status_code == 200
    ), "The profile editing page could not be accessed."
    assert (
        b"Edit profile" in response.data
    ), "The expected content is not present on the page"

    logout(test_client)
