From a3db856bbd59771917c953a2e6693c85c0e3d4e9 Mon Sep 17 00:00:00 2001 From: Thibault Dethier Date: Tue, 17 Dec 2024 16:00:31 +0100 Subject: [PATCH] Fixed error profile/me without profile --- iaso/api/profiles/profiles.py | 2 +- iaso/tests/api/test_profiles.py | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/iaso/api/profiles/profiles.py b/iaso/api/profiles/profiles.py index 17e8ecd1e5..1a0e6e9561 100644 --- a/iaso/api/profiles/profiles.py +++ b/iaso/api/profiles/profiles.py @@ -232,8 +232,8 @@ def retrieve(self, request, *args, **kwargs): account_user = request.user.tenant_users.first().account_user account_user.backend = "django.contrib.auth.backends.ModelBackend" login(request, account_user) - queryset = self.get_queryset() try: + queryset = self.get_queryset() profile = queryset.get(user=request.user) profile_dict = profile.as_dict() return Response(profile_dict) diff --git a/iaso/tests/api/test_profiles.py b/iaso/tests/api/test_profiles.py index d7d38a7d51..45e0eebb1e 100644 --- a/iaso/tests/api/test_profiles.py +++ b/iaso/tests/api/test_profiles.py @@ -3,11 +3,13 @@ import jsonschema import numpy as np import pandas as pd +from django.contrib.auth import get_user_model from django.contrib.auth.models import Group, Permission from django.core import mail from django.test import override_settings from django.utils.translation import gettext as _ +from rest_framework import status from hat.menupermissions import models as permission from hat.menupermissions.constants import MODULES @@ -274,6 +276,28 @@ def test_profile_me_ok(self): self.assertHasField(response_data, "is_superuser", bool) self.assertHasField(response_data, "org_units", list) + def test_profile_me_no_profile(self): + """GET /profiles/me/ with auth, but without profile + The goal is to know that this call doesn't result in a 500 error + """ + User = get_user_model() + username = "I don't have a profile, i'm sad :(" + user_without_profile = User.objects.create(username=username) + self.client.force_authenticate(user_without_profile) + response = self.client.get("/api/profiles/me/") + self.assertJSONResponse(response, status.HTTP_200_OK) + + response_data = response.json() + self.assertEqual(response_data["user_name"], username) + self.assertEqual(response_data["first_name"], "") + self.assertEqual(response_data["last_name"], "") + self.assertEqual(response_data["user_id"], user_without_profile.id) + self.assertEqual(response_data["email"], "") + self.assertEqual(response_data["projects"], []) + self.assertFalse(response_data["is_staff"]) + self.assertFalse(response_data["is_superuser"]) + self.assertIsNone(response_data["account"]) + def test_profile_me_superuser_ok(self): """GET /profiles/me/ with auth (superuser)"""