def test_setup(self): self.drop_tables() # verify they don't exist cursor = self.conn.cursor() cursor.execute( 'SELECT EXISTS(SELECT * FROM information_schema.tables' + ' WHERE table_name=%s)', ('users', )) self.assertFalse(cursor.fetchone()[0]) cursor.execute( 'SELECT EXISTS(SELECT * FROM information_schema.tables' + ' WHERE table_name=%s)', ('urls', )) self.assertFalse(cursor.fetchone()[0]) # create Dorm and check tables exist dorm = Dorm(DB_NAME) cursor = self.conn.cursor() cursor.execute( 'SELECT EXISTS(SELECT * FROM information_schema.tables' + ' WHERE table_name=%s)', ('users', )) self.assertTrue(cursor.fetchone()[0]) cursor.execute( 'SELECT EXISTS(SELECT * FROM information_schema.tables' + ' WHERE table_name=%s)', ('urls', )) self.assertTrue(cursor.fetchone()[0]) # create another dorm and verify no errors are thrown new_dorm = Dorm(DB_NAME)
def generate_link(): # necessary info from json request info = request.json meeting_len = int(info['meeting_len']) buffer = int(info['buffer']) workday_start = datetime.strptime(info['workday_start'], '%H:%M').time() workday_end = datetime.strptime(info['workday_end'], '%H:%M').time() days_out = int(info['days_out']) weeks_out = int(info['weeks_out']) weekdays = info['weekdays'] half_hours = info['half_hours'] # create db and create link db = Dorm() target = db.add_scheduling_link(session['profile']['user_id'], half_hours=half_hours, buffer_time=buffer, workday_start=workday_start, workday_end=workday_end, start_scheduling=days_out, weeks_out=weeks_out, weekdays=weekdays, meeting_length=meeting_len) # updating day return make_response(json.dumps(target.get_target()))
def test_gen_link(self): with self.client as c: with c.session_transaction() as session: session['profile'] = {'user_id': self.user_id} meeting_len = '50' buffer = '10' start = '12:00' end = '16:00' days_out = '1' weeks_out = '1' half_hours = False weekdays = [1, 2, 3] # Tues, Wed, Thurs response = c.post('/generate_link', json={ 'meeting_len': meeting_len, 'buffer': buffer, 'workday_start': start, 'workday_end': end, 'days_out': days_out, 'weeks_out': weeks_out, 'weekdays': weekdays, 'half_hours': half_hours }) target = json.loads(response.data) self.assertRegex(target, '[a-zA-Z0-9]{16}') db = Dorm() links = db.get_user(self.user_id).get_links() self.assertEqual(len(links), 1) self.assertEqual(links[0].get_target(), target)
def schedule_meeting(): try: info = request.json db = Dorm() user, _ = db.fetch_scheduling_link(info['target']) db.delete_scheduling_link(info['target']) if 'profile' not in session or session['profile'][ 'user_id'] != 'test_user': # send emails send_guest_email(info, user.get_email(), user.get_name()) send_host_email(info, user.get_email()) # add to calendar/generate ical add_event(user.get_refresh_key(), user.get_email(), user.get_name(), info) return make_response(json.dumps('Meeting scheduled!')) except: return make_response( json.dumps({ 'error': 'An error occurred. Please contact the system administrator.' }))
def fetch_user(): target = request.json['target'] db = Dorm() user, _ = db.fetch_scheduling_link(target) if not user: return make_response(json.dumps({'error': 'No scheduling link found.'})) return make_response(json.dumps(user.for_json()))
def test_delete_user_and_links(self): cursor = self.conn.cursor() dorm = Dorm(DB_NAME) user_id = 'evuong' name = 'Erin Vuong' refresh_key = 'awoeisld3$#9o' optimal_hour = datetime.time(hour=10, minute=30) optimal_day = 1 email = '*****@*****.**' default_scaling_factor = 1.2 user = User(user_id, name, refresh_key, optimal_hour, optimal_day, email) # create user dorm.create_user(user) # create a few link dorm.add_scheduling_link(user_id) dorm.add_scheduling_link(user_id) # delete user dorm.delete_user(user_id) # verify users table empty cursor.execute('SELECT * FROM users') self.assertEqual(len(cursor.fetchall()), 0)
def test_schedule_meeting(self): with self.client as c: with c.session_transaction() as session: session['profile'] = {'user_id': self.user_id} # add a link db = Dorm() db.add_scheduling_link(self.user_id) links = db.get_user(self.user_id).get_links() target = links[0].get_target() response = c.post('/schedule_meeting', json={'target': target}) self.assertEqual(json.loads(response.data), 'Meeting scheduled!')
def test_fetch_user(self): with self.client as c: with c.session_transaction() as session: session['profile'] = {'user_id': self.user_id} # add a link db = Dorm() db.add_scheduling_link(self.user_id) links = db.get_user(self.user_id).get_links() target = links[0].get_target() response = c.post('/fetch_user', json={'target': target}) data = json.loads(response.data) self.assertEqual(data['name'], self.name)
def fetch_times(): user = None try: target = request.json['target'] DAYS_PER_WEEK = 7 db = Dorm() user, link = db.fetch_scheduling_link(target) if not link: msg = 'This scheduling link is not available! Please contact ' + \ 'the intended host for a working link.' return make_response(json.dumps({'error': msg})) busy = [] # don't get times for tests if 'profile' not in session or session['profile'][ 'user_id'] != 'test_user': busy = get_freebusy(user.get_refresh_key(), user.get_email(), link.get_start_scheduling(), link.get_weeks_out() * DAYS_PER_WEEK) busy = parse_free_busy(busy) free = generate_time_slots(link.get_buffer_time(), link.get_workday_start(), link.get_workday_end(), link.get_weekdays(), busy, link.get_meeting_length(), link.get_weeks_out() * DAYS_PER_WEEK, link.get_start_scheduling(), link.get_half_hours()) sort_slots(free) return make_response(json.dumps([slot.for_json() for slot in free])) except: if 'profile' not in session or session['profile'][ 'user_id'] != 'test_user': send_host_refresh_email(user.get_email(), url_for('login_force', _external=True)) return make_response( json.dumps({ 'error': 'An error ocurred. The host has been notified of the issue. ' + 'Please contact them for further information.' }))
def callback_handling(): # Handles response from token endpoint auth0.authorize_access_token() resp = auth0.get('userinfo') userinfo = resp.json() # Store the user information in flask session (used for checking login) session['jwt_payload'] = userinfo session['profile'] = { 'user_id': userinfo['sub'], 'name': userinfo['name'], 'picture': userinfo['picture'] } # check if user in db db = Dorm() user = db.get_user(userinfo['sub']) refresh_key = '' if not user: # get refresh key refresh_key = get_refresh_token(userinfo['sub']) user = User(userinfo['sub'], userinfo['name'], refresh_key, email=userinfo['email']) db.create_user(user) else: refresh_key = get_refresh_token(userinfo['sub'], True) # update refresh key if refresh_key: db.update_refresh_key(userinfo['sub'], refresh_key) # check refresh key validity and force refresh if not valid else: success = True try: refresh_key = db.get_user(userinfo['sub']).get_refresh_key() success = get_avail(refresh_key) except: success = False if not success: session['error'] = 'refresh_key_error' return redirect(url_for('home'))
def test_delete_link(self): with self.client as c: with c.session_transaction() as session: session['profile'] = {'user_id': self.user_id} # add a link db = Dorm() db.add_scheduling_link(self.user_id) links = db.get_user(self.user_id).get_links() self.assertEqual(len(links), 1) response = c.post('/delete_link', json={'target': links[0].get_target()}) self.assertEqual(json.loads(response.data), '') links = db.get_user(self.user_id).get_links() self.assertEqual(len(links), 0)
def tearDown(self): # delete test user db = Dorm() db.delete_user(self.user_id)
def setUp(self): # set up test user in db db = Dorm() db.delete_user(self.user_id) db.create_user(User(self.user_id, self.name, self.refresh))
def test_user_simple(self): cursor = self.conn.cursor() dorm = Dorm(DB_NAME) # verify users table empty cursor.execute('SELECT * FROM users') self.assertEqual(len(cursor.fetchall()), 0) user_id = 'evuong' name = 'Erin Vuong' refresh_key = 'awoeisld3$#9o' optimal_hour = datetime.time(hour=10, minute=30) optimal_day = 1 email = '*****@*****.**' default_scaling_factor = 1.2 user = User(user_id, name, refresh_key, optimal_hour, optimal_day, email) # create user dorm.create_user(user) cursor.execute('SELECT * FROM users') users = cursor.fetchall() self.assertEqual(len(users), 1) user_found = users[0] self.assertEqual(len(user_found), 7) self.assertEqual(user_found[0], user_id) self.assertEqual(user_found[1], name) self.assertEqual(user_found[2], refresh_key) self.assertEqual(user_found[3], optimal_hour) self.assertEqual(user_found[4], optimal_day) self.assertEqual(user_found[5], email) self.assertEqual(user_found[6], default_scaling_factor) # get user get_user = dorm.get_user(user_id) self.assert_users_equal(get_user, user) # update refresh key refresh_key = 'sd9w3lsdkfsodi' dorm.update_refresh_key(user_id, refresh_key) # update optimal hour optimal_hour = datetime.time(hour=11, minute=30) dorm.update_optimal_hour(user_id, optimal_hour) # update optimal day optimal_day = 2 dorm.update_optimal_day(user_id, optimal_day) # check info updated cursor.execute('SELECT * FROM users') users = cursor.fetchall() self.assertEqual(len(users), 1) user_found = users[0] self.assertEqual(len(user_found), 7) self.assertEqual(user_found[0], user_id) self.assertEqual(user_found[1], name) self.assertEqual(user_found[2], refresh_key) self.assertEqual(user_found[3], optimal_hour) self.assertEqual(user_found[4], optimal_day) self.assertEqual(user_found[5], email) self.assertEqual(user_found[6], default_scaling_factor) # get user user = User(user_id, name, refresh_key, optimal_hour, optimal_day, email) get_user = dorm.get_user(user_id) self.assert_users_equal(get_user, user) # delete user dorm.delete_user(user_id) # verify users table empty cursor.execute('SELECT * FROM users') self.assertEqual(len(cursor.fetchall()), 0) # try getting a nonexistent user no_user = dorm.get_user(user_id) self.assertEqual(no_user, None) # try deleting a nonexistent user dorm.delete_user(user_id)
def user_info(): if 'profile' in session: db = Dorm() user = db.get_user(session['profile']['user_id']) return make_response(json.dumps(user.for_json())) return make_response(json.dumps('No user found'))
def change_hour(): hour = datetime.strptime(request.json['hour'], '%H:%M').time() db = Dorm() db.update_optimal_hour(session['profile']['user_id'], hour) return make_response(json.dumps(request.json['hour']))
def delete_link(): db = Dorm() db.delete_scheduling_link(request.json['target']) return make_response(json.dumps(''))
def change_day(): # updating day day = request.json['weekday'] db = Dorm() db.update_optimal_day(session['profile']['user_id'], day) return make_response(json.dumps(day))