Skip to content

Commit

Permalink
Add endpoint for updating user language
Browse files Browse the repository at this point in the history
  • Loading branch information
matti-lamppu committed Oct 3, 2024
1 parent 67f1c90 commit c67851c
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 3 deletions.
1 change: 1 addition & 0 deletions tests/test_graphql_api/test_user/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

current_user_query = partial(build_query, "currentUser")

UPDATE_MUTATION = build_mutation("updateUser", "UserUpdateMutation")
STAFF_UPDATE_MUTATION = build_mutation("updateStaffUser", "UserStaffUpdateMutation")
43 changes: 43 additions & 0 deletions tests/test_graphql_api/test_user/test_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import pytest

from tests.factories import UserFactory
from tilavarauspalvelu.enums import Language

from .helpers import UPDATE_MUTATION

# Applied to all tests
pytestmark = [
pytest.mark.django_db,
]


def test_user__update__language(graphql):
user = UserFactory.create_superuser(preferred_language=Language.EN.value)

data = {
"pk": user.pk,
"preferredLanguage": Language.FI.value.upper(),
}

graphql.force_login(user)
response = graphql(UPDATE_MUTATION, input_data=data)

assert response.has_errors is False
assert response.first_query_object["pk"] == user.pk

user.refresh_from_db()
assert user.preferred_language == Language.FI.value


def test_user__update__language_not_available(graphql):
user = UserFactory.create_superuser(preferred_language=Language.EN.value)

data = {
"pk": user.pk,
"preferredLanguage": "UK",
}

graphql.force_login(user)
response = graphql(UPDATE_MUTATION, input_data=data)

assert response.has_schema_errors is True, response
67 changes: 67 additions & 0 deletions tests/test_graphql_api/test_user/test_update_permissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import pytest

from tests.factories import UserFactory
from tilavarauspalvelu.enums import Language

from .helpers import UPDATE_MUTATION

# Applied to all tests
pytestmark = [
pytest.mark.django_db,
]


def test_user__update__language__superuser(graphql):
user = UserFactory.create_superuser()

data = {
"pk": user.pk,
"preferredLanguage": Language.FI.value.upper(),
}

graphql.force_login(user)
response = graphql(UPDATE_MUTATION, input_data=data)

assert response.has_errors is False


def test_user__update__language__regular_user(graphql):
user = UserFactory.create()

data = {
"pk": user.pk,
"preferredLanguage": Language.FI.value.upper(),
}

graphql.force_login(user)
response = graphql(UPDATE_MUTATION, input_data=data)

assert response.has_errors is False


def test_user__update__language__other_user(graphql):
user = UserFactory.create_superuser()

data = {
"pk": user.pk,
"preferredLanguage": Language.FI.value.upper(),
}

graphql.login_with_regular_user()
response = graphql(UPDATE_MUTATION, input_data=data)

assert response.error_message() == "No permission to update."


def test_user__update__language__admin(graphql):
user = UserFactory.create_with_general_role()

data = {
"pk": user.pk,
"preferredLanguage": Language.FI.value.upper(),
}

graphql.force_login(user)
response = graphql(UPDATE_MUTATION, input_data=data)

assert response.has_errors is False
3 changes: 2 additions & 1 deletion tilavarauspalvelu/api/graphql/mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
from .types.resource.mutations import ResourceCreateMutation, ResourceDeleteMutation, ResourceUpdateMutation
from .types.space.mutations import SpaceCreateMutation, SpaceDeleteMutation, SpaceUpdateMutation
from .types.unit.mutations import UnitUpdateMutation
from .types.user.mutations import UserStaffUpdateMutation
from .types.user.mutations import UserStaffUpdateMutation, UserUpdateMutation

__all__ = [
"AllocatedTimeSlotCreateMutation",
Expand Down Expand Up @@ -135,4 +135,5 @@
"SpaceUpdateMutation",
"UnitUpdateMutation",
"UserStaffUpdateMutation",
"UserUpdateMutation",
]
2 changes: 2 additions & 0 deletions tilavarauspalvelu/api/graphql/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
SpaceUpdateMutation,
UnitUpdateMutation,
UserStaffUpdateMutation,
UserUpdateMutation,
)
from .queries import (
AgeGroupNode,
Expand Down Expand Up @@ -345,6 +346,7 @@ class Mutation(graphene.ObjectType):
refresh_order = RefreshOrderMutation.Field()
#
# User
update_user = UserUpdateMutation.Field()
update_staff_user = UserStaffUpdateMutation.Field()
#
# Misc.
Expand Down
11 changes: 9 additions & 2 deletions tilavarauspalvelu/api/graphql/types/user/mutations.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
from graphene_django_extensions import UpdateMutation

from .permissions import UserStaffPermission
from .serializers import UserStaffUpdateSerializer
from .permissions import UserPermission, UserStaffPermission
from .serializers import UserStaffUpdateSerializer, UserUpdateSerializer

__all__ = [
"UserStaffUpdateMutation",
"UserUpdateMutation",
]


class UserUpdateMutation(UpdateMutation):
class Meta:
serializer_class = UserUpdateSerializer
permission_classes = [UserPermission]


class UserStaffUpdateMutation(UpdateMutation):
class Meta:
serializer_class = UserStaffUpdateSerializer
Expand Down
10 changes: 10 additions & 0 deletions tilavarauspalvelu/api/graphql/types/user/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@

__all__ = [
"UserStaffUpdateSerializer",
"UserUpdateSerializer",
]


class UserUpdateSerializer(NestingModelSerializer):
class Meta:
model = User
fields = [
"pk",
"preferred_language",
]


class UserStaffUpdateSerializer(NestingModelSerializer):
class Meta:
model = User
Expand Down

0 comments on commit c67851c

Please sign in to comment.