def test_generate_otp_reactive_past(self): """ Check generate_otp generates a new otp if the reactive time is yet to be over """ otp_validation1 = utils.generate_otp("email", "*****@*****.**") otp_validation2 = utils.generate_otp("email", "*****@*****.**") assert otp_validation1.otp != otp_validation2.otp
def test_generate_otp_reactive_future(self): """ Check generate_otp returns the same otp if the reactive time is already over """ otp_validation1 = utils.generate_otp("email", "*****@*****.**") """ Simulating that the reactive time is already been over 5 minutes ago """ otp_validation1.reactive_at = timezone.now() + datetime.timedelta(minutes=5) otp_validation1.save() otp_validation2 = utils.generate_otp("email", "*****@*****.**") assert otp_validation1.otp == otp_validation2.otp
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"]))
def test_generate_otp(self): """Check generate_otp successfully generates OTPValidation object or not""" utils.generate_otp("email", "*****@*****.**") assert OTPValidation.objects.count() == 1
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) 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)