def create_tables(): # note that the import is *inside* this function so that we can catch # errors that happen at import time from app import app from app import models models.create_tables() # models.Role.create(name='admin',description='Administrator') # models.Role.create(name='editor',description='Editor') #models.UserRoles.create(user=1,role=1) #models.UserRoles.create(user=1,role=2) print('Successfully initialized database') return app
# coding: utf-8 from app.models import create_tables create_tables()
def setUp(self): """Define test variables and initialize app""" self.app = create_app(config_name="testing") self.client = self.app.test_client self.conn = psycopg2.connect(os.getenv('database')) self.cur = self.conn.cursor() with self.app.app_context(): # connect() create_tables() # Creating user hashed_password = generate_password_hash("123456789", method='sha256') query = "INSERT INTO users (email, username, password, is_driver, is_admin) VALUES " \ "('*****@*****.**', 'test_user', '" + hashed_password + "', '" + '0' + "','" + '0' + "' )" self.cur.execute(query) self.conn.commit() # Creating driver conn = psycopg2.connect(os.getenv('database')) cur = conn.cursor() hashed_password = generate_password_hash("123456789", method='sha256') query = "INSERT INTO users (email, username, password, is_driver, is_admin) VALUES " \ "('*****@*****.**', 'test_driver', '" + hashed_password + "', '" + '1' + "','" + '0' + "' )" cur.execute(query) conn.commit() # Creating Admin conn = psycopg2.connect(os.getenv('database')) cur = conn.cursor() hashed_password = generate_password_hash("admin2018", method='sha256') query = "INSERT INTO users (email, username, password, is_driver, is_admin) VALUES " \ "('*****@*****.**', 'admin', '" + hashed_password + "', '" + '0' + "','" + '1' + "' )" cur.execute(query) conn.commit() # Add ride conn = psycopg2.connect(os.getenv('database')) cur = conn.cursor() route = 'Syo - Nai' driver = 'James' time = '10:00' request = 'Request to join this ride' query = "INSERT INTO rides (route, driver, time, request) VALUES " \ "('" + route + "', '" + driver + "', '" + time + "', '" + request + "')" cur.execute(query) conn.commit() # Add request conn = psycopg2.connect(os.getenv('database')) cur = conn.cursor() query = "INSERT INTO request (username, pickup_point, time, accept) VALUES " \ "('Teddy Kavoo', ' Vota ', '8:00 ', '" + '0' + "')" cur.execute(query) conn.commit() test_user_cred = { "email": "*****@*****.**", "password": "******" } test_driver_cred = { "email": "*****@*****.**", "password": "******" } test_admin_cred = { "email": "*****@*****.**", "password": "******" } # login user user_response = self.client().post('/api/v3/login', data=json.dumps(test_user_cred), content_type='application/json') driver_response = self.client().post( '/api/v3/login', data=json.dumps(test_driver_cred), content_type='application/json') admin_response = self.client().post( '/api/v3/login', data=json.dumps(test_admin_cred), content_type='application/json') # retrieve json user_token_dict = json.loads(user_response.get_data(as_text=True)) driver_token_dict = json.loads( driver_response.get_data(as_text=True)) admin_token_dict = json.loads( admin_response.get_data(as_text=True)) user_token = user_token_dict["token"] driver_token = driver_token_dict["token"] admin_token = admin_token_dict["token"] self.user_header = { "Content-Type": "application/json", "x-access-token": user_token } self.driver_header = { "Content-Type": "application/json", "x-access-token": driver_token } self.admin_header = { "Content-Type": "application/json", "x-access-token": admin_token }
def setUp(self): """Define test variables and initialize app""" self.app = create_app(config_name="testing") self.client = self.app.test_client with self.app.app_context(): create_tables() ride = Rides() ride.post_a_ride("Lewis Liu", "Meru - Embu", "6:30pm") ride.post_a_ride("Tedd", "Timba - Head", "5:30pm") user = Users() user.register("Elneny Mohah", "*****@*****.**", "01234", "True", False) user.register("Honeybunch Kaindu", "*****@*****.**", "1440", "False", False) """Admin Creation""" conn = psycopg2.connect(os.getenv('Db')) cur = conn.cursor() hidden = generate_password_hash("teddy0725143787") query = "INSERT INTO users (username, email, password, driver, admin) VALUES " \ "('Teddy Kavooh', '*****@*****.**', '" + hidden + "', '" + '1' + "', '" + '1' + "')" cur.execute(query) conn.commit() """Driver Creation""" conn = psycopg2.connect(os.getenv('Db')) cur = conn.cursor() hidden = generate_password_hash("123") query = "INSERT INTO users (username, email, password, driver, admin) VALUES " \ "('Hola Delmonte', '*****@*****.**', '" + hidden + "', '" + '1' + "','" + '0' + "' )" cur.execute(query) conn.commit() """User Creation""" conn = psycopg2.connect(os.getenv('Db')) cur = conn.cursor() hidden = generate_password_hash("456") query = "INSERT INTO users (username, email, password, driver, admin) VALUES " \ "('User Pele', '*****@*****.**', '" + hidden + "', '" + '0' + "','" + '0' + "' )" cur.execute(query) conn.commit() """Getting Tokens""" """User Login""" test_admin = {"username": "******", "password": "******"} test_driver = {"username": "******", "password": "******"} test_user = {"username": "******", "password": "******"} admin_response = self.client().post('/api/v2/users/login', data=json.dumps(test_admin), content_type='application/json') driver_response = self.client().post('/api/v2/users/login', data=json.dumps(test_driver), content_type='application/json') user_response = self.client().post('/api/v2/users/login', data=json.dumps(test_user), content_type='application/json') admin_token_dict = json.loads(admin_response.get_data(as_text=True)) driver_token_dict = json.loads(driver_response.get_data(as_text=True)) user_token_dict = json.loads(user_response.get_data(as_text=True)) admin = admin_token_dict["token"] driver = driver_token_dict["token"] user = user_token_dict["token"] self.admin_header = {"Content-Type": "application/json", "x-access-token": admin} self.driver_header = {"Content-Type": "application/json", "x-access-token": driver} self.user_header = {"Content-Type": "application/json", "x-access-token": user}