Skip to content

Commit

Permalink
test: Test user in project query returns deleted memberships
Browse files Browse the repository at this point in the history
  • Loading branch information
David Code Howard committed Oct 6, 2023
1 parent 4e5efa8 commit 3d2d852
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
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
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

0 comments on commit 3d2d852

Please sign in to comment.