def test_get_potentially_retired_user_does_not_exist():
    """
    Check that the call to get a user with a non-existent
    username and hashed username bubbles up User.DoesNotExist
    """
    fake_username = "******"
    hashed_username = get_retired_username_by_username(fake_username)

    with pytest.raises(User.DoesNotExist):
        get_potentially_retired_user_by_username_and_hash(fake_username, hashed_username)
def test_get_potentially_retired_user_bad_hash():
    """
    Check that the call will raise an exeption if the given hash
    of the username doesn't match any salted hashes the system
    knows about.
    """
    fake_username = "******"

    with pytest.raises(Exception):
        get_potentially_retired_user_by_username_and_hash(fake_username, "bad hash")
def test_get_potentially_retired_user_bad_hash():
    """
    Check that the call will raise an exeption if the given hash
    of the username doesn't match any salted hashes the system
    knows about.
    """
    fake_username = "******"

    with pytest.raises(Exception):
        get_potentially_retired_user_by_username_and_hash(fake_username, "bad hash")
def test_get_potentially_retired_user_does_not_exist():
    """
    Check that the call to get a user with a non-existent
    username and hashed username bubbles up User.DoesNotExist
    """
    fake_username = "******"
    hashed_username = get_retired_username_by_username(fake_username)

    with pytest.raises(User.DoesNotExist):
        get_potentially_retired_user_by_username_and_hash(fake_username, hashed_username)
Example #5
0
    def post(self, request, username):
        """
        POST /api/user/v1/accounts/{username}/retire_mailings/

        Allows an administrative user to take the following actions
        on behalf of an LMS user:
        -  Update UserOrgTags to opt the user out of org emails
        -  Call Sailthru API to force opt-out the user from all email lists
        """
        user_model = get_user_model()
        retired_username = request.data['retired_username']

        try:
            user = get_potentially_retired_user_by_username_and_hash(
                username, retired_username)

            with transaction.atomic():
                # Take care of org emails first, using the existing API for consistency
                for preference in UserOrgTag.objects.filter(user=user,
                                                            key='email-optin'):
                    update_email_opt_in(user, preference.org, False)

                # This signal allows lms' email_marketing and other 3rd party email
                # providers to unsubscribe the user as well
                USER_RETIRE_MAILINGS.send(sender=self.__class__, user=user)
        except user_model.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)
        except Exception as exc:  # pylint: disable=broad-except
            return Response(text_type(exc),
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)

        return Response(status=status.HTTP_204_NO_CONTENT)
def test_get_potentially_retired_user_username_match(retirement_user):
    """
    Check that we can pass in an un-retired username and get the
    user-to-be-retired back.
    """
    hashed_username = get_retired_username_by_username(retirement_user.username)
    assert get_potentially_retired_user_by_username_and_hash(retirement_user.username, hashed_username) == retirement_user
def test_get_potentially_retired_user_username_match(retirement_user):
    """
    Check that we can pass in an un-retired username and get the
    user-to-be-retired back.
    """
    hashed_username = get_retired_username_by_username(retirement_user.username)
    assert get_potentially_retired_user_by_username_and_hash(retirement_user.username, hashed_username) == retirement_user
Example #8
0
def test_get_potentially_retired_user_username_match():
    """
    Check that we can pass in an un-retired username and get the
    user-to-be-retired back.
    """
    user = UserFactory()
    hashed_username = get_retired_username_by_username(user.username)
    assert get_potentially_retired_user_by_username_and_hash(user.username, hashed_username) == user
def test_get_potentially_retired_user_hashed_match(retirement_user):
    """
    Check that we can pass in a hashed username and get the
    user-to-be-retired back.
    """
    orig_username = retirement_user.username
    hashed_username = get_retired_username_by_username(orig_username)

    # Fake username retirement.
    retirement_user.username = hashed_username
    retirement_user.save()

    # Check to find the user by original username should fail,
    # 2nd check by hashed username should succeed.
    assert get_potentially_retired_user_by_username_and_hash(orig_username, hashed_username) == retirement_user
def test_get_potentially_retired_user_hashed_match(retirement_user):
    """
    Check that we can pass in a hashed username and get the
    user-to-be-retired back.
    """
    orig_username = retirement_user.username
    hashed_username = get_retired_username_by_username(orig_username)

    # Fake username retirement.
    retirement_user.username = hashed_username
    retirement_user.save()

    # Check to find the user by original username should fail,
    # 2nd check by hashed username should succeed.
    assert get_potentially_retired_user_by_username_and_hash(orig_username, hashed_username) == retirement_user