from __future__ import annotations import os import pytest from flask_security import hash_password from flask_security import SQLAlchemySessionUserDatastore from QuizTheWord.database import AllQuestions from QuizTheWord.database import destroy_db from QuizTheWord.database import get_session from QuizTheWord.database import HiddenAnswer from QuizTheWord.database import init_db from QuizTheWord.database import MultipleChoice from QuizTheWord.database import Role from QuizTheWord.database import User @pytest.fixture(scope="module") def client(): os.environ["CONFIGURATION_SETUP"] = "QuizTheWord.config.Testing" from QuizTheWord import app with app.app.test_client() as client: with app.app.app_context(): yield client @pytest.fixture(scope="module") def session(): return get_session() @pytest.fixture(scope="module") def init_database(session): init_db() user_datastore = SQLAlchemySessionUserDatastore(session, User, Role) admin_role = user_datastore.create_role(name="admin") basic_user = user_datastore.create_user(email="basic@test.com", password=hash_password("password")) admin_user = user_datastore.create_user(email="admin@test.com", password=hash_password("admin")) main_question = AllQuestions(question_id=0, question="question", answer="answer", addresses="") multiple_choice = MultipleChoice(question_id=0, difficulty=1, hint="", wrong_answers=["", ""], base_question=main_question) hidden_answer = HiddenAnswer(question_id=0, difficulty=1, hint="", base_question=main_question) session.add(admin_role) session.add(basic_user) session.add(admin_user) session.add(main_question) session.add(multiple_choice) session.add(hidden_answer) user_datastore.add_role_to_user(admin_user, admin_role) session.commit() yield destroy_db() @pytest.fixture def login_basic_user(client, init_database): client.post("/login", data=dict(email="basic@test.com", password="password", remember=False), follow_redirects=True) yield client.get('/logout', follow_redirects=True) @pytest.fixture def login_admin_user(client, init_database): client.post("/login", data=dict(email="admin@test.com", password="admin", remember=False), follow_redirects=True) yield client.get('/logout', follow_redirects=True)