| 1 |
"""Notification channels tests.""" |
| 2 |
from __future__ import absolute_import |
| 3 |
|
| 4 |
__author__ = "[email protected] (Joe Gregorio)" |
| 5 |
|
| 6 |
import datetime |
| 7 |
import unittest |
| 8 |
|
| 9 |
from googleapiclient import channel, errors |
| 10 |
|
| 11 |
|
| 12 |
class TestChannel(unittest.TestCase): |
| 13 |
def test_basic(self): |
| 14 |
ch = channel.Channel( |
| 15 |
"web_hook", |
| 16 |
"myid", |
| 17 |
"mytoken", |
| 18 |
"http://example.org/callback", |
| 19 |
expiration=0, |
| 20 |
params={"extra": "info"}, |
| 21 |
resource_id="the_resource_id", |
| 22 |
resource_uri="http://example.com/resource_1", |
| 23 |
) |
| 24 |
|
| 25 |
# Converting to a body. |
| 26 |
body = ch.body() |
| 27 |
self.assertEqual("http://example.org/callback", body["address"]) |
| 28 |
self.assertEqual("myid", body["id"]) |
| 29 |
self.assertEqual("missing", body.get("expiration", "missing")) |
| 30 |
self.assertEqual("info", body["params"]["extra"]) |
| 31 |
self.assertEqual("the_resource_id", body["resourceId"]) |
| 32 |
self.assertEqual("http://example.com/resource_1", body["resourceUri"]) |
| 33 |
self.assertEqual("web_hook", body["type"]) |
| 34 |
|
| 35 |
# Converting to a body with expiration set. |
| 36 |
ch.expiration = 1 |
| 37 |
body = ch.body() |
| 38 |
self.assertEqual(1, body.get("expiration", "missing")) |
| 39 |
|
| 40 |
# Converting to a body after updating with a response body. |
| 41 |
ch.update( |
| 42 |
{ |
| 43 |
"resourceId": "updated_res_id", |
| 44 |
"resourceUri": "updated_res_uri", |
| 45 |
"some_random_parameter": 2, |
| 46 |
} |
| 47 |
) |
| 48 |
|
| 49 |
body = ch.body() |
| 50 |
self.assertEqual("http://example.org/callback", body["address"]) |
| 51 |
self.assertEqual("myid", body["id"]) |
| 52 |
self.assertEqual(1, body.get("expiration", "missing")) |
| 53 |
self.assertEqual("info", body["params"]["extra"]) |
| 54 |
self.assertEqual("updated_res_id", body["resourceId"]) |
| 55 |
self.assertEqual("updated_res_uri", body["resourceUri"]) |
| 56 |
self.assertEqual("web_hook", body["type"]) |
| 57 |
|
| 58 |
def test_new_webhook_channel(self): |
| 59 |
ch = channel.new_webhook_channel("http://example.com/callback") |
| 60 |
self.assertEqual(0, ch.expiration) |
| 61 |
self.assertEqual("http://example.com/callback", ch.address) |
| 62 |
self.assertEqual(None, ch.params) |
| 63 |
|
| 64 |
# New channel with an obviously wrong expiration time. |
| 65 |
ch = channel.new_webhook_channel( |
| 66 |
"http://example.com/callback", expiration=datetime.datetime(1965, 1, 1) |
| 67 |
) |
| 68 |
self.assertEqual(0, ch.expiration) |
| 69 |
|
| 70 |
# New channel with an expiration time. |
| 71 |
ch = channel.new_webhook_channel( |
| 72 |
"http://example.com/callback", |
| 73 |
expiration=datetime.datetime(1970, 1, 1, second=5), |
| 74 |
) |
| 75 |
self.assertEqual(5000, ch.expiration) |
| 76 |
self.assertEqual("http://example.com/callback", ch.address) |
| 77 |
self.assertEqual(None, ch.params) |
| 78 |
|
| 79 |
# New channel with an expiration time and params. |
| 80 |
ch = channel.new_webhook_channel( |
| 81 |
"http://example.com/callback", |
| 82 |
expiration=datetime.datetime(1970, 1, 1, second=5, microsecond=1000), |
| 83 |
params={"some": "stuff"}, |
| 84 |
) |
| 85 |
self.assertEqual(5001, ch.expiration) |
| 86 |
self.assertEqual("http://example.com/callback", ch.address) |
| 87 |
self.assertEqual({"some": "stuff"}, ch.params) |
| 88 |
|
| 89 |
|
| 90 |
class TestNotification(unittest.TestCase): |
| 91 |
def test_basic(self): |
| 92 |
n = channel.Notification( |
| 93 |
12, "sync", "http://example.org", "http://example.org/v1" |
| 94 |
) |
| 95 |
|
| 96 |
self.assertEqual(12, n.message_number) |
| 97 |
self.assertEqual("sync", n.state) |
| 98 |
self.assertEqual("http://example.org", n.resource_uri) |
| 99 |
self.assertEqual("http://example.org/v1", n.resource_id) |
| 100 |
|
| 101 |
def test_notification_from_headers(self): |
| 102 |
headers = { |
| 103 |
"X-GoOG-CHANNEL-ID": "myid", |
| 104 |
"X-Goog-MESSAGE-NUMBER": "1", |
| 105 |
"X-Goog-rESOURCE-STATE": "sync", |
| 106 |
"X-Goog-reSOURCE-URI": "http://example.com/", |
| 107 |
"X-Goog-resOURCE-ID": "http://example.com/resource_1", |
| 108 |
} |
| 109 |
|
| 110 |
ch = channel.Channel( |
| 111 |
"web_hook", |
| 112 |
"myid", |
| 113 |
"mytoken", |
| 114 |
"http://example.org/callback", |
| 115 |
expiration=0, |
| 116 |
params={"extra": "info"}, |
| 117 |
resource_id="the_resource_id", |
| 118 |
resource_uri="http://example.com/resource_1", |
| 119 |
) |
| 120 |
|
| 121 |
# Good test case. |
| 122 |
n = channel.notification_from_headers(ch, headers) |
| 123 |
self.assertEqual("http://example.com/resource_1", n.resource_id) |
| 124 |
self.assertEqual("http://example.com/", n.resource_uri) |
| 125 |
self.assertEqual("sync", n.state) |
| 126 |
self.assertEqual(1, n.message_number) |
| 127 |
|
| 128 |
# Detect id mismatch. |
| 129 |
ch.id = "different_id" |
| 130 |
try: |
| 131 |
n = channel.notification_from_headers(ch, headers) |
| 132 |
self.fail("Should have raised exception") |
| 133 |
except errors.InvalidNotificationError: |
| 134 |
pass |
| 135 |
|
| 136 |
# Set the id back to a correct value. |
| 137 |
ch.id = "myid" |