def check_used_handle(handle_str): handle_store = get_auth_data_store() for i in handle_store: if i['handle_str'] == handle_str: raise InputError(description='Handle is already in use') else: return 1
def test_reset(): 'testing functionability of passwordreset request' workspace_reset() reset_store = get_reset_code_store() reg_user1() auth.request({'email': '*****@*****.**'}) for i in reset_store: if i['email'] == '*****@*****.**': code = i['reset_code'] auth.reset({'reset_code': code, 'new_password': '******'}) auth_store = get_auth_data_store() password_check = 0 #if new password is in auth store let = 1 for i in auth_store: if i['password'] == 'thisiscool': password_check = 1 assert password_check == 1
def check_used_email(email): email_store = get_auth_data_store() for i in email_store: if i['email'] == email: raise InputError(description='Email is already in use') else: return 1
def validate_uid(u_id): user_store = get_auth_data_store() for i in user_store: if i['u_id'] == int(u_id): return True return False
def is_valid_user_id(u_id): auth_store = get_auth_data_store() for user in auth_store: if user['u_id'] == u_id: return 1 else: raise InputError(description='Invalid u_id')
def get_user_email(email): ''' Function to validate a users email and return that users data ''' auth_store = get_auth_data_store() for i in auth_store: if i['email'] == email: return i raise InputError(description='Email does not belong to a retgistered user')
def get_user_token(token): ''' Function to validate a token and returns the users info otherwise raises an error ''' auth_store = get_auth_data_store() for i in auth_store: if i['token'] == token: return i raise InputError(description='Invalid Token')
def get_user_uid(u_id): auth_store = get_auth_data_store() user = {} for i in auth_store: if i['u_id'] == u_id: user = i if user != {}: return user else: raise InputError(description='Invalid u_id')
def user_details(u_id): auth_store = get_auth_data_store() for user in auth_store: if u_id == user['u_id']: return { 'u_id': u_id, 'name_first': user['name_first'], 'name_last': user['name_last'] } return False
def test_register1(): ''' Test valid use of test_register ''' workspace_reset() payload = { 'email' : '*****@*****.**', 'password': '******', 'name_first': 'Kennan', 'name_last': 'Wong' } result1 = auth.register(payload) auth_store = get_auth_data_store() assert result1 in auth_store
def get_user_from(field, request): ''' Function will return a users data based on a given a field and test it against a requested value i.e if get_user_from(email, payload[email]) will search each users email whether or not it matches the payload ''' auth_store = get_auth_data_store() for i in auth_store: if i[str(field)] == request: return i if str(field) == 'token': raise InputError(description='Invalid Token') if str(field) == 'u_id': raise InputError(description='Invalid u_id') if str(field) == 'email': raise InputError( description='Email does not belong to a registered user')
def user_remove(payload): 'This is the function for admin_user_remove' auth_store = get_auth_data_store() channel_store = get_channel_data_store() messages_store = get_messages_store() # remover_id = user_id_from_token(payload['token']) # remover_dets = user_details(remover_id) removee = get_user_uid(payload['u_id']) removee_dets = user_details(payload['u_id']) # InputError for invalid user ID of the person we are removing if not is_valid_user_id(int(payload['u_id'])): raise InputError(description='Invalid user_id') removee_token = removee['token'] # Removing any messages the user had sent for messages in messages_store: if message_belong_user(removee_token, messages['message_id']): messages_store.remove(messages) # Removing user from any channels they were in for channel in channel_store: if removee_dets in channel['members']: channel['members'].remove(removee_dets) if removee_dets in channel['owners']: channel['owners'].remove(removee_dets) for message in channel['messages']: if message['u_id'] == removee['u_id']: channel['messages'].remove(message) # AccessError when the authorised user is not an owner of the slackr if not check_owner_slackr(payload['token']): raise AccessError(description='User is not an owner of the slackr') else: auth_store.remove(removee) return {}
def users_all(payload): ''' Returns a list of all users and their associated details ''' user_store = get_auth_data_store() ret = [] user_data = {} for i in user_store: user_data = { 'u_id': i['u_id'], 'email': i['email'], 'name_first': i['name_first'], 'name_last': i['name_last'], 'handle_str': i['handle_str'], 'profile_img_url': i['profile_img_url'] } ret.append(user_data) return ret
def register(payload): ''' Function to register a user to the slack ''' auth_store = get_auth_data_store() handle = (payload['name_first']+payload['name_last']) if len(handle) > 24: handle = handle[0:20] #test if valid email email = test_email(payload['email']) #test strength of password if len(payload['password']) > 6: password = payload['password'] else: raise InputError(description='Password is too short') #test length of Last and First names if 1 < len(payload['name_first']) <= 50: first_name = payload['name_first'] else: raise InputError(description='Not a valid first name') if 1 < len(payload['name_last']) <= 50: last_name = payload['name_last'] else: raise InputError(description='Not a valid last name') u_id = int(len(auth_store)+1) token = generate_token(u_id) new_user = { 'u_id' : u_id, 'email': email, 'password': password, 'name_first': first_name, 'name_last': last_name, 'handle_str': handle.lower(), 'token': token, 'status' : LOGGED_ON, 'messages':[], 'permission_id': 2, 'slack_owner' : False, 'profile_img_url': "https://img.buzzfeed.com/buzzfeed-static/static/2020-04/15/19/campaign_images/fdc9b0680e75/the-social-media-shame-machine-is-in-overdrive-ri-2-4824-1586980284-7_dblbig.jpg" } if u_id == 1: # this is the first person in a slack they are now an owner new_user['slack_owner'] = True new_user['permission_id'] = 1 #test if an email is alread taken for i in auth_store: if i['email'] == email: raise InputError(description='Email is already in use') auth_store.append(new_user) # for debugging save_auth_store() return new_user