コード例 #1
0
    def test_clean_username(self):
        # A user with proto_user params does not exist yet.
        proto_user = UserFactory.build()

        form = UserCreationForm({
            "username": proto_user.username,
            "password1": proto_user._password,
            "password2": proto_user._password,
        })

        assert form.is_valid()
        assert form.clean_username() == proto_user.username

        # Creating a user.
        form.save()

        # The user with proto_user params already exists,
        # hence cannot be created.
        form = UserCreationForm({
            "username": proto_user.username,
            "password1": proto_user._password,
            "password2": proto_user._password,
        })

        assert not form.is_valid()
        assert len(form.errors) == 1
        assert "username" in form.errors
コード例 #2
0
ファイル: test_email.py プロジェクト: ToeKnee/chat-demo
    def test_welcome_email(self):
        user = UserFactory.build()
        send_welcome_email(user)

        self.assertEqual(len(mail.outbox), 1)
        self.assertIn(user.email, mail.outbox[0].recipients())
        self.assertIn(user.username, mail.outbox[0].body)
コード例 #3
0
ファイル: user_test.py プロジェクト: mitodl/mitxpro
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
コード例 #4
0
 def get_data(self):
     self.email = UserFactory.build().email
     return {
         u'attachment_set-0-DELETE': '',
         u'attachment_set-0-attachment': '',
         u'attachment_set-0-id': '',
         u'attachment_set-0-letter': '',
         u'attachment_set-1-DELETE': '',
         u'attachment_set-1-attachment': '',
         u'attachment_set-1-id': '',
         u'attachment_set-1-letter': '',
         u'attachment_set-2-DELETE': '',
         u'attachment_set-2-attachment': '',
         u'attachment_set-2-id': '',
         u'attachment_set-2-letter': '',
         u'attachment_set-INITIAL_FORMS': '0',
         u'attachment_set-MAX_NUM_FORMS': '1000',
         u'attachment_set-MIN_NUM_FORMS': '0',
         u'attachment_set-TOTAL_FORMS': '3',
         u'client-autocomplete': '',
         u'email': self.email,
         u'giodo': 'on',
         u'name': 'Lorem ipsum subject example',
         u'text': 'Lorem ipsum example text'
     }
コード例 #5
0
def test_create_user_reattempt(mocker):
    """
    Test that create_user_with_generated_username reattempts User creation multiple times when
    username collisions are experienced repeatedly
    """
    username = "******"
    fake_user = UserFactory.build()
    patched_find_username = mocker.patch(
        "authentication.api.find_available_username",
        side_effect=["testuser1", "testuser2", "testuser3"],
    )
    patched_save = mocker.patch.object(
        UserSerializer,
        "save",
        side_effect=[
            IntegrityError("(username)=(testuser) already exists"),
            IntegrityError("(username)=(testuser1) already exists"),
            IntegrityError("(username)=(testuser2) already exists"),
            fake_user,
        ],
    )

    created_user = api.create_user_with_generated_username(
        UserSerializer(data={}), username)
    assert patched_save.call_count == 4
    patched_save.assert_any_call(username="******")
    patched_save.assert_any_call(username="******")
    patched_save.assert_any_call(username="******")
    patched_save.assert_any_call(username="******")
    # `find_available_username` should be called as many times as serializer.save() failed
    # with a duplicate username error
    assert patched_find_username.call_count == 3
    patched_find_username.assert_called_with(username)
    assert created_user == fake_user
コード例 #6
0
ファイル: test_views.py プロジェクト: ToeKnee/chat-demo
    def test_register__user_already_logged_in(self):
        logged_in_user = UserFactory()  # Create, don't build
        new_user = UserFactory.build()  # Build, don't create

        data = {
            "username": new_user.username,
            "email": new_user.email,
            "password": "******",
        }
        request = self.factory.post("/api/user/", data)
        force_authenticate(request, logged_in_user)
        with mock.patch("users.api.views.send_welcome_email"
                        ) as mock_send_welcome_email:
            response = UserCreateAPIView.as_view()(request)
            response.render()
            self.assertEqual(response.status_code, 400)
            self.assertEqual(response.data, ["You have already signed up"])
            self.assertEqual(mock_send_welcome_email.call_count, 0)
コード例 #7
0
ファイル: test_views.py プロジェクト: ToeKnee/chat-demo
    def test_register__new_user(self):
        user = UserFactory.build()  # Build, don't create

        data = {
            "username": user.username,
            "email": user.email,
            "password": "******"
        }
        request = self.factory.post("/api/user/", data)
        with mock.patch("users.api.views.send_welcome_email"
                        ) as mock_send_welcome_email:
            response = UserCreateAPIView.as_view()(request)
            self.assertEqual(response.status_code, 201)
            self.assertEqual(mock_send_welcome_email.call_count, 1)
            self.assertTrue(get_user_model().objects.filter(
                username=user.username, email=user.email))
            test_user = get_user_model().objects.get(username=user.username)
            self.assertTrue(test_user.check_password(data["password"]))
コード例 #8
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"]
コード例 #9
0
ファイル: test_views.py プロジェクト: gitter-badger/poradnia
 def get_data(self):
     self.email = UserFactory.build().email
     return {u'attachment_set-0-DELETE': '',
             u'attachment_set-0-attachment': '',
             u'attachment_set-0-id': '',
             u'attachment_set-0-letter': '',
             u'attachment_set-1-DELETE': '',
             u'attachment_set-1-attachment': '',
             u'attachment_set-1-id': '',
             u'attachment_set-1-letter': '',
             u'attachment_set-2-DELETE': '',
             u'attachment_set-2-attachment': '',
             u'attachment_set-2-id': '',
             u'attachment_set-2-letter': '',
             u'attachment_set-INITIAL_FORMS': '0',
             u'attachment_set-MAX_NUM_FORMS': '1000',
             u'attachment_set-MIN_NUM_FORMS': '0',
             u'attachment_set-TOTAL_FORMS': '3',
             u'client-autocomplete': '',
             u'email': self.email,
             u'giodo': 'on',
             u'name': 'Lorem ipsum subject example',
             u'text': 'Lorem ipsum example text'}
コード例 #10
0
ファイル: test_email.py プロジェクト: ToeKnee/chat-demo
 def test_user_with_no_email_address(self):
     user = UserFactory.build(email=None)
     self.assertIsNone(send_welcome_email(user))
     self.assertEqual(len(mail.outbox), 0)
コード例 #11
0
ファイル: user_test.py プロジェクト: mitodl/mitxpro
):
    """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,
        )


@pytest.mark.django_db
@pytest.mark.parametrize(
    "create_user_return_val,create_user_exception",
    [[None, None], [UserFactory.build(), ValueError("bad value")]],
)
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):
コード例 #12
0
def test_format_recipient(name, email, expected):
    """format_recipient should return the expected string"""
    user = UserFactory.build(name=name, email=email)
    assert format_recipient(user) == expected
コード例 #13
0
                                                    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,
        )


@pytest.mark.django_db
@pytest.mark.parametrize(
    "create_user_return_val,create_user_exception",
    [[None, None], [UserFactory.build(),
                    ValueError("bad value")]],
)
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,
    )