def test_confirm_verification_code(self):
        """
        Ensure verification code is confirm and user profile is
        set to is_verified.
        """
        username = "******"
        user = User.objects.create_user(username, 'johnpassword')
        code = generate_random_code(6)
        PasswordVerification.objects.create(
            user=user,
            code=code,
            mode=PasswordVerificationModeChoice.EMAIL_VERIFY,
        )

        self.client.force_authenticate(user=user)

        url_invalid = reverse('confirm-verification-code',
                              kwargs={'verification_code': "invalid-code"})
        response = self.client.get(url_invalid)
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(response.content, b'"Invalid code."')
        self.assertEqual(User.objects.get().profile.is_verified, False)

        url_valid = reverse('confirm-verification-code',
                            kwargs={'verification_code': code})
        response = self.client.get(url_valid)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(User.objects.get(username=username).profile.is_verified, True)
Esempio n. 2
0
    def test_confirm_verification_code(self):
        """
        Ensure verification code is confirm and user profile is
        set to is_verified.
        """
        user = User.objects.create_user('john', 'johnpassword')
        code = generate_random_code(6)
        PasswordVerification.objects.create(user=user,
                                            code=code,
                                            created=timezone.now())

        self.client.force_authenticate(user=user)

        url_invalid = reverse('confirm-verification-code',
                              kwargs={'verification_code': "invalid-code"})
        response = self.client.get(url_invalid)
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
        self.assertEqual(response.content,
                         b'"Unable to confirm verification code"')
        self.assertEqual(User.objects.get().profile.is_verified, False)

        url_valid = reverse('confirm-verification-code',
                            kwargs={'verification_code': code})
        response = self.client.get(url_valid)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(User.objects.get().profile.is_verified, True)
Esempio n. 3
0
def forgot_password_email_code(request, username):
    """
    create and send email containing code for forgot password
    :param request:
    :param username: email to identify the user
    :return: 400 if code generation or email sending fails
    :return: 404 if invalid username
    :return: 200 successful
    """
    # generating/retrieving code
    try:
        # check if user with given username exists
        user = User.objects.get(username=username)
    except User.DoesNotExist:
        error_message = "Invalid username"
        return Response(error_message, status=status.HTTP_404_NOT_FOUND)

    code = None
    pass_verify_obj = None
    try:
        # if code already exists
        pass_verify_obj = PasswordVerification.objects.get(
            user=user, mode=PasswordVerificationModeChoice.FORGET_PASSWORD)
        if is_password_verification_code_valid(pass_verify_obj):
            code = pass_verify_obj.code
    except PasswordVerification.DoesNotExist:
        pass

    if not code:
        code = generate_random_code()
    if not pass_verify_obj:
        pass_verify_obj = PasswordVerification(
            user=user,
            mode=PasswordVerificationModeChoice.FORGET_PASSWORD,
            code=code)
    pass_verify_obj.code = code
    pass_verify_obj.save()

    # sending code via email
    try:
        to_list = [user.username]
        fullname = "{} {}".format(user.first_name, user.last_name)
        mail_subject = FORGOT_PASSWORD_MAIL_SUBJECT
        mail_content = FORGOT_PASSWORD_MAIL_CONTENT.format(fullname, code)
        send_mail(mail_subject,
                  mail_content,
                  DEFAULT_EMAIL_SENDER,
                  to_list,
                  fail_silently=False)
    except SMTPException:
        error_message = "Unable to send a forgot password email to user"
        return Response(error_message, status=status.HTTP_400_BAD_REQUEST)

    return Response("Email sent", status=status.HTTP_200_OK)
Esempio n. 4
0
def generate_verification_code(request):
    """
    Generate and send Email verification code to user
    :param request:
    :return: 400 if email sending fails
    :return: 200 successful
    """

    code = None
    pass_verify_obj = None
    try:
        # if code already exists
        pass_verify_obj = PasswordVerification.objects.get(
            user=request.user,
            mode=PasswordVerificationModeChoice.EMAIL_VERIFY)
        if is_password_verification_code_valid(pass_verify_obj):
            code = pass_verify_obj.code
    except PasswordVerification.DoesNotExist:
        pass

    if not code:
        code = generate_random_code()
    if not pass_verify_obj:
        pass_verify_obj = PasswordVerification(
            user=request.user,
            mode=PasswordVerificationModeChoice.EMAIL_VERIFY,
            code=code)
    pass_verify_obj.code = code
    pass_verify_obj.save()

    # sending code via email
    try:
        to_list = [request.user.username]
        fullname = "{} {}".format(request.user.first_name,
                                  request.user.last_name)
        mail_subject = VERIFICATION_CODE_MAIL_SUBJECT
        mail_content = VERIFICATION_CODE_MAIL_CONTENT.format(fullname, code)
        send_mail(mail_subject,
                  mail_content,
                  DEFAULT_EMAIL_SENDER,
                  to_list,
                  fail_silently=False)
    except SMTPException:
        error_message = "Unable to send verification code email to user"
        return Response(error_message, status=status.HTTP_400_BAD_REQUEST)
    return Response("Verification code sent", status=status.HTTP_200_OK)
Esempio n. 5
0
def generate_verification_code(request):
    """
    Generate and send Email verification code to user
    :param request:
    :return: 400 if email sending fails
    :return: 200 successful
    """

    code = None
    pass_verify_obj = None
    try:
        # if code already exists
        pass_verify_obj = PasswordVerification.objects.get(
            user=request.user,
            mode=PasswordVerificationModeChoice.EMAIL_VERIFY)
        if is_password_verification_code_valid(pass_verify_obj):
            code = pass_verify_obj.code
    except PasswordVerification.DoesNotExist:
        pass

    if not code:
        code = generate_random_code()
    if not pass_verify_obj:
        pass_verify_obj = PasswordVerification(
            user=request.user,
            mode=PasswordVerificationModeChoice.EMAIL_VERIFY,
            code=code)
    pass_verify_obj.code = code
    pass_verify_obj.save()

    # Attempt sending email
    to_list = [request.user.username]
    fullname = "{} {}".format(request.user.first_name, request.user.last_name)
    mail_subject = VERIFICATION_CODE_MAIL_SUBJECT
    mail_content = VERIFICATION_CODE_MAIL_CONTENT.format(fullname, code)

    if is_send_email(to_list, mail_subject, mail_content):
        message = "Email sent."
        return Response(message, status=status.HTTP_200_OK)
    else:
        message = "Unable to send email to registered email address"
        return Response(message, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 6
0
def generate_verification_code(request):
    """
    generate and send verification code to user
    :param request:
    :return: 400 if email sending fails
    :return: 200 successful
    """
    code = generate_random_code(6)
    created_at = timezone.now()
    pass_ver, _ = PasswordVerification.objects.update_or_create(
        user=request.user, defaults={"code": code, "created": created_at})
    try:
        to_list = [request.user.username]
        fullname = "{} {}".format(request.user.first_name, request.user.last_name).title()
        mail_subject = VERIFICATION_CODE_MAIL_SUBJECT
        mail_content = VERIFICATION_CODE_MAIL_CONTENT.format(fullname, code)
        send_mail(mail_subject, mail_content, DEFAULT_EMAIL_SENDER, to_list, fail_silently=False)
    except SMTPException as e:
        error_message = "Unable to send verification code email to user"
        return Response(error_message, status=status.HTTP_400_BAD_REQUEST)
    return Response("Verification code sent", status=status.HTTP_200_OK)
Esempio n. 7
0
def forgot_password_email_code(request):
    """
    create and send email containing code for forgot password
    :param request:
    :return: 400 if code generation or email sending fails
    :return: 200 successful
    """
    # generating/retrieving code
    try:
        # if code already exists
        pass_ver = PasswordVerification.objects.get(user=request.user)
        code = pass_ver.code

    except PasswordVerification.DoesNotExist:
        # generate and save new code
        code = generate_random_code()
        pass_ver = PasswordVerification(user=request.user, code=code)
        pass_ver.save()

    except Exception as e:
        return Response(str(e), status=status.HTTP_400_BAD_REQUEST)

    # sending code via email
    try:
        to_list = [request.user.username]
        fullname = "{} {}".format(request.user.first_name,
                                  request.user.last_name)
        mail_subject = FORGOT_PASSWORD_MAIL_SUBJECT
        mail_content = FORGOT_PASSWORD_MAIL_CONTENT.format(fullname, code)
        send_mail(mail_subject,
                  mail_content,
                  DEFAULT_EMAIL_SENDER,
                  to_list,
                  fail_silently=False)
    except SMTPException as e:
        error_message = "Unable to send a forgot password email to user"
        return Response(error_message, status=status.HTTP_400_BAD_REQUEST)

    return Response("Email sent", status=status.HTTP_200_OK)