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: Ignore soft-deleted memberships when querying if user belongs to project #874

Merged
merged 2 commits into from
Oct 12, 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
2 changes: 1 addition & 1 deletion terraso_backend/apps/collaboration/models/memberships.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def is_approved_member(self, user: User) -> bool:
return self.approved_members.filter(id=user.id).exists()

def is_member(self, user: User) -> bool:
return self.members.filter(id=user.id).exists()
return self.memberships.filter(user=user).exists()

def get_membership(self, user: User):
if not user:
Expand Down
10 changes: 7 additions & 3 deletions terraso_backend/apps/graphql/schema/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
import graphene
import rules
import structlog
from django_filters import FilterSet
from django_filters import CharFilter, FilterSet
from graphene import relay
from graphene_django import DjangoObjectType
from graphene_django.filter import TypedFilter

from apps.auth.services import JWTService
from apps.collaboration.models import Membership
from apps.core.models import User, UserPreference
from apps.core.models.users import NOTIFICATION_KEYS
from apps.graphql.exceptions import GraphQLNotAllowedException
Expand All @@ -38,7 +38,7 @@


class UserFilter(FilterSet):
project = TypedFilter(field_name="collaboration_memberships__membership_list__project")
project = CharFilter(method="filter_user_in_project")

class Meta:
model = User
Expand All @@ -48,6 +48,10 @@ class Meta:
"last_name": ["icontains"],
}

def filter_user_in_project(self, queryset, name, value):
memberships = Membership.objects.filter(membership_list__project=value)
return queryset.filter(collaboration_memberships__in=memberships)


class UserNode(DjangoObjectType):
id = graphene.ID(source="pk", required=True)
Expand Down
6 changes: 6 additions & 0 deletions terraso_backend/apps/project_management/models/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,11 @@ def get_membership(self, user: User):
def mark_seen_by(self, user: User):
self.seen_by.add(user)

def remove_user(self, user: User):
membership = self.get_membership(user)
if membership:
membership.delete()
self.save()

def __str__(self):
return self.name
24 changes: 23 additions & 1 deletion terraso_backend/tests/graphql/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
#
# You should have received a copy of the GNU Affero General Public License
# 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 apps.core.models import User

pytestmark = pytest.mark.django_db

Expand Down Expand Up @@ -84,6 +87,7 @@ def test_users_query_has_total_count(client_query, users):
id
}
}
totalCount
}
}
"""
Expand All @@ -98,3 +102,21 @@ def test_users_query_in_project(client_query, project, project_user):
contents = response.json()
assert "errors" not in contents
assert contents["data"]["users"]["edges"][0]["node"]["id"] == str(project_user.id)


def test_users_query_in_project_deleted_member(client, project, project_user):
user = mixer.blend(User)
project.add_viewer(user)
assert project.is_member(user)
project.remove_user(user)
assert not project.is_member(user)

client.force_login(project_user)
response = graphql_query(
USER_IN_PROJECT_QUERY,
client=client,
variables={"projectId": str(project.id), "email": user.email},
)
contents = response.json()
assert "errors" not in contents
assert contents["data"]["users"]["totalCount"] == 0