def test_userRead(self): self.assertIsNone(userRead(col='id', value=1)) user = User('johndoe', encrypt('password'), 'John Doe') db.session.add(user) db.session.commit() self.assertEqual(userRead(col='id', value=1).name, 'John Doe') self.assertEqual( userRead(col='username', value='johndoe').name, 'John Doe')
def user_update_operation(username, name, age, height, weight): user = userRead(col='username', value=username) # user is not found if user is None: raise ErrorWithCode(409, "No user found") if name is not None: user.name = name if age is not None: user.age = age if height is not None: user.height = height if weight is not None: user.weight = weight if userUpdate() == False: raise ErrorWithCode(503, "Unsuccessful") # success case return user
def user_read_operation(col, value): user = userRead(col, value) # user is not found if user is None: raise ErrorWithCode(409, "No user found") # success case return user
def auth_operation(username, password): user = userRead(col='username', value=username) # user is not found if user is None: raise ErrorWithCode(409, "No user found") # if password is wrong if not authenticate(password, user.encrypted_password): raise ErrorWithCode(401, "Unauthorised - Incorrect password") return True
def user_create_operation(username, plaintext_password, name): user = userRead(col='username', value=username) # user existing if user is not None: raise ErrorWithCode(409, "Existing user found") user = initialize_user(username, plaintext_password, name) if userCreate(user) == False: raise ErrorWithCode(503, "Unsuccessful") # success case return user
def user_update_password_operation(username, current_password, new_password): user = userRead(col='username', value=username) # user is not found if user is None: raise ErrorWithCode(409, "No user found") # incorrect password provided if authenticate(current_password, user.encrypted_password) is False: raise ErrorWithCode(401, "Wrong password provided") user.encrypted_password = encrypt(new_password) if userUpdate() == False: raise ErrorWithCode(503, "Unsuccessful") # success case return user
def daily_calories_read_operations(user_id): # check if user exist if userRead(col='id', value=user_id) is None: raise ErrorWithCode(409, "No user found") raw_stats = dailyCaloriesRead(user_id) stat_dict = {} for item in raw_stats: i_dict = item._asdict() date_str = str(i_dict['Date']) stat_dict[date_str] = { 'total_calories': i_dict['total_calories'], 'total_distance': i_dict['total_distance'] } return stat_dict