def create_app(test_config=None): app = Flask(__name__) make_config(app) dbconfig = get_db_config() connection_pool = mysql.connector.pooling.MySQLConnectionPool(pool_name="mypool", pool_size=3, **dbconfig) CORS(app) # DataModel layer user_dao = UserDao(connection_pool) move_dao = MoveDao(connection_pool) housecleaning_dao = HouseCleaningDao(connection_pool) # Service layer services = Services services.user_service = UserService(user_dao) services.move_service = MoveService(move_dao) services.housecleaning_service = HouseCleaningService(housecleaning_dao) # Create endpoints UserView.create_endpoint(app, services) Moveview.create_endpoint(app, services) HouseCleaningView.create_endpoint(app, services) return app
def cons_user_blueprint(secret): user_bp = Blueprint('user_bp', __name__) user_control = UserService() @user_bp.route('/login', methods = ['POST']) def login(): if request.method == 'POST': if request.content_type == content_type: requestJson = request.get_json(force = True) content = removePass(user_control.login(requestJson, secret)) else: content = errorContent() return buildResponse('application/json', content) @user_bp.route('/users', methods = ['POST']) @token_required def register(current_user): if request.method == 'POST': if request.content_type == content_type: requestJson = request.get_json(force = True) content = removePass(user_control.add_user(requestJson)) else: content = errorContent() return buildResponse('application/json', content) @user_bp.route('/users/<id>', methods = ['PUT', 'PATCH']) @token_required def update(current_user, id): method = request.method if method == 'PUT' or method == 'PATCH': if request.content_type == content_type: requestJson = request.get_json(force = True) content = removePass(user_control.update_user(id, requestJson)) else: content = errorContent() return buildResponse('application/json', content) @user_bp.route('/users', methods = ['GET']) @token_required def get_all(current_user): content = removePassSet(user_control.get_all()) if request.content_type == content_type else errorContent() print('********', content) return buildResponse('application/json', content) @user_bp.route('/users/<id>', methods = ['GET']) @token_required def get(current_user, id): content = removePass(user_control.get(id)) if request.content_type == content_type else errorContent() return buildResponse('application/json', content) @user_bp.route('/users/<id>', methods = ['DELETE']) @token_required def delete(current_user, id): content = removePass(user_control.delete(id)) if request.content_type == content_type else errorContent() return buildResponse('application/json', content) return user_bp
def setUp(self): # self.db_fd, flask.app.config['DATABASE'] = tempfile.mkstemp() # self.app = flask.app.test_client() # flask.init_db() app = Flask(__name__) env_config = DevelopmentConfig app.config.from_object(env_config) db = create_engine( 'postgresql://*****:*****@localhost/heartsync_data') con = db.connect() metadata = MetaData() mail = Mail(app) self.user_repo = UserRepo(con, metadata, db, app, mail) self.user_service = UserService(self.user_repo) self.file_repo = FileRepo(con, metadata, db, app) self.file_service = FileService(self.file_repo) self.scan_repo = ScanRepo(con, metadata, db, app) self.scan_service = ScanService(self.scan_repo) self.notes_repo = NoteRepo(con, metadata, db, app) self.notes_service = NoteService(self.notes_repo) self.comment_repo = CommentRepo(con, metadata, db, app) self.comment_service = CommentService(self.comment_repo)
def decorated(*args, **kwargs): token = None if 'x-access-token' in request.headers: token = request.headers['x-access-token'] if not token: content = 401, {"error": 'Unauthorized, token is missing'} return buildResponse('application/json', content) secret = 'developedbydbgroldan' try: data = jwt.decode(token, secret) code, user = UserService().get(data.get('id')) current_user = user if code == 200 else False if not current_user: content = 401, 'Unauthorized, token is invalid' return buildResponse('application/json', content) except Exception as e: content = 401, {"error": 'Unauthorized, token is invalid'} return buildResponse('application/json', content) return f(current_user, *args, **kwargs)
app = Flask(__name__) CORS(app) env_config = DevelopmentConfig app.config.from_object(env_config) try: db = create_engine('postgresql://*****:*****@localhost/heartsync_data') con = db.connect() metadata = MetaData() except: logging.error("Failed to connect to database.") mail = Mail(app) user_repo = UserRepo(con, metadata, db, app, mail) user_service = UserService(user_repo) file_repo = FileRepo(con, metadata, db, app) file_service = FileService(file_repo) scan_repo = ScanRepo(con, metadata, db, app) scan_service = ScanService(scan_repo) notes_repo = NoteRepo(con, metadata, db, app) notes_service = NoteService(notes_repo) comment_repo = CommentRepo(con, metadata, db, app) comment_service = CommentService(comment_repo) @app.route('/api/auth/login', methods=['POST']) def login(): user = json.loads(request.get_data()) if 'username' not in user.keys() or 'password' not in user.keys(): response = make_response('Invalid login parameters', 400)
class Test(): def setUp(self): # self.db_fd, flask.app.config['DATABASE'] = tempfile.mkstemp() # self.app = flask.app.test_client() # flask.init_db() app = Flask(__name__) env_config = DevelopmentConfig app.config.from_object(env_config) db = create_engine( 'postgresql://*****:*****@localhost/heartsync_data') con = db.connect() metadata = MetaData() mail = Mail(app) self.user_repo = UserRepo(con, metadata, db, app, mail) self.user_service = UserService(self.user_repo) self.file_repo = FileRepo(con, metadata, db, app) self.file_service = FileService(self.file_repo) self.scan_repo = ScanRepo(con, metadata, db, app) self.scan_service = ScanService(self.scan_repo) self.notes_repo = NoteRepo(con, metadata, db, app) self.notes_service = NoteService(self.notes_repo) self.comment_repo = CommentRepo(con, metadata, db, app) self.comment_service = CommentService(self.comment_repo) def tearDown(self): os.close(self.db_fd) os.unlink(flask.app.config['DATABASE']) def comment_repo_test(self): scanid = 13 username = '******' text = 'mytest' date = '13/05/1999' id = self.comment_repo.insert_comment(scanid, username, text, date) assert (self.comment_repo.get_comment_by_id(id) is not None) assert (self.comment_repo.get_comments_by_scan(scanid) != "") self.comment_repo.delete_comment(id[0]) def comment_service_test(self): scanid = 13 username = '******' text = 'mytest' date = '13/05/1999' comment = self.comment_service.save_comment(scanid, username, text) assert (comment['scanid'] == 13) assert (comment['username'] == 'dariamarc') assert (comment['text'] == 'mytest') assert (self.comment_service.get_comments_by_scan(scanid) != "") self.comment_repo.delete_comment(comment['id']) def file_repo_test(self): file_path = "test/path" file_type = "obj" file_size = 13 id = self.file_repo.insert_file(file_path, file_type, file_size) assert (self.file_repo.get_file(id[0]) is not None) self.file_repo.delete_file(id[0]) def file_service_test(self): file_path = "../mytest/testfile/test.txt" file_type = "obj" file_size = 13 id = self.file_repo.insert_file(file_path, file_type, file_size) assert (self.file_service.get_file(id[0]) is not None) self.file_repo.delete_file(id[0]) def note_repo_test(self): scanid = 13 text = 'mytest' id = self.notes_repo.insert_note(scanid, text) assert (self.notes_repo.get_note_by_id(id[0]) is not None) assert (self.notes_repo.get_note_by_scan(scanid) is not None) self.notes_repo.delete_note(id[0]) def note_service_test(self): scanid = 13 text = 'mytest' note = self.notes_service.save_note(scanid, text) assert (self.notes_repo.get_note_by_id(note['id']) is not None) assert (self.notes_service.get_note_by_scan(scanid) is not None) self.notes_repo.delete_note(note['id']) def scan_repo_test(self): username = '******' fileid = 13 name = 'testscan' id = self.scan_repo.insert_scan(username, fileid, name) assert (self.scan_repo.get_scan(id[0]) is not None) assert (self.scan_repo.get_user_scans(username) != "") self.scan_repo.delete_scan(id[0]) def scan_service_test(self): username = '******' fileid = 13 name = 'testscan' scan = self.scan_service.save_scan(username, fileid, name) assert (self.scan_service.get_scan_by_id(scan['id']) is not None) assert (self.scan_service.get_user_scans(username) != "") self.scan_repo.delete_scan(scan['id']) def user_repo_test(self): username = '******' password = '******' firstname = 'test' lastname = 'test' email = 'test' self.user_repo.signup(username, password, email, firstname, lastname) token = self.user_repo.login(username, password) assert (token is not None) self.user_repo.delete_user(username) def user_service_test(self): username = '******' password = '******' firstname = 'test' lastname = 'test' email = 'test' self.user_repo.signup(username, password, email, firstname, lastname) token = self.user_service.login(username, password) assert (token is not None) self.user_repo.delete_user(username)
def user_service_control(): user_control = UserService() yield user_control