def get(self, request, uidb64, token):
        """
        Override to get the activation uid and token

        Args:
            request (object): Request object
            uidb64 (string): The uid
            token (string): The user token

        """
        user = get_user_from_uid(uidb64)

        if user and activation_token.check_token(user, token):
            set_user_verified(user)

            send_email_welcome(user)

            if drfr_settings.USER_ACTIVATE_SUCSSESS_TEMPLATE:
                return render(request, drfr_settings.USER_ACTIVATE_SUCSSESS_TEMPLATE) # pragma: no cover
            return HttpResponse(_('Your account has been activate successfully.'))

        if drfr_settings.USER_ACTIVATE_FAILED_TEMPLATE:
            return render(request, drfr_settings.USER_ACTIVATE_FAILED_TEMPLATE) # pragma: no cover
        return HttpResponse(_('Either the provided activation token is '
                              'invalid or this account has already been activated.'))
    def test_login_ok(self):
        set_user_verified(self.user, True)
        params = {
            'username': self.user.username,
            'password': '******',
        }
        resp = self.post_json_ok('login/', params)

        self.assertHasProps(resp.data,
                            ['id', 'username', 'email', 'is_active', 'token'])
    def create(self, validated_data):
        """
        Override create method to create user password
        """
        user = super().create(validated_data)
        user.set_password(validated_data['password'])

        # Disable veriried if enable verify user, else set it enabled
        if has_user_activate_token() or has_user_verify_code():
            set_user_verified(user, False)
        else:
            set_user_verified(user, True)
        user.save()
        return user
Exemple #4
0
    def post(self, request, *args, **kwargs):
        """
        Authenticate user through the provider and access_token
        """

        serializer = self.serializer_class(data=request.data)
        serializer.is_valid(raise_exception=True)

        provider = serializer.data.get('provider', None)

        # Check is invalid provider
        if not socials.is_valid_provider(provider):
            raise InvalidProvider()

        # Check valid token
        access_token = serializer.data.get('access_token', None)

        user_data = socials.get_user_info(provider, access_token)

        # None value mean the access token is not valid
        if not user_data:
            raise InvalidAccessToken()

        # Check the case can not get user email address
        if not user_data.get('email'):
            raise MissingEmail()

        # Create user if not exist
        User = get_user_model()
        try:
            user = User.objects.get(email=user_data['email'])
        except User.DoesNotExist:
            user = User.objects.create(
                username=user_data['email'],
                email=user_data['email'],
                first_name=user_data.get('first_name'),
                last_name=user_data.get('last_name'),
            )

            # Always verified user if they using Google or Facebook
            set_user_verified(user)

        # Update last logged in
        update_last_login(None, user)
        data = get_user_profile_data(user)

        return Response(data, status=status.HTTP_200_OK)
    def test_login_inactivated_user(self):
        set_user_verified(self.user, False)

        params = {'username': self.user.username, 'password': '******'}
        resp = self.post_json_unauthorized('login/', params)
        self.assertEqual(resp.data['detail'], 'Account is not activated.')
Exemple #6
0
    def test_has_user_verified(self):
        users.set_user_verified(self.user, True)

        self.assertTrue(users.has_user_verified(self.user))
Exemple #7
0
 def test_not_has_user_verified(self):
     users.set_user_verified(self.user, False)
     self.assertFalse(users.has_user_verified(self.user))