Exemplo n.º 1
0
def test_create_user_via_email_with_shorter_name(mocker, mock_email_backend):
    """Tests that create_user_via_email raises an error if name field is shorter than 2 characters"""
    mock_strategy = mocker.Mock()
    mock_strategy.request_data.return_value = {
        "name": "a",
        "password": "******",
        "legal_address": {
            "first_name": "Jane",
            "last_name": "Doe",
            "street_address_1": "1 Main st",
            "city": "Boston",
            "state_or_territory": "US-MA",
            "country": "US",
            "postal_code": "02101",
        },
    }

    with pytest.raises(RequirePasswordAndPersonalInfoException) as exc:
        user_actions.create_user_via_email(
            mock_strategy,
            mock_email_backend,
            details=dict(email="*****@*****.**"),
            pipeline_index=0,
            flow=SocialAuthState.FLOW_REGISTER,
        )

    assert exc.value.errors == ["Full name must be at least 2 characters long."]
Exemplo n.º 2
0
def test_create_user_via_email_affiliate(
    mocker, mock_create_user_strategy, mock_email_backend
):
    """
    create_user_via_email passes an affiliate id into the user serializer if the affiliate code exists
    on the request object
    """
    affiliate = AffiliateFactory.create()
    mock_create_user_strategy.request.affiliate_code = affiliate.code
    patched_create_user = mocker.patch(
        "authentication.pipeline.user.create_user_with_generated_username",
        return_value=UserFactory.build(),
    )
    user_actions.create_user_via_email(
        mock_create_user_strategy,
        mock_email_backend,
        details=dict(email="*****@*****.**"),
        pipeline_index=0,
        flow=SocialAuthState.FLOW_REGISTER,
    )
    patched_create_user.assert_called_once()
    # Confirm that a UserSerializer object was passed to create_user_with_generated_username, and
    # that it was instantiated with the affiliate id in the context.
    serializer = patched_create_user.call_args_list[0][0][0]
    assert "affiliate_id" in serializer.context
    assert serializer.context["affiliate_id"] == affiliate.id
Exemplo n.º 3
0
def test_create_user_via_email_no_data(mocker, mock_email_backend):
    """Tests that create_user_via_email raises an error if no data for name and password provided"""
    mock_strategy = mocker.Mock()
    mock_strategy.request_data.return_value = {}
    with pytest.raises(RequirePasswordAndPersonalInfoException):
        user_actions.create_user_via_email(
            mock_strategy,
            mock_email_backend,
            pipeline_index=0,
            flow=SocialAuthState.FLOW_REGISTER,
        )
Exemplo n.º 4
0
def test_create_user_via_email_existing_user_raises(user, mock_email_backend,
                                                    mock_create_user_strategy):
    """Tests that create_user_via_email raises an error if a user already exists in the pipeline"""
    with pytest.raises(UnexpectedExistingUserException):
        user_actions.create_user_via_email(
            mock_create_user_strategy,
            mock_email_backend,
            user=user,
            pipeline_index=0,
            flow=SocialAuthState.FLOW_REGISTER,
        )
Exemplo n.º 5
0
def test_create_user_via_email_create_fail(
    mocker,
    mock_email_backend,
    mock_create_user_strategy,
    create_user_return_val,
    create_user_exception,
):
    """Tests that create_user_via_email raises an error if user creation fails"""
    patched_create_user = mocker.patch(
        "authentication.pipeline.user.create_user_with_generated_username",
        return_value=create_user_return_val,
        side_effect=create_user_exception,
    )
    with pytest.raises(UserCreationFailedException):
        user_actions.create_user_via_email(
            mock_create_user_strategy,
            mock_email_backend,
            details=dict(email="*****@*****.**"),
            pipeline_index=0,
            flow=SocialAuthState.FLOW_REGISTER,
        )
    patched_create_user.assert_called_once()
Exemplo n.º 6
0
def test_create_user_via_email_exit(mocker, backend_name, flow):
    """
    Tests that create_user_via_email returns if not using the email backend and attempting the
    'register' step of the auth flow
    """
    mock_strategy = mocker.Mock()
    mock_backend = mocker.Mock()
    mock_backend.name = backend_name
    assert (user_actions.create_user_via_email(mock_strategy,
                                               mock_backend,
                                               pipeline_index=0,
                                               flow=flow) == {})

    mock_strategy.request_data.assert_not_called()
Exemplo n.º 7
0
def test_create_user_via_email(mocker, mock_email_backend,
                               mock_create_user_strategy):
    """
    Tests that create_user_via_email creates a user via social_core.pipeline.user.create_user_via_email,
    generates a username, and sets a name and password
    """
    email = "*****@*****.**"
    generated_username = "******"
    fake_user = UserFactory.build(username=generated_username)
    patched_usernameify = mocker.patch(
        "authentication.pipeline.user.usernameify",
        return_value=generated_username)
    patched_create_user = mocker.patch(
        "authentication.pipeline.user.create_user_with_generated_username",
        return_value=fake_user,
    )

    response = user_actions.create_user_via_email(
        mock_create_user_strategy,
        mock_email_backend,
        details=dict(email=email),
        pipeline_index=0,
        flow=SocialAuthState.FLOW_REGISTER,
    )
    assert response == {
        "user": fake_user,
        "username": generated_username,
        "is_new": True,
    }
    request_data = mock_create_user_strategy.request_data()
    patched_usernameify.assert_called_once_with(request_data["name"],
                                                email=email)
    patched_create_user.assert_called_once()
    # Confirm that a UserSerializer object was passed to create_user_with_generated_username, and
    # that it was instantiated with the data we expect.
    serializer = patched_create_user.call_args_list[0][0][0]
    assert serializer.initial_data["username"] == generated_username
    assert serializer.initial_data["email"] == email
    assert serializer.initial_data["name"] == request_data["name"]
    assert serializer.initial_data["password"] == request_data["password"]