Exemplo n.º 1
0
async def test_create_user(client, users):
    new_user = get_fake_user()
    new_user.pop("id")

    res = await client.post("/users", json=new_user)
    assert res.status == 200

    body = await res.json()
    assert "data" in body
    assert isinstance(body["data"], dict)

    all_users = await User.query.gino.all()
    assert len(all_users) == len(users) + 1
    assert profile_created_from_origin(new_user, all_users[-1].to_dict())

    # Ignore param args
    # POST request will have its query parameter (args) ignored.
    new_user = get_fake_user()
    new_user.pop("id")
    res = await client.post("/users", json=new_user)
    assert res.status == 200

    body = await res.json()
    assert "data" in body
    assert isinstance(body["data"], dict)

    all_users = await User.query.gino.all()
    assert len(all_users) == len(users) + 2
    assert profile_created_from_origin(new_user, all_users[-1].to_dict())
Exemplo n.º 2
0
async def test_create_internal_id_users(client, users):
    """Ensure that `internal_id` cannot be created"""
    new_user = get_fake_user()
    new_user.pop("id")

    res = await client.post("/users", json={**new_user, "internal_id": 3})
    assert res.status == 400
Exemplo n.º 3
0
async def test_replace_user(client, users, token_user):
    new_user = get_fake_user()
    new_user.pop("id")

    # Missing token
    res = await client.put("/users/{}".format(users[0]["id"]), json=new_user)
    assert res.status == 401

    # Valid request
    res = await client.put(
        "/users/{}".format(users[0]["id"]),
        json=new_user,
        headers={"Authorization": token_user},
    )
    assert res.status == 200

    body = await res.json()
    assert "data" in body
    assert isinstance(body["data"], dict)

    updated_user = await get_one(User, internal_id=1)
    updated_user = updated_user.to_dict()
    assert profile_created_from_origin(new_user, updated_user)
Exemplo n.º 4
0
import sys
from os.path import abspath, dirname

root_dir = dirname(dirname(dirname(abspath(__file__))))
sys.path.append(root_dir)

from quizard_backend.tests import (
    get_fake_user,
    get_fake_quiz,
    get_fake_questions_for_quiz,
    number_of_users,
    number_of_quizzes,
    number_of_questions_per_quiz,
)

users = [get_fake_user() for _ in range(number_of_users)]
users += [
    {
        "full_name": "Quizard",
        "email": "quizard",
        "password": "******"
    },
    {
        "full_name": "User 1",
        "email": "user1",
        "password": "******"
    },
    {
        "full_name": "User 2",
        "email": "user2",
        "password": "******"
Exemplo n.º 5
0
async def test_pagination_created_attempted_quizzes(app, client, users,
                                                    questions, quizzes,
                                                    token_user):
    # Create a fresh user, as the created user already has some previously created quizzes
    new_user = get_fake_user()
    new_user.pop("id")

    res = await client.post("/users", json=new_user)
    assert res.status == 200
    body = await res.json()
    new_user_id = body["data"]["id"]
    new_user_token = await get_access_token_for_user(body["data"], app)

    # Create a few quizzes
    created_quizzes = []
    for _ in range(35):
        fake_quiz = get_fake_quiz()
        fake_quiz.pop("creator_id")
        new_quiz = {
            **fake_quiz, "questions": get_fake_quiz_questions(has_id=False)
        }
        new_quiz.pop("id", None)

        # Create a quiz with valid args
        res = await client.post("/quizzes",
                                json=new_quiz,
                                headers={"Authorization": new_user_token})
        assert res.status == 200
        body = await res.json()
        created_quizzes.append(body["data"])

    # Check pagination
    res = await client.get("/users/{}/quizzes/created".format(new_user_id))
    assert res.status == 200
    body = await res.json()
    assert "data" in body
    assert isinstance(body["data"], list)
    assert len(body["data"]) == 15

    for created, retrieve in zip(created_quizzes[:15], body["data"]):
        assert profile_created_from_origin(retrieve,
                                           created,
                                           ignore={"questions", "updated_at"})

    # Check second page
    next_page_link = body["links"]["next"]
    # Strip the host, as it is a testing host
    next_page_link = "/" + "/".join(next_page_link.split("/")[3:])
    res = await client.get(next_page_link)
    assert res.status == 200
    body = await res.json()
    assert "data" in body
    assert isinstance(body["data"], list)
    assert len(body["data"]) == 15

    for created, retrieve in zip(created_quizzes[15:30], body["data"]):
        assert profile_created_from_origin(retrieve,
                                           created,
                                           ignore={"questions", "updated_at"})

    # Check last page
    next_page_link = body["links"]["next"]
    # Strip the host, as it is a testing host
    next_page_link = "/" + "/".join(next_page_link.split("/")[3:])
    res = await client.get(next_page_link)
    assert res.status == 200
    body = await res.json()
    assert "data" in body
    assert isinstance(body["data"], list)
    assert len(body["data"]) == 5

    for created, retrieve in zip(created_quizzes[30:], body["data"]):
        assert profile_created_from_origin(retrieve,
                                           created,
                                           ignore={"questions", "updated_at"})

    ## ATTEMPTED
    # Attempt to do a few quizzes as well
    attempt_user_id = users[0]["id"]
    attempted_quizzes = created_quizzes[::-1]
    for quiz in created_quizzes:
        question_index = 5
        selected_option = 3
        res = await client.post(
            "/quizzes/{}/questions/{}/answers".format(
                quiz["id"], quiz["questions"][question_index]),
            json={"selected_option": selected_option},
            headers={"Authorization": token_user},
        )
        assert res.status == 200
        body = await res.json()

    # Check pagination
    res = await client.get(
        "/users/{}/quizzes/attempted".format(attempt_user_id))
    assert res.status == 200
    body = await res.json()
    assert "data" in body
    assert isinstance(body["data"], list)
    assert len(body["data"]) == 15
    assert "links" in body
    assert "next" in body["links"]

    for created, retrieve in zip(attempted_quizzes[:15], body["data"]):
        assert profile_created_from_origin(
            retrieve,
            {
                **created, "num_attempts": 1,
                "is_finished": False
            },
            ignore={"questions", "updated_at"},
        )

    # Check second page
    next_page_link = body["links"]["next"]
    # Strip the host, as it is a testing host
    next_page_link = "/" + "/".join(next_page_link.split("/")[3:])
    res = await client.get(next_page_link)
    assert res.status == 200
    body = await res.json()
    assert "data" in body
    assert isinstance(body["data"], list)
    assert len(body["data"]) == 15

    for created, retrieve in zip(attempted_quizzes[15:30], body["data"]):
        assert profile_created_from_origin(
            retrieve,
            {
                **created, "num_attempts": 1,
                "is_finished": False
            },
            ignore={"questions", "updated_at"},
        )

    # Check last page
    next_page_link = body["links"]["next"]
    # Strip the host, as it is a testing host
    next_page_link = "/" + "/".join(next_page_link.split("/")[3:])
    res = await client.get(next_page_link)
    assert res.status == 200
    body = await res.json()
    assert "data" in body
    assert isinstance(body["data"], list)
    assert len(body["data"]) == 5

    for created, retrieve in zip(attempted_quizzes[30:], body["data"]):
        assert profile_created_from_origin(
            retrieve,
            {
                **created, "num_attempts": 1,
                "is_finished": False
            },
            ignore={"questions", "updated_at"},
        )
Exemplo n.º 6
0
async def test_get_own_created_and_attempted_quizzes(app, client, users,
                                                     questions, quizzes):
    # Create a fresh user
    new_user = get_fake_user()
    new_user.pop("id")

    res = await client.post("/users", json=new_user)
    assert res.status == 200
    body = await res.json()
    new_user_id = body["data"]["id"]
    new_user_token = await get_access_token_for_user(body["data"], app)

    # Create a few quizzes
    created_quizzes = []
    for _ in range(20):
        fake_quiz = get_fake_quiz()
        fake_quiz.pop("creator_id")
        new_quiz = {
            **fake_quiz, "questions": get_fake_quiz_questions(has_id=False)
        }
        new_quiz.pop("id", None)

        # Cannot create an quiz without token
        res = await client.post("/quizzes", json=new_quiz)
        assert res.status == 401

        # Create a quiz with valid args
        res = await client.post("/quizzes",
                                json=new_quiz,
                                headers={"Authorization": new_user_token})
        assert res.status == 200
        body = await res.json()
        created_quizzes.append(body["data"])

    # Attempt to do a few quizzes as well
    attempted_quizzes = []
    for quiz_index in range(1, 17):
        question_index = 3
        selected_option_3 = 1
        res = await client.post(
            "/quizzes/{}/questions/{}/answers".format(
                quizzes[quiz_index]["id"],
                questions[quiz_index][question_index]["id"]),
            json={"selected_option": selected_option_3},
            headers={"Authorization": new_user_token},
        )
        assert res.status == 200
        body = await res.json()
        attempted_quizzes.append(quizzes[quiz_index])

    # Check if the attempted and created quizzes are correct
    res = await client.get("/users/{}/quizzes/created".format(new_user_id))
    assert res.status == 200
    body = await res.json()
    assert "data" in body
    assert isinstance(body["data"], list)
    assert len(body["data"]) == 15

    # Check if the created quizzes are correct
    for created, retrieve in zip(created_quizzes, body["data"]):
        assert profile_created_from_origin(retrieve,
                                           created,
                                           ignore={"questions", "updated_at"})

    # Check if the attempted quizzes are correct
    res = await client.get("/users/{}/quizzes/attempted".format(new_user_id))
    assert res.status == 200
    body = await res.json()
    assert "data" in body
    assert isinstance(body["data"], list)
    assert len(body["data"]) == 15

    for expected_quiz, actual_quiz in zip(attempted_quizzes[::-1],
                                          body["data"]):
        assert profile_created_from_origin(
            {
                **expected_quiz, "is_finished": False
            },
            actual_quiz,
            ignore={"questions", "updated_at"},
        )