Пример #1
0
def test_comment_dump_json(session):
    """Assert the comment json serialization works correctly."""
    identifier = 'CP7654321'
    b = factory_business(session, identifier)
    c = Comment()
    c.business_id = b.id
    c.timestamp = EPOCH_DATETIME
    c.comment = 'a comment'

    assert c.json() == {'comment': 'a comment',
                        'id': None,
                        'staff': 'unknown',
                        'timestamp': datetime.datetime(1970, 1, 1, 0, 0)}
Пример #2
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
Пример #3
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.'
Пример #4
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)
Пример #5
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