Repository navigation
draft: add endpoints to create and modify users #38894
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
viadanna
wants to merge
4
commits into
openedx:master
Choose a base branch
from
open-craft:viadanna/create-user-endpoint
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+881
−11
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
openedx/core/djangoapps/user_api/tests/test_serializers.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| """Unit tests for the combined User/UserProfile serializer.""" | ||
|
|
||
| import pytest | ||
| from django.contrib.auth import get_user_model | ||
| from django.core.exceptions import ValidationError as DjangoValidationError | ||
| from django.test import TestCase | ||
|
|
||
| from common.djangoapps.student.models.user import UserProfile | ||
| from common.djangoapps.student.tests.factories import UserFactory | ||
|
|
||
| from ..serializers import UserProfileSerializer | ||
|
|
||
| User = get_user_model() | ||
|
|
||
|
|
||
| class UserProfileSerializerTest(TestCase): | ||
| """Tests for updates spanning the User and UserProfile models.""" | ||
|
|
||
| def test_create_user_and_profile_fields(self): | ||
| data = { | ||
| "username": "new-user", | ||
| "email": "new@example.com", | ||
| "password": "Password1234", | ||
| "name": "New User", | ||
| "year_of_birth": UserProfile.VALID_YEARS[-1], | ||
| "gender": "f", | ||
| "level_of_education": "b", | ||
| "country": "US", | ||
| } | ||
|
|
||
| serializer = UserProfileSerializer(data=data) | ||
| assert serializer.is_valid(), serializer.errors | ||
| user = serializer.save() | ||
|
|
||
| assert user.username == data["username"] | ||
| assert user.email == data["email"] | ||
| assert user.check_password(data["password"]) | ||
| assert user.profile.name == data["name"] | ||
| assert user.profile.year_of_birth == data["year_of_birth"] | ||
| assert user.profile.gender == data["gender"] | ||
| assert user.profile.level_of_education == data["level_of_education"] | ||
| assert user.profile.country == data["country"] | ||
|
|
||
| def test_create_user_with_no_profile_fields(self): | ||
| data = { | ||
| "username": "new-user", | ||
| "email": "new@example.com", | ||
| "password": "Password1234", | ||
| } | ||
|
|
||
| serializer = UserProfileSerializer(data=data) | ||
| assert serializer.is_valid(), serializer.errors | ||
| user = serializer.save() | ||
|
|
||
| assert user.username == data["username"] | ||
| assert user.email == data["email"] | ||
| assert user.check_password(data["password"]) | ||
| assert UserProfile.objects.filter(user=user).exists() | ||
| assert user.profile.name == "" | ||
| assert user.profile.year_of_birth is None | ||
| assert user.profile.gender is None | ||
| assert user.profile.level_of_education is None | ||
| assert user.profile.country is None | ||
|
|
||
| def test_update_user_and_profile_fields(self): | ||
| user = UserFactory.create(email="original@example.com", profile__name="Original Name") | ||
| data = { | ||
| "email": "updated@example.com", | ||
| "name": "Updated Name", | ||
| "password": "new-password", | ||
| "year_of_birth": UserProfile.VALID_YEARS[-1], | ||
| "gender": "f", | ||
| "level_of_education": "b", | ||
| "country": "US", | ||
| } | ||
|
|
||
| serializer = UserProfileSerializer(instance=user, data=data, partial=True) | ||
| assert serializer.is_valid(), serializer.errors | ||
| serializer.save() | ||
|
|
||
| user.refresh_from_db() | ||
| user.profile.refresh_from_db() | ||
|
|
||
| assert user.email == data["email"] | ||
| assert user.check_password(data["password"]) | ||
| assert user.profile.name == data["name"] | ||
| assert user.profile.year_of_birth == data["year_of_birth"] | ||
| assert user.profile.gender == data["gender"] | ||
| assert user.profile.level_of_education == data["level_of_education"] | ||
| assert user.profile.country == data["country"] | ||
|
|
||
| def test_blank_user_fields_are_rejected(self): | ||
| for field in ("email", "username", "password", "name"): | ||
| serializer = UserProfileSerializer(data={field: ""}, partial=True) | ||
| assert not serializer.is_valid() | ||
| assert field in serializer.errors | ||
|
|
||
| def test_whitespace_only_fields_are_rejected(self): | ||
| for field in ("email", "username", "name"): | ||
| serializer = UserProfileSerializer(data={field: " "}, partial=True) | ||
| assert not serializer.is_valid() | ||
| assert field in serializer.errors | ||
|
|
||
| def test_missing_required_fields_are_rejected(self): | ||
| for field, message in (("email", "Email is required."), ("username", "Username is required.")): | ||
| data = {"email": "new@example.com", "username": "new-user"} | ||
| data.pop(field) | ||
| serializer = UserProfileSerializer(data=data) | ||
| assert not serializer.is_valid(), serializer.errors | ||
| assert field in serializer.errors | ||
| assert message in serializer.errors[field] | ||
|
|
||
| def test_duplicate_email_and_username_are_rejected(self): | ||
| existing = UserFactory.create(username="existing-user", email="existing@example.com") | ||
|
|
||
| cases = ( | ||
| ("email", existing.email, "User already exists with this email"), | ||
| ("username", existing.username, "User already exists with this username"), | ||
| ) | ||
|
|
||
| for field, value, message in cases: | ||
| serializer = UserProfileSerializer( | ||
| data={"email": "new@example.com", "username": "new-user", field: value} | ||
| ) | ||
| assert not serializer.is_valid(), serializer.errors | ||
| assert field in serializer.errors | ||
| assert message in serializer.errors[field] | ||
|
|
||
| def test_weak_password_is_rejected(self): | ||
| serializer = UserProfileSerializer(data={ | ||
| "username": "new-user", | ||
| "email": "new@example.com", | ||
| "password": "weak", | ||
| }) | ||
| assert not serializer.is_valid() | ||
| assert "password" in serializer.errors, serializer.errors | ||
|
|
||
| def test_no_password_is_accepted(self): | ||
| serializer = UserProfileSerializer(data={ | ||
| "username": "new-user", | ||
| "email": "new@example.com", | ||
| }) | ||
| assert serializer.is_valid(), serializer.errors | ||
| user = serializer.save() | ||
| assert not user.has_usable_password() | ||
|
|
||
| def test_username_cannot_be_changed(self): | ||
| user = UserFactory.create(username="original-user", email="original@example.com") | ||
| with pytest.raises(DjangoValidationError, match=r"Username cannot be changed\."): | ||
| UserProfileSerializer().update(user, {"username": "new-user"}) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.