Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tech: correction du comportement de member_required #5267

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions itou/common_apps/organizations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.conf import settings
from django.db import models
from django.db.models import Prefetch, Q
from django.db.models import Exists, OuterRef, Prefetch, Q
from django.forms import ValidationError
from django.utils import timezone

Expand All @@ -16,7 +16,16 @@ class OrganizationQuerySet(models.QuerySet):
"""

def member_required(self, user):
return self.filter(members=user, members__is_active=True)
membership_model = self.model.members.through
# through_fields contains a tuple like ("company", "user")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Je vois que l’ordre est documenté, peut-être le préciser. 🤷

structure_field, _user_field = self.model.members.rel.through_fields
return self.filter(
Exists(
membership_model.objects.filter(
user=user, is_active=True, user__is_active=True, **{structure_field: OuterRef("pk")}
)
)
)

def prefetch_active_memberships(self):
membership_model = self.model.members.through
Expand Down
14 changes: 14 additions & 0 deletions tests/companies/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,20 @@ def test_add_or_activate_member(self):


class TestCompanyQuerySet:
def test_member_required(self):
company = CompanyFactory()
user = EmployerFactory()
assert Company.objects.member_required(user).count() == 0

company.add_or_activate_member(user)
assert Company.objects.member_required(user).get() == company

membership = company.memberships.get()
membership.is_active = False
membership.save(update_fields=("is_active",))

assert Company.objects.member_required(user).count() == 0

def test_with_count_recent_received_job_applications(self):
company = CompanyFactory()
model = JobApplicationFactory._meta.model
Expand Down
16 changes: 16 additions & 0 deletions tests/institutions/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ def test_add_or_activate_member(self):
institution.add_or_activate_member(wrong_kind_user)


class TestInstitutionQuerySet:
def test_member_required(self):
institution = InstitutionFactory()
user = LaborInspectorFactory()
assert Institution.objects.member_required(user).count() == 0

institution.add_or_activate_member(user)
assert Institution.objects.member_required(user).get() == institution

membership = institution.memberships.get()
membership.is_active = False
membership.save(update_fields=("is_active",))

assert Institution.objects.member_required(user).count() == 0


def test_deactivate_last_admin(admin_client, mailoutbox):
institution = InstitutionWithMembershipFactory(department="")
membership = institution.memberships.first()
Expand Down
16 changes: 16 additions & 0 deletions tests/prescribers/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,22 @@ def test_update_prescriber_with_api_entreprise(self, settings):
assert organization.is_head_office is True


class TestPrescriberOrganizationQuerySet:
def test_member_required(self):
organization = PrescriberOrganizationFactory()
user = PrescriberFactory()
assert PrescriberOrganization.objects.member_required(user).count() == 0

organization.add_or_activate_member(user)
assert PrescriberOrganization.objects.member_required(user).get() == organization

membership = organization.memberships.get()
membership.is_active = False
membership.save(update_fields=("is_active",))

assert PrescriberOrganization.objects.member_required(user).count() == 0


class TestPrescriberOrganizationAdmin:
ACCEPT_BUTTON_LABEL = "Valider l'habilitation"
REFUSE_BUTTON_LABEL = "Refuser l'habilitation"
Expand Down
13 changes: 9 additions & 4 deletions tests/www/apply/__snapshots__/test_submit.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,16 @@
"companies_company"."is_searchable",
"companies_company"."rdv_solidarites_id"
FROM "companies_company"
INNER JOIN "companies_companymembership" ON ("companies_company"."id" = "companies_companymembership"."company_id")
INNER JOIN "users_user" ON ("companies_companymembership"."user_id" = "users_user"."id")
WHERE (NOT ("companies_company"."siret" = %s)
AND "companies_companymembership"."user_id" = %s
AND "users_user"."is_active"
AND EXISTS
(SELECT %s AS "a"
FROM "companies_companymembership" U0
INNER JOIN "users_user" U2 ON (U0."user_id" = U2."id")
WHERE (U0."company_id" = ("companies_company"."id")
AND U0."is_active"
AND U0."user_id" = %s
AND U2."is_active")
LIMIT 1)
AND "companies_company"."id" = %s)
LIMIT 21
''',
Expand Down
Loading