예제 #1
0
def test_minimal_comment(session):
    """Assert that a minimal comment can be created."""
    comment = Comment()
    comment.comment = 'some words'
    comment.save()

    assert comment.id is not None
예제 #2
0
    def post(identifier, filing_id):
        """Create a new comment for the filing."""
        # basic checks
        err_msg, err_code = CommentResource._basic_checks(
            identifier, filing_id, request)
        if err_msg:
            return jsonify(err_msg), err_code

        json_input = request.get_json()

        # check authorization
        if not authorized(identifier, jwt, action=['add_comment']):
            return jsonify({'message':
                            f'You are not authorized to submit a comment for {identifier}.'}), \
                HTTPStatus.UNAUTHORIZED

        # validate comment
        err = validate(json_input, True)
        if err:
            json_input['errors'] = err.msg
            return jsonify(json_input), err.code

        # confirm that the filing ID in the URL is the same as in the json
        if json_input['comment']['filingId'] != filing_id:
            json_input['errors'] = [
                {
                    'error': 'Invalid filingId in request'
                },
            ]
            return jsonify(json_input), HTTPStatus.BAD_REQUEST

        # save comment
        user = User.get_or_create_user_by_jwt(g.jwt_oidc_token_info)
        try:
            comment = Comment()
            comment.comment = json_input['comment']['comment']
            comment.staff_id = user.id
            comment.filing_id = filing_id
            comment.timestamp = datetime.datetime.utcnow()

            comment.save()
        except BusinessException as err:
            reply = json_input
            reply['errors'] = [
                {
                    'error': err.error
                },
            ]
            return jsonify(reply), err.status_code or \
                (HTTPStatus.CREATED if (request.method == 'POST') else HTTPStatus.ACCEPTED)

        # all done
        return jsonify(comment.json), HTTPStatus.CREATED
예제 #3
0
def factory_business_comment(business: Business = None, comment_text: str = 'some text', user: User = None):
    """Create a comment."""
    if not business:
        business = factory_business('CP1234567')

    c = Comment()
    c.business_id = business.id
    c.timestamp = EPOCH_DATETIME
    c.comment = comment_text
    if user:
        c.staff_id = user.id
    c.save()

    return c
예제 #4
0
def test_comment_save(session):
    """Assert that the comment was saved."""
    from sqlalchemy.orm.session import Session

    comment = Comment()
    comment.timestamp = EPOCH_DATETIME
    comment.comment = 'a comment'

    assert not session.new
    assert not Session.object_session(comment)

    comment.save()

    assert comment.id
    assert not session.dirty
    assert Session.object_session(comment)
예제 #5
0
def test_comment_block_orm_delete(session):
    """Assert that attempting to delete a filing will raise a BusinessException."""
    from legal_api.exceptions import BusinessException

    b = factory_business('CP1234567')
    c = Comment()
    c.business_id = b.id
    c.timestamp = EPOCH_DATETIME
    c.comment = 'a comment'
    c.save()

    with pytest.raises(BusinessException) as excinfo:
        session.delete(c)
        session.commit()

    assert excinfo.value.status_code == HTTPStatus.FORBIDDEN
    assert excinfo.value.error == 'Deletion not allowed.'
예제 #6
0
def factory_comment(
        business: Business = None, filing: Filing = None, comment_text: str = 'some text', user: User = None):
    """Create a comment."""
    if not business:
        business = factory_business('CP1234567')

    if not filing:
        filing = factory_filing(business, ANNUAL_REPORT)

    c = Comment()
    c.filing_id = filing.id
    c.timestamp = EPOCH_DATETIME
    c.comment = comment_text
    if user:
        c.staff_id = user.id
    c.save()

    return c
예제 #7
0
def test_comment_save(session):
    """Assert that the comment was saved."""
    from sqlalchemy.orm.session import Session
    b = factory_business('CP1234567')

    comment = Comment()
    comment.business_id = b.id
    comment.timestamp = EPOCH_DATETIME
    comment.comment = 'a comment'

    assert not session.new
    assert not Session.object_session(comment)

    comment.save()

    assert comment.id
    assert not session.dirty
    assert Session.object_session(comment)
예제 #8
0
    def post(identifier):
        """Create a new comment for the business."""
        # basic checks
        business = Business.find_by_identifier(identifier)
        err_msg, err_code = BusinessCommentResource._basic_checks(
            identifier, business, request)
        if err_msg:
            return jsonify(err_msg), err_code

        json_input = request.get_json()

        # check authorization
        if not authorized(identifier, jwt, action=['add_comment']):
            return jsonify({'message':
                            f'You are not authorized to submit a comment for {identifier}.'}), \
                HTTPStatus.UNAUTHORIZED

        # validate comment
        err = validate(json_input, False)
        if err:
            json_input['errors'] = err.msg
            return jsonify(json_input), err.code

        # save comment
        user = User.get_or_create_user_by_jwt(g.jwt_oidc_token_info)
        try:
            comment = Comment()
            comment.comment = json_input['comment']['comment']
            comment.staff_id = user.id
            comment.business_id = business.id
            comment.timestamp = datetime.datetime.utcnow()
            comment.save()
        except BusinessException as err:
            reply = json_input
            reply['errors'] = [
                {
                    'error': err.error
                },
            ]
            return jsonify(reply), err.status_code

        # all done
        return jsonify(comment.json), HTTPStatus.CREATED