Example #1
0
def test_boulder_gradings():
    """
    Tests grading functionality of the boulderGrading endpoint.
    """
    login_jwts = ratings_fixture()
    gradings = ['VB', 'V5', '6A+', '6B', '6C+', 'V16', '2', '7A', '6A+', '8A+']

    # POST gradings
    for i in range(10):
        h = [('loginJWT', login_jwts[i])]
        d = dict(grade=gradings[i], boulder_id=4)
        rv = test_app.post('%sboulder-gradings/' % api_prefix, data=d, headers=h)
        check_content_type(rv.headers)
        eq_(rv.status_code, 201)

    # GET single
    rv = test_app.get('%s%s' % (endpoint, 4))
    check_content_type(rv.headers)
    eq_(rv.status_code, 200)
    resp = json.loads(rv.data)['data']
    eq_(resp['grading'], '6B+')

    # Try to grade a unconfirmed boulder
    h = [('loginJWT', login_jwts[i])]
    d = dict(grade=gradings[i], boulder_id=8)
    rv = test_app.post('%sboulder-gradings/' % api_prefix, data=d, headers=h)
    check_content_type(rv.headers)
    eq_(rv.status_code, 400)
    check_error_code(rv.data, "074")

    # Try to grade a non existing boulder
    h = [('loginJWT', login_jwts[i])]
    d = dict(grade=gradings[i], boulder_id=42)
    rv = test_app.post('%sboulder-gradings/' % api_prefix, data=d, headers=h)
    check_content_type(rv.headers)
    eq_(rv.status_code, 404)
    check_error_code(rv.data, "073")

    # Try to give an invalid grading score
    h = [('loginJWT', login_jwts[i])]
    d = dict(grade='9C+', boulder_id=4)
    rv = test_app.post('%sboulder-gradings/' % api_prefix, data=d, headers=h)
    check_content_type(rv.headers)
    eq_(rv.status_code, 400)
    check_error_code(rv.data, "075")

    # Regrade boulders
    for i in range(10):
        h = [('loginJWT', login_jwts[i])]
        d = dict(grade="2", boulder_id=4)
        rv = test_app.post('%sboulder-gradings/' % api_prefix, data=d, headers=h)
        check_content_type(rv.headers)
        eq_(rv.status_code, 201)

    # GET single (should now be graded fb 2)
    rv = test_app.get('%s%s' % (endpoint, 4))
    check_content_type(rv.headers)
    eq_(rv.status_code, 200)
    resp = json.loads(rv.data)['data']
    eq_(resp['grading'], '2')
Example #2
0
def test_ratings():
    """
    Tests rating functionality of the rating endpoint.
    """
    login_jwts = ratings_fixture()
    ratings = [1, 3, 5, 5, 2, 5, 3, 5, 4, 3]

    # POST ratings
    for i in range(10):
        h = [('loginJWT', login_jwts[i])]
        d = dict(score=ratings[i], entity_id=1)
        rv = test_app.post(endpoint, data=d, headers=h)
        check_content_type(rv.headers)
        eq_(rv.status_code, 201)

    # GET single entity
    rv = test_app.get('%sareas/%s' % (api_prefix, 1))
    check_content_type(rv.headers)
    eq_(rv.status_code, 200)
    resp = json.loads(rv.data)['data']
    eq_(resp['rating'], 3.6)

    # Try to rate an unconfirmed entity
    h = [('loginJWT', login_jwts[i])]
    d = dict(score=ratings[i], entity_id=5)
    rv = test_app.post(endpoint, data=d, headers=h)
    check_content_type(rv.headers)
    eq_(rv.status_code, 400)
    check_error_code(rv.data, "077")

    # Try to rate a non existing entity
    h = [('loginJWT', login_jwts[i])]
    d = dict(score=ratings[i], entity_id=42)
    rv = test_app.post(endpoint, data=d, headers=h)
    check_content_type(rv.headers)
    eq_(rv.status_code, 404)
    check_error_code(rv.data, "078")

    # Try to give an invalid rating score
    h = [('loginJWT', login_jwts[i])]
    d = dict(score=0, entity_id=1)
    rv = test_app.post(endpoint, data=d, headers=h)
    check_content_type(rv.headers)
    eq_(rv.status_code, 400)
    check_error_code(rv.data, "076")

    # rePOST ratings
    for i in range(10):
        h = [('loginJWT', login_jwts[i])]
        d = dict(score='5', entity_id=1)
        rv = test_app.post(endpoint, data=d, headers=h)
        check_content_type(rv.headers)
        eq_(rv.status_code, 201)

    # GET single entity (should now be rated at 5)
    rv = test_app.get('%sareas/%s' % (api_prefix, 1))
    check_content_type(rv.headers)
    eq_(rv.status_code, 200)
    resp = json.loads(rv.data)['data']
    eq_(resp['rating'], 5)

    # Simple tests for the other entity types
    for i in range(2,5):
        h = [('loginJWT', login_jwts[i])]
        d = dict(score='3', entity_id=i)
        rv = test_app.post(endpoint, data=d, headers=h)
        check_content_type(rv.headers)
        eq_(rv.status_code, 201)

    # GET single entitys
    rv = test_app.get('%scrags/%s' % (api_prefix, 2))
    check_content_type(rv.headers)
    eq_(rv.status_code, 200)
    resp = json.loads(rv.data)['data']
    eq_(resp['rating'], 3)

    rv = test_app.get('%sblocks/%s' % (api_prefix, 3))
    check_content_type(rv.headers)
    eq_(rv.status_code, 200)
    resp = json.loads(rv.data)['data']
    eq_(resp['rating'], 3)

    rv = test_app.get('%sboulders/%s' % (api_prefix, 4))
    check_content_type(rv.headers)
    eq_(rv.status_code, 200)
    resp = json.loads(rv.data)['data']
    eq_(resp['rating'], 3)
Example #3
0
def test_comments():
    """
    Tests CRUD functionality of the comment endpoint.
    """
    login_jwts = ratings_fixture()

    # POST comments
    for i in range(3):
        h = [('loginJWT', login_jwts[i])]
        d = dict(text="Lorem ipsum number %s" % i, entity_id=1)
        rv = test_app.post(endpoint, data=d, headers=h)
        check_content_type(rv.headers)
        eq_(rv.status_code, 201)

    # GET comment list
    rv = test_app.get(endpoint)
    check_content_type(rv.headers)
    eq_(rv.status_code, 200)
    resp = json.loads(rv.data)['data']
    eq_(len(resp), 3)

    # GET single
    rv = test_app.get("%s%s" % (endpoint, 1))
    check_content_type(rv.headers)
    eq_(rv.status_code, 200)
    resp = json.loads(rv.data)['data']
    eq_(resp['text'], "Lorem ipsum number 0")

    # GET non existing single
    rv = test_app.get("%s%s" % (endpoint, 42))
    check_content_type(rv.headers)
    eq_(rv.status_code, 404)
    check_error_code(rv.data, "065")

    # POST children
    h = [('loginJWT', login_jwts[i])]
    d = dict(text="This is a child comment", entity_id=1, parent_id=1)
    rv = test_app.post(endpoint, data=d, headers=h)
    check_content_type(rv.headers)
    eq_(rv.status_code, 201)
    child_resp = json.loads(rv.data)['data']

    # POST grandchildren
    h = [('loginJWT', login_jwts[i])]
    d = dict(text="This is a child comment", entity_id=1, parent_id=child_resp['id'])
    rv = test_app.post(endpoint, data=d, headers=h)
    check_content_type(rv.headers)
    eq_(rv.status_code, 400)
    check_error_code(rv.data, "064")

    # POST children for non exiting parent
    h = [('loginJWT', login_jwts[i])]
    d = dict(text="This is a child comment", entity_id=1, parent_id=42)
    rv = test_app.post(endpoint, data=d, headers=h)
    check_content_type(rv.headers)
    eq_(rv.status_code, 400)
    check_error_code(rv.data, "063")

    # GET comment list
    rv = test_app.get(endpoint)
    check_content_type(rv.headers)
    eq_(rv.status_code, 200)
    resp = json.loads(rv.data)['data']
    eq_(len(resp), 4)

    # GET comment list for entity
    rv = test_app.get('%sentity/1/comments/' % api_prefix)
    check_content_type(rv.headers)
    eq_(rv.status_code, 200)
    resp = json.loads(rv.data)['data']
    eq_(len(resp), 3)

    # GET comment list for entity without comments
    rv = test_app.get('%sentity/42/comments/' % api_prefix)
    check_content_type(rv.headers)
    eq_(rv.status_code, 200)
    resp = json.loads(rv.data)['data']
    eq_(len(resp), 0)

    # Try to comment on an unconfirmed entity
    h = [('loginJWT', login_jwts[i])]
    d = dict(text="Lorem ipsum", entity_id=5)
    rv = test_app.post(endpoint, data=d, headers=h)
    check_content_type(rv.headers)
    eq_(rv.status_code, 400)
    check_error_code(rv.data, "062")

    # Try to comment on a non existing entity
    h = [('loginJWT', login_jwts[i])]
    d = dict(text="Lorem ipsum", entity_id=42)
    rv = test_app.post(endpoint, data=d, headers=h)
    check_content_type(rv.headers)
    eq_(rv.status_code, 404)
    check_error_code(rv.data, "061")

    # UPDATE as author
    h = [('loginJWT', login_jwts[0])]
    d = dict(text="New text", entity_id=1)
    rv = test_app.put("%s%s" % (endpoint, 1), data=d, headers=h)
    check_content_type(rv.headers)
    eq_(rv.status_code, 201)
    resp = json.loads(rv.data)['data']
    eq_(resp['text'], "New text")

    # UPDATE as non author
    h = [('loginJWT', login_jwts[1])]
    d = dict(text="Newer text", entity_id=1)
    rv = test_app.put("%s%s" % (endpoint, 1), data=d, headers=h)
    check_content_type(rv.headers)
    eq_(rv.status_code, 401)
    check_error_code(rv.data, "069")

    # UPDATE non existing
    h = [('loginJWT', login_jwts[1])]
    d = dict(text="Newer text", entity_id=1)
    rv = test_app.put("%s%s" % (endpoint, 42), data=d, headers=h)
    check_content_type(rv.headers)
    eq_(rv.status_code, 404)
    check_error_code(rv.data, "068")

    # DELETE single as author
    h = [('loginJWT', login_jwts[0])]
    rv = test_app.delete("%s%s" % (endpoint, 1), headers=h)
    check_content_type(rv.headers)
    eq_(rv.status_code, 204)

    # GET now non existing child comment
    rv = test_app.get("%s%s" % (endpoint, child_resp['id']))
    check_content_type(rv.headers)
    eq_(rv.status_code, 404)
    check_error_code(rv.data, "065")

    # DELETE non existing single as author
    h = [('loginJWT', login_jwts[0])]
    rv = test_app.delete("%s%s" % (endpoint, 1), headers=h)
    check_content_type(rv.headers)
    eq_(rv.status_code, 404)
    check_error_code(rv.data, "066")

    # DELETE single as non-author
    h = [('loginJWT', login_jwts[2])]
    rv = test_app.delete("%s%s" % (endpoint, 2), headers=h)
    check_content_type(rv.headers)
    eq_(rv.status_code, 401)
    check_error_code(rv.data, "067")