def test_recursive_queries_for_user_groups(self) -> None:
        realm = get_realm("zulip")
        iago = self.example_user("iago")
        desdemona = self.example_user("desdemona")
        shiva = self.example_user("shiva")

        leadership_group = UserGroup.objects.create(realm=realm,
                                                    name="Leadership")
        UserGroupMembership.objects.create(user_profile=desdemona,
                                           user_group=leadership_group)

        staff_group = UserGroup.objects.create(realm=realm, name="Staff")
        UserGroupMembership.objects.create(user_profile=iago,
                                           user_group=staff_group)
        GroupGroupMembership.objects.create(supergroup=staff_group,
                                            subgroup=leadership_group)

        everyone_group = UserGroup.objects.create(realm=realm, name="Everyone")
        UserGroupMembership.objects.create(user_profile=shiva,
                                           user_group=everyone_group)
        GroupGroupMembership.objects.create(supergroup=everyone_group,
                                            subgroup=staff_group)

        self.assertCountEqual(list(get_recursive_subgroups(leadership_group)),
                              [leadership_group])
        self.assertCountEqual(list(get_recursive_subgroups(staff_group)),
                              [leadership_group, staff_group])
        self.assertCountEqual(
            list(get_recursive_subgroups(everyone_group)),
            [leadership_group, staff_group, everyone_group],
        )

        self.assertCountEqual(
            list(get_recursive_group_members(leadership_group)), [desdemona])
        self.assertCountEqual(list(get_recursive_group_members(staff_group)),
                              [desdemona, iago])
        self.assertCountEqual(
            list(get_recursive_group_members(everyone_group)),
            [desdemona, iago, shiva])

        self.assertIn(leadership_group,
                      list(get_recursive_membership_groups(desdemona)))
        self.assertIn(staff_group,
                      list(get_recursive_membership_groups(desdemona)))
        self.assertIn(everyone_group,
                      list(get_recursive_membership_groups(desdemona)))

        self.assertIn(staff_group, list(get_recursive_membership_groups(iago)))
        self.assertIn(everyone_group,
                      list(get_recursive_membership_groups(iago)))

        self.assertIn(everyone_group,
                      list(get_recursive_membership_groups(shiva)))
    def test_subgroups_of_role_based_system_groups(self) -> None:
        realm = get_realm("zulip")
        owners_group = UserGroup.objects.get(realm=realm,
                                             name="@role:owners",
                                             is_system_group=True)
        admins_group = UserGroup.objects.get(realm=realm,
                                             name="@role:administrators",
                                             is_system_group=True)
        moderators_group = UserGroup.objects.get(realm=realm,
                                                 name="@role:moderators",
                                                 is_system_group=True)
        full_members_group = UserGroup.objects.get(realm=realm,
                                                   name="@role:fullmembers",
                                                   is_system_group=True)
        members_group = UserGroup.objects.get(realm=realm,
                                              name="@role:members",
                                              is_system_group=True)
        everyone_group = UserGroup.objects.get(realm=realm,
                                               name="@role:everyone",
                                               is_system_group=True)
        everyone_on_internet_group = UserGroup.objects.get(
            realm=realm, name="@role:internet", is_system_group=True)

        self.assertCountEqual(list(get_recursive_subgroups(owners_group)),
                              [owners_group])
        self.assertCountEqual(list(get_recursive_subgroups(admins_group)),
                              [owners_group, admins_group])
        self.assertCountEqual(
            list(get_recursive_subgroups(moderators_group)),
            [owners_group, admins_group, moderators_group],
        )
        self.assertCountEqual(
            list(get_recursive_subgroups(full_members_group)),
            [owners_group, admins_group, moderators_group, full_members_group],
        )
        self.assertCountEqual(
            list(get_recursive_subgroups(members_group)),
            [
                owners_group, admins_group, moderators_group,
                full_members_group, members_group
            ],
        )
        self.assertCountEqual(
            list(get_recursive_subgroups(everyone_group)),
            [
                owners_group,
                admins_group,
                moderators_group,
                full_members_group,
                members_group,
                everyone_group,
            ],
        )
        self.assertCountEqual(
            list(get_recursive_subgroups(everyone_on_internet_group)),
            [
                owners_group,
                admins_group,
                moderators_group,
                full_members_group,
                members_group,
                everyone_group,
                everyone_on_internet_group,
            ],
        )