Пример #1
0
    def test_list_user_succeeds(
        self,
        mock_user_repo_find_first,
    ):
        with self.app.app_context():
            role = RoleFactory()

            UserRoleFactory(user_id=self.mock_user.id, role_id=role.id)
            PermissionFactory.create(keyword="view_users", role=role)
            mock_user_repo_find_first.return_value = self.mock_user
            user_controller = UserController(self.request_context)

            response = user_controller.list_user(id=self.mock_user.id)

            self.assertEqual(response.status_code, 200)
            self.assertEqual(response.get_json()["msg"], "OK")

            self.assertEqual(
                response.get_json()["payload"]["user"]["first_name"],
                self.mock_user.first_name,
            )
            self.assertEqual(
                response.get_json()["payload"]["user"]["last_name"],
                self.mock_user.last_name,
            )
Пример #2
0
    def test_list_admin_users_ok_response(self, mock_user_repo_find_first,
                                          mock_filter_by,
                                          mock_pagination_meta):
        """
        Test list_admin_users OK response.
        """
        # Arrange
        with self.app.app_context():
            mock_filter_by.return_value.items = [
                self.mock_user_role,
            ]
            mock_user_repo_find_first.return_value = self.mock_user
            mock_pagination_meta.return_value = {
                "total_rows": 1,
                "total_pages": 1,
                "current_page": 1,
                "next_page": None,
                "prev_page": None,
            }
            user_controller = UserController(self.request_context)

            # Act
            result = user_controller.list_admin_users()

            # Assert
            assert result.status_code == 200
            assert result.get_json()["msg"] == "OK"
            assert result.get_json()["payload"]["meta"]["current_page"] == 1
            assert result.get_json()["payload"]["meta"]["next_page"] is None
Пример #3
0
    def test_create_user_method_handles_user_creation_with_non_existent_role_id(
            self, mock_request_params):
        with self.app.app_context():
            user = UserFactory()
            role = RoleFactory(name="test_role")
            UserRoleFactory(role_id=role.id, user_id=user.id)

            non_existent_role_id = 100

            mock_request_params.return_value = [
                "Joseph",
                "Serunjogi",
                "*****@*****.**",
                non_existent_role_id,
                "male",
                str(datetime.now()),
                1,
                "password",
            ]

            user_controller = UserController(self.request_context)

            response = user_controller.create_user()

            self.assertEqual(response.status_code, 400)
            self.assertEqual(
                response.get_json()["msg"],
                "Role with userTypeId(roleId) {} does not exist".format(
                    non_existent_role_id),
            )
    def test_list_admin_users_ok_response(self, mock_get_user_by_email_or_id,
                                          mock_filter_by,
                                          mock_pagination_meta):
        '''
        Test list_admin_users OK response.
        '''
        # Arrange
        with self.app.app_context():
            mock_filter_by.return_value.items = [
                self.mock_user_role,
            ]
            mock_get_user_by_email_or_id.return_value = {
                "id": "-LXTuXlk2W4Gskt8KTte",
                "email": "*****@*****.**",
                "first_name": "Joseph",
                "last_name": "Serunjogi",
                "name": "Joseph Serunjogi"
            }
            mock_pagination_meta.return_value = {
                "total_rows": 1,
                "total_pages": 1,
                "current_page": 1,
                "next_page": None,
                "prev_page": None
            }
            user_controller = UserController(self.request_context)

            # Act
            result = user_controller.list_admin_users()

            # Assert
            assert result.status_code == 200
            assert result.get_json()['msg'] == 'OK'
            assert result.get_json()['payload']['meta']['current_page'] == 1
            assert result.get_json()['payload']['meta']['next_page'] == None
    def test_list_user_when_user_found_succeeds(self):
        with self.app.app_context():

            user_controller = UserController(self.request_context)

            response = user_controller.list_user(slack_id="-LXTuXlk2W4Gskt8KTtedhmdydsbnyw")

            self.assertEqual(response.status_code, 404)
            self.assertEqual(response.get_json()['msg'], 'User not found')
Пример #6
0
    def test_list_user_when_user_found_succeeds(
        self,
        mock_user_repo_find_first,
    ):
        with self.app.app_context():
            user_controller = UserController(self.request_context)
            mock_user_repo_find_first.return_value = self.mock_user
            response = user_controller.list_user(id=1)

            self.assertEqual(response.status_code, 200)
    def test_create_user_succeeds(self, mock_request_params):

        with self.app.app_context():
            mock_request_params.return_value = [
                "*****@*****.**", "Eno", "Bassey", "-LXTuXlk2W4Gskt8KTte",
                False
            ]
            user_controller = UserController(self.request_context)

            # Act
            result = user_controller.create_user()

            # Assert
            assert result.status_code == 201
            assert result.get_json()['msg'] == 'OK'
    def test_list_user_succeeds(self):

        with self.app.app_context():
            role = RoleFactory()
            user_role = UserRoleFactory(role_id=role.id)
            user = UserFactory(slack_id="-LXTuXlk2W4Gskt8KTte", user_type_id=user_role.id)

            user_controller = UserController(self.request_context)

            response = user_controller.list_user(slack_id=user.slack_id)

            self.assertEqual(response.status_code, 200)
            self.assertEqual(response.get_json()['msg'], "OK")
            self.assertEqual(response.get_json()['payload']['user']['slackId'], user.slack_id)
            self.assertEqual(response.get_json()['payload']['user']['firstName'], user.first_name)
            self.assertEqual(response.get_json()['payload']['user']['lastName'], user.last_name)
    def test_create_user_method_handles_user_creation_with_duplicate_user_id(
            self, mock_request_params):
        with self.app.app_context():
            user = UserFactory(user_id="-LXTuXlk2W4Gskt8KTte")
            role = RoleFactory(name='test_role')
            UserRoleFactory(role_id=role.id, user_id=user.id)

            mock_request_params.return_value = [
                "Joseph", "Serunjogi", None, None, user.user_id, role.id
            ]

            user_controller = UserController(self.request_context)

            response = user_controller.create_user()

            self.assertEqual(response.status_code, 400)
            self.assertEqual(
                response.get_json()['msg'],
                "User with userId '{}' already exists".format(user.user_id))
    def test_create_user_method_handles_user_creation_with_duplicate_email_id(
            self, mock_request_params):
        with self.app.app_context():
            user = UserFactory(email="*****@*****.**",
                               password="******")

            mock_request_params.return_value = [
                user.email, "Joseph", "Serunjogi", "-LXTuXlk2W4Gskt8KTte",
                False
            ]

            user_controller = UserController(self.request_context)

            response = user_controller.create_user()

            self.assertEqual(response.status_code, 400)
            self.assertEqual(
                response.get_json()['msg'],
                "User with email '{}' already exists".format(user.email))
    def test_create_user_succeeds(self, mock_request_params,
                                  mock_get_location):
        location = LocationFactory()
        role = RoleFactory(name='test_role')

        with self.app.app_context():
            mock_get_location.return_value = location.id

            mock_request_params.return_value = [
                "Joseph", "Serunjogi", None, None, "-LXTuXlk2W4Gskt8KTte",
                role.id
            ]
            user_controller = UserController(self.request_context)

            # Act
            result = user_controller.create_user()

            # Assert
            assert result.status_code == 201
            assert result.get_json()['msg'] == 'OK'
    def test_list_user_succeeds(self):

        with self.app.app_context():
            user = UserFactory.create(email="*****@*****.**",
                                      password="******")

            user_controller = UserController(self.request_context)

            response = user_controller.list_user(email="*****@*****.**")

            self.assertEqual(response.status_code, 200)
            self.assertEqual(response.get_json()['msg'], "OK")
            self.assertEqual(response.get_json()['payload']['user']['email'],
                             user.email)
            self.assertEqual(
                response.get_json()['payload']['user']['firstName'],
                user.first_name)
            self.assertEqual(
                response.get_json()['payload']['user']['lastName'],
                user.last_name)
Пример #13
0
    def test_create_user_fails_for_existing_user(
        self,
        mock_user_role_repo_new_user_role,
        # mock_user_repo_new_user,
        mock_user_repo_exists,
        mock_role_repo_find_first,
        mock_request_params,
        mock_get_location,
    ):
        location = LocationFactory()
        role = RoleFactory(name="test_role")

        with self.app.app_context():
            mock_get_location.return_value = location.id
            mock_role_repo_find_first.return_value = self.mock_role
            mock_user_repo_exists.return_value = self.mock_user2
            # mock_user_repo_new_user.return_value = None
            mock_user_role_repo_new_user_role.return_value = self.mock_user_role
            mock_request_params.return_value = [
                "Joseph",
                "Serunjogi",
                self.mock_user2.email,
                role.id,
                "male",
                str(datetime.now()),
                1,
                "password",
            ]
            user_controller = UserController(self.request_context)

            # Act
            result = user_controller.create_user()
            print(result)
            print(result.get_json())
            # Assert
            assert result.status_code == 400
            assert (
                result.get_json()["msg"] ==
                f"User with email '{self.mock_user2.email}' already exists")
    def test_create_user_method_handles_user_creation_with_non_existent_role_id(
            self, mock_request_params):
        with self.app.app_context():
            user = UserFactory(user_id="-LXTuXlk2W4Gskt8KTte")
            role = RoleFactory(name='test_role')
            UserRoleFactory(role_id=role.id, user_id=user.id)

            non_existent_role_id = 100

            mock_request_params.return_value = [
                "Joseph", "Serunjogi", None, None, user.user_id,
                non_existent_role_id
            ]

            user_controller = UserController(self.request_context)

            response = user_controller.create_user()

            self.assertEqual(response.status_code, 400)
            self.assertEqual(
                response.get_json()['msg'],
                "Role with userTypeId(roleId) {} does not exist".format(
                    non_existent_role_id))
Пример #15
0
    def test_create_user_succeeds(
        self,
        mock_user_role_repo_new_user_role,
        mock_user_repo_new_user,
        mock_user_repo_exists,
        mock_role_repo_find_first,
        mock_request_params,
        mock_get_location,
    ):
        location = LocationFactory()
        role = RoleFactory(name="test_role")

        with self.app.app_context():
            mock_get_location.return_value = location.id
            mock_role_repo_find_first.return_value = self.mock_role
            mock_user_repo_exists.return_value = None
            mock_user_repo_new_user.return_value = self.mock_user2
            mock_user_role_repo_new_user_role.return_value = self.mock_user_role
            mock_request_params.return_value = [
                "Joseph",
                "Serunjogi",
                "*****@*****.**",
                role.id,
                "male",
                str(datetime.now()),
                1,
                "password",
            ]
            user_controller = UserController(self.request_context)

            # Act
            result = user_controller.create_user()

            # Assert
            assert result.status_code == 201
            assert result.get_json()["msg"] == "OK"
Пример #16
0
from app.blueprints.base_blueprint import Blueprint, BaseBlueprint, Security, request, Auth
from app.controllers.user_controller import UserController

url_prefix = '{}/user'.format(BaseBlueprint.base_url_prefix)
user_blueprint = Blueprint('user', __name__, url_prefix=url_prefix)
user_controller = UserController(request)


@user_blueprint.route('/', strict_slashes=False, methods=['POST'])
@Security.validator([
    'firstName|required:string', 'lastName|required:string',
    'emailAddress|required:string', 'password|required:string'
])
def create_user():
    return user_controller.create_user()


@user_blueprint.route('/upload', strict_slashes=False, methods=['POST'])
def upload_picture():
    return user_controller.upload_picture()


@user_blueprint.route('/login/', strict_slashes=False, methods=['POST'])
@Security.validator(
    ['emailAddress|required:string', 'password|required:string'])
def login():
    return user_controller.login()


@user_blueprint.route('/reset', strict_slashes=False, methods=['PUT'])
@Security.validator(
Пример #17
0
def get_user(id_user):
    return UserController().get_user(id_user)
Пример #18
0
def index():

    user = UserController()
    test = user.getData()
    return test
Пример #19
0
from flask import jsonify, request, Blueprint
from flask.views import MethodView
from datetime import datetime
from flask_jwt_extended import get_jwt_identity, jwt_required
from app.validation import Validation
from app.decorator import admin_permission_required
from app.controllers.product_controller import ProductController
from app.controllers.sale_controller import SaleController
from app.controllers.user_controller import UserController
from app.db.db_functions import DBFunctions

validate = Validation()
product_controller = ProductController()
user_controller = UserController()
sale_controller = SaleController()
db_func = DBFunctions()
views_blueprint = Blueprint("views_blueprint", __name__)

"""PRODUCT VIEWS"""
class AddProduct(MethodView):
    @admin_permission_required
    def post(self):
        try:
            data = request.get_json()
            search_keys = ("product", "quantity", "unit_price")
            if all(key in data.keys() for key in search_keys):
                product = data.get("product")
                quantity = data.get("quantity")
                unit_price = data.get("unit_price")

                invalid = validate.product_validation(
Пример #20
0
 def _validate_username(self, username: str) -> User:
     user_controller = UserController()
     saved_user = user_controller.find_user(username)
     if not len(saved_user):
         raise Exception('Usuário não encontrado')
     return saved_user[0]