Merge branch 'add-account-endpoints' into 'develop'

Add user account endpoints

See merge request bramw/baserow!356
This commit is contained in:
Nigel Gott committed 2021-09-09 08:59:20 +00:00
commit ded94de6f8
7 files changed
+168 -1

No files matched your search

+21 -1
View File
@@ -15,7 +15,13 @@ User = get_user_model()
class UserSerializer(serializers.ModelSerializer):
language = serializers.CharField(
source="profile.language", required=False, validators=[language_validation]
source="profile.language",
required=False,
min_length=2,
max_length=10,
validators=[language_validation],
help_text="An ISO 639 language code (with optional variant) "
"selected by the user. Ex: en-GB.",
)
class Meta:
@@ -37,6 +43,7 @@ class RegisterSerializer(serializers.Serializer):
language = serializers.CharField(
required=False,
default=settings.LANGUAGE_CODE,
min_length=2,
max_length=10,
validators=[language_validation],
help_text="An ISO 639 language code (with optional variant) "
@@ -63,6 +70,19 @@ class RegisterSerializer(serializers.Serializer):
)
class AccountSerializer(serializers.Serializer):
first_name = serializers.CharField(min_length=1, max_length=32)
language = serializers.CharField(
source="profile.language",
required=False,
min_length=2,
max_length=10,
validators=[language_validation],
help_text="An ISO 639 language code (with optional variant) "
"selected by the user. Ex: en-GB.",
)
class SendResetPasswordEmailBodyValidationSerializer(serializers.Serializer):
email = serializers.EmailField(
help_text="The email address of the user that has requested a password reset."
+2
View File
@@ -1,6 +1,7 @@
from django.conf.urls import url
from .views import (
AccountView,
UserView,
SendResetPasswordEmailView,
ResetPasswordView,
@@ -15,6 +16,7 @@ from .views import (
app_name = "baserow.api.user"
urlpatterns = [
url(r"^account/$", AccountView.as_view(), name="account"),
url(r"^token-auth/$", ObtainJSONWebToken.as_view(), name="token_auth"),
url(r"^token-refresh/$", RefreshJSONWebToken.as_view(), name="token_refresh"),
url(r"^token-verify/$", VerifyJSONWebToken.as_view(), name="token_verify"),
+49
View File
@@ -41,6 +41,7 @@ from baserow.core.user.exceptions import (
)
from .serializers import (
AccountSerializer,
RegisterSerializer,
UserSerializer,
SendResetPasswordEmailBodyValidationSerializer,
@@ -325,6 +326,54 @@ class ChangePasswordView(APIView):
return Response("", status=204)
class AccountView(APIView):
permission_classes = (IsAuthenticated,)
@extend_schema(
tags=["User"],
operation_id="get_account",
description=(
"Responds with account information of the authenticated user. "
"For example the chosen `language` and `name` are included "
"in the response."
),
responses={
200: AccountSerializer,
},
)
@transaction.atomic
def get(self, request):
"""Returns user account."""
account_serializer = AccountSerializer(request.user)
return Response(account_serializer.data)
@extend_schema(
tags=["User"],
request=AccountSerializer,
operation_id="update_account",
description=("Updates the account information of the authenticated user."),
responses={
200: AccountSerializer,
400: get_error_schema(
[
"ERROR_REQUEST_BODY_VALIDATION",
]
),
},
)
@transaction.atomic
@validate_body(AccountSerializer)
def patch(self, request, data):
"""Update editable user account information."""
user = UserHandler().update_user(
request.user,
**data,
)
return Response(AccountSerializer(user).data)
class DashboardView(APIView):
permission_classes = (IsAuthenticated,)
+22
View File
@@ -160,6 +160,28 @@ class UserHandler:
return user
def update_user(self, user, first_name=None, language=None):
"""
Update user modifiable properties
:param user: The user instance to update.
:type user: User
:param language: The language selected by the user.
:type language: str
:return: The user object.
:rtype: User
"""
if first_name is not None:
user.first_name = first_name
user.save()
if language is not None:
user.profile.language = language
user.profile.save()
return user
def get_reset_password_signer(self):
"""
Instantiates the password reset serializer that can dump and load values.
@@ -148,6 +148,59 @@ def test_create_user(client, data_fixture):
)
@pytest.mark.django_db
def test_user_account(data_fixture, api_client):
user, token = data_fixture.create_user_and_token(
email="test@localhost.nl", language="en", first_name="Nikolas"
)
response = api_client.get(
reverse("api:user:account"),
HTTP_AUTHORIZATION=f"JWT {token}",
)
response_json = response.json()
assert response.status_code == HTTP_200_OK
assert response_json["first_name"] == "Nikolas"
assert response_json["language"] == "en"
response = api_client.patch(
reverse("api:user:account"),
{
"first_name": "NewOriginalName",
"language": "fr",
},
format="json",
HTTP_AUTHORIZATION=f"JWT {token}",
)
response_json = response.json()
assert response.status_code == HTTP_200_OK
assert response_json["first_name"] == "NewOriginalName"
assert response_json["language"] == "fr"
user.refresh_from_db()
assert user.first_name == "NewOriginalName"
assert user.profile.language == "fr"
response = api_client.patch(
reverse("api:user:account"),
{
"language": "invalid",
},
format="json",
HTTP_AUTHORIZATION=f"JWT {token}",
)
response_json = response.json()
assert response.status_code == 400
assert response_json["error"] == "ERROR_REQUEST_BODY_VALIDATION"
assert response_json["detail"]["language"][0]["code"] == "invalid_language"
assert response_json["detail"]["language"][0]["error"] == (
"Only the following language keys are "
f"valid: {','.join([l[0] for l in settings.LANGUAGES])}"
)
@pytest.mark.django_db
def test_create_user_with_invitation(data_fixture, client):
core_handler = CoreHandler()
@@ -137,6 +137,26 @@ def test_create_user(data_fixture):
user_handler.create_user("Test1", "test@test.nl", valid_password)
@pytest.mark.django_db
def test_update_user(data_fixture):
user_handler = UserHandler()
user = data_fixture.create_user(first_name="Initial", language="fr")
user_handler.update_user(user, first_name="Updated")
user.refresh_from_db()
user.profile.refresh_from_db()
assert user.first_name == "Updated"
assert user.profile.language == "fr"
user_handler.update_user(user, language="en")
user.refresh_from_db()
user.profile.refresh_from_db()
assert user.first_name == "Updated"
assert user.profile.language == "en"
@pytest.mark.django_db
@pytest.mark.parametrize("invalid_password", invalid_passwords)
def test_create_user_invalid_password(data_fixture, invalid_password):
+1
View File
@@ -2,6 +2,7 @@
## Unreleased
* Introduced new endpoint to get and update user account information.
* Fixed bug where a user could not be edited in the admin interface without providing
a password.
* Fixed bug where sometimes fields would not be ordered correctly in view exports.