Skip to content

Commit

Permalink
fix: login without profile
Browse files Browse the repository at this point in the history
[IA-3797] Fixed error login without profile
  • Loading branch information
tdethier authored Dec 17, 2024
2 parents 54cc636 + a3db856 commit 8896049
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion iaso/api/profiles/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions iaso/tests/api/test_profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)"""

Expand Down

0 comments on commit 8896049

Please sign in to comment.