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
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
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)}
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
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)
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.'
def load_ledger(business, founding_date): """Create a ledger of all filing types.""" i = 0 for k, filing_meta in Filing.FILINGS.items(): filing = copy.deepcopy(FILING_TEMPLATE) filing['filing']['header']['name'] = filing_meta['name'] f = factory_completed_filing(business, filing, filing_date=founding_date + datedelta.datedelta(months=i)) for c in range(i): comment = Comment() comment.comment = f'this comment {c}' f.comments.append(comment) f.save() i += 1 return i
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
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)
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
def test_ledger_comment_count(session, client, jwt): """Assert that the ledger returns the correct number of comments.""" # setup identifier = 'BC1234567' number_of_comments = 10 business, filing_storage = ledger_element_setup_help(identifier) for c in range(number_of_comments): comment = Comment() comment.comment = f'this comment {c}' filing_storage.comments.append(comment) filing_storage.save() # test rv = client.get(f'/api/v2/businesses/{identifier}/filings', headers=create_header(jwt, [UserRoles.SYSTEM.value], identifier)) # validate assert rv.json['filings'][0]['commentsCount'] == number_of_comments