diff --git a/README.md b/README.md index e234fc5e8..70963fc31 100644 --- a/README.md +++ b/README.md @@ -100,3 +100,6 @@ You can set environment variables in your OS, write on ```.env``` file or pass v | BOTHUB_ENGINE_AWS_SECRET_ACCESS_KEY | ```string``` | ```None``` | | BOTHUB_ENGINE_AWS_REGION_NAME | ```string``` | ```None``` | | BOTHUB_ENGINE_AWS_SEND | ```bool``` | ```False``` | +| BOTHUB_BOT_EMAIL | ```string``` | ```bot_repository@bothub.it``` | Email that the system will automatically create for existing repositories that the owner deleted the account +| BOTHUB_BOT_NAME | ```string``` | ```Bot Repository``` | Name that the system will use to create the account +| BOTHUB_BOT_NICKNAME | ```string``` | ```bot_repository``` | Nickname that the system will use to create the account diff --git a/bothub/api/v2/account/views.py b/bothub/api/v2/account/views.py index 7aed33801..60ec1050a 100644 --- a/bothub/api/v2/account/views.py +++ b/bothub/api/v2/account/views.py @@ -11,6 +11,8 @@ from bothub.api.v2.metadata import Metadata from bothub.authentication.models import User +from bothub.common.models import Repository +from bothub.common.models import RepositoryUpdate from .serializers import ChangePasswordSerializer from .serializers import LoginSerializer from .serializers import RegisterUserSerializer @@ -117,7 +119,10 @@ def create(self, request, *args, **kwargs): class MyUserProfileViewSet( - mixins.RetrieveModelMixin, mixins.UpdateModelMixin, GenericViewSet + mixins.RetrieveModelMixin, + mixins.UpdateModelMixin, + mixins.DestroyModelMixin, + GenericViewSet, ): """ Manager current user profile. @@ -143,6 +148,17 @@ def get_object(self, *args, **kwargs): return user + def destroy(self, request, *args, **kwargs): + repositories = Repository.objects.filter(owner=self.request.user) + repository_update = RepositoryUpdate.objects.filter(by=self.request.user) + user = User.generate_repository_user_bot() + + if repositories.count() > 0: + repositories.update(owner_id=user.pk) + if repository_update.count() > 0: + repository_update.update(by=user.pk) + return super().destroy(request, *args, **kwargs) + class UserProfileViewSet(mixins.RetrieveModelMixin, GenericViewSet): """ diff --git a/bothub/api/v2/tests/test_account.py b/bothub/api/v2/tests/test_account.py index c6b118fad..cceec0960 100644 --- a/bothub/api/v2/tests/test_account.py +++ b/bothub/api/v2/tests/test_account.py @@ -1,21 +1,21 @@ import json -from django.test import TestCase from django.test import RequestFactory +from django.test import TestCase from django.test.client import MULTIPART_CONTENT from rest_framework import status from bothub.authentication.models import User - -from ..account.views import RegisterUserViewSet -from ..account.views import LoginViewSet +from bothub.common import languages +from bothub.common.models import Repository +from .utils import create_user_and_token from ..account.views import ChangePasswordViewSet +from ..account.views import LoginViewSet +from ..account.views import MyUserProfileViewSet +from ..account.views import RegisterUserViewSet from ..account.views import RequestResetPasswordViewSet from ..account.views import ResetPasswordViewSet from ..account.views import UserProfileViewSet -from ..account.views import MyUserProfileViewSet - -from .utils import create_user_and_token class LoginTestCase(TestCase): @@ -255,6 +255,31 @@ def test_okay(self): self.assertEqual(content_data.get("nickname"), self.user.nickname) +class DestroyMyProfileTestCase(TestCase): + def setUp(self): + self.factory = RequestFactory() + self.user, self.user_token = create_user_and_token() + + self.repository = Repository.objects.create( + owner=self.user, name="Testing", slug="test", language=languages.LANGUAGE_EN + ) + + def request(self, token): + authorization_header = {"HTTP_AUTHORIZATION": "Token {}".format(token.key)} + request = self.factory.delete("/v2/account/my-profile/", **authorization_header) + response = MyUserProfileViewSet.as_view({"delete": "destroy"})(request) + response.render() + return response + + def test_okay(self): + response = self.request(self.user_token) + self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) + + repository = Repository.objects.get(pk=self.repository.pk) + + self.assertEqual(repository.owner, User.generate_repository_user_bot()) + + class UserUpdateTestCase(TestCase): def setUp(self): self.factory = RequestFactory() diff --git a/bothub/authentication/models.py b/bothub/authentication/models.py index f0b8c4067..41a5506bb 100644 --- a/bothub/authentication/models.py +++ b/bothub/authentication/models.py @@ -127,6 +127,18 @@ def send_reset_password_email(self): def check_password_reset_token(self, token): return self.token_generator.check_token(self, token) + @staticmethod + def generate_repository_user_bot(): + user, create = User.objects.get_or_create( + email=settings.BOTHUB_BOT_EMAIL, + name=settings.BOTHUB_BOT_NAME, + nickname=settings.BOTHUB_BOT_NICKNAME, + locale="", + is_staff=False, + is_active=False, + ) + return user + @receiver(models.signals.post_save, sender=User) def send_welcome_email(instance, created, **kwargs): diff --git a/bothub/settings.py b/bothub/settings.py index e0e65efe3..88cb81d9d 100644 --- a/bothub/settings.py +++ b/bothub/settings.py @@ -46,6 +46,9 @@ BOTHUB_ENGINE_AWS_REGION_NAME=(str, "us-east-1"), BOTHUB_ENGINE_AWS_SEND=(bool, False), BASE_URL=(str, "http://api.bothub.it"), + BOTHUB_BOT_EMAIL=(str, "bot_repository@bothub.it"), + BOTHUB_BOT_NAME=(str, "Bot Repository"), + BOTHUB_BOT_NICKNAME=(str, "bot_repository"), ) # Build paths inside the project like this: os.path.join(BASE_DIR, ...) @@ -278,3 +281,10 @@ AWS_SECRET_ACCESS_KEY = env.str("BOTHUB_ENGINE_AWS_SECRET_ACCESS_KEY") AWS_BUCKET_NAME = env.str("BOTHUB_ENGINE_AWS_S3_BUCKET_NAME") AWS_REGION_NAME = env.str("BOTHUB_ENGINE_AWS_REGION_NAME") + + +# Account System for bots deleted + +BOTHUB_BOT_EMAIL = env.str("BOTHUB_BOT_EMAIL") +BOTHUB_BOT_NAME = env.str("BOTHUB_BOT_NAME") +BOTHUB_BOT_NICKNAME = env.str("BOTHUB_BOT_NICKNAME")