def test_flag_bool_unique_user(): """Assert that a unique user can retrieve a flag, when using the local Flag.json file.""" app = Flask(__name__) app.env = 'development' app.config['LD_SDK_KEY'] = 'https://no.flag/avail' user = User(username='******', firstname='firstname', lastname='lastname', sub='sub', iss='iss') app_env = app.env try: with app.app_context(): flags = Flags() flags.init_app(app) app.env = 'development' val = flags.value('bool-flag', user) flag_on = flags.is_on('bool-flag', user) assert val assert flag_on except: # pylint: disable=bare-except; # noqa: B901, E722 # for tests we don't care assert False finally: app.env = app_env
def test_comment_json_output(session, client, jwt): """Assert the json output of a comment is correctly formatted.""" identifier = 'CP7654321' b = factory_business(identifier) f = factory_filing(b, ANNUAL_REPORT) u = User(username='******', firstname='firstname', lastname='lastname', sub='sub', iss='iss') u.save() c = factory_comment(b, f, 'some specific text', u) system_timezone = datetime.datetime.now().astimezone().tzinfo expected_timestamp = \ datetime.datetime(1970, 1, 1, 0, 0).replace(tzinfo=datetime.timezone.utc).astimezone(tz=system_timezone) rv = client.get( f'/api/v1/businesses/{identifier}/filings/{f.id}/comments/{c.id}', headers=create_header(jwt, [STAFF_ROLE])) assert HTTPStatus.OK == rv.status_code assert 'some specific text' == rv.json.get('comment').get('comment') assert 'firstname lastname' == rv.json.get('comment').get( 'submitterDisplayName') assert expected_timestamp.isoformat() == rv.json.get('comment').get( 'timestamp')
def test_business_comment_json_output(session, client, jwt): """Assert the json output of a comment is correctly formatted.""" identifier = 'CP7654321' b = factory_business(identifier) u = User(username='******', firstname='firstname', lastname='lastname', sub='sub', iss='iss') u.save() now = datetime.datetime(1970, 1, 1, 0, 0).replace(tzinfo=datetime.timezone.utc) with freeze_time(now): factory_business_comment(b, 'some specific text', u) rv = client.get(f'/api/v2/businesses/{identifier}/comments', headers=create_header(jwt, [STAFF_ROLE])) assert HTTPStatus.OK == rv.status_code assert 'some specific text' == rv.json.get('comments')[0].get( 'comment').get('comment') assert 'firstname lastname' == rv.json.get('comments')[0].get( 'comment').get('submitterDisplayName') assert now.isoformat() == rv.json.get('comments')[0].get( 'comment').get('timestamp')
def test_user_delete(session): """Assert the User record is deleted.""" user = User(username='******', firstname='firstname', lastname='lastname', sub='sub', iss='iss') user.save() user.delete() assert user.id is not None
def factory_user(username: str, firstname: str = None, lastname: str = None): user = User() user.username = username user.firstname = firstname user.lastname = lastname user.save() return user
def test_find_by_sub(session): """Assert find User by the unique sub key.""" user = User(username='******', firstname='firstname', lastname='lastname', sub='sub', iss='iss') session.add(user) session.commit() u = User.find_by_sub('sub') assert u.id is not None
def test_find_by_username(session): """Assert User can be found by the most current username.""" user = User(username='******', firstname='firstname', lastname='lastname', sub='sub', iss='iss') session.add(user) session.commit() u = User.find_by_username('username') assert u.id is not None
def test_user_display_name(session, test_description, username, firstname, lastname, display_name): """Assert the User record is deleted.""" user = User(username=username, firstname=firstname, lastname=lastname, sub='sub', iss='iss') assert display_name == user.display_name
def test_user(session): """Assert that a User can be stored in the service. Start with a blank database. """ user = User(username='******', firstname='firstname', lastname='lastname', sub='sub', iss='iss') session.add(user) session.commit() assert user.id is not None
def test_user_find_by_jwt_token(session): """Assert that a User can be stored in the service. Start with a blank database. """ user = User(username='******', firstname='firstname', lastname='lastname', sub='sub', iss='iss') session.add(user) session.commit() token = {'sub': 'sub'} u = User.find_by_jwt_token(token) assert u.id is not None
def create_user(username='******', firstname='firstname', lastname='lastname', sub='sub', iss='iss'): """Create a user.""" from legal_api.models import User new_user = User( username=username, firstname=firstname, lastname=lastname, sub=sub, iss=iss, ) new_user.save() return new_user
def test_flag_bool_unique_user(app): """Assert that a unique user can retrieve a flag.""" from legal_api import flags user = User(username='******', firstname='firstname', lastname='lastname', sub='sub', iss='iss') with app.app_context(): val = flags.value('bool-flag', user) on = flags.is_on('bool-flag', user) assert val assert on
def test_filing_dump_json(session): """Assert the filing json serialization works correctly.""" import copy identifier = 'CP7654321' b = factory_business(identifier) # Check base JSON filings = factory_filing(b, ANNUAL_REPORT) assert filings.json['filing']['business'] == ANNUAL_REPORT['filing'][ 'business'] assert filings.json['filing']['annualReport'] == ANNUAL_REPORT['filing'][ 'annualReport'] # Check payment token ar = copy.deepcopy(ANNUAL_REPORT) token = 'token' ar['filing']['header']['paymentToken'] = token filings = factory_filing(b, ar) assert filings.json['filing']['header']['paymentToken'] == token # check submitter u = User() u.username = '******' u.save() ar = copy.deepcopy(ANNUAL_REPORT) filings = factory_filing(b, ar) filings.submitter_id = u.id filings.save() assert filings.json['filing']['header']['submitter'] == u.username # check Exception ar = copy.deepcopy(ANNUAL_REPORT) filings = factory_filing(b, ar) filings.save() filings.submitter_id = -1 # some bogus id to throw an error with pytest.raises(KeyError): filings.json()
def test_flag_bool_unique_user(app): """Assert that a unique user can retrieve a flag, when using the local Flag.json file.""" from legal_api import flags user = User(username='******', firstname='firstname', lastname='lastname', sub='sub', iss='iss') app_env = app.env try: with app.app_context(): app.env = 'development' val = flags.value('bool-flag', user) flag_on = flags.is_on('bool-flag', user) assert val assert flag_on except: # pylint: disable=bare-except; # noqa: B901, E722 # for tests we don't care assert False finally: app.env = app_env
def create_user(user_name: str): """Return a new user model.""" user = User() user.username = user_name user.save() return user