def testIsNumberExist(self): """Test wether isMobileNumberExist function working correctly. 0811221122112 was made in reset database function, hence will return True. 0812345678 has not been made, hence will return False""" assert Users.isMobileNumberExist("0812345678") == False assert Users.isMobileNumberExist("0811221122112") == True
def testIsEmailExist(self): """Test wether isEmailExist function working correctly. [email protected] was made in reset database function, hence will return True. [email protected] has not been made, hence will return False""" assert Users.isEmailExist("*****@*****.**") == True assert Users.isEmailExist("*****@*****.**") == False
def resetDatabase(): """Reset database for testing purpose""" db.drop_all() db.create_all() user_password_encrypted = sha256_crypt.hash('user') admin_password_encrypted = sha256_crypt.hash('admin') user = Users('user', '*****@*****.**', '081122112211', user_password_encrypted, False) admin = Users('admin', '*****@*****.**', '0811221122112', admin_password_encrypted, True) trash_category = ListTrashCategory(2, 'dummy_category') trash_one = { "trash_category_id": 1, "admin_id": 2, "trash_name": "dummy_trash", "price": 1000, "photo": "dummy_photo", "point": 1 } trash_two = { "trash_category_id": 1, "admin_id": 2, "trash_name": "dummy_trash", "price": 2000, "photo": "dummy_photo", "point": 2 } trash_instance_one = ListTrash(trash_one) trash_instance_two = ListTrash(trash_two) reward = Rewards(2, "reward dummy", 20, "photo", 20, True) reward1 = Rewards(2, "reward dummy", 20, "photo", 20, True) reward2 = Rewards(2, "reward dummy", 20, "photo", 20, True) order = ListOrders({ 'user_id': 1, 'adress': "dummy", 'time': datetime.datetime.utcnow(), 'photo': 'url', 'status': 'waiting' }) db.session.add(user) db.session.add(admin) db.session.add(trash_category) db.session.add(trash_instance_one) db.session.add(trash_instance_two) db.session.add(reward) db.session.add(reward1) db.session.add(reward2) db.session.add(order) db.session.commit() user_attr = UserAttributes(1, 0, 0, False) admin_attr = UserAttributes(2, 0, 0, False) db.session.add(user_attr) db.session.add(admin_attr) db.session.commit()
def create_user(db: Session, user_info: UserAchemas): user_uuid = shortuuid.uuid() new_user = Users(uuid=user_uuid, name=user_info.name, nick_name=user_info.nick_name, email=user_info.email, avatar=user_info.avatar) # TODO:还没写完 下次继续 pass
def post(self): """Post data from user to create token Retrieve data from user input located in JSON, validate the data, then create token. Args (located in JSON): email: A string of user's email password: A string of user's password Returns: A dict mapping keys to the corresponding value, for example: { "status": "ok", "email": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c", } Raises: Bad Request (400): An error that occured when some of the field is missing, or if the data is not valid (email and mobile phone inputted is wrong formatted) Unauthorized (401): A 401 error response indicates that the client tried to operate on a protected resource without providing the proper authorization. It may have provided the wrong credentials or none at all. """ parser = reqparse.RequestParser() parser.add_argument('name', location='json') parser.add_argument('email', location='json', required=True) parser.add_argument('mobile_number', location='json') parser.add_argument('password', location='json', required=True) args = parser.parse_args() # We use isEmailAddressValid function to check whether email inputted is valid or not users = Users(args['name'], args['email'], args['mobile_number'], args['password'], False) if not users.isEmailAddressValid(args['email']): return { 'message': 'Invalid email format!' }, 400, { 'Content-Type': 'application/json' } # Check whether email is exist in database user = Users.query.filter_by(email=args['email']).first() if user is None: return { 'status': 'UNATHORIZED', 'message': 'invalid email or password' }, 401, { 'Content-Type': 'application/json' } # Check whether password is valid user_data = marshal(user, Users.login_response_fields) if not sha256_crypt.verify(args['password'], user_data['password']): return { 'status': 'UNATHORIZED', 'message': 'invalid email or password' }, 401, { 'Content-Type': 'application/json' } # Create token user_data.pop( 'password') # Put password information out from user_claim access = create_access_token(identity=args['email'], user_claims=user_data) refresh = create_refresh_token(identity=args['email'], user_claims=user_data) return { 'access': access, 'refresh': refresh }, 200, { 'Content-Type': 'application/json' }
def testUserNumberValid(self): """Test wether isMobileNumberValid function working correctly""" assert Users.isMobileNumberValid(self, "876542372864") == False assert Users.isMobileNumberValid(self, "0876542372864") == True assert Users.isMobileNumberValid(self, "064") == False
def testUserEmailValid(self): """Test wether isEmailAdressValid function working correctly""" assert Users.isEmailAddressValid(self, 'happy@trash') == False assert Users.isEmailAddressValid(self, '*****@*****.**') == True