示例#1
0
    def test_confirm_email__invalid_new_email(test_user):
        test_user.is_active = False
        test_user.save()

        pending_action = gen_pending_action(test_user, email='anything')
        with pytest.raises(ValidationError):
            AuthService.confirm_email(pending_action)
        assert test_user.is_active
示例#2
0
    def test_confirm_email(test_user):
        test_user.is_active = True
        test_user.save(update_fields=['is_active'])

        pending_action = gen_pending_action(user=test_user)
        AuthService.confirm_email(pending_action)

        test_user.refresh_from_db()
        assert test_user.is_active
示例#3
0
    def test_confirm_email__new_email_alredy_used(test_user):
        test_user.email = "*****@*****.**"
        test_user.save()

        another_user = UserFactory(is_active=False)
        pending_action = gen_pending_action(user=another_user, email=test_user.email)
        with pytest.raises(ValidationError):
            AuthService.confirm_email(pending_action)

        assert another_user.is_active
        assert another_user != test_user.email
示例#4
0
    def test_confirm_email__new_email(test_user):
        new_email = '*****@*****.**'
        test_user.is_active = False
        test_user.save(update_fields=['is_active'])

        pending_action = gen_pending_action(test_user, email=new_email)
        AuthService.confirm_email(pending_action)

        assert test_user.is_active
        test_user.refresh_from_db()
        assert test_user.email == new_email
示例#5
0
    def email_confirmation(self, request):
        """Confirms an email."""
        serializer = TokenSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)

        pending_action = PendingActionSelector.get_by_token(
            token=serializer.validated_data['token'],
            category=ActionCategory.CONFIRM_EMAIL.value,
        )
        AuthService.confirm_email(pending_action)
        return DoneResponse(**response_codes.EMAIL_VERIFIED)
示例#6
0
    def post(self, request):
        """Registers an user using the info."""
        serializer = RegisterSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)

        if config.REGISTER_REQUIRES_EMAIL_CONFIRMATION:
            user = UserService.register_new_user(
                user_data=serializer.validated_data)
            AuthService.send_confirmation_email(user)
            return DoneResponse(**CONFIRMATION_EMAIL_SENT)
        else:
            user = UserService.register_new_user(
                user_data=serializer.validated_data)
            return Response(SessionSerializer(user).data)
示例#7
0
    def get(self, request, token, **kwargs):
        """It renders the html template to confirm email."""
        context = {}
        try:
            pending_action = PendingAction.objects.get(
                token=token,
                category=ActionCategory.CONFIRM_EMAIL,
            )
            context['user'] = pending_action.user
            context['next'] = pending_action.extra.get('next')
            AuthService.confirm_email(pending_action)

        except PendingAction.DoesNotExist:
            context['user'] = None

        return render(request, 'transactions/confirm_email.html', context)
示例#8
0
    def email_confirmation_request(self, request):
        """Requests a confirmation email."""
        serializer = UsernameOrEmailSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)

        username_or_email = serializer.validated_data['user']
        user = UserSelector.get_by_username_or_email(username_or_email)

        if user.is_active:
            return DoneResponse(
                **response_codes.EMAIL_VERIFIED,
                status=status.HTTP_400_BAD_REQUEST,
            )
        else:
            AuthService.send_confirmation_email(user)
            return DoneResponse(**response_codes.CONFIRMATION_EMAIL_SENT)
示例#9
0
 def test_send_confirmation_email__with_new_email(test_user):
     AuthService.send_confirmation_email(test_user, new_email='*****@*****.**')
     assert mail_outbox() == 1
示例#10
0
 def test_send_confirmation_email(test_user):
     AuthService.send_confirmation_email(test_user)
     assert mail_outbox() == 1