Exemplo n.º 1
0
def test_get_sub_tiers_from_perms(database):
    """ Test getting sub tiers of agencies from the permissions provided """
    cgacs = [CGACFactory(cgac_code=str(i)) for i in range(3)]
    frec_cgac = CGACFactory()
    frecs = [FRECFactory(frec_code=str(i), cgac=frec_cgac) for i in range(3)]
    cgac_sub_tiers = [SubTierAgencyFactory(sub_tier_agency_code=str(i), cgac=cgacs[i], frec=None, is_frec=False,
                                           sub_tier_agency_name="Test Subtier Agency "+str(i)) for i in range(3)]
    frec_sub_tiers = [SubTierAgencyFactory(sub_tier_agency_code=str(3 + i), cgac=frec_cgac, frec=frecs[i], is_frec=True,
                                           sub_tier_agency_name="Test Subtier Agency " + str(3 + i)) for i in range(3)]
    database.session.add_all(cgacs + [frec_cgac] + frecs + cgac_sub_tiers + frec_sub_tiers)
    database.session.commit()

    # Test non-admin, should have 1 and 2 results for cgac and frec respectively
    cgac_result, frec_result = get_sub_tiers_from_perms(False, [cgacs[0].cgac_id], [frecs[0].frec_id, frecs[1].frec_id])
    assert cgac_result.count() == 1
    assert frec_result.count() == 2

    # Test non-admin with no permissions, should have 0 results for either
    cgac_result, frec_result = get_sub_tiers_from_perms(False, [], [])
    assert cgac_result.count() == 0
    assert frec_result.count() == 0

    # Test non-admin with permissions, should have 3 results for both (ignore permissions)
    cgac_result, frec_result = get_sub_tiers_from_perms(True, [cgacs[0].cgac_id], [frecs[0].frec_id, frecs[1].frec_id])
    assert cgac_result.count() == 3
    assert frec_result.count() == 3

    # Test non-admin with no permissions, should have 3 results for both
    cgac_result, frec_result = get_sub_tiers_from_perms(True, [], [])
    assert cgac_result.count() == 3
    assert frec_result.count() == 3
Exemplo n.º 2
0
def test_list_agencies_superuser(domain_app, database):
    """ All agencies should be visible to website admins """
    user = UserFactory(website_admin=True)
    cgacs = [CGACFactory(cgac_code=str(i)) for i in range(3)]
    frec_cgac = CGACFactory()
    frecs = [FRECFactory(frec_code=str(i), cgac=frec_cgac) for i in range(3)]
    cgac_sub_tiers = [
        SubTierAgencyFactory(sub_tier_agency_code=str(i),
                             cgac=cgacs[i],
                             frec=None,
                             is_frec=False,
                             sub_tier_agency_name="Test Subtier Agency " +
                             str(i)) for i in range(3)
    ]
    frec_sub_tiers = [
        SubTierAgencyFactory(sub_tier_agency_code=str(3 + i),
                             cgac=frec_cgac,
                             frec=frecs[i],
                             is_frec=True,
                             sub_tier_agency_name="Test Subtier Agency " +
                             str(3 + i)) for i in range(3)
    ]
    database.session.add_all(cgacs + [frec_cgac] + frecs + cgac_sub_tiers +
                             frec_sub_tiers + [user])
    database.session.commit()

    g.user = user
    result = domain_app.get('/v1/list_agencies/').data.decode('UTF-8')
    response = json.loads(result)
    result = {el['cgac_code'] for el in response['cgac_agency_list']}
    assert result == {'0', '1', '2'}  # i.e. all of them
    result = {el['frec_code'] for el in response['frec_agency_list']}
    assert result == {'0', '1', '2'}  # i.e. all of them
Exemplo n.º 3
0
def test_organize_sub_tier_agencies(database):
    """ Test organization of passed sub tier agencies """
    cgac = CGACFactory()
    frec_cgac = CGACFactory()
    frec = FRECFactory(cgac=frec_cgac)
    sub_tiers = [SubTierAgencyFactory(sub_tier_agency_code='0', cgac=cgac, frec=None, is_frec=False,
                                      sub_tier_agency_name="Test Subtier Agency 0"),
                 SubTierAgencyFactory(sub_tier_agency_code='1', cgac=frec_cgac, frec=frec, is_frec=True,
                                      sub_tier_agency_name="Test Subtier Agency 1")]
    database.session.add_all([cgac] + [frec_cgac] + [frec] + sub_tiers)
    database.session.commit()

    # Test with no sub tiers
    results = organize_sub_tier_agencies([])
    assert len(results["sub_tier_agency_list"]) == 0

    # Test with just one sub tier passed in
    results = organize_sub_tier_agencies([sub_tiers[0]])
    assert len(results["sub_tier_agency_list"]) == 1
    assert results["sub_tier_agency_list"][0] == {
        "agency_name": '{}: {}'.format(cgac.agency_name, sub_tiers[0].sub_tier_agency_name),
        "agency_code": sub_tiers[0].sub_tier_agency_code,
        "priority": sub_tiers[0].priority}

    # Test with both sub tiers passed
    results = organize_sub_tier_agencies([sub_tiers[0], sub_tiers[1]])
    assert len(results["sub_tier_agency_list"]) == 2
Exemplo n.º 4
0
def test_list_agencies_limits(monkeypatch, user_constants, domain_app):
    """List agencies should limit to only the user's agencies"""
    sess = GlobalDB.db().session
    user = UserFactory()
    cgac = CGACFactory()
    frec_cgac = CGACFactory()
    frec = FRECFactory(cgac=frec_cgac)
    sub_tiers = [
        SubTierAgencyFactory(sub_tier_agency_code='0',
                             cgac=cgac,
                             frec=None,
                             is_frec=False,
                             sub_tier_agency_name="Test Subtier Agency 0"),
        SubTierAgencyFactory(sub_tier_agency_code='1',
                             cgac=frec_cgac,
                             frec=frec,
                             is_frec=True,
                             sub_tier_agency_name="Test Subtier Agency 1")
    ]
    user.affiliations = [
        UserAffiliation(cgac=cgac, frec=None, permission_type_id=2),
        UserAffiliation(cgac=None, frec=frec, permission_type_id=2)
    ]
    sess.add_all([cgac] + [frec_cgac] + [frec] + sub_tiers + [user])
    sess.commit()
    monkeypatch.setattr(domainRoutes, 'g', Mock(user=user))

    result = domain_app.get('/v1/list_agencies/').data.decode('UTF-8')
    res = json.loads(result)
    assert len(res['cgac_agency_list']) == 1
    assert len(res['frec_agency_list']) == 1
    assert res['cgac_agency_list'][0]['agency_name'] == cgac.agency_name
    assert res['cgac_agency_list'][0]['cgac_code'] == cgac.cgac_code
    assert res['frec_agency_list'][0]['agency_name'] == frec.agency_name
    assert res['frec_agency_list'][0]['frec_code'] == frec.frec_code
def test_list_agencies_limits(domain_app, database):
    """ List agencies should limit to only the user's agencies and should not duplicate the same agency even if there
        are multiple instances of the same agency in the user permissions.
    """
    user = UserFactory()
    cgac = CGACFactory()
    frec_cgac = CGACFactory()
    frec = FRECFactory(cgac=frec_cgac)
    sub_tiers = [SubTierAgencyFactory(sub_tier_agency_code='0', cgac=cgac, frec=None, is_frec=False,
                                      sub_tier_agency_name="Test Subtier Agency 0"),
                 SubTierAgencyFactory(sub_tier_agency_code='1', cgac=frec_cgac, frec=frec, is_frec=True,
                                      sub_tier_agency_name="Test Subtier Agency 1")]
    user.affiliations = [UserAffiliation(cgac=cgac, frec=None, permission_type_id=PERMISSION_SHORT_DICT['w']),
                         UserAffiliation(cgac=cgac, frec=None, permission_type_id=PERMISSION_SHORT_DICT['f']),
                         UserAffiliation(cgac=None, frec=frec, permission_type_id=PERMISSION_SHORT_DICT['w']),
                         UserAffiliation(cgac=None, frec=frec, permission_type_id=PERMISSION_SHORT_DICT['f'])]
    database.session.add_all([cgac] + [frec_cgac] + [frec] + sub_tiers + [user])
    database.session.commit()

    g.user = user
    result = domain_app.get('/v1/list_agencies/').data.decode('UTF-8')
    res = json.loads(result)
    assert len(res['cgac_agency_list']) == 1
    assert len(res['frec_agency_list']) == 1
    assert res['cgac_agency_list'][0]['agency_name'] == cgac.agency_name
    assert res['cgac_agency_list'][0]['cgac_code'] == cgac.cgac_code
    assert res['frec_agency_list'][0]['agency_name'] == frec.agency_name
    assert res['frec_agency_list'][0]['frec_code'] == frec.frec_code
def test_get_accessible_agencies(database):
    """ Test listing all the agencies (CGAC and FREC) that are accessible based on permissions given """
    cgacs = [
        CGACFactory(cgac_code=str(i), agency_name="Test Agency " + str(i))
        for i in range(3)
    ]
    frec_cgac = CGACFactory()
    frecs = [FRECFactory(frec_code=str(i), cgac=frec_cgac) for i in range(3)]
    cgac_sub_tiers = [
        SubTierAgencyFactory(sub_tier_agency_code=str(i),
                             cgac=cgacs[i],
                             frec=None,
                             is_frec=False,
                             sub_tier_agency_name="Test Subtier Agency " +
                             str(i)) for i in range(3)
    ]
    frec_sub_tiers = [
        SubTierAgencyFactory(sub_tier_agency_code=str(3 + i),
                             cgac=frec_cgac,
                             frec=frecs[i],
                             is_frec=True,
                             sub_tier_agency_name="Test Subtier Agency " +
                             str(3 + i)) for i in range(3)
    ]
    database.session.add_all(cgacs + [frec_cgac] + frecs + cgac_sub_tiers +
                             frec_sub_tiers)
    database.session.commit()

    # Test one CGAC and 2 FRECs, have to decode it because we send it back as a response already
    results = get_accessible_agencies([cgac_sub_tiers[0]],
                                      [frec_sub_tiers[0], frec_sub_tiers[2]])
    frec_code_result = {el["frec_code"] for el in results["frec_agency_list"]}
    frec_name_result = {
        el["agency_name"]
        for el in results["frec_agency_list"]
    }
    assert len(results["cgac_agency_list"]) == 1
    assert len(results["frec_agency_list"]) == 2
    assert results["cgac_agency_list"][0]["agency_name"] == cgacs[
        0].agency_name
    assert results["cgac_agency_list"][0]["cgac_code"] == cgacs[0].cgac_code
    assert frec_name_result == {frecs[0].agency_name, frecs[2].agency_name}
    assert frec_code_result == {frecs[0].frec_code, frecs[2].frec_code}

    # Test when there are no FRECs
    results = get_accessible_agencies([cgac_sub_tiers[0]], [])
    assert len(results["cgac_agency_list"]) == 1
    assert len(results["frec_agency_list"]) == 0
Exemplo n.º 7
0
def test_get_accessible_agencies(database):
    """ Test listing all the agencies (CGAC and FREC) that are accessible based on permissions given """
    # The first cgac/frec don't have sub tiers associated, they should still show up when affiliations are present
    cgacs = [CGACFactory(cgac_code=str(i), agency_name="Test Agency " + str(i)) for i in range(3)]
    frec_cgac = CGACFactory()
    frecs = [FRECFactory(frec_code=str(i), cgac=frec_cgac) for i in range(3)]
    cgac_sub_tiers = [SubTierAgencyFactory(sub_tier_agency_code=str(i), cgac=cgacs[i], frec=None, is_frec=False,
                                           sub_tier_agency_name="Test Subtier Agency C" + str(i)) for i in range(1, 3)]
    frec_sub_tiers = [SubTierAgencyFactory(sub_tier_agency_code=str(3 + i), cgac=frec_cgac, frec=frecs[i], is_frec=True,
                                           sub_tier_agency_name="Test Subtier Agency F" + str(i)) for i in range(1, 3)]
    database.session.add_all(cgacs + [frec_cgac] + frecs + cgac_sub_tiers + frec_sub_tiers)
    database.session.commit()

    user = UserFactory(affiliations=[
        UserAffiliation(user_affiliation_id=1, cgac=cgacs[0], frec=None,
                        permission_type_id=PERMISSION_TYPE_DICT['writer']),
        UserAffiliation(user_affiliation_id=2, cgac=None, frec=frecs[1],
                        permission_type_id=PERMISSION_TYPE_DICT['reader']),
        UserAffiliation(user_affiliation_id=3, cgac=None, frec=frecs[2],
                        permission_type_id=PERMISSION_TYPE_DICT['reader'])
    ])
    database.session.add(user)
    database.session.commit()

    g.user = user

    # Test one CGAC and 2 FRECs, have to decode it because we send it back as a response already
    results = get_accessible_agencies()
    frec_code_result = {el["frec_code"] for el in results["frec_agency_list"]}
    frec_name_result = {el["agency_name"] for el in results["frec_agency_list"]}
    assert len(results["cgac_agency_list"]) == 1
    assert len(results["frec_agency_list"]) == 2
    assert results["cgac_agency_list"][0]["agency_name"] == cgacs[0].agency_name
    assert results["cgac_agency_list"][0]["cgac_code"] == cgacs[0].cgac_code
    assert frec_name_result == {frecs[1].agency_name, frecs[2].agency_name}
    assert frec_code_result == {frecs[1].frec_code, frecs[2].frec_code}

    # Test when user is website admin, should return everything, but only 2 frecs because only 2 of the 3 have the
    # frec flag
    user.affiliations = []
    user.website_admin = True
    results = get_accessible_agencies()
    assert len(results["cgac_agency_list"]) == 3
    assert len(results["frec_agency_list"]) == 2
Exemplo n.º 8
0
def test_get_all_agencies(database):
    """ Test printing out all agencies, FRECs only retrieved if they have a sub tier, CGACs always """
    cgacs = [CGACFactory(cgac_code=str(i), agency_name="Test Agency " + str(i)) for i in range(3)]
    frec_cgac = CGACFactory()
    frecs = [FRECFactory(frec_code=str(i), cgac=frec_cgac) for i in range(6, 9)]
    cgac_sub_tiers = [SubTierAgencyFactory(sub_tier_agency_code=str(i), cgac=cgacs[i], frec=None, is_frec=False,
                                           sub_tier_agency_name="Test Subtier Agency " + str(i)) for i in range(2)]
    frec_sub_tiers = [SubTierAgencyFactory(sub_tier_agency_code=str(3 + i), cgac=frec_cgac, frec=frecs[i], is_frec=True,
                                           sub_tier_agency_name="Test Subtier Agency " + str(3 + i)) for i in range(2)]
    database.session.add_all(cgacs + [frec_cgac] + frecs + cgac_sub_tiers + frec_sub_tiers)
    database.session.commit()

    results = get_all_agencies()
    cgac_result = {el["cgac_code"] for el in results["agency_list"]}
    frec_result = {el["frec_code"] for el in results["shared_agency_list"]}
    assert len(results["agency_list"]) == 3
    assert len(results["shared_agency_list"]) == 2
    assert cgac_result == {"0", "1", "2"}
    assert frec_result == {"6", "7"}
Exemplo n.º 9
0
def test_list_sub_tier_agencies_admin(domain_app, database):
    """ List all sub tiers that a user has FABS permissions for """
    user = UserFactory(website_admin=True)
    cgacs = [CGACFactory(cgac_code=str(i)) for i in range(3)]
    frec_cgac = CGACFactory()
    frecs = [FRECFactory(frec_code=str(i), cgac=frec_cgac) for i in range(3)]
    cgac_sub_tiers = [
        SubTierAgencyFactory(sub_tier_agency_code=str(i),
                             cgac=cgacs[i],
                             frec=None,
                             is_frec=False,
                             sub_tier_agency_name="Test Subtier Agency " +
                             str(i)) for i in range(3)
    ]
    frec_sub_tiers = [
        SubTierAgencyFactory(sub_tier_agency_code=str(3 + i),
                             cgac=frec_cgac,
                             frec=frecs[i],
                             is_frec=True,
                             sub_tier_agency_name="Test Subtier Agency " +
                             str(3 + i)) for i in range(3)
    ]
    user.affiliations = [
        UserAffiliation(cgac=cgacs[0],
                        frec=None,
                        permission_type_id=PERMISSION_SHORT_DICT['f']),
        UserAffiliation(cgac=None,
                        frec=frecs[2],
                        permission_type_id=PERMISSION_SHORT_DICT['f'])
    ]
    database.session.add_all(cgacs + [frec_cgac] + frecs + cgac_sub_tiers +
                             frec_sub_tiers + [user])
    database.session.commit()

    g.user = user
    result = domain_app.get('/v1/list_sub_tier_agencies/').data.decode('UTF-8')
    response = json.loads(result)
    result = {el['agency_code'] for el in response['sub_tier_agency_list']}
    assert len(response["sub_tier_agency_list"]
               ) == 6  # All of them, ignores affiliations
    assert result == {'0', '1', '2', '3', '4',
                      '5'}  # All of them, ignores affiliations
Exemplo n.º 10
0
def test_list_agencies_all(monkeypatch, user_constants, domain_app):
    """All agencies should be visible to website admins"""
    sess = GlobalDB.db().session
    user = UserFactory()
    cgacs = [CGACFactory(cgac_code=str(i)) for i in range(3)]
    frec_cgac = CGACFactory()
    frecs = [FRECFactory(frec_code=str(i), cgac=frec_cgac) for i in range(3)]
    cgac_sub_tiers = [
        SubTierAgencyFactory(sub_tier_agency_code=str(i),
                             cgac=cgacs[i],
                             frec=None,
                             is_frec=False,
                             sub_tier_agency_name="Test Subtier Agency " +
                             str(i)) for i in range(3)
    ]
    frec_sub_tiers = [
        SubTierAgencyFactory(sub_tier_agency_code=str(3 + i),
                             cgac=frec_cgac,
                             frec=frecs[i],
                             is_frec=True,
                             sub_tier_agency_name="Test Subtier Agency " +
                             str(3 + i)) for i in range(3)
    ]
    user.affiliations = [
        UserAffiliation(cgac=cgacs[0], frec=frecs[0], permission_type_id=2)
    ]
    sess.add_all(cgacs + [frec_cgac] + frecs + cgac_sub_tiers +
                 frec_sub_tiers + [user])
    sess.commit()
    monkeypatch.setattr(domainRoutes, 'g', Mock(user=user))

    result = domain_app.get('/v1/list_all_agencies/').data.decode('UTF-8')
    response = json.loads(result)
    result = {el['cgac_code'] for el in response['agency_list']}
    assert result == {'0', '1', '2'}  # i.e. all of them
    result = {el['frec_code'] for el in response['shared_agency_list']}
    assert result == {'0', '1', '2'}  # i.e. all of them
Exemplo n.º 11
0
def test_get_cgacs_without_sub_tier_agencies(database):
    """ Test getting all cgacs without any sub tier agencies """
    sub_tier_cgac = CGACFactory()
    no_sub_cgac = CGACFactory()
    sub_tier = SubTierAgencyFactory(sub_tier_agency_code="0", cgac=sub_tier_cgac, frec=None, is_frec=False,
                                    sub_tier_agency_name="Test Subtier Agency")
    database.session.add_all([sub_tier_cgac] + [no_sub_cgac] + [sub_tier])
    database.session.commit()

    # Test while passing the session
    results = get_cgacs_without_sub_tier_agencies(database.session)
    assert len(results) == 1
    assert results[0].cgac_id == no_sub_cgac.cgac_id

    # Test without passing the session
    results = get_cgacs_without_sub_tier_agencies()
    assert len(results) == 1
    assert results[0].cgac_id == no_sub_cgac.cgac_id
Exemplo n.º 12
0
def initialize_db_values(db,
                         cfda_title=None,
                         cgac_code=None,
                         frec_code=None,
                         use_frec=False):
    """ Initialize the values in the DB that can be used throughout the tests """
    if cgac_code:
        cgac = CGACFactory(cgac_code=cgac_code, agency_name="Test CGAC Agency")
    else:
        cgac = CGACFactory()
    if frec_code:
        frec = FRECFactory(frec_code=frec_code,
                           agency_name="Test FREC Agency",
                           cgac=cgac)
    else:
        frec = FRECFactory(cgac=cgac)
    db.session.add_all([cgac, frec])
    db.session.commit()

    cfda_number = CFDAProgramFactory(program_number=12.345,
                                     program_title=cfda_title)
    sub_tier = SubTierAgencyFactory(sub_tier_agency_code="1234",
                                    cgac=cgac,
                                    frec=frec,
                                    is_frec=use_frec,
                                    sub_tier_agency_name="Test Subtier Agency")
    state = StatesFactory(state_code="NY", state_name="New York")
    zip_code_1 = ZipsFactory(zip5="12345",
                             zip_last4="6789",
                             state_abbreviation=state.state_code,
                             county_number="001",
                             congressional_district_no="01")
    zip_code_2 = ZipsFactory(zip5="12345",
                             zip_last4="4321",
                             state_abbreviation=state.state_code,
                             county_number="001",
                             congressional_district_no="02")
    zip_code_3 = ZipsFactory(zip5="54321",
                             zip_last4="4321",
                             state_abbreviation=state.state_code,
                             county_number="001",
                             congressional_district_no="05")
    zip_code_4 = ZipsFactory(zip5="98765",
                             zip_last4="4321",
                             state_abbreviation=state.state_code,
                             county_number="001",
                             congressional_district_no=None)
    zip_city = ZipCityFactory(zip_code=zip_code_1.zip5,
                              city_name="Test Zip City")
    zip_city_2 = ZipCityFactory(zip_code=zip_code_3.zip5,
                                city_name="Test Zip City 2")
    zip_city_3 = ZipCityFactory(zip_code=zip_code_4.zip5,
                                city_name="Test Zip City 3")
    county_code = CountyCodeFactory(state_code=state.state_code,
                                    county_number=zip_code_1.county_number,
                                    county_name="Test County")
    city_code = CityCodeFactory(feature_name="Test City",
                                city_code="00001",
                                state_code=state.state_code,
                                county_number=zip_code_1.county_number,
                                county_name="Test City County")
    contracting_office = FPDSContractingOfficeFactory(
        contracting_office_code='033103', contracting_office_name='Office')
    country_code = CountryCodeFactory(country_code='USA',
                                      country_name='United States of America')
    db.session.add_all([
        sub_tier, state, cfda_number, zip_code_1, zip_code_2, zip_code_3,
        zip_code_4, zip_city, zip_city_2, zip_city_3, county_code, city_code,
        contracting_office, country_code
    ])
    db.session.commit()