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

Fix: Duplicate projects in query #949

Merged
merged 4 commits into from
Nov 2, 2023
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
5 changes: 4 additions & 1 deletion terraso_backend/apps/project_management/graphql/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ def resolve_seen(self, info):
def get_queryset(cls, queryset, info):
# limit queries to membership lists of projects to which the user belongs
user_pk = getattr(info.context.user, "pk", None)
return queryset.filter(membership_list__memberships__user_id=user_pk)
return queryset.filter(
membership_list__memberships__user_id=user_pk,
membership_list__memberships__deleted_at__isnull=True,
)


class ProjectPrivacy(graphene.Enum):
Expand Down
24 changes: 24 additions & 0 deletions terraso_backend/tests/graphql/test_projects_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
# along with this program. If not, see https://www.gnu.org/licenses/.
import pytest
from graphene_django.utils.testing import graphql_query
from mixer.backend.django import mixer
from tests.utils import match_json

from apps.core.models import User
from apps.project_management.models.projects import Project

pytestmark = pytest.mark.django_db
Expand Down Expand Up @@ -61,3 +64,24 @@ def test_query_by_non_member(client, project):
assert "errors" not in payload
assert len(payload["data"]["projects"]["edges"]) == 0
assert payload["data"]["projects"]["totalCount"] == 0


def test_project_query_with_deleted_member(client, project):
user = mixer.blend(User)
project.add_manager(user)
project.remove_user(user)
project.add_viewer(user)
client.force_login(user)
payload = graphql_query(PROJECT_QUERY, client=client).json()
assert "errors" not in payload
assert len(match_json("data.projects.edges[*]", payload)) == 1


def test_project_query_with_deleted_member_not_in_project(client, project):
user = mixer.blend(User)
project.add_manager(user)
project.remove_user(user)
client.force_login(user)
payload = graphql_query(PROJECT_QUERY, client=client).json()
assert "errors" not in payload
assert len(match_json("data.projects.edges[*]", payload)) == 0