Skip to content

Commit

Permalink
Merge pull request #310 from openimis/feature/OM-182
Browse files Browse the repository at this point in the history
OM-182: Added user language change mutation.
  • Loading branch information
delcroip authored Jun 6, 2024
2 parents d89d01d + 53000cd commit 1fada6a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
3 changes: 3 additions & 0 deletions core/models/openimis_graphql_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class openIMISGraphQLTestCase(GraphQLTestCase):
GRAPHQL_URL = f"/{settings.SITE_ROOT()}graphql"
GRAPHQL_SCHEMA = True

class BaseTestContext:
def __init__(self, user):
self.user = user
# client = None
@classmethod
def setUpClass(cls):
Expand Down
36 changes: 35 additions & 1 deletion core/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1423,6 +1423,34 @@ def async_mutate(cls, user, **data):
return errors


class ChangeUserLanguageMutation(OpenIMISMutation):
"""
Update an existing User Language and do not change history data
"""
_mutation_module = "core"
_mutation_class = "ChangeUserLanguageMutation"

class Input(OpenIMISMutation.Input):
language_id = graphene.String(required=True)

@classmethod
def async_mutate(cls, user, **data):
try:
if type(user) is AnonymousUser or not user.id:
raise ValidationError("mutation.authentication_required")
data['audit_user_id'] = user.id_for_audit
change_user_language(user, language_id=data["language_id"])

return None
except Exception as exc:
return [
{
'message': "core.mutation.failed_to_update_user_language",
'detail': str(exc)
}]



@transaction.atomic
@validate_payload_for_obligatory_fields(CoreConfig.fields_controls_user, 'data')
def update_or_create_user(data, user):
Expand Down Expand Up @@ -1520,6 +1548,12 @@ def set_user_deleted(user):
}


def change_user_language(user, language_id):
updated_user = User.objects.get(id=user.id)
updated_user.i_user.language_id = language_id
updated_user.save()


class ChangePasswordMutation(graphene.relay.ClientIDMutation):
"""
Change a user's password. Either the user can update his own by providing the old password, or an administrator
Expand Down Expand Up @@ -1652,7 +1686,6 @@ def mutate(cls, root, info, **kwargs):
return super().mutate(cls, info, **kwargs)



class Mutation(graphene.ObjectType):
create_role = CreateRoleMutation.Field()
update_role = UpdateRoleMutation.Field()
Expand All @@ -1662,6 +1695,7 @@ class Mutation(graphene.ObjectType):
create_user = CreateUserMutation.Field()
update_user = UpdateUserMutation.Field()
delete_user = DeleteUserMutation.Field()
change_user_language = ChangeUserLanguageMutation.Field()

change_password = ChangePasswordMutation.Field()
reset_password = ResetPasswordMutation.Field()
Expand Down
26 changes: 25 additions & 1 deletion core/tests/test_graphql.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from core.models.openimis_graphql_test_case import openIMISGraphQLTestCase
from core.test_helpers import create_test_interactive_user
from graphql_jwt.shortcuts import get_token

import json

class gqlTest(openIMISGraphQLTestCase):
Expand All @@ -11,6 +13,7 @@ class gqlTest(openIMISGraphQLTestCase):
def setUpClass(cls):
super().setUpClass()
cls.admin_user = create_test_interactive_user(username=cls.admin_username, password=cls.admin_password)
cls.admin_token = get_token(cls.admin_user, cls.BaseTestContext(user=cls.admin_user))

def test_login_successful(self):
variables = {
Expand All @@ -34,7 +37,7 @@ def test_login_successful(self):
content = json.loads(response.content)


def test_login_deafult_successful(self):
def test_login_default_successful(self):
variables = {
"username": "Admin",
"password": "admin123"
Expand Down Expand Up @@ -74,4 +77,25 @@ def test_login_wrong_credentials(self):
variables=variables
)
self.assertResponseHasErrors(response)
content = json.loads(response.content)


def test_change_langue(self):
query = """
mutation {
changeUserLanguage(
input: {clientMutationId: "b2a639a9-1a85-4643-bf84-69d05160c8ee",
clientMutationLabel: "Change User Language",
languageId: "fr"}
) {
clientMutationId
internalId
}
}
"""
response = self.query(
query,
headers={"HTTP_AUTHORIZATION": f"Bearer {self.admin_token}"}
)
self.assertResponseNoErrors(response)
content = json.loads(response.content)

0 comments on commit 1fada6a

Please sign in to comment.