def __init__(self): self.first_name = "" self.last_name = "" self.user_name = "" self.email = "" self.password = "" self.created_at = None self.admin = False self.querry = UserQueries() self.user = {} self.users_list = []
def setUp(self): My_app.config.from_object(app_config["testing"]) self.app = My_app.test_client() self.db_obj = DbConn() self.con = self.db_obj.create_connection() self.db_obj.create_users_table() self.db_obj.create_menu_table() self.db_obj.create_orders_table() self.querry = UserQueries() self.hostname = "http://*****:*****@gmail.com", "password": "******" } self.resgistered_user = {"user_name": "mos", "password": "******"}
class BaseTestCase(unittest.TestCase): def setUp(self): My_app.config.from_object(app_config["testing"]) self.app = My_app.test_client() self.db_obj = DbConn() self.con = self.db_obj.create_connection() self.db_obj.create_users_table() self.db_obj.create_menu_table() self.db_obj.create_orders_table() self.querry = UserQueries() self.hostname = "http://*****:*****@gmail.com", "password": "******" } self.resgistered_user = {"user_name": "mos", "password": "******"} def tearDown(self): cursor = self.con.cursor() cursor.execute("DROP TABLE IF EXISTS users CASCADE") cursor.execute("DROP TABLE IF EXISTS menu CASCADE") cursor.execute("DROP TABLE IF EXISTS orders CASCADE") self.con.commit() self.con.close() def register_user(self, new_user): return self.app.post(self.hostname + "auth/signup", data=json.dumps(new_user), content_type='application/json') def login_user(self, user): return self.app.post(self.hostname + "auth/login", data=json.dumps(user), content_type='application/json') def make_admin(self, user): self.querry.authorise_user(user, True) def post_food(self, food, token): return self.app.post(self.hostname + "menu", data=json.dumps(food), content_type='application/json', headers=({ "acces-token": token })) def get_all_orders(self, token): return self.app.get(self.hostname + "orders", headers=({ "acces-token": token })) def post_order(self, order, token): return self.app.post(self.hostname + "users/orders", data=json.dumps(order), content_type='application/json', headers=({ "acces-token": token })) def fetch_specific_order(self, uuid, token): return self.app.get(self.hostname + "orders/" + str(uuid), headers=({ "acces-token": token })) def update_status(self, status, uuid, token): return self.app.put(self.hostname + "orders/" + str(uuid), data=json.dumps(status), content_type='application/json', headers=({ "acces-token": token })) def get_user_order_history(self, token): return self.app.get(self.hostname + "users/orders", headers=({ "acces-token": token })) def get_menu(self, token): return self.app.get(self.hostname + "menu", headers=({ "acces-token": token })) def get_all_users(self, token): return self.app.get(self.hostname + "users", headers=({ "acces-token": token })) def promote_user(self, user, token): return self.app.put(self.hostname + "promote", data=json.dumps(user), content_type='application/json', headers=({ "acces-token": token }))
class User: def __init__(self): self.first_name = "" self.last_name = "" self.user_name = "" self.email = "" self.password = "" self.created_at = None self.admin = False self.querry = UserQueries() self.user = {} self.users_list = [] def validate_password(self, password): password_error = {} if len(password)< 6 : password_error = {"error" : "weak password. Password must be atleat 6 charactors long"} return password_error if not re.search('[0-9]', password): password_error ={"error" : "Weak password. Password should have atleast one integer"} return password_error return password_error def validate_email(self, email): "function to verify email" match = re.match('[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z])', email) if match == None: return False else: return True def add_user(self, first_name, last_name, user_name, email,pasword ): self.first_name = first_name self.last_name = last_name self.user_name = user_name self.email = email self.password = generate_password_hash(pasword, method="sha256") self.created_at = datetime.datetime.now() self.admin = False self.querry.insert_user(self.first_name,self.last_name, self.user_name, self.email, self.password, self.created_at, self.admin) return self.querry.get_user(self.user_name) def validate_user_obj(self, user_obj): "A method to validate a food object" if ( "first_name" in user_obj and "last_name" in user_obj and "user_name" in user_obj and "email" in user_obj and "password" in user_obj): return True return False def check_existing_user(self,user_name, email): "a method to check whethr a given order already exists" exist = False for user in self.querry.get_all_users(self.users_list): if user["user_name"] == user_name or user["email"] == email: exist = True break else: exist = False return exist def validate_login_obj(self, login_obj): "A method to validate a food object" if ("user_name" in login_obj and "password" in login_obj ): return True return False def validate_login_user(self, username, password): login_user = {} for user in self.querry.get_all_users(self.users_list): if user['user_name'] == username and check_password_hash(user["password"], password): login_user =user break; pass return login_user def generate_auth_token(self, user_id): "generating the tocken" payload = { #user name "user_id": user_id, #expiry ate of token "exp": datetime.datetime.utcnow() + datetime.timedelta(minutes=30) } token = jwt.encode( payload, current_app.config.get('SECRET_KEY'),algorithm='HS256' ) return token.decode('UTF-8')
from functools import wraps import jwt from app.models.orders import Orders from app.models.users import User from app.models.db_user_sql_queries import UserQueries from flasgger import swag_from from flask import Blueprint, jsonify, request, current_app from flask import redirect user_blue = Blueprint('user_b', __name__) user_obj = User() order_obj = Orders() querry = UserQueries() @user_blue.route('/api/v1/auth/signup', methods=['POST']) @swag_from('../docs/signup.yml') def user_signup(): " a function to signp user" new_user = request.json #validating user object if not user_obj.validate_user_obj(new_user): return jsonify({"error": "ivalid user obj"}), 400 new_first_name = new_user["first_name"] new_last_name = new_user["last_name"] new_user_name = new_user["user_name"] new_email = new_user["email"] new_password = new_user["password"]