Skip to content

Commit

Permalink
Merge 0aba408 into 973c5d9
Browse files Browse the repository at this point in the history
  • Loading branch information
dyohan9 committed Nov 4, 2019
2 parents 973c5d9 + 0aba408 commit f3b66f8
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 8 deletions.
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
39 changes: 32 additions & 7 deletions bothub/api/v2/tests/test_account.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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()
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 f3b66f8

Please sign in to comment.