Exemplo n.º 1
0
    def create_retirement(cls, user):
        """
        Creates a UserRetirementStatus for the given user, in the correct initial state. Will
        fail if the user already has a UserRetirementStatus row or if states are not yet populated.
        """
        try:
            pending = RetirementState.objects.all().order_by('state_execution_order')[0]
        except IndexError:
            raise RetirementStateError('Default state does not exist! Populate retirement states to retire users.')  # lint-amnesty, pylint: disable=raise-missing-from

        if cls.objects.filter(user=user).exists():
            raise RetirementStateError(u'User {} already has a retirement status row!'.format(user))

        retired_username = get_retired_username_by_username(user.username)
        retired_email = get_retired_email_by_email(user.email)

        UserRetirementRequest.create_retirement_request(user)

        return cls.objects.create(
            user=user,
            original_username=user.username,
            original_email=user.email,
            original_name=user.profile.name,
            retired_username=retired_username,
            retired_email=retired_email,
            current_state=pending,
            last_state=pending,
            responses=u'Created in state {} by create_retirement'.format(pending)
        )
Exemplo n.º 2
0
def test_get_retired_username(retirement_user):
    """
    Basic testing of getting retired usernames. The hasher is opaque
    to us, we just care that it's succeeding and using our format.
    """
    hashed_username = get_retired_username_by_username(
        retirement_user.username)
    check_username_against_fmt(hashed_username)
Exemplo n.º 3
0
def test_get_retired_username(retirement_user):  # lint-amnesty, pylint: disable=redefined-outer-name
    """
    Basic testing of getting retired usernames. The hasher is opaque
    to us, we just care that it's succeeding and using our format.
    """
    hashed_username = get_retired_username_by_username(
        retirement_user.username)
    check_username_against_fmt(hashed_username)
Exemplo n.º 4
0
def test_get_potentially_retired_user_username_match(retirement_user):  # lint-amnesty, pylint: disable=redefined-outer-name
    """
    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  # lint-amnesty, pylint: disable=line-too-long
Exemplo n.º 5
0
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
Exemplo n.º 6
0
def test_get_retired_username_status_exists(retirement_user,
                                            retirement_status):  # pylint: disable=redefined-outer-name
    """
    Checks that a retired username is gotten from a UserRetirementStatus
    object when one already exists for a user.
    """
    hashed_username = get_retired_username_by_username(
        retirement_user.username)
    check_username_against_fmt(hashed_username)
    assert retirement_status.retired_username == hashed_username
Exemplo n.º 7
0
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)
Exemplo n.º 8
0
def _fake_logged_out_user(user, retire_username=False):
    """
    Simulate the initial logout retirement endpoint.
    """
    # By default, do not change the username to the retired hash version because
    # that is not what happens upon actual retirement requests immediately after
    # logout.
    if retire_username:
        user.username = get_retired_username_by_username(user.username)
    user.email = get_retired_email_by_email(user.email)
    user.set_unusable_password()
    user.save()
Exemplo n.º 9
0
def test_is_username_retired_is_retired(retirement_user):
    """
    Check functionality of is_username_retired when username is retired
    """
    original_username = retirement_user.username
    retired_username = get_retired_username_by_username(
        retirement_user.username)

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

    assert is_username_retired(original_username)
Exemplo n.º 10
0
def test_is_username_retired_is_retired(retirement_user):  # lint-amnesty, pylint: disable=redefined-outer-name
    """
    Check functionality of is_username_retired when username is retired
    """
    original_username = retirement_user.username
    retired_username = get_retired_username_by_username(
        retirement_user.username)

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

    assert is_username_retired(original_username)
Exemplo n.º 11
0
    def test_retired_username(self):
        """
        Ensure that a retired username cannot be registered again.
        """
        user = UserFactory()
        orig_username = user.username

        # Fake retirement of the username.
        user.username = get_retired_username_by_username(orig_username)
        user.save()

        # Attempt to create another account with the same username that's been retired.
        self.url_params['username'] = orig_username
        response = self.client.post(self.url, self.url_params)
        self._validate_exiting_username_response(orig_username, response, self.INVALID_ERR_MSG[0], self.INVALID_ERR_MSG[1])  # lint-amnesty, pylint: disable=line-too-long
Exemplo n.º 12
0
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
Exemplo n.º 13
0
def _assert_retirementstatus_is_user(retirement, user):
    """
    Helper function to compare a newly created UserRetirementStatus object to expected values for
    the given user.
    """
    pending = RetirementState.objects.all().order_by(
        'state_execution_order')[0]
    retired_username = get_retired_username_by_username(user.username)
    retired_email = get_retired_email_by_email(user.email)

    assert retirement.user == user
    assert retirement.original_username == user.username
    assert retirement.original_email == user.email
    assert retirement.original_name == user.profile.name
    assert retirement.retired_username == retired_username
    assert retirement.retired_email == retired_email
    assert retirement.current_state == pending
    assert retirement.last_state == pending
    assert pending.state_name in retirement.responses