Ali Afshar | 164f37e | 2013-01-07 14:05:45 -0800 | [diff] [blame^] | 1 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | # you may not use this file except in compliance with the License. |
| 3 | # You may obtain a copy of the License at |
| 4 | # |
| 5 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | # |
| 7 | # Unless required by applicable law or agreed to in writing, software |
| 8 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | # See the License for the specific language governing permissions and |
| 11 | # limitations under the License. |
| 12 | |
| 13 | """Push notifications tests.""" |
| 14 | |
| 15 | __author__ = 'afshar@google.com (Ali Afshar)' |
| 16 | |
| 17 | import unittest |
| 18 | |
| 19 | from apiclient import push |
| 20 | from apiclient import model |
| 21 | from apiclient import http |
| 22 | |
| 23 | |
| 24 | class ClientTokenGeneratorTest(unittest.TestCase): |
| 25 | |
| 26 | def test_next(self): |
| 27 | t = push.new_token() |
| 28 | self.assertTrue(t) |
| 29 | |
| 30 | |
| 31 | class ChannelTest(unittest.TestCase): |
| 32 | |
| 33 | def test_creation_noargs(self): |
| 34 | c = push.Channel(channel_type='my_channel_type', channel_args={}) |
| 35 | self.assertEqual('my_channel_type', c.channel_type) |
| 36 | self.assertEqual({}, c.channel_args) |
| 37 | |
| 38 | def test_creation_args(self): |
| 39 | c = push.Channel(channel_type='my_channel_type', |
| 40 | channel_args={'a': 'b'}) |
| 41 | self.assertEqual('my_channel_type', c.channel_type) |
| 42 | self.assertEqual({'a':'b'}, c.channel_args) |
| 43 | |
| 44 | def test_as_header_value_noargs(self): |
| 45 | c = push.Channel(channel_type='my_channel_type', channel_args={}) |
| 46 | self.assertEqual('my_channel_type?', c.as_header_value()) |
| 47 | |
| 48 | def test_as_header_value_args(self): |
| 49 | c = push.Channel(channel_type='my_channel_type', |
| 50 | channel_args={'a': 'b'}) |
| 51 | self.assertEqual('my_channel_type?a=b', c.as_header_value()) |
| 52 | |
| 53 | def test_as_header_value_args_space(self): |
| 54 | c = push.Channel(channel_type='my_channel_type', |
| 55 | channel_args={'a': 'b c'}) |
| 56 | self.assertEqual('my_channel_type?a=b+c', c.as_header_value()) |
| 57 | |
| 58 | def test_as_header_value_args_escape(self): |
| 59 | c = push.Channel(channel_type='my_channel_type', |
| 60 | channel_args={'a': 'b%c'}) |
| 61 | self.assertEqual('my_channel_type?a=b%25c', c.as_header_value()) |
| 62 | |
| 63 | def test_write_header_noargs(self): |
| 64 | c = push.Channel(channel_type='my_channel_type', channel_args={}) |
| 65 | headers = {} |
| 66 | c.write_header(headers) |
| 67 | self.assertEqual('my_channel_type?', headers['X-GOOG-SUBSCRIBE']) |
| 68 | |
| 69 | def test_write_header_args(self): |
| 70 | c = push.Channel(channel_type='my_channel_type', |
| 71 | channel_args={'a': 'b'}) |
| 72 | headers = {} |
| 73 | c.write_header(headers) |
| 74 | self.assertEqual('my_channel_type?a=b', headers['X-GOOG-SUBSCRIBE']) |
| 75 | |
| 76 | def test_write_header_args_space(self): |
| 77 | c = push.Channel(channel_type='my_channel_type', |
| 78 | channel_args={'a': 'b c'}) |
| 79 | headers = {} |
| 80 | c.write_header(headers) |
| 81 | self.assertEqual('my_channel_type?a=b+c', headers['X-GOOG-SUBSCRIBE']) |
| 82 | |
| 83 | def test_write_header_args_escape(self): |
| 84 | c = push.Channel(channel_type='my_channel_type', |
| 85 | channel_args={'a': 'b%c'}) |
| 86 | headers = {} |
| 87 | c.write_header(headers) |
| 88 | self.assertEqual('my_channel_type?a=b%25c', headers['X-GOOG-SUBSCRIBE']) |
| 89 | |
| 90 | |
| 91 | class WebhookChannelTest(unittest.TestCase): |
| 92 | |
| 93 | def test_creation_no_appengine(self): |
| 94 | c = push.WebhookChannel('http://example.org') |
| 95 | self.assertEqual('web_hook?url=http%3A%2F%2Fexample.org&app_engine=false', |
| 96 | c.as_header_value()) |
| 97 | |
| 98 | def test_creation_appengine(self): |
| 99 | c = push.WebhookChannel('http://example.org', app_engine=True) |
| 100 | self.assertEqual('web_hook?url=http%3A%2F%2Fexample.org&app_engine=true', |
| 101 | c.as_header_value()) |
| 102 | |
| 103 | |
| 104 | class HeadersTest(unittest.TestCase): |
| 105 | |
| 106 | def test_creation(self): |
| 107 | h = push.Headers() |
| 108 | self.assertEqual('', h[push.SUBSCRIBE]) |
| 109 | |
| 110 | def test_items(self): |
| 111 | h = push.Headers() |
| 112 | h[push.SUBSCRIBE] = 'my_channel_type' |
| 113 | self.assertEqual([(push.SUBSCRIBE, 'my_channel_type')], list(h.items())) |
| 114 | |
| 115 | def test_items_non_whitelisted(self): |
| 116 | h = push.Headers() |
| 117 | def set_bad_header(h=h): |
| 118 | h['X-Banana'] = 'my_channel_type' |
| 119 | self.assertRaises(ValueError, set_bad_header) |
| 120 | |
| 121 | def test_read(self): |
| 122 | h = push.Headers() |
| 123 | h.read({'x-goog-subscribe': 'my_channel_type'}) |
| 124 | self.assertEqual([(push.SUBSCRIBE, 'my_channel_type')], list(h.items())) |
| 125 | |
| 126 | def test_read_non_whitelisted(self): |
| 127 | h = push.Headers() |
| 128 | h.read({'X-Banana': 'my_channel_type'}) |
| 129 | self.assertEqual([], list(h.items())) |
| 130 | |
| 131 | def test_write(self): |
| 132 | h = push.Headers() |
| 133 | h[push.SUBSCRIBE] = 'my_channel_type' |
| 134 | headers = {} |
| 135 | h.write(headers) |
| 136 | self.assertEqual({'x-goog-subscribe': 'my_channel_type'}, headers) |
| 137 | |
| 138 | |
| 139 | class SubscriptionTest(unittest.TestCase): |
| 140 | |
| 141 | def test_create(self): |
| 142 | s = push.Subscription() |
| 143 | self.assertEqual('', s.client_token) |
| 144 | |
| 145 | def test_create_for_channnel(self): |
| 146 | c = push.WebhookChannel('http://example.org') |
| 147 | s = push.Subscription.for_channel(c) |
| 148 | self.assertTrue(s.client_token) |
| 149 | self.assertEqual('web_hook?url=http%3A%2F%2Fexample.org&app_engine=false', |
| 150 | s.subscribe) |
| 151 | |
| 152 | def test_create_for_channel_client_token(self): |
| 153 | c = push.WebhookChannel('http://example.org') |
| 154 | s = push.Subscription.for_channel(c, client_token='my_token') |
| 155 | self.assertEqual('my_token', s.client_token) |
| 156 | self.assertEqual('web_hook?url=http%3A%2F%2Fexample.org&app_engine=false', |
| 157 | s.subscribe) |
| 158 | |
| 159 | def test_subscribe(self): |
| 160 | s = push.Subscription() |
| 161 | s.headers[push.SUBSCRIBE] = 'my_header' |
| 162 | self.assertEqual('my_header', s.subscribe) |
| 163 | |
| 164 | def test_subscription_id(self): |
| 165 | s = push.Subscription() |
| 166 | s.headers[push.SUBSCRIPTION_ID] = 'my_header' |
| 167 | self.assertEqual('my_header', s.subscription_id) |
| 168 | |
| 169 | def test_subscription_id_set(self): |
| 170 | c = push.WebhookChannel('http://example.org') |
| 171 | s = push.Subscription.for_channel(c) |
| 172 | self.assertTrue(s.subscription_id) |
| 173 | |
| 174 | def test_topic_id(self): |
| 175 | s = push.Subscription() |
| 176 | s.headers[push.TOPIC_ID] = 'my_header' |
| 177 | self.assertEqual('my_header', s.topic_id) |
| 178 | |
| 179 | def test_topic_uri(self): |
| 180 | s = push.Subscription() |
| 181 | s.headers[push.TOPIC_URI] = 'my_header' |
| 182 | self.assertEqual('my_header', s.topic_uri) |
| 183 | |
| 184 | def test_client_token(self): |
| 185 | s = push.Subscription() |
| 186 | s.headers[push.CLIENT_TOKEN] = 'my_header' |
| 187 | self.assertEqual('my_header', s.client_token) |
| 188 | |
| 189 | def test_event_type(self): |
| 190 | s = push.Subscription() |
| 191 | s.headers[push.EVENT_TYPE] = 'my_header' |
| 192 | self.assertEqual('my_header', s.event_type) |
| 193 | |
| 194 | def test_unsubscribe(self): |
| 195 | s = push.Subscription() |
| 196 | s.headers[push.UNSUBSCRIBE] = 'my_header' |
| 197 | self.assertEqual('my_header', s.unsubscribe) |
| 198 | |
| 199 | def test_do_subscribe(self): |
| 200 | m = model.JsonModel() |
| 201 | request = http.HttpRequest( |
| 202 | None, |
| 203 | m.response, |
| 204 | 'https://www.googleapis.com/someapi/v1/collection/?foo=bar', |
| 205 | method='GET', |
| 206 | body='{}', |
| 207 | headers={'content-type': 'application/json'}) |
| 208 | h = http.HttpMockSequence([ |
| 209 | ({'status': 200, |
| 210 | 'X-Goog-Subscription-ID': 'my_subscription'}, |
| 211 | '{}')]) |
| 212 | c = push.Channel('my_channel', {}) |
| 213 | s = push.Subscription.for_request(request, c) |
| 214 | request.execute(http=h) |
| 215 | self.assertEqual('my_subscription', s.subscription_id) |
| 216 | |
| 217 | def test_subscribe_with_token(self): |
| 218 | m = model.JsonModel() |
| 219 | request = http.HttpRequest( |
| 220 | None, |
| 221 | m.response, |
| 222 | 'https://www.googleapis.com/someapi/v1/collection/?foo=bar', |
| 223 | method='GET', |
| 224 | body='{}', |
| 225 | headers={'content-type': 'application/json'}) |
| 226 | h = http.HttpMockSequence([ |
| 227 | ({'status': 200, |
| 228 | 'X-Goog-Subscription-ID': 'my_subscription'}, |
| 229 | '{}')]) |
| 230 | c = push.Channel('my_channel', {}) |
| 231 | s = push.Subscription.for_request(request, c, client_token='my_token') |
| 232 | request.execute(http=h) |
| 233 | self.assertEqual('my_subscription', s.subscription_id) |
| 234 | self.assertEqual('my_token', s.client_token) |
| 235 | |
| 236 | def test_verify_good_token(self): |
| 237 | s = push.Subscription() |
| 238 | s.headers['X-Goog-Client-Token'] = '123' |
| 239 | notification_headers = {'x-goog-client-token': '123'} |
| 240 | self.assertTrue(s.verify(notification_headers)) |
| 241 | |
| 242 | def test_verify_bad_token(self): |
| 243 | s = push.Subscription() |
| 244 | s.headers['X-Goog-Client-Token'] = '321' |
| 245 | notification_headers = {'x-goog-client-token': '123'} |
| 246 | self.assertFalse(s.verify(notification_headers)) |
| 247 | |
| 248 | def test_request_is_post(self): |
| 249 | m = model.JsonModel() |
| 250 | request = http.HttpRequest( |
| 251 | None, |
| 252 | m.response, |
| 253 | 'https://www.googleapis.com/someapi/v1/collection/?foo=bar', |
| 254 | method='GET', |
| 255 | body='{}', |
| 256 | headers={'content-type': 'application/json'}) |
| 257 | c = push.Channel('my_channel', {}) |
| 258 | push.Subscription.for_request(request, c) |
| 259 | self.assertEqual('POST', request.method) |
| 260 | |
| 261 | def test_non_get_error(self): |
| 262 | m = model.JsonModel() |
| 263 | request = http.HttpRequest( |
| 264 | None, |
| 265 | m.response, |
| 266 | 'https://www.googleapis.com/someapi/v1/collection/?foo=bar', |
| 267 | method='POST', |
| 268 | body='{}', |
| 269 | headers={'content-type': 'application/json'}) |
| 270 | c = push.Channel('my_channel', {}) |
| 271 | self.assertRaises(push.InvalidSubscriptionRequestError, |
| 272 | push.Subscription.for_request, request, c) |