def activateAccount(token): try: user = User.getUserByEmail(UserHandler.verifyUserToken(token)) if user is None: pass User.activateUser(user) except Exception as err: return jsonify( message="Server error!", error=err.__str__()), HttpStatus.INTERNAL_SERVER_ERROR return jsonify(status='Success!'), HttpStatus.OK
def test_get_all_users(self): data1 = {'email': 'test', 'password': '******', 'username': '******'} user1 = User(**data1).create() data2 = {'email': 'test2', 'password': '******', 'username': '******'} user2 = User(**data2).create() data3 = {'email': 'test3', 'password': '******', 'username': '******'} user3 = User(**data3).create() users = User.getAllUsers() assert len(users) == 3 assert users[0] == user1 assert users[1] == user2 assert users[2] == user3
def createUser(json): valid_params = verify_parameters(json, User.REQUIRED_PARAMETERS) if valid_params: try: email_exists = User.getUserByEmail(json['email']) username_exists = User.getUserByUsername(json['username']) if username_exists and email_exists: return jsonify( message= "Username and email already taken. Please use another one." ), HttpStatus.BAD_REQUEST elif username_exists: return jsonify( message="Username already taken. Please use another one." ), HttpStatus.BAD_REQUEST elif email_exists: return jsonify( message="Email already taken. Please use another one." ), HttpStatus.BAD_REQUEST valid_params['password'] = sha256.hash( valid_params['password']) created_user = User(**valid_params).create() user_dict = to_dict(created_user) result = { "message": "Success!", "user": user_dict, } #sends an activation email to the user UserHandler.sendActivationEmail(json['email']) #Create unlisted category. Will serve as default category for unlisted packages Category(**{ 'user_id': created_user.user_id, 'name': 'unlisted' }).create() #returns created user, however, if send activation email fails, user must request another email return jsonify(result), HttpStatus.CREATED except Exception as err: return jsonify( message="Server error!", error=err.__str__()), HttpStatus.INTERNAL_SERVER_ERROR else: return jsonify(message="Bad Request!"), HttpStatus.BAD_REQUEST
def test_get_all_packages(self): userData = {'email': 'test', 'password': '******', 'username': '******'} user = User(**userData).create() categoryData = {'user_id': user.user_id, 'name': 'test_category'} category = Category(**categoryData).create() packageData = { 'carrier': 'ups', 'tracking_number': '122833234', 'name': 'test', 'creation_date': '21-04-2021', 'category_id': category.category_id } package = Package(**packageData).create() packageData = { 'carrier': 'ups', 'tracking_number': '128332353', 'name': 'test2', 'creation_date': '21-04-2021', 'category_id': category.category_id } package1 = Package(**packageData).create() packages = Package.getAllPackages() assert packages[0].package_id == package.package_id assert packages[1].package_id == package1.package_id
def test_delete_category_by_user_id(self): userData = {'email': 'test', 'password': '******', 'username': '******'} user = User(**userData).create() categoryData = {'user_id': user.user_id, 'name': 'unlisted'} unlisted_category = Category(**categoryData).create() categoryData = {'user_id': user.user_id, 'name': 'test_category'} category = Category(**categoryData).create() packageData = { 'carrier': 'usps', 'tracking_number': '212323123', 'name': 'test', 'creation_date': '2021-02-01', 'category_id': category.category_id } package = Package(**packageData).create() response = self.client.delete( f'/users/{user.user_id}/categories/{category.category_id}') deleted_category = response.json['category'] moved_packages = Package.getPackagesByCategory( unlisted_category.category_id) assert response.json['message'] == 'Success!' assert deleted_category['name'] == 'test_category' assert moved_packages[0].carrier == 'usps' assert moved_packages[0].name == 'test'
def test_create_category(self): userData = {'email': 'test', 'password': '******', 'username': '******'} user = User(**userData).create() categoryData = {'user_id': user.user_id, 'name': 'test_category'} category = Category(**categoryData).create() assert category.name == 'test_category'
def test_delete_package(self): userData = {'email': 'test', 'password': '******', 'username': '******'} user = User(**userData).create() categoryData = {'user_id': user.user_id, 'name': 'test_category'} category = Category(**categoryData).create() packageData = { 'carrier': 'usps', 'tracking_number': '212323123', 'name': 'test', 'creation_date': '2021-02-01', 'category_id': category.category_id } package = Package(**packageData).create() response = self.client.delete('/packages', data=json.dumps( dict(package_id=package.package_id)), content_type='application/json') assert response.json['message'] == 'Success!' assert response.json['package']['carrier'] == package.carrier assert response.json['package']['name'] == package.name assert response.json['package'][ 'tracking_number'] == package.tracking_number
def test_get_category_by_user_id(self): userData = {'email': 'test', 'password': '******', 'username': '******'} user = User(**userData).create() categoryData = {'user_id': user.user_id, 'name': 'test_category'} category = Category(**categoryData).create() result_categories = Category.getCategoriesByUserId(user.user_id) assert result_categories[0] == category
def test_get_category_by_user_id_and_name(self): userData = {'email': 'test', 'password': '******', 'username': '******'} user = User(**userData).create() categoryData = {'user_id': user.user_id, 'name': 'test_category'} category = Category(**categoryData).create() result_category = Category.getCategoryByUserIdAndName( user.user_id, category.name) assert result_category == category
def getAllUsers(): try: users = User.getAllUsers() result_list = [] for user in users: result_list.append(to_dict(user)) result = {"message": "Success!", "users": result_list} return jsonify(result), HttpStatus.OK except Exception as e: return jsonify(reason="Server error", error=e.__str__()), HttpStatus.INTERNAL_SERVER_ERROR
def test_create_user(self): data = { 'email': 'test', 'password': '******', 'username': '******', 'active': True } user = User(**data).create() assert user.username == 'test' assert user.email == 'test' assert user.active == True
def test_get_all_users(self): data = {'email': 'test', 'password': '******', 'username': '******'} user = User(**data).create() response = self.client.get("/users") response_user = response.json['users'][0] assert response.json['message'] == 'Success!' assert response_user['active'] == False assert response_user['email'] == 'test' assert response_user['username'] == 'test'
def test_get_category_by_user_id(self): userData = {'email': 'test', 'password': '******', 'username': '******'} user = User(**userData).create() categoryData = {'user_id': user.user_id, 'name': 'test_category'} category = Category(**categoryData).create() response = self.client.get(f'/users/{user.user_id}/categories') category1 = response.json['categories'][0] assert response.json['message'] == 'Success!' assert category1['user_id'] == str(category.user_id) assert category1['name'] == category.name
def test_delete_category(self): userData = {'email': 'test', 'password': '******', 'username': '******'} user = User(**userData).create() categoryData = {'user_id': user.user_id, 'name': 'test_category'} category = Category(**categoryData).create() response = self.client.delete( f'/users/{user.user_id}/categories/{category.category_id}') assert response.json['message'] == 'Success!' assert response.json['category']['name'] == category.name
def test_create_category(self): userData = {'email': 'test', 'password': '******', 'username': '******'} user = User(**userData).create() response = self.client.post('/categories', data=json.dumps( dict(user_id=str(user.user_id), name='test_category')), content_type='application/json') assert response.json['message'] == 'Success!' assert response.json['category']['user_id'] == str(user.user_id) assert response.json['category']['name'] == 'test_category'
def getUserById(uid): try: user = User.getUserById(uid) if user: user_dict = to_dict(user) result = {"message": "Success!", "user": user_dict} return jsonify(result), HttpStatus.OK else: return jsonify( reason="User does not exist."), HttpStatus.NOT_FOUND except Exception as e: return jsonify(reason="Server error", error=e.__str__()), HttpStatus.INTERNAL_SERVER_ERROR
def test_get_all_categories(self): userData = {'email': 'test', 'password': '******', 'username': '******'} user = User(**userData).create() categoryData = {'user_id': user.user_id, 'name': 'test_category1'} category1 = Category(**categoryData).create() categoryData = {'user_id': user.user_id, 'name': 'test_category2'} category2 = Category(**categoryData).create() categoryData = {'user_id': user.user_id, 'name': 'test_category3'} category3 = Category(**categoryData).create() categories = Category.getAllCategories() assert categories[0] == category1 assert categories[1] == category2 assert categories[2] == category3
def test_fail_create_category(self): userData = {'email': 'test', 'password': '******', 'username': '******'} user = User(**userData).create() categoryData = {'user_id': user.user_id, 'name': 'test_category'} category = Category(**categoryData).create() response = self.client.post('/categories', data=json.dumps( dict(user_id=str(user.user_id), name='test_category')), content_type='application/json') assert response.json[ 'message'] == 'The category you tried to create already exsists.'
def signIn(json): #validate data if ('email' in json) and ('password' in json): #get user data from db user = User.getUserByEmail(json['email']) #if user exists and password match return a valid JWT in the response otherwise return error. if user.active == False: return jsonify(Error='Please activate your account via email' ), HttpStatus.NOT_FOUND if user and sha256.verify(json['password'], user.password): access_token = create_access_token( identity=user.user_id) #pragma: no mutate return jsonify(access_token=access_token), HttpStatus.OK else: return jsonify(Error='Email or password is incorrect' ), HttpStatus.NOT_FOUND return jsonify(Error='Malformed body'), HttpStatus.BAD_REQUEST
def test_get_package_by_tracking_number(self): userData = {'email': 'test', 'password': '******', 'username': '******'} user = User(**userData).create() categoryData = {'user_id': user.user_id, 'name': 'test_category'} category = Category(**categoryData).create() packageData = { 'carrier': 'ups', 'tracking_number': '122833234', 'name': 'test', 'creation_date': '21-04-2021', 'category_id': category.category_id } package = Package(**packageData).create() result_package = Package.getPackageByTrackingNumber('122833234') assert result_package.package_id == package.package_id
def test_create_package(self): userData = {'email': 'test', 'password': '******', 'username': '******'} user = User(**userData).create() categoryData = {'user_id': user.user_id, 'name': 'test_category'} category = Category(**categoryData).create() response = self.client.post('/packages', data=json.dumps( dict(carrier='usps', tracking_number='212323123', name='test', creation_date='2021-02-01', category_id=category.category_id, user_id=str(user.user_id))), content_type='application/json') assert response.json['message'] == 'Success!' assert response.json['package']['carrier'] == 'usps' assert response.json['package']['name'] == 'test' assert response.json['package']['tracking_number'] == '212323123'
def test_create_package(self): userData = {'email': 'test', 'password': '******', 'username': '******'} user = User(**userData).create() categoryData = {'user_id': user.user_id, 'name': 'test_category'} category = Category(**categoryData).create() packageData = { 'carrier': 'ups', 'tracking_number': '122833234', 'name': 'test', 'creation_date': '21-04-2021', 'category_id': category.category_id } package = Package(**packageData).create() assert package.carrier == 'ups' assert package.tracking_number == '122833234' assert package.name == 'test' assert str(package.creation_date) == '2021-04-21' assert package.category_id == category.category_id
def test_fail_create_user(self): data = {'email': 'test', 'password': '******', 'username': '******'} user = User(**data).create() response = self.client.post('/users', data=json.dumps( dict(email='test2', password='******', username='******')), content_type='application/json') assert response.json == { 'message': 'Username already taken. Please use another one.' } response = self.client.post('/users', data=json.dumps( dict(email='test', password='******', username='******')), content_type='application/json') assert response.json == { 'message': 'Email already taken. Please use another one.' } response = self.client.post('/users', data=json.dumps( dict(email='test', password='******', username='******')), content_type='application/json') assert response.json == { 'message': 'Username and email already taken. Please use another one.' } response = self.client.post('/users', data=json.dumps( dict(password='******', username='******')), content_type='application/json') assert response.json == {'message': 'Bad Request!'}
def test_get_all_packages(self): userData = {'email': 'test', 'password': '******', 'username': '******'} user = User(**userData).create() categoryData = {'user_id': user.user_id, 'name': 'test_category'} category = Category(**categoryData).create() packageData = { 'carrier': 'usps', 'tracking_number': '212323123', 'name': 'test', 'creation_date': '2021-02-01', 'category_id': category.category_id } package = Package(**packageData).create() response = self.client.get('/packages') package1 = response.json['packages'][0] assert response.json['message'] == 'Success!' assert package1['carrier'] == package.carrier assert package1['name'] == package.name assert package1['tracking_number'] == package.tracking_number
def test_activate_user(self): data = {'email': 'test', 'password': '******', 'username': '******'} user = User(**data).create() user.activateUser() assert user.active == True
def test_get_user_by_username(self): data = {'email': 'test', 'password': '******', 'username': '******'} user = User(**data).create() result = User.getUserByUsername(user.username) assert user == result
def test_get_by_email(self): data = {'email': 'test', 'password': '******', 'username': '******'} user = User(**data).create() result = User.getUserByEmail(user.email) assert user == result