|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
| 3 | +import asyncio |
| 4 | + |
3 | 5 | import pytest
|
4 | 6 | from django.http import HttpRequest
|
5 | 7 | from django.http import JsonResponse
|
| 8 | +from gidgethub import sansio |
6 | 9 |
|
| 10 | +from django_github_app.commands import CommandScope |
7 | 11 | from django_github_app.github import SyncGitHubAPI
|
8 | 12 | from django_github_app.routing import GitHubRouter
|
9 | 13 | from django_github_app.views import BaseWebhookView
|
@@ -109,3 +113,174 @@ def test_router_memory_stress_test_legacy(self):
|
109 | 113 |
|
110 | 114 | assert len(views) == view_count
|
111 | 115 | assert not all(view.router is view1_router for view in views)
|
| 116 | + |
| 117 | + |
| 118 | +class TestMentionDecorator: |
| 119 | + def test_basic_mention_no_command(self, test_router): |
| 120 | + handler_called = False |
| 121 | + handler_args = None |
| 122 | + |
| 123 | + @test_router.mention() |
| 124 | + def handle_mention(event, *args, **kwargs): |
| 125 | + nonlocal handler_called, handler_args |
| 126 | + handler_called = True |
| 127 | + handler_args = (event, args, kwargs) |
| 128 | + |
| 129 | + event = sansio.Event( |
| 130 | + {"action": "created", "comment": {"body": "@bot hello"}}, |
| 131 | + event="issue_comment", |
| 132 | + delivery_id="123", |
| 133 | + ) |
| 134 | + test_router.dispatch(event, None) |
| 135 | + |
| 136 | + assert handler_called |
| 137 | + assert handler_args[0] == event |
| 138 | + |
| 139 | + def test_mention_with_command(self, test_router): |
| 140 | + handler_called = False |
| 141 | + |
| 142 | + @test_router.mention(command="help") |
| 143 | + def help_command(event, *args, **kwargs): |
| 144 | + nonlocal handler_called |
| 145 | + handler_called = True |
| 146 | + return "help response" |
| 147 | + |
| 148 | + event = sansio.Event( |
| 149 | + {"action": "created", "comment": {"body": "@bot help"}}, |
| 150 | + event="issue_comment", |
| 151 | + delivery_id="123", |
| 152 | + ) |
| 153 | + test_router.dispatch(event, None) |
| 154 | + |
| 155 | + assert handler_called |
| 156 | + |
| 157 | + def test_mention_with_scope(self, test_router): |
| 158 | + pr_handler_called = False |
| 159 | + |
| 160 | + @test_router.mention(command="deploy", scope=CommandScope.PR) |
| 161 | + def deploy_command(event, *args, **kwargs): |
| 162 | + nonlocal pr_handler_called |
| 163 | + pr_handler_called = True |
| 164 | + |
| 165 | + pr_event = sansio.Event( |
| 166 | + {"action": "created", "comment": {"body": "@bot deploy"}}, |
| 167 | + event="pull_request_review_comment", |
| 168 | + delivery_id="123", |
| 169 | + ) |
| 170 | + test_router.dispatch(pr_event, None) |
| 171 | + |
| 172 | + assert pr_handler_called |
| 173 | + |
| 174 | + issue_event = sansio.Event( |
| 175 | + {"action": "created", "comment": {"body": "@bot deploy"}}, |
| 176 | + event="commit_comment", # This is NOT a PR event |
| 177 | + delivery_id="124", |
| 178 | + ) |
| 179 | + pr_handler_called = False # Reset |
| 180 | + |
| 181 | + test_router.dispatch(issue_event, None) |
| 182 | + |
| 183 | + assert not pr_handler_called |
| 184 | + |
| 185 | + def test_mention_with_permission(self, test_router): |
| 186 | + handler_called = False |
| 187 | + |
| 188 | + @test_router.mention(command="delete", permission="admin") |
| 189 | + def delete_command(event, *args, **kwargs): |
| 190 | + nonlocal handler_called |
| 191 | + handler_called = True |
| 192 | + |
| 193 | + event = sansio.Event( |
| 194 | + {"action": "created", "comment": {"body": "@bot delete"}}, |
| 195 | + event="issue_comment", |
| 196 | + delivery_id="123", |
| 197 | + ) |
| 198 | + test_router.dispatch(event, None) |
| 199 | + |
| 200 | + assert handler_called |
| 201 | + |
| 202 | + def test_case_insensitive_command(self, test_router): |
| 203 | + handler_called = False |
| 204 | + |
| 205 | + @test_router.mention(command="HELP") |
| 206 | + def help_command(event, *args, **kwargs): |
| 207 | + nonlocal handler_called |
| 208 | + handler_called = True |
| 209 | + |
| 210 | + event = sansio.Event( |
| 211 | + {"action": "created", "comment": {"body": "@bot help"}}, |
| 212 | + event="issue_comment", |
| 213 | + delivery_id="123", |
| 214 | + ) |
| 215 | + test_router.dispatch(event, None) |
| 216 | + |
| 217 | + assert handler_called |
| 218 | + |
| 219 | + def test_multiple_decorators_on_same_function(self, test_router): |
| 220 | + call_count = 0 |
| 221 | + |
| 222 | + @test_router.mention(command="help") |
| 223 | + @test_router.mention(command="h") |
| 224 | + @test_router.mention(command="?") |
| 225 | + def help_command(event, *args, **kwargs): |
| 226 | + nonlocal call_count |
| 227 | + call_count += 1 |
| 228 | + return f"help called {call_count} times" |
| 229 | + |
| 230 | + event1 = sansio.Event( |
| 231 | + {"action": "created", "comment": {"body": "@bot help"}}, |
| 232 | + event="issue_comment", |
| 233 | + delivery_id="123", |
| 234 | + ) |
| 235 | + test_router.dispatch(event1, None) |
| 236 | + |
| 237 | + assert call_count == 3 |
| 238 | + |
| 239 | + call_count = 0 |
| 240 | + event2 = sansio.Event( |
| 241 | + {"action": "created", "comment": {"body": "@bot h"}}, |
| 242 | + event="issue_comment", |
| 243 | + delivery_id="124", |
| 244 | + ) |
| 245 | + test_router.dispatch(event2, None) |
| 246 | + |
| 247 | + assert call_count == 3 |
| 248 | + |
| 249 | + # This behavior will change once we implement command parsing |
| 250 | + |
| 251 | + def test_async_mention_handler(self, test_router): |
| 252 | + handler_called = False |
| 253 | + |
| 254 | + @test_router.mention(command="async-test") |
| 255 | + async def async_handler(event, *args, **kwargs): |
| 256 | + nonlocal handler_called |
| 257 | + handler_called = True |
| 258 | + return "async response" |
| 259 | + |
| 260 | + event = sansio.Event( |
| 261 | + {"action": "created", "comment": {"body": "@bot async-test"}}, |
| 262 | + event="issue_comment", |
| 263 | + delivery_id="123", |
| 264 | + ) |
| 265 | + |
| 266 | + asyncio.run(test_router.adispatch(event, None)) |
| 267 | + |
| 268 | + assert handler_called |
| 269 | + |
| 270 | + def test_sync_mention_handler(self, test_router): |
| 271 | + handler_called = False |
| 272 | + |
| 273 | + @test_router.mention(command="sync-test") |
| 274 | + def sync_handler(event, *args, **kwargs): |
| 275 | + nonlocal handler_called |
| 276 | + handler_called = True |
| 277 | + return "sync response" |
| 278 | + |
| 279 | + event = sansio.Event( |
| 280 | + {"action": "created", "comment": {"body": "@bot sync-test"}}, |
| 281 | + event="issue_comment", |
| 282 | + delivery_id="123", |
| 283 | + ) |
| 284 | + test_router.dispatch(event, None) |
| 285 | + |
| 286 | + assert handler_called |
0 commit comments