Skip to content

Commit

Permalink
Added route delete user
Browse files Browse the repository at this point in the history
if there is a repository whose user owns, the system replaces for the user of the system
  • Loading branch information
dyohan9 committed Nov 4, 2019
1 parent 973c5d9 commit 685b3da
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 17 additions & 1 deletion bothub/api/v2/account/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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):
"""
Expand Down
28 changes: 28 additions & 0 deletions bothub/api/v2/tests/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
from rest_framework import status

from bothub.authentication.models import User
from bothub.common import languages
from bothub.common.models import Repository
from bothub.common.models import RepositoryExample

from ..account.views import RegisterUserViewSet
from ..account.views import LoginViewSet
Expand Down Expand Up @@ -255,6 +258,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()
Expand Down
12 changes: 12 additions & 0 deletions bothub/authentication/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
10 changes: 10 additions & 0 deletions bothub/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...)
Expand Down Expand Up @@ -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")

0 comments on commit 685b3da

Please sign in to comment.