Пример #1
0
def test_get_user(user, client):
    resp = client.get("/user/" + str(user.id))

    with assert_api_response(resp) as data:
        us = data["user"]
        assert us["name"] == user.name
        assert us["id"] == user.id
def test_question_create_as_child(auth_client, session, paper_with_course_and_questions):
    paper = paper_with_course_and_questions
    course = paper.course
    question = paper.questions[0]

    resp = auth_client.post("/course/{code}/paper/{year}/{period}/q/{question}".format(
        code=paper.course.code.lower(), 
        year=paper.year_start,
        period=paper.period.lower(),
        question=".".join(map(str, question.path))
    ), data={
        "index": 4,
        "index_type": "alpha",
        "content": "Hello world"
    })

    with assert_api_response(resp) as data:
        assert "question" in data
        question_data = data["question"]
        assert "id" in question_data
        assert "revision" in question_data
        assert "parent" in question_data

        revision_data = question_data["revision"]
        assert revision_data["content"] == "Hello world"

        session.refresh(question)
        assert find(question.children, lambda q: q.id == question_data["id"])
Пример #3
0
def test_create(session, institution, client):
    """POST /user { name, email, password }"""
    resp = client.post("/user", data={
        "name": USER_NAME,
        "email": USER_EMAIL,
        "password": USER_PASSWORD
    })

    # Ensure the user is in the database
    user = session.query(User).filter(User.email == USER_EMAIL).one()

    assert user, "User does not exist"
    assert user.sessions[0], "User is not logged in"
    assert user.institution.id == institution.id, "Insitution is not added to user"

    with assert_api_response(resp) as data:

        # Check we have the key returned and user
        assert data["key"] == user.sessions[0].key

        us = data["user"]
        assert "name" in us
        assert "id" in us
        assert not "password" in us
        assert not "salt" in us
def test_auth_check(auth_client):
    resp = auth_client.get("/auth")

    with assert_api_response(resp) as data:
        # Check to see if a session exists for the user
        assert data["key"] == getattr(auth_client, "key")
        assert "user" in data
Пример #5
0
def test_create(session, institution, client):
    """POST /user { name, email, password }"""
    resp = client.post("/user",
                       data={
                           "name": USER_NAME,
                           "email": USER_EMAIL,
                           "password": USER_PASSWORD
                       })

    # Ensure the user is in the database
    user = session.query(User).filter(User.email == USER_EMAIL).one()

    assert user, "User does not exist"
    assert user.sessions[0], "User is not logged in"
    assert user.institution.id == institution.id, "Insitution is not added to user"

    with assert_api_response(resp) as data:

        # Check we have the key returned and user
        assert data["key"] == user.sessions[0].key

        us = data["user"]
        assert "name" in us
        assert "id" in us
        assert not "password" in us
        assert not "salt" in us
Пример #6
0
def test_get_user(user, client):
    resp = client.get("/user/" + str(user.id))

    with assert_api_response(resp) as data:
        us = data["user"]
        assert us["name"] == user.name
        assert us["id"] == user.id
Пример #7
0
def test_question_create_as_child(auth_client, session,
                                  paper_with_course_and_questions):
    paper = paper_with_course_and_questions
    course = paper.course
    question = paper.questions[0]

    resp = auth_client.post(
        "/course/{code}/paper/{year}/{period}/q/{question}".format(
            code=paper.course.code.lower(),
            year=paper.year_start,
            period=paper.period.lower(),
            question=".".join(map(str, question.path))),
        data={
            "index": 4,
            "index_type": "alpha",
            "content": "Hello world"
        })

    with assert_api_response(resp) as data:
        assert "question" in data
        question_data = data["question"]
        assert "id" in question_data
        assert "revision" in question_data
        assert "parent" in question_data

        revision_data = question_data["revision"]
        assert revision_data["content"] == "Hello world"

        session.refresh(question)
        assert find(question.children, lambda q: q.id == question_data["id"])
Пример #8
0
def test_question_get_notes(auth_client, paper_with_course_and_questions,
                            session):
    paper = paper_with_course_and_questions
    course = paper.course
    question = paper.questions[0]

    note_link = NoteLink(link="http://foo.com", question=question)
    note_upload = NoteUpload(file_path="/foo/bar", question=question)
    session.add_all([note_link, note_upload])
    session.commit()

    resp = auth_client.get(
        "/course/{code}/paper/{year}/{period}/q/{question}/notes".format(
            code=paper.course.code.lower(),
            year=paper.year_start,
            period=paper.period.lower(),
            question=".".join(map(str, question.path))))

    with assert_api_response(resp) as data:
        assert "question" in data
        assert "notes" in data
        notes = data["notes"]
        assert len(notes) == 2
        assert find(notes, lambda n: n["type"] == "note_link")
        assert find(notes, lambda n: n["type"] == "note_upload")
Пример #9
0
def test_comment_get_all(question_with_comments, auth_client):
    question = question_with_comments

    resp = auth_client.get("/comments/%d" % question.id)

    with assert_api_response(resp) as data:
        assert "comments" in data
        comments_data = data["comments"]
Пример #10
0
def test_comment_get_all(question_with_comments, auth_client):
    question = question_with_comments

    resp = auth_client.get("/comments/%d" % question.id)

    with assert_api_response(resp) as data:
        assert "comments" in data
        comments_data = data["comments"]
Пример #11
0
def test_comment_create(paper_with_course_and_questions, auth_client):
    paper = paper_with_course_and_questions
    question = paper.questions[0]
    content = "Hello world!"

    resp = auth_client.post("/comment/%d" % question.id,
                            data={"content": content})

    with assert_api_response(resp) as data:
        assert "comment" in data
        comment_data = data["comment"]
        assert comment_data["id"]
        assert comment_data["content"] == content
Пример #12
0
def test_comment_create(paper_with_course_and_questions, auth_client):
    paper = paper_with_course_and_questions
    question = paper.questions[0]
    content = "Hello world!"

    resp = auth_client.post("/comment/%d" % question.id, data={
        "content": content
    })

    with assert_api_response(resp) as data:
        assert "comment" in data
        comment_data = data["comment"]
        assert comment_data["id"]
        assert comment_data["content"] == content
Пример #13
0
def test_login(user, client):
    resp = client.post("/login", data={
        "email": user.email,
        "password": "******"
    })

    assert user.sessions[0]

    with assert_api_response(resp) as data:
        # Check to see if a session exists for the user
        assert data["key"] == user.sessions[0].key
        assert "user" in data
        user_data = data["user"]
        assert "id" in user_data
Пример #14
0
def test_course_get(auth_client, course_with_papers):
    course = course_with_papers
    resp = auth_client.get("/course/" + course.code.lower())

    with assert_api_response(resp) as data:
        co = data["course"]
        assert co
        assert co["id"] == course.id
        assert len(co["papers"]) == 5

        ps = data["papers"]
        assert len(ps) > 0

        paper = ps[0]
        assert paper["course"] == co["id"]
def test_paper_get(auth_client, paper_with_course_and_questions):
    paper = paper_with_course_and_questions
    resp = auth_client.get("/course/{code}/paper/{year}/{period}".format(
        code=paper.course.code.lower(),
        year=paper.year_start,
        period=paper.period.lower()))

    with assert_api_response(resp) as data:
        assert "paper" in data
        assert "questions" in data
        assert "course" in data

        question_data = data["questions"][0]

        assert "parent" in question_data
Пример #16
0
def test_paper_get(auth_client, paper_with_course_and_questions):
    paper = paper_with_course_and_questions
    resp = auth_client.get("/course/{code}/paper/{year}/{period}".format(
        code=paper.course.code.lower(), 
        year=paper.year_start,
        period=paper.period.lower()
    ))

    with assert_api_response(resp) as data:
        assert "paper" in data
        assert "questions" in data
        assert "course" in data

        question_data = data["questions"][0]

        assert "parent" in question_data
Пример #17
0
def test_question_get(auth_client, paper_with_course_and_questions):
    paper = paper_with_course_and_questions
    question = paper.questions[0]

    resp = auth_client.get(
        "/course/{code}/paper/{year}/{period}/q/{question}".format(
            code=paper.course.code.lower(),
            year=paper.year_start,
            period=paper.period.lower(),
            question=".".join(map(str, question.path))))

    with assert_api_response(resp) as data:
        assert "question" in data
        assert "children" in data

        question_data = data["question"]
        assert "parent" in question_data
        assert "comment_count" in question_data
def test_question_get(auth_client, paper_with_course_and_questions):
    paper = paper_with_course_and_questions
    question = paper.questions[0]

    resp = auth_client.get("/course/{code}/paper/{year}/{period}/q/{question}".format(
        code=paper.course.code.lower(), 
        year=paper.year_start,
        period=paper.period.lower(),
        question=".".join(map(str, question.path))
    ))

    with assert_api_response(resp) as data:
        assert "question" in data
        assert "children" in data

        question_data = data["question"]
        assert "parent" in question_data
        assert "comment_count" in question_data
def test_question_update(auth_client, session, paper_with_course_and_questions):
    paper = paper_with_course_and_questions
    question = paper.questions[0]

    resp = auth_client.put("/course/{code}/paper/{year}/{period}/q/{question}".format(
        code=paper.course.code.lower(), 
        year=paper.year_start,
        period=paper.period.lower(),
        question=".".join(map(str, question.path))
    ), data={
        "content": "Hello world!",
        "marks": 1238
    })

    with assert_api_response(resp) as data:
        session.refresh(question)
        assert question.revision.content == "Hello world!"
        assert len(question.revisions) > 0
        assert question.marks == 1238
Пример #20
0
def test_comment_delete(question_with_comments, course_with_papers, session, auth_client):
    course = course_with_papers
    paper = course.papers[0]
    question = question_with_comments
    comment = question.comments[0]

    question.paper = paper
    session.add(question)
    session.flush()

    resp = auth_client.delete("/comment/%d/%d" % (question.id, comment.id))

    with assert_api_response(resp) as data:
        assert "comment" in data
        comment_data = data["comment"]
        assert comment_data["id"] == comment.id
        assert comment_data["content"] == ""
        assert comment_data["updated_at"]
        assert comment_data["deleted"]
Пример #21
0
def test_comment_delete(question_with_comments, course_with_papers, session,
                        auth_client):
    course = course_with_papers
    paper = course.papers[0]
    question = question_with_comments
    comment = question.comments[0]

    question.paper = paper
    session.add(question)
    session.flush()

    resp = auth_client.delete("/comment/%d/%d" % (question.id, comment.id))

    with assert_api_response(resp) as data:
        assert "comment" in data
        comment_data = data["comment"]
        assert comment_data["id"] == comment.id
        assert comment_data["content"] == ""
        assert comment_data["updated_at"]
        assert comment_data["deleted"]
Пример #22
0
def test_comment_edit(question_with_comments, course_with_papers, session,
                      auth_client):
    course = course_with_papers
    paper = course.papers[0]
    question = question_with_comments
    comment = question.comments[0]
    content = "New hello world!"

    question.paper = paper
    session.add(question)
    session.flush()

    resp = auth_client.put("/comment/%d/%d" % (question.id, comment.id),
                           data={"content": content})

    with assert_api_response(resp) as data:
        assert "comment" in data
        comment_data = data["comment"]
        assert comment_data["id"] == comment.id
        assert comment_data["content"] == content
        assert comment_data["updated_at"]
def test_question_delete_sibling(auth_client, paper_with_course_and_questions, session):
    paper = paper_with_course_and_questions
    root = paper.questions[0]
    sibling = root.children[1]
    nextSibling = root.children[2]

    resp = auth_client.delete("/course/{code}/paper/{year}/{period}/q/{question}".format(
        code=paper.course.code.lower(), 
        year=paper.year_start,
        period=paper.period.lower(),
        question=".".join(map(str, sibling.path))
    ))

    with assert_api_response(resp) as data:
        assert "questions" in data

    index_before = nextSibling.index
    session.refresh(nextSibling)
    index_after = nextSibling.index

    assert index_before == index_after 
Пример #24
0
def test_question_update(auth_client, session,
                         paper_with_course_and_questions):
    paper = paper_with_course_and_questions
    question = paper.questions[0]

    resp = auth_client.put(
        "/course/{code}/paper/{year}/{period}/q/{question}".format(
            code=paper.course.code.lower(),
            year=paper.year_start,
            period=paper.period.lower(),
            question=".".join(map(str, question.path))),
        data={
            "content": "Hello world!",
            "marks": 1238
        })

    with assert_api_response(resp) as data:
        session.refresh(question)
        assert question.revision.content == "Hello world!"
        assert len(question.revisions) > 0
        assert question.marks == 1238
Пример #25
0
def test_question_create(auth_client, session, course_with_papers):
    course = course_with_papers
    paper = course.papers[0]

    resp = auth_client.post("/course/{code}/paper/{year}/{period}/q/".format(
        code=paper.course.code.lower(),
        year=paper.year_start,
        period=paper.period.lower()),
                            data={
                                "index": 1,
                                "index_type": "alpha",
                                "content": "Hello world"
                            })

    with assert_api_response(resp) as data:
        assert "question" in data
        question_data = data["question"]
        assert "id" in question_data
        assert "paper" in question_data
        assert "revision" in question_data
        revision_data = question_data["revision"]
        assert revision_data["content"] == "Hello world"
def test_question_create(auth_client, session, course_with_papers):
    course = course_with_papers
    paper = course.papers[0]

    resp = auth_client.post("/course/{code}/paper/{year}/{period}/q/".format(
        code=paper.course.code.lower(), 
        year=paper.year_start,
        period=paper.period.lower()
    ), data={
        "index": 1,
        "index_type": "alpha",
        "content": "Hello world"
    })

    with assert_api_response(resp) as data:
        assert "question" in data
        question_data = data["question"]
        assert "id" in question_data
        assert "paper" in question_data
        assert "revision" in question_data
        revision_data = question_data["revision"]
        assert revision_data["content"] == "Hello world"
Пример #27
0
def test_question_delete_sibling(auth_client, paper_with_course_and_questions,
                                 session):
    paper = paper_with_course_and_questions
    root = paper.questions[0]
    sibling = root.children[1]
    nextSibling = root.children[2]

    resp = auth_client.delete(
        "/course/{code}/paper/{year}/{period}/q/{question}".format(
            code=paper.course.code.lower(),
            year=paper.year_start,
            period=paper.period.lower(),
            question=".".join(map(str, sibling.path))))

    with assert_api_response(resp) as data:
        assert "questions" in data

    index_before = nextSibling.index
    session.refresh(nextSibling)
    index_after = nextSibling.index

    assert index_before == index_after
def test_question_delete_leaf(auth_client, paper_with_course_and_questions, session):
    paper = paper_with_course_and_questions
    question = find(paper.questions, lambda q: not len(q.children))

    resp = auth_client.delete("/course/{code}/paper/{year}/{period}/q/{question}".format(
        code=paper.course.code.lower(), 
        year=paper.year_start,
        period=paper.period.lower(),
        question=".".join(map(str, question.path))
    ))

    with assert_api_response(resp) as data:
        assert "questions" in data

    try:
        question = session.query(Question)\
            .filter(Question.id == question.id)\
            .one()

        assert not question
    except NoResultFound:
        pass
def test_question_create_as_child_in_list(auth_client, session, paper_with_course_and_questions):
    paper = paper_with_course_and_questions
    course = paper.course
    question = paper.questions[0]

    resp = auth_client.post("/course/{code}/paper/{year}/{period}/q/{question}".format(
        code=paper.course.code.lower(), 
        year=paper.year_start,
        period=paper.period.lower(),
        question=".".join(map(str, question.path))
    ), data={
        "index": 5,
        "content": "Hello world"
    })

    with assert_api_response(resp) as data:
        assert "question" in data
        question_data = data["question"]
        id = question_data["id"]

        new_question = session.query(Question).filter(Question.id == id).one()
        assert new_question.index_type == "alpha"
Пример #30
0
def test_comment_edit(question_with_comments, course_with_papers, session, auth_client):
    course = course_with_papers
    paper = course.papers[0]
    question = question_with_comments
    comment = question.comments[0]
    content = "New hello world!"

    question.paper = paper
    session.add(question)
    session.flush()


    resp = auth_client.put("/comment/%d/%d" % (question.id, comment.id), data={
        "content": content
    })

    with assert_api_response(resp) as data:
        assert "comment" in data
        comment_data = data["comment"]
        assert comment_data["id"] == comment.id
        assert comment_data["content"] == content
        assert comment_data["updated_at"]
Пример #31
0
def test_question_create_note(auth_client, paper_with_course_and_questions):
    paper = paper_with_course_and_questions
    course = paper.course
    question = paper.questions[0]

    resp = auth_client.post("/course/{code}/paper/{year}/{period}/q/{question}/note".format(
        code=paper.course.code.lower(), 
        year=paper.year_start,
        period=paper.period.lower(),
        question=".".join(map(str, question.path))
    ), data={
        "link": "http://google.com",
        "description": "Use Google, dumbass."
    })

    with assert_api_response(resp) as data:
        assert "question" in data
        assert "note" in data
        note_data = data["note"]

        assert "id" in note_data
        assert note_data["type"] == "note_link"
Пример #32
0
def test_question_create_note(auth_client, paper_with_course_and_questions):
    paper = paper_with_course_and_questions
    course = paper.course
    question = paper.questions[0]

    resp = auth_client.post(
        "/course/{code}/paper/{year}/{period}/q/{question}/note".format(
            code=paper.course.code.lower(),
            year=paper.year_start,
            period=paper.period.lower(),
            question=".".join(map(str, question.path))),
        data={
            "link": "http://google.com",
            "description": "Use Google, dumbass."
        })

    with assert_api_response(resp) as data:
        assert "question" in data
        assert "note" in data
        note_data = data["note"]

        assert "id" in note_data
        assert note_data["type"] == "note_link"
Пример #33
0
def test_question_delete_leaf(auth_client, paper_with_course_and_questions,
                              session):
    paper = paper_with_course_and_questions
    question = find(paper.questions, lambda q: not len(q.children))

    resp = auth_client.delete(
        "/course/{code}/paper/{year}/{period}/q/{question}".format(
            code=paper.course.code.lower(),
            year=paper.year_start,
            period=paper.period.lower(),
            question=".".join(map(str, question.path))))

    with assert_api_response(resp) as data:
        assert "questions" in data

    try:
        question = session.query(Question)\
            .filter(Question.id == question.id)\
            .one()

        assert not question
    except NoResultFound:
        pass
Пример #34
0
def test_question_create_as_child_in_list(auth_client, session,
                                          paper_with_course_and_questions):
    paper = paper_with_course_and_questions
    course = paper.course
    question = paper.questions[0]

    resp = auth_client.post(
        "/course/{code}/paper/{year}/{period}/q/{question}".format(
            code=paper.course.code.lower(),
            year=paper.year_start,
            period=paper.period.lower(),
            question=".".join(map(str, question.path))),
        data={
            "index": 5,
            "content": "Hello world"
        })

    with assert_api_response(resp) as data:
        assert "question" in data
        question_data = data["question"]
        id = question_data["id"]

        new_question = session.query(Question).filter(Question.id == id).one()
        assert new_question.index_type == "alpha"
Пример #35
0
def test_question_get_notes(auth_client, paper_with_course_and_questions, session):
    paper = paper_with_course_and_questions
    course = paper.course
    question = paper.questions[0]

    note_link = NoteLink(link="http://foo.com", question=question)
    note_upload = NoteUpload(file_path="/foo/bar", question=question)
    session.add_all([note_link, note_upload])
    session.commit()

    resp = auth_client.get("/course/{code}/paper/{year}/{period}/q/{question}/notes".format(
        code=paper.course.code.lower(), 
        year=paper.year_start,
        period=paper.period.lower(),
        question=".".join(map(str, question.path))
    ))

    with assert_api_response(resp) as data:
        assert "question" in data
        assert "notes" in data
        notes = data["notes"]
        assert len(notes) == 2
        assert find(notes, lambda n: n["type"] == "note_link")
        assert find(notes, lambda n: n["type"] == "note_upload")
Пример #36
0
def test_profile_courses_empty(auth_client):
    resp = auth_client.get("/profile/courses")

    with assert_api_response(resp) as data:
        assert len(data["courses"]) == 0
Пример #37
0
def test_course_search_empty(auth_client, courses):
    resp = auth_client.get("/course/search?q=Ridiculous+query")

    with assert_api_response(resp) as data:
        assert len(data["courses"]) == 0
def test_institution_get(institution, client):
    resp = client.get("/institution/" + str(institution.id))

    with assert_api_response(resp) as data:
        instit = data["institution"]
        assert instit["id"] == institution.id
def test_institution_search_institution(institution, client):
    resp = client.get("/institution/search?domain=nuigalway.ie")

    with assert_api_response(resp) as data:
        instit = data["institution"]
        assert instit["id"] == institution.id
Пример #40
0
def test_course_search(auth_client, courses):
    resp = auth_client.get("/course/search?q=computer+security")

    with assert_api_response(resp) as data:
        assert len(data["courses"]) > 0
def test_institution_search_institution(institution, client):
    resp = client.get("/institution/search?domain=nuigalway.ie")

    with assert_api_response(resp) as data:
        instit = data["institution"]
        assert instit["id"] == institution.id
Пример #42
0
def test_profile_courses_empty(auth_client):
    resp = auth_client.get("/profile/courses")

    with assert_api_response(resp) as data:
        assert len(data["courses"]) == 0
def test_institution_get(institution, client):
    resp = client.get("/institution/" + str(institution.id))

    with assert_api_response(resp) as data:
        instit = data["institution"]
        assert instit["id"] == institution.id