Esempio n. 1
0
    def test_validate_otp_raises_invalid_otp_exception(self):
        """Check function raises attempt exceeded exception"""
        with self.assertRaises(AuthenticationFailed) as context_manager:
            utils.validate_otp("*****@*****.**", 5623)

        assert (
            str(context_manager.exception.detail)
            == "OTP Validation failed! 2 attempts left!"
        )
Esempio n. 2
0
    def test_validate_otp_raises_attempt_exceeded_exception(self):
        """Check function raises attempt exceeded exception"""
        """
        Set the validate_attempt to 0. Raises attempt exceeded exception
        """
        self.otp_validation.validate_attempt = 0
        self.otp_validation.save()

        with self.assertRaises(AuthenticationFailed) as context_manager:
            utils.validate_otp("*****@*****.**", 56123)

        self.assertEqual(
            "Incorrect OTP. Attempt exceeded! OTP has been reset.",
            str(context_manager.exception.detail),
        )
Esempio n. 3
0
    def post(self, request, *args, **kwargs):
        """Overrides post method to validate serialized data"""
        serializer = self.serializer_class(data=request.data)
        serializer.is_valid(raise_exception=True)

        destination = serializer.validated_data.get("destination")
        prop = serializer.validated_data.get("prop")
        user = serializer.validated_data.get("user")
        email = serializer.validated_data.get("email")
        is_login = serializer.validated_data.get("is_login")

        if "verify_otp" in request.data.keys():
            if validate_otp(destination, request.data.get("verify_otp")):
                if is_login:
                    return Response(login_user(user, self.request),
                                    status=status.HTTP_202_ACCEPTED)
                else:
                    return Response(
                        data={"OTP": [
                            _("OTP Validated successfully!"),
                        ]},
                        status=status.HTTP_202_ACCEPTED,
                    )
        else:
            otp_obj = generate_otp(prop, destination)
            sentotp = send_otp(destination, otp_obj, email)

            if sentotp["success"]:
                otp_obj.send_counter += 1
                otp_obj.save()

                return Response(sentotp, status=status.HTTP_201_CREATED)
            else:
                raise APIException(detail=_("A Server Error occurred: " +
                                            sentotp["message"]))
Esempio n. 4
0
    def post(self, request, *args, **kwargs):
        """Overrides post method to validate OTP and reset password"""
        serializer = PasswordResetSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)

        user = User.objects.get(email=serializer.validated_data["email"])

        if validate_otp(serializer.validated_data["email"],
                        serializer.validated_data["otp"]):
            # OTP Validated, Change Password
            user.set_password(serializer.validated_data["password"])
            user.save()
            return JsonResponse(
                content="Password Updated Successfully.",
                status=status.HTTP_202_ACCEPTED,
            )
Esempio n. 5
0
    def post(self, request, *args, **kwargs):
        from drfaddons.utils import JsonResponse
        from rest_framework import status
        from drf_user.utils import validate_otp

        from .models import User
        from .serializers import PasswordResetSerializer

        serializer = PasswordResetSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)

        user = User.objects.get(email=serializer.validated_data["email"])

        if validate_otp(serializer.validated_data["email"],
                        serializer.validated_data["otp"]):
            # OTP Validated, Change Password
            user.set_password(serializer.validated_data["password"])
            user.save()
            return JsonResponse(
                content="Password Updated Successfully.",
                status=status.HTTP_202_ACCEPTED,
            )
Esempio n. 6
0
    def post(self, request, *args, **kwargs):
        """Overrides post method to validate serialized data"""

        from rest_framework.response import Response
        from rest_framework import status

        from rest_framework.exceptions import APIException

        from .utils import validate_otp, generate_otp, send_otp
        from .utils import login_user
        from .models import User
        from .variables import EMAIL, MOBILE

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

        verify_otp = serializer.validated_data.get("verify_otp", None)
        name = serializer.validated_data.get("name")
        mobile = serializer.validated_data.get("mobile")
        email = serializer.validated_data.get("email")
        user = serializer.validated_data.get("user", None)

        message = {}

        if verify_otp:
            if validate_otp(email, verify_otp):
                if not user:
                    user = User.objects.create_user(
                        name=name,
                        mobile=mobile,
                        email=email,
                        username=mobile,
                        password=User.objects.make_random_password(),
                    )
                    user.is_active = True
                    user.save()
            return Response(login_user(user, self.request),
                            status=status.HTTP_202_ACCEPTED)

        else:
            otp_obj_email = generate_otp(EMAIL, email)
            otp_obj_mobile = generate_otp(MOBILE, mobile)

            # Set same OTP for both Email & Mobile
            otp_obj_mobile.otp = otp_obj_email.otp
            otp_obj_mobile.save()

            # Send OTP to Email & Mobile
            sentotp_email = send_otp(email, otp_obj_email, email)
            sentotp_mobile = send_otp(mobile, otp_obj_mobile, email)

            if sentotp_email["success"]:
                otp_obj_email.send_counter += 1
                otp_obj_email.save()
                message["email"] = {
                    "otp": _("OTP has been sent successfully.")
                }
            else:
                message["email"] = {
                    "otp":
                    _("OTP sending failed {}".format(sentotp_email["message"]))
                }

            if sentotp_mobile["success"]:
                otp_obj_mobile.send_counter += 1
                otp_obj_mobile.save()
                message["mobile"] = {
                    "otp": _("OTP has been sent successfully.")
                }
            else:
                message["mobile"] = {
                    "otp":
                    _("OTP sending failed {}".format(
                        sentotp_mobile["message"]))
                }

            if sentotp_email["success"] or sentotp_mobile["success"]:
                curr_status = status.HTTP_201_CREATED
            else:
                raise APIException(detail=_("A Server Error occurred: " +
                                            sentotp_mobile["message"]))

            return Response(data=message, status=curr_status)
Esempio n. 7
0
 def test_validate_otp(self):
     """Check if OTPValidation object is created or not"""
     assert utils.validate_otp("*****@*****.**", 12345)