예제 #1
0
    def test_create_tenant(self, mock_ses, mock_role_model, mock_user_model):
        # mock準備
        mock_request_user = mock.Mock()
        mock_request_user.can_control_tenant.return_value = True

        mock_tenant = mock.Mock()

        mock_user = mock.Mock()

        mock_scheduler = mock.Mock()
        mock_user_model.return_value = mock_scheduler

        res_tenant, res_user = ControlTenantUseCase(mock.Mock()).create_tenant(
            mock_request_user, mock_tenant, mock_user
        )

        # 呼び出し検証
        mock_request_user.can_control_tenant.assert_called_once()
        mock_tenant.save.assert_called()
        mock_role_model.objects.get.assert_called_once_with(id=mock_role_model.SCHEDULER_ID)
        mock_user_model.assert_called_once_with(
            email=mock_tenant.email,
            name="SCHEDULER",
            tenant=mock_tenant,
            role=mock_role_model.objects.get.return_value
        )
        mock_scheduler.reset_password.assert_called_once()
        mock_scheduler.save.assert_called_once()
        mock_user.reset_password.assert_called()
        mock_user.save.assert_called()
        mock_ses.assert_called()
        mock_ses.return_value.send_signup_user.assert_called()
        self.assertEqual(res_tenant, mock_tenant)
        self.assertEqual(res_user, mock_user)
예제 #2
0
    def test_create_tenant_cant_control_tenant(self, mock_ses, mock_role_model, mock_user_model):
        # mock準備
        mock_request_user = mock.Mock()
        mock_request_user.can_control_tenant.return_value = False

        mock_tenant = mock.Mock()

        mock_user = mock.Mock()

        mock_scheduler = mock.Mock()
        mock_user_model.return_value = mock_scheduler

        with self.assertRaises(InvalidRoleException):
            ControlTenantUseCase(mock.Mock()).create_tenant(
                mock_request_user, mock_tenant, mock_user
            )

        # 呼び出し検証
        mock_request_user.can_control_tenant.assert_called_once()
        mock_tenant.save.assert_not_called()
        mock_role_model.objects.get.assert_not_called()
        mock_user_model.assert_not_called()
        mock_scheduler.reset_password.assert_not_called()
        mock_scheduler.save.assert_not_called()
        mock_user.reset_password.assert_not_called()
        mock_user.save.assert_not_called()
        mock_ses.assert_not_called()
        mock_ses.return_value.send_signup_user.assert_not_called()
예제 #3
0
    def test_delete_tenant(self):
        mock_user = mock.Mock()
        mock_tenant = mock.Mock(spec=TenantModel)
        ControlTenantUseCase(mock.Mock()).delete_tenant(mock_user, mock_tenant)

        mock_tenant.delete.assert_called()
        mock_user.can_control_tenant.assert_called_once()
예제 #4
0
    def create(self, request):
        log = NarukoLogging(request)
        logger = log.get_logger(__name__)
        logger.info("START: create")
        # バリデーション:Company
        tenant_serializer = TenantModelSerializer(data=request.data["tenant"])
        tenant_serializer.is_valid(raise_exception=True)
        tenant_serializer_save = tenant_serializer.save()

        # バリデーション:User
        data_user = request.data["user"]
        data_user["tenant"] = tenant_serializer_save.id
        data_user["role"] = RoleModel.ADMIN_ID
        user_serializer = UserModelSerializer(data=data_user)
        user_serializer.is_valid(raise_exception=True)
        user_serializer_save = user_serializer.save()

        # 作成
        tenant, user = ControlTenantUseCase(log).create_tenant(
            request.user,
            tenant_serializer_save,
            user_serializer_save
        )
        logger.info("END: create")
        return Response(
            data={
                "tenant": TenantModelDetailSerializer(tenant).data,
                "user": UserModelDetailSerializer(user).data
            },
            status=status.HTTP_201_CREATED)
예제 #5
0
 def list(self, request):
     log = NarukoLogging(request)
     logger = log.get_logger(__name__)
     logger.info("START: list")
     tenants = ControlTenantUseCase(log).fetch_tenants(request.user)
     logger.info("END: list")
     return Response(data={"tenants": [TenantModelDetailSerializer(tenant).data for tenant in tenants]})
예제 #6
0
    def update(self, request, pk=None):
        log = NarukoLogging(request)
        logger = log.get_logger(__name__)
        logger.info("START: update")
        try:
            with transaction.atomic():
                # バリデーション
                target_tenant = TenantModel.objects.get(pk=pk)
                serializer = TenantModelSerializer(instance=target_tenant,
                                                   data=request.data)
                serializer.is_valid(raise_exception=True)

                # 更新
                tenant = ControlTenantUseCase(log).update_tenant(
                    request.user, serializer.save())
                data = TenantModelSerializer(tenant).data
        except (TypeError, ValueError, KeyError) as e:
            # リクエストデータが不正
            logger.exception(e)
            return Response(status=status.HTTP_400_BAD_REQUEST)
        except 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: update")
            return Response(data=data, status=status.HTTP_200_OK)
예제 #7
0
    def test_fetch_tenants_cant_control(self, mock_tenant):
        mock_user = mock.Mock()
        mock_user.can_control_tenant.return_value = False

        with self.assertRaises(InvalidRoleException):
            ControlTenantUseCase(mock.Mock()).fetch_tenants(mock_user)

        mock_user.can_control_tenant.assert_called_once()
예제 #8
0
    def test_update_tenant(self):
        mock_user = mock.Mock()
        mock_tenant = mock.Mock(spec=TenantModel)
        res = ControlTenantUseCase(mock.Mock()).update_tenant(mock_user, mock_tenant)

        mock_tenant.save.assert_called()
        mock_user.can_control_tenant.assert_called_once()
        self.assertEqual(res, mock_tenant)
예제 #9
0
 def destroy(self, request, pk=None):
     log = NarukoLogging(request)
     logger = log.get_logger(__name__)
     logger.info("START: destroy")
     tenant = TenantModel.objects.get(id=int(pk))
     ControlTenantUseCase(log).delete_tenant(request.user, tenant)
     logger.info("END: destroy")
     return Response(status=status.HTTP_204_NO_CONTENT)
예제 #10
0
    def test_delete_tenant_cant_control_tenant(self):
        mock_user = mock.Mock()
        mock_user.can_control_tenant.return_value = False
        mock_tenant = mock.Mock(spec=TenantModel)

        with self.assertRaises(InvalidRoleException):
            ControlTenantUseCase(mock.Mock()).delete_tenant(mock_user, mock_tenant)

        mock_user.can_control_tenant.assert_called_once()
        mock_tenant.delete.assert_not_called()
예제 #11
0
    def test_fetch_tenants(self, mock_tenant):
        mock_user = mock.Mock()
        mock_tenant1 = mock.Mock(spec=TenantModel)
        mock_tenant2 = mock.Mock(spec=TenantModel)
        mock_tenant3 = mock.Mock(spec=TenantModel)
        expected_response = [mock_tenant1, mock_tenant2, mock_tenant3]
        mock_tenant.objects.all.return_value = expected_response
        response = ControlTenantUseCase(mock.Mock()).fetch_tenants(mock_user)

        mock_user.can_control_tenant.assert_called_once()
        self.assertEqual(response, expected_response)
예제 #12
0
    def update(self, request, pk=None):
        log = NarukoLogging(request)
        logger = log.get_logger(__name__)
        logger.info("START: update")
        # バリデーション
        target_tenant = TenantModel.objects.get(pk=pk)
        serializer = TenantModelSerializer(instance=target_tenant, data=request.data)
        serializer.is_valid(raise_exception=True)

        # 更新
        tenant = ControlTenantUseCase(log).update_tenant(request.user, serializer.save())
        data = TenantModelSerializer(tenant).data
        logger.info("END: update")
        return Response(data=data, status=status.HTTP_200_OK)
예제 #13
0
 def destroy(self, request, pk=None):
     log = NarukoLogging(request)
     logger = log.get_logger(__name__)
     logger.info("START: destroy")
     try:
         with transaction.atomic():
             tenant = TenantModel.objects.get(id=int(pk))
             ControlTenantUseCase(log).delete_tenant(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: destroy")
         return Response(status=status.HTTP_204_NO_CONTENT)