def create(self, request, tenant_pk=None, detail=True): log = NarukoLogging(request) logger = log.get_logger(__name__) logger.info("START: create") try: with transaction.atomic(): # バリデーション data = request.data data["tenant"] = tenant_pk serializer = UserModelSerializer(data=data) serializer.is_valid(raise_exception=True) # 作成 user = ControlUserUseCase(log).create_user( request.user, serializer.save(), AwsEnvironmentModel.objects.filter( id__in=data["aws_environments"]), data["password"]) except (TypeError, ValueError, KeyError, NarukoException) as e: # リクエストデータが不正 logger.exception(e) return Response(status=status.HTTP_400_BAD_REQUEST) except ObjectDoesNotExist as e: logger.exception(e) # ロールまたはテナントが存在しない return Response(status=status.HTTP_404_NOT_FOUND) except Exception as e: logger.exception(e) raise else: # 成功 print(user) print(UserModelDetailSerializer(user)) logger.info("END: create") return Response(data=UserModelDetailSerializer(user).data, status=status.HTTP_201_CREATED)
def destroy(self, request, pk=None, tenant_pk=None): log = NarukoLogging(request) logger = log.get_logger(__name__) logger.info("START: destroy") target_user = UserModel.objects.get(id=pk, tenant_id=tenant_pk) ControlUserUseCase(log).delete_user(request.user, target_user) logger.info("END: destroy") return Response(status=status.HTTP_204_NO_CONTENT)
def list(self, request, tenant_pk=None, detail=True): log = NarukoLogging(request) logger = log.get_logger(__name__) logger.info("START: list") tenant = TenantModel.objects.get(id=tenant_pk) users = ControlUserUseCase(log).fetch_users(request.user, tenant) logger.info("END: list") return Response(data={ "users": [UserModelDetailSerializer(user).data for user in users] })
def test_delete_user(self): mock_request_user = mock.Mock() mock_user = mock.Mock() ControlUserUseCase(mock.Mock()).delete_user(mock_request_user, mock_user) mock_request_user.is_belong_to_tenant.assert_called_once() mock_request_user.can_delete_user.assert_called_once() mock_user.delete.assert_called()
def test_fetch_user_not_belong_to_tenant(self, mock_user_model: mock.Mock): mock_request_user = mock.Mock() mock_request_user.is_belong_to_tenant.return_value = False mock_tenant = mock.Mock() with self.assertRaises(PermissionDenied): ControlUserUseCase(mock.Mock()).fetch_users( mock_request_user, mock_tenant) mock_request_user.is_belong_to_tenant.assert_called_once() mock_request_user.can_control_other_user.assert_not_called()
def test_delete_user_not_control_other_user(self): mock_request_user = mock.Mock() mock_request_user.can_delete_user.return_value = False mock_user = mock.Mock() with self.assertRaises(PermissionDenied): ControlUserUseCase(mock.Mock()).delete_user( mock_request_user, mock_user) mock_request_user.is_belong_to_tenant.assert_called_once() mock_request_user.can_delete_user.assert_called_once() mock_user.delete.assert_not_called()
def test_fetch_user(self, mock_user_model: mock.Mock): mock_request_user = mock.Mock() mock_tenant = mock.Mock() objects_filter = mock_user_model.objects.filter res = ControlUserUseCase(mock.Mock()).fetch_users( mock_request_user, mock_tenant) mock_request_user.is_belong_to_tenant.assert_called_once() mock_request_user.can_control_other_user.assert_called_once() objects_filter.assert_called_once_with(tenant=mock_tenant) self.assertEqual(res, [])
def test_create_user(self): mock_request_user = mock.Mock() mock_user = mock.Mock() mock_aws = mock.Mock() password = "******" res = ControlUserUseCase(mock.Mock()).create_user( mock_request_user, mock_user, mock_aws, password) mock_request_user.is_belong_to_tenant.assert_called_once() mock_request_user.can_save_user.assert_called_once() mock_user.set_password.assert_called_once() mock_user.save.assert_called_once() mock_request_user.realignment_aws_environments.assert_called_once() self.assertEqual(res, mock_user)
def create(self, request, tenant_pk=None, detail=True): log = NarukoLogging(request) logger = log.get_logger(__name__) logger.info("START: create") # バリデーション data = request.data data["tenant"] = tenant_pk serializer = UserModelSerializer(data=data) serializer.is_valid(raise_exception=True) # 作成 user = ControlUserUseCase(log).create_user( request.user, serializer.save(), AwsEnvironmentModel.objects.filter( id__in=data["aws_environments"]), data["password"]) logger.info("END: create") return Response(data=UserModelDetailSerializer(user).data, status=status.HTTP_201_CREATED)
def test_create_user_cant_save_aws(self): mock_request_user = mock.Mock() mock_request_user.realignment_aws_environments.return_value = False mock_user = mock.Mock() mock_aws = mock.Mock() password = "******" with self.assertRaises(PermissionDenied): ControlUserUseCase(mock.Mock()).create_user( mock_request_user, mock_user, mock_aws, password) mock_request_user.is_belong_to_tenant.assert_called_once() mock_request_user.can_save_user.assert_called_once() mock_user.set_password.assert_called_once() mock_user.save.assert_called_once() mock_request_user.realignment_aws_environments.assert_called_once()
def test_create_user_invalid_password(self): mock_request_user = mock.Mock() mock_user = mock.Mock() mock_user.set_password.return_value = False mock_aws = mock.Mock() password = "******" with self.assertRaises(InvalidPasswordException): ControlUserUseCase(mock.Mock()).create_user( mock_request_user, mock_user, mock_aws, password) mock_request_user.is_belong_to_tenant.assert_called_once() mock_request_user.can_save_user.assert_called_once() mock_user.set_password.assert_called_once() mock_user.save.assert_not_called() mock_request_user.realignment_aws_environments.assert_not_called()
def destroy(self, request, pk=None, tenant_pk=None): log = NarukoLogging(request) logger = log.get_logger(__name__) logger.info("START: destroy") try: with transaction.atomic(): target_user = UserModel.objects.get(id=pk, tenant_id=tenant_pk) ControlUserUseCase(log).delete_user(request.user, target_user) except (TypeError, ValueError, UserModel.DoesNotExist) as e: logger.exception(e) return Response(status=status.HTTP_404_NOT_FOUND) except Exception as e: logger.exception(e) raise else: logger.info("END: destroy") return Response(status=status.HTTP_204_NO_CONTENT)
def list(self, request, tenant_pk=None, detail=True): log = NarukoLogging(request) logger = log.get_logger(__name__) logger.info("START: list") try: tenant = TenantModel.objects.get(id=tenant_pk) users = ControlUserUseCase(log).fetch_users(request.user, tenant) except (TypeError, ValueError, TenantModel.DoesNotExist) as e: logger.exception(e) return Response(status=status.HTTP_404_NOT_FOUND) except Exception as e: logger.exception(e) raise else: logger.info("END: list") return Response(data={ "users": [UserModelDetailSerializer(user).data for user in users] })
def test_update_user(self, mock_role_model, mock_aws_model): post_user_data = { "email": "*****@*****.**", "name": "test_name", "password": "******", "role": 3, "aws_environments": [1, 2, 3] } mock_request_user = mock.Mock() mock_user = mock.Mock() res = ControlUserUseCase(mock.Mock()).update_user( post_user_data, mock_request_user, mock_user) mock_request_user.is_belong_to_tenant.assert_called_once() mock_request_user.can_save_user.assert_called() mock_user.can_changed_role.assert_called_once() mock_user.set_password.assert_called_once() mock_request_user.realignment_aws_environments.assert_called_once() mock_user.save.assert_called_once() self.assertEqual(res, mock_user)
def update(self, request, pk=None, tenant_pk=None, detail=True): log = NarukoLogging(request) logger = log.get_logger(__name__) logger.info("START: update") try: with transaction.atomic(): # バリデーション target_user = UserModel.objects.get(pk=pk, tenant_id=tenant_pk) UserModelUpdateValidationSerializer( instance=target_user, data=request.data).is_valid(raise_exception=True) # 更新 user = ControlUserUseCase(log).update_user( request.data, request.user, target_user) data = dict() data["user"] = UserModelDetailSerializer(user).data if request.user.id == user.id: jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER payload = jwt_payload_handler(user) token = jwt_encode_handler(payload) data["token"] = token except (TypeError, ValueError, KeyError, NarukoException) as e: # リクエストデータが不正 logger.exception(e) return Response(status=status.HTTP_400_BAD_REQUEST) except ObjectDoesNotExist as e: logger.exception(e) # ロールまたはテナントが存在しない return Response(status=status.HTTP_404_NOT_FOUND) except Exception as e: logger.exception(e) raise else: # 成功 logger.info("END: update") return Response(data=data, status=status.HTTP_200_OK)
def test_update_user_cant_save_aws(self, mock_role_model, mock_aws_model): post_user_data = { "email": "*****@*****.**", "name": "test_name", "password": "******", "role": 3, "aws_environments": [1, 2, 3] } mock_request_user = mock.Mock() mock_request_user.realignment_aws_environments.return_value = False mock_user = mock.Mock() with self.assertRaises(PermissionDenied): ControlUserUseCase(mock.Mock()).update_user( post_user_data, mock_request_user, mock_user) mock_request_user.is_belong_to_tenant.assert_called_once() mock_request_user.can_save_user.assert_called() mock_user.can_changed_role.assert_called_once() mock_user.set_password.assert_called_once() mock_request_user.realignment_aws_environments.assert_called_once() mock_user.save.assert_not_called()
def update(self, request, pk=None, tenant_pk=None, detail=True): log = NarukoLogging(request) logger = log.get_logger(__name__) logger.info("START: update") # バリデーション target_user = UserModel.objects.get(pk=pk, tenant_id=tenant_pk) UserModelUpdateValidationSerializer( instance=target_user, data=request.data).is_valid(raise_exception=True) # 更新 user = ControlUserUseCase(log).update_user(request.data, request.user, target_user) data = dict() data["user"] = UserModelDetailSerializer(user).data if request.user.id == user.id: jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER payload = jwt_payload_handler(user) token = jwt_encode_handler(payload) data["token"] = token logger.info("END: update") return Response(data=data, status=status.HTTP_200_OK)