disnake

Форк
0
/
test_abc.py 
167 строк · 6.3 Кб
1
# SPDX-License-Identifier: MIT
2

3
from typing import cast
4
from unittest import mock
5

6
import pytest
7

8
import disnake
9
from disnake.abc import GuildChannel
10
from disnake.utils import MISSING
11

12

13
@pytest.mark.asyncio
14
class TestGuildChannelEdit:
15
    # TODO: use proper mock models once we have state/guild mocks
16
    @pytest.fixture
17
    def channel(self):
18
        ch = mock.Mock(GuildChannel, id=123, category_id=456)
19
        ch._state = mock.Mock(http=mock.AsyncMock())
20
        ch.guild = mock.Mock()
21

22
        parent = mock.Mock()
23
        parent._overwrites = [mock.Mock() for _ in range(3)]
24
        ch.guild.get_channel.return_value = parent
25

26
        return ch
27

28
    async def test_none(self, channel) -> None:
29
        res = await GuildChannel._edit(channel, reason="h")
30
        assert res is None
31

32
        channel._move.assert_not_called()
33
        channel._state.http.edit_channel.assert_not_called()
34

35
    async def test_all(self, channel) -> None:
36
        mock_role = mock.Mock(disnake.Role)
37
        mock_member = mock.Mock(disnake.Member)
38

39
        res = await GuildChannel._edit(
40
            channel,
41
            name="new name",
42
            topic="talk about things here",
43
            position=10,
44
            nsfw=True,
45
            sync_permissions=False,
46
            category=disnake.Object(321),
47
            slowmode_delay=8,
48
            default_thread_slowmode_delay=42,
49
            default_auto_archive_duration=disnake.ThreadArchiveDuration.hour,
50
            type=disnake.ChannelType.news,
51
            overwrites={
52
                mock_role: disnake.PermissionOverwrite(kick_members=False, send_messages=True),
53
                mock_member: disnake.PermissionOverwrite(ban_members=False),
54
            },
55
            bitrate=42000,
56
            user_limit=3,
57
            rtc_region="there",
58
            video_quality_mode=disnake.VideoQualityMode.full,
59
            flags=disnake.ChannelFlags(pinned=False, require_tag=True),
60
            available_tags=[disnake.ForumTag(name="tag", emoji="woo")],
61
            default_reaction=disnake.PartialEmoji(name="woo", id=9876),
62
            default_sort_order=disnake.ThreadSortOrder.creation_date,
63
            default_layout=disnake.ThreadLayout.gallery_view,
64
            reason="stuff",
65
        )
66
        assert res is channel._state.http.edit_channel.return_value
67

68
        channel._move.assert_awaited_once_with(
69
            10, parent_id=321, lock_permissions=False, reason="stuff"
70
        )
71
        channel._state.http.edit_channel.assert_awaited_once_with(
72
            channel.id,
73
            name="new name",
74
            topic="talk about things here",
75
            nsfw=True,
76
            rate_limit_per_user=8,
77
            default_thread_rate_limit_per_user=42,
78
            default_auto_archive_duration=60,
79
            type=5,
80
            permission_overwrites=[
81
                {"allow": "2048", "deny": "2", "id": mock_role.id, "type": 0},
82
                {"allow": "0", "deny": "4", "id": mock_member.id, "type": 1},
83
            ],
84
            bitrate=42000,
85
            user_limit=3,
86
            rtc_region="there",
87
            video_quality_mode=2,
88
            flags=16,
89
            available_tags=[
90
                {"name": "tag", "moderated": False, "emoji_name": "woo", "emoji_id": None}
91
            ],
92
            default_reaction_emoji={"emoji_name": None, "emoji_id": 9876},
93
            default_sort_order=1,
94
            default_forum_layout=2,
95
            reason="stuff",
96
        )
97

98
    async def test_move_only(self, channel) -> None:
99
        # test position and related parameters, shouldn't call `edit_channel`
100
        res = await GuildChannel._edit(
101
            channel, position=42, category=disnake.Object(999), sync_permissions=True, reason="h"
102
        )
103
        assert res is None
104

105
        channel._move.assert_awaited_once_with(42, parent_id=999, lock_permissions=True, reason="h")
106
        channel._state.http.edit_channel.assert_not_called()
107

108
    async def test_sync_permissions(self, channel) -> None:
109
        # test common behavior
110
        res = await GuildChannel._edit(channel, sync_permissions=True, reason="yes")
111
        assert res is not None
112

113
        channel.guild.get_channel.assert_called_once_with(channel.category_id)
114
        channel._move.assert_not_called()
115
        channel._state.http.edit_channel.assert_awaited_once_with(
116
            channel.id,
117
            permission_overwrites=[
118
                o._asdict() for o in channel.guild.get_channel.return_value._overwrites
119
            ],
120
            reason="yes",
121
        )
122

123
    async def test_sync_permissions__no_parent(self, channel) -> None:
124
        # moving channel out of category, `sync_permissions` should do nothing
125
        res = await GuildChannel._edit(channel, category=None, sync_permissions=True)
126
        assert res is not None
127

128
        channel._move.assert_not_called()
129
        channel._state.http.edit_channel.assert_awaited_once_with(
130
            channel.id,
131
            parent_id=None,
132
            reason=None,
133
        )
134

135
    async def test_sync_permissions__no_parent_cache(self, channel) -> None:
136
        # assume parent category doesn't exist in cache
137
        channel.guild.get_channel.return_value = None
138

139
        res = await GuildChannel._edit(channel, sync_permissions=True)
140
        assert res is None
141

142
        channel._move.assert_not_called()
143
        channel._state.http.edit_channel.assert_not_called()
144

145
    @pytest.mark.parametrize("sync_permissions", [MISSING, True])
146
    async def test_overwrites(self, channel, sync_permissions) -> None:
147
        # overwrites should override `sync_permissions` parameter
148
        res = await GuildChannel._edit(channel, sync_permissions=sync_permissions, overwrites={})
149
        assert res is not None
150

151
        channel._move.assert_not_called()
152
        channel._state.http.edit_channel.assert_awaited_once_with(
153
            channel.id, permission_overwrites=[], reason=None
154
        )
155

156

157
class TestUserProtocol:
158
    def _test_typing_assignable(self) -> None:
159
        def handle_abc_user(user: disnake.abc.User) -> None:
160
            ...
161

162
        # All of these should match the abc.User protocol and thus type-check correctly
163
        # (they could just inherit from the protocol to ensure correct implementation,
164
        # but we really only want structural (i.e. implicit) subtyping)
165
        handle_abc_user(cast(disnake.User, ...))
166
        handle_abc_user(cast(disnake.ClientUser, ...))
167
        handle_abc_user(cast(disnake.Member, ...))
168

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.