def test_routes_simple(self): """The Journey.routes_simple property should return one (endpoint, route, methods) per route""" bp_name = 'test' expected_bp_path = '/test' bp_route_path = '/route' bpb_path = '/api/test' bp = Blueprint(bp_name, __name__) @bp.route(bp_route_path) def test_route(): return None bpb = BlueprintBundle(bpb_path) bpb.attach_bp(bp) j = Journey() j.attach_bundle(bpb) j.init_app(self.app) expected_endpoint_name = "{0}.{1}".format(bp_name, 'test_route') expected_route_path = "{0}{1}{2}".format(bpb_path, expected_bp_path, bp_route_path) self.assertEqual(j.routes_simple[0][0], expected_endpoint_name) self.assertEqual(j.routes_simple[0][1], expected_route_path)
def test_routes_detailed(self): """The Journey.routes_detailed property should return a list of bundles with blueprints and routes""" bp_name = 'test' expected_bp_path = '/test' bp_route_path = '/route' bpb_path = '/api/test' bp = Blueprint(bp_name, __name__) @bp.route(bp_route_path) def test_route(): return None bpb = BlueprintBundle(bpb_path) bpb.attach_bp(bp) j = Journey() j.attach_bundle(bpb) j.init_app(self.app) regged_bpb = j.routes_detailed[0] regged_bp = regged_bpb['blueprints'][0] regged_bp_route = regged_bp['routes'][0] self.assertEqual(regged_bpb['path'], bpb_path) self.assertEqual(regged_bp['path'], expected_bp_path) self.assertEqual(regged_bp_route['path'], bp_route_path)
def test_bundle_journey_conflict(self): """Adding a bundle for a path that already exists should raise ConflictingPath""" bpb_path = '/' bpb = BlueprintBundle(bpb_path) bpb.attach_bp(self.blueprint) j = Journey() j._journey_path = '/' self.assertRaises(ConflictingPath, j.attach_bundle, bpb)
def test_attach_blueprint(self): """Attached blueprints should end up in BlueprintBundle.blueprints""" bp1 = Blueprint('bp1', __name__) bp2 = Blueprint('bp2', __name__) bpb = BlueprintBundle(path='/test') bpb.attach_bp(bp1) bpb.attach_bp(bp2) # The bundle should contain the attached blueprints self.assertTrue(bpb.blueprints.__contains__, bp1) self.assertTrue(bpb.blueprints.__contains__, bp2)
def test_attach_invalid_blueprint(self): """Attaching a non-flask.Blueprint object should raise an InvalidBlueprint exception""" bpb = BlueprintBundle(path='/test') self.assertRaises(InvalidBlueprint, bpb.attach_bp, dict()) self.assertRaises(InvalidBlueprint, bpb.attach_bp, object()) self.assertRaises(InvalidBlueprint, bpb.attach_bp, None)
def test_add_bundle_description(self): """Bundle description should equal whatever what passed""" description = 'test' bpb = BlueprintBundle(path='/test', description=description) self.assertEqual(bpb.description, description)
def test_create_valid_path(self): """Improper paths should get sanitized, valid paths should return what was provided""" b1_path = '/' b2_path = '/test///' b3_path = '/test/test' b1 = BlueprintBundle(b1_path) b2 = BlueprintBundle(b2_path) b3 = BlueprintBundle(b3_path) # Single slash shouldn't get stripped self.assertEqual(b1.path, b1_path) # Trailing slashes should get stripped self.assertEqual(b2.path, b2_path.rstrip('/')) # Clean paths should equal what was provided self.assertEqual(b3.path, b3_path)
def setUp(self): app = Flask(__name__) app.config['DEBUG'] = True app.config['TESTING'] = True app.logger.disabled = True self.app = app self.bundle = BlueprintBundle(path='/test') self.blueprint = Blueprint('test', __name__)
def test_duplicate_bundle(self): """Adding a bundle for a path that already exists should raise ConflictingPath""" bpb1_path = '/api/v1' bpb2_path = '/api/v1' bpb1 = BlueprintBundle(bpb1_path) bpb1.attach_bp(self.blueprint) bpb2 = BlueprintBundle(bpb2_path) bpb2.attach_bp(self.blueprint) j = Journey() j.attach_bundle(bpb1) self.assertRaises(ConflictingPath, j.attach_bundle, bpb2)
def test_add_blueprint_description(self): """Bundle description should equal whatever what passed""" b1_description = 'test1' b2_description = 'test2' bp1 = Blueprint('bp1', __name__) bp2 = Blueprint('bp2', __name__) bpb = BlueprintBundle(path='/test') bpb.attach_bp(bp1, b1_description) bpb.attach_bp(bp2, b2_description) b1_match = False b2_match = False for (bp, description) in bpb.blueprints: if bp == bp1 and description == b1_description: b1_match = True elif bp == bp2 and description == b2_description: b2_match = True self.assertTrue(b1_match) self.assertTrue(b2_match)
def test_constructor_direct(self): """Initializing the app directly with a list of bundles should work""" bp1_name = 'test1' bp2_name = 'test2' expected_bp1_path = '/test1' expected_bp2_path = '/test2' bpb1_path = '/api/v1' bpb2_path = '/api/v2' bp1 = Blueprint(bp1_name, __name__) bp2 = Blueprint(bp2_name, __name__) bpb1 = BlueprintBundle(path=bpb1_path) bpb1.attach_bp(bp1) bpb2 = BlueprintBundle(path=bpb2_path) bpb2.attach_bp(bp2) j = Journey(self.app, bundles=[bpb1, bpb2]) matched_bpb1 = matched_bpb2 = matched_bp1 = matched_bp2 = False for registered_bundle in j._registered_bundles: if registered_bundle['path'] == bpb1_path: matched_bpb1 = True elif registered_bundle['path'] == bpb2_path: matched_bpb2 = True for blueprint in registered_bundle['blueprints']: if blueprint['name'] == bp1_name and blueprint[ 'path'] == expected_bp1_path: matched_bp1 = True elif blueprint['name'] == bp2_name and blueprint[ 'path'] == expected_bp2_path: matched_bp2 = True self.assertTrue(matched_bpb1) self.assertTrue(matched_bpb2) self.assertTrue(matched_bp1) self.assertTrue(matched_bp2)
def test_bp_reuse(self): """Reusing blueprints in bundles should work""" bpb1_path = '/api/v1' bpb2_path = '/api/v2' bp_name = 'bp_name' route_ep = 'test' bp = Blueprint(bp_name, __name__) @bp.route('/test') def test_route(): return False bpb1 = BlueprintBundle(bpb1_path) bpb1.attach_bp(bp) bpb2 = BlueprintBundle(bpb2_path) bpb2.attach_bp(bp) j = Journey() j.attach_bundle(bpb1) j.attach_bundle(bpb2) j.init_app(self.app) expected_bpb1_route = "{0}/{1}/{2}".format(bpb1_path, bp_name, route_ep) expected_bpb2_route = "{0}/{1}/{2}".format(bpb2_path, bp_name, route_ep) matched_bpb1_route = matched_bpb2_route = False for mapping in j.routes_simple: if mapping[1] == expected_bpb1_route: matched_bpb1_route = True elif mapping[1] == expected_bpb2_route: matched_bpb2_route = True self.assertTrue(matched_bpb1_route) self.assertTrue(matched_bpb2_route)
# -*- coding: utf-8 -*- from flask_journey import BlueprintBundle from .planes import bp as planes from .pilots import bp as pilots from .routes import bp as routes server_info = BlueprintBundle(path='/server-info') server_info.attach_bp(routes) v1 = BlueprintBundle(path='/api/v1') v1.attach_bp(planes, description='Planes API, CRUD') v1.attach_bp(pilots, description='Info about pilots')