|
17 | 17 | import os
|
18 | 18 | import unittest
|
19 | 19 | from builtins import open
|
| 20 | +import inspect |
20 | 21 |
|
21 | 22 | from linebot import (
|
22 | 23 | SignatureValidator, WebhookParser, WebhookHandler
|
|
29 | 30 | LocationMessage, StickerMessage, FileMessage,
|
30 | 31 | SourceUser, SourceRoom, SourceGroup,
|
31 | 32 | DeviceLink, DeviceUnlink, ScenarioResult, ActionResult)
|
| 33 | +from linebot.utils import PY3 |
32 | 34 |
|
33 | 35 |
|
34 | 36 | class TestSignatureValidator(unittest.TestCase):
|
@@ -527,5 +529,88 @@ def test_handler(self):
|
527 | 529 | self.handler.handle(body, 'signature')
|
528 | 530 |
|
529 | 531 |
|
| 532 | +class TestInvokeWebhookHandler(unittest.TestCase): |
| 533 | + def setUp(self): |
| 534 | + def wrap(func): |
| 535 | + def wrapper(*args): |
| 536 | + if PY3: |
| 537 | + arg_spec = inspect.getfullargspec(func) |
| 538 | + else: |
| 539 | + arg_spec = inspect.getargspec(func) |
| 540 | + return func(*args[0:len(arg_spec.args)]) |
| 541 | + return wrapper |
| 542 | + |
| 543 | + def func_with_0_args(): |
| 544 | + assert True |
| 545 | + |
| 546 | + def func_with_1_arg(arg): |
| 547 | + assert arg |
| 548 | + |
| 549 | + def func_with_2_args(arg1, arg2): |
| 550 | + assert arg1 and arg2 |
| 551 | + |
| 552 | + def func_with_1_arg_with_default(arg=False): |
| 553 | + assert arg |
| 554 | + |
| 555 | + def func_with_2_args_with_default(arg1=False, arg2=False): |
| 556 | + assert arg1 and arg2 |
| 557 | + |
| 558 | + def func_with_1_arg_and_1_arg_with_default(arg1, arg2=False): |
| 559 | + assert arg1 and arg2 |
| 560 | + |
| 561 | + @wrap |
| 562 | + def wrapped_func_with_0_args(): |
| 563 | + assert True |
| 564 | + |
| 565 | + @wrap |
| 566 | + def wrapped_func_with_1_arg(arg): |
| 567 | + assert arg |
| 568 | + |
| 569 | + @wrap |
| 570 | + def wrapped_func_with_2_args(arg1, arg2): |
| 571 | + assert arg1 and arg2 |
| 572 | + |
| 573 | + @wrap |
| 574 | + def wrapped_func_with_1_arg_with_default(arg=False): |
| 575 | + assert arg |
| 576 | + |
| 577 | + @wrap |
| 578 | + def wrapped_func_with_2_args_with_default(arg1=False, arg2=False): |
| 579 | + assert arg1 and arg2 |
| 580 | + |
| 581 | + @wrap |
| 582 | + def wrapped_func_with_1_arg_and_1_arg_with_default( |
| 583 | + arg1, arg2=False): |
| 584 | + assert arg1 and arg2 |
| 585 | + |
| 586 | + self.functions = [ |
| 587 | + func_with_0_args, |
| 588 | + func_with_1_arg, |
| 589 | + func_with_2_args, |
| 590 | + func_with_1_arg_with_default, |
| 591 | + func_with_2_args_with_default, |
| 592 | + func_with_1_arg_and_1_arg_with_default, |
| 593 | + wrapped_func_with_0_args, |
| 594 | + wrapped_func_with_1_arg, |
| 595 | + wrapped_func_with_2_args, |
| 596 | + wrapped_func_with_1_arg_with_default, |
| 597 | + wrapped_func_with_2_args_with_default, |
| 598 | + wrapped_func_with_1_arg_and_1_arg_with_default, |
| 599 | + ] |
| 600 | + |
| 601 | + def test_invoke_func(self): |
| 602 | + class PayloadMock(object): |
| 603 | + def __init__(self): |
| 604 | + self.destination = True |
| 605 | + |
| 606 | + event = True |
| 607 | + payload = PayloadMock() |
| 608 | + |
| 609 | + for func in self.functions: |
| 610 | + WebhookHandler._WebhookHandler__invoke_func( |
| 611 | + func, event, payload |
| 612 | + ) |
| 613 | + |
| 614 | + |
530 | 615 | if __name__ == '__main__':
|
531 | 616 | unittest.main()
|
0 commit comments