Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
seanh committed Nov 14, 2024
1 parent 68c7e25 commit 32f1a93
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions tests/unit/h/traversal/group_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest

from h.exceptions import InvalidUserId
from h.models import GroupMembership, GroupMembershipRoles
from h.traversal.group import (
GroupMembershipContext,
Expand Down Expand Up @@ -58,36 +59,59 @@ def test_it(
group_service.fetch.return_value, user_service.fetch.return_value
)
assert isinstance(context, GroupMembershipContext)
assert context.group == group_service.fetch.return_value
assert context.user == user_service.fetch.return_value
assert context.membership == group_members_service.get.return_value

def test_when_no_matching_group(
self, group_service, group_members_service, pyramid_request
self, group_service, user_service, group_members_service, pyramid_request
):
group_service.fetch.return_value = None

context = group_membership_api_factory(pyramid_request)

group_members_service.get.assert_not_called()
assert isinstance(context, GroupMembershipContext)
assert context.user == user_service.fetch.return_value
assert context.group is None
assert context.membership is None

def test_when_no_matching_user(
self, user_service, group_members_service, pyramid_request
self, group_service, user_service, group_members_service, pyramid_request
):
user_service.fetch.return_value = None

context = group_membership_api_factory(pyramid_request)

group_members_service.get.assert_not_called()
assert isinstance(context, GroupMembershipContext)
assert context.user is None
assert context.group == group_service.fetch.return_value
assert context.membership is None

def test_when_no_matching_membership(self, group_members_service, pyramid_request):
def test_when_invalid_userid(
self, group_service, user_service, group_members_service, pyramid_request
):
user_service.fetch.side_effect = InvalidUserId(sentinel.userid)

context = group_membership_api_factory(pyramid_request)

group_members_service.get.assert_not_called()
assert isinstance(context, GroupMembershipContext)
assert context.user is None
assert context.group == group_service.fetch.return_value
assert context.membership is None

def test_when_no_matching_membership(
self, group_service, user_service, group_members_service, pyramid_request
):
group_members_service.get.return_value = None

context = group_membership_api_factory(pyramid_request)

assert isinstance(context, GroupMembershipContext)
assert context.group == group_service.fetch.return_value
assert context.user == user_service.fetch.return_value
assert context.membership is None

def test_me_alias(self, pyramid_config, pyramid_request, user_service):
Expand All @@ -99,7 +123,7 @@ def test_me_alias(self, pyramid_config, pyramid_request, user_service):
user_service.fetch.assert_called_once_with(sentinel.userid)

def test_me_alias_when_not_authenticated(
self, pyramid_request, group_members_service, user_service
self, pyramid_request, group_members_service, user_service, group_service
):
pyramid_request.matchdict["userid"] = "me"

Expand All @@ -108,6 +132,8 @@ def test_me_alias_when_not_authenticated(
user_service.fetch.assert_not_called()
group_members_service.get.assert_not_called()
assert isinstance(context, GroupMembershipContext)
assert context.group == group_service.fetch.return_value
assert context.user is None
assert context.membership is None

@pytest.fixture
Expand All @@ -119,13 +145,13 @@ def pyramid_request(self, pyramid_request):

class TestGroupMembershipContext:
def test_roles(self, factories):
group = factories.Group.build()
user = factories.User.build()
membership = GroupMembership(
group=factories.Group.build(),
user=factories.User.build(),
roles=[GroupMembershipRoles.MEMBER],
group=group, user=user, roles=[GroupMembershipRoles.MEMBER]
)

context = GroupMembershipContext(membership)
context = GroupMembershipContext(group, user, membership)

assert context.roles == membership.roles

Expand Down

0 comments on commit 32f1a93

Please sign in to comment.