Ejemplo n.º 1
0
 def create_user(self, *args, **kwargs):
     """Creates the user during the pipeline execution"""
     # this is normally delegated to the storage mechanism,
     # specifically social_django.storage.DjangoUserMixin.create_user
     # but we want to call our own method to create the user so we override at the strategy level
     username = kwargs.pop("username")
     email = kwargs.pop("email")
     return auth_api.create_user(username, email, user_extra=kwargs)
Ejemplo n.º 2
0
def test_create_user_errors(mocker, mock_method):
    """Test that we don't end up in a partial state if there are errors"""
    mocker.patch(mock_method, side_effect=Exception("error"))
    auth_token_mock = mocker.patch("channels.api.get_or_create_auth_tokens")

    with pytest.raises(Exception):
        api.create_user(
            "username",
            "email@localhost",
            {
                "name": "My Name",
                "image": "http://localhost/image.jpg"
            },
        )

    assert User.objects.all().count() == 0
    assert Profile.objects.count() == 0
    auth_token_mock.assert_not_called()
Ejemplo n.º 3
0
    def create(self, validated_data):
        profile_data = validated_data.pop("profile") or {}
        username = ulid.new()
        email = validated_data.get("email")
        uid = validated_data.get("uid", None)

        with transaction.atomic():
            user = auth_api.create_user(username, email, profile_data)

            if uid:
                auth_api.create_or_update_micromasters_social_auth(
                    user, uid, {"email": email})

        return user
Ejemplo n.º 4
0
def test_create_user_token_error(mocker):
    """Test that an error creating a token fails the user creation"""
    auth_token_mock = mocker.patch("channels.api.get_or_create_auth_tokens",
                                   side_effect=Exception("error"))

    with pytest.raises(Exception):
        assert (api.create_user(
            "username",
            "email@localhost",
            {
                "name": "My Name",
                "image": "http://localhost/image.jpg"
            },
        ) is not None)

    assert User.objects.all().count() == 0
    assert Profile.objects.count() == 0
    assert NotificationSettings.objects.count() == 0
    auth_token_mock.assert_called_once()
Ejemplo n.º 5
0
def test_create_user(mocker, profile_data):
    """Tests that a user and associated objects are created"""
    auth_token_mock = mocker.patch("channels.api.get_or_create_auth_tokens")
    email = "email@localhost"
    username = "******"
    user = api.create_user(username, email, profile_data,
                           {"first_name": "Bob"})

    assert isinstance(user, User)
    assert user.email == email
    assert user.username == username
    assert user.first_name == "Bob"
    assert NotificationSettings.objects.count() == 2

    auth_token_mock.assert_called_once()

    if "name" in profile_data:
        assert user.profile.name == profile_data["name"]
    else:
        assert user.profile.name is None