forked from xybu/onedrived-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_webhook_worker.py
52 lines (41 loc) · 1.99 KB
/
test_webhook_worker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import json
import threading
import unittest
import onedrivesdk
from onedrived import get_resource, od_webhook
from tests.test_repo import get_sample_repo
class TestWebhookWorker(unittest.TestCase):
def setUp(self):
self.temp_config_dir, self.temp_repo_dir, self.drive_config, self.repo = get_sample_repo()
self.worker = od_webhook.WebhookWorkerThread('https://localhost:12345',
callback_func=self._dummy_webhook_callback,
action_delay_sec=0)
self.callback_called_sem = threading.Semaphore(value=0)
self.callback_repos = []
self.callback_count = 0
def tearDown(self):
self.temp_config_dir.cleanup()
self.temp_repo_dir.cleanup()
def _dummy_webhook_callback(self, repo):
self.callback_repos.append(repo)
self.callback_count += 1
self.callback_called_sem.release()
def test_execution(self):
self.worker.start()
notification_data = json.loads(get_resource('data/webhook_notification.json', pkg_name='tests'))
subscription = onedrivesdk.Subscription()
subscription.id = notification_data['subscriptionId']
self.worker.add_subscription(subscription, self.repo)
# Send a notification.
self.worker.queue_input(json.dumps({'value': [notification_data]}).encode('utf-8'))
# Duplicate notifications should be ignored.
self.worker.queue_input(json.dumps(notification_data).encode('utf-8'))
# Unknown subscriptions should be ignored.
notification_data['subscriptionId'] = '233'
self.worker.queue_input(json.dumps(notification_data).encode('utf-8'))
self.assertTrue(self.callback_called_sem.acquire(timeout=3))
self.assertFalse(self.callback_called_sem.acquire(timeout=1))
self.assertEqual([self.repo], self.callback_repos)
self.assertEqual(1, self.callback_count)
if __name__ == '__main__':
unittest.main()