|
| 1 | +import json |
| 2 | + |
| 3 | +from base64 import b64encode |
| 4 | +from beanstalk_dispatch import ARGS |
| 5 | +from beanstalk_dispatch import FUNCTION |
| 6 | +from beanstalk_dispatch import KWARGS |
| 7 | +from beanstalk_dispatch.common import create_request_body |
| 8 | +from django.core.urlresolvers import reverse |
| 9 | +from django.test import Client |
| 10 | +from django.test import TestCase |
| 11 | +from django.test import override_settings |
| 12 | + |
| 13 | +# Don't log logger errors. |
| 14 | +import logging |
| 15 | +logging.disable(logging.CRITICAL) |
| 16 | + |
| 17 | + |
| 18 | +CALL_COUNTER = 0 |
| 19 | + |
| 20 | + |
| 21 | +def counter_incrementer(first_arg, second_arg=None): |
| 22 | + global CALL_COUNTER |
| 23 | + CALL_COUNTER += first_arg |
| 24 | + if second_arg: |
| 25 | + CALL_COUNTER += second_arg |
| 26 | + |
| 27 | + |
| 28 | +DISPATCH_SETTINGS = { |
| 29 | + 'BEANSTALK_DISPATCH_TABLE': { |
| 30 | + 'the_counter': ( |
| 31 | + 'beanstalk_dispatch.tests.test_dispatcher', |
| 32 | + 'counter_incrementer')}} |
| 33 | + |
| 34 | + |
| 35 | +class DispatcherTestCase(TestCase): |
| 36 | + |
| 37 | + """ Test the server-side function dispatcher. |
| 38 | +
|
| 39 | + In these tests, we base64-encode every message we send to the server |
| 40 | + because this is what boto does. |
| 41 | + """ |
| 42 | + |
| 43 | + def setUp(self): |
| 44 | + global CALL_COUNTER |
| 45 | + CALL_COUNTER = 0 |
| 46 | + self.client = Client() |
| 47 | + self.url = reverse('beanstalk_dispatcher') |
| 48 | + |
| 49 | + @override_settings(**DISPATCH_SETTINGS) |
| 50 | + def test_no_get(self): |
| 51 | + response = self.client.get(self.url) |
| 52 | + self.assertEquals(response.status_code, 405) |
| 53 | + |
| 54 | + @override_settings(BEANSTALK_DISPATCH_TABLE=None) |
| 55 | + def test_no_dispatch(self): |
| 56 | + response = self.client.post( |
| 57 | + self.url, b64encode( |
| 58 | + create_request_body('some_func').encode('ascii')), |
| 59 | + content_type='application/json') |
| 60 | + self.assertEquals(response.status_code, 400) |
| 61 | + self.assertEquals(json.loads(response.content.decode()), |
| 62 | + {'message': 'No beanstalk dispatch table configured', |
| 63 | + 'error': 400}) |
| 64 | + |
| 65 | + @override_settings(**DISPATCH_SETTINGS) |
| 66 | + def test_missing_function(self): |
| 67 | + response = self.client.post( |
| 68 | + self.url, |
| 69 | + b64encode(create_request_body('nonexistent_func').encode('ascii')), |
| 70 | + content_type='application/json') |
| 71 | + self.assertEquals(response.status_code, 400) |
| 72 | + self.assertEquals( |
| 73 | + json.loads(response.content.decode()), |
| 74 | + {'message': 'Requested function not found: nonexistent_func', |
| 75 | + 'error': 400}) |
| 76 | + |
| 77 | + @override_settings(**DISPATCH_SETTINGS) |
| 78 | + def test_malformed_request(self): |
| 79 | + keys = {FUNCTION, ARGS, KWARGS} |
| 80 | + for missing_key in keys: |
| 81 | + request_body = {key: 'test' for key in |
| 82 | + keys - {missing_key}} |
| 83 | + response = self.client.post( |
| 84 | + self.url, |
| 85 | + b64encode(json.dumps(request_body).encode('ascii')), |
| 86 | + content_type='application/json') |
| 87 | + self.assertEquals(response.status_code, 400) |
| 88 | + self.assertEquals(json.loads(response.content.decode()), { |
| 89 | + 'message': 'Please provide a {} argument'.format(missing_key), |
| 90 | + 'error': 400}) |
| 91 | + |
| 92 | + @override_settings(**DISPATCH_SETTINGS) |
| 93 | + def test_both_args_kwargs(self): |
| 94 | + body = b64encode( |
| 95 | + create_request_body('the_counter', 1, second_arg=5) |
| 96 | + .encode('ascii')) |
| 97 | + response = self.client.post(self.url, |
| 98 | + body, |
| 99 | + content_type='application/json') |
| 100 | + self.assertEquals(response.status_code, 200) |
| 101 | + self.assertEquals(json.loads(response.content.decode()), |
| 102 | + {}) |
| 103 | + self.assertEquals(CALL_COUNTER, 6) |
| 104 | + |
| 105 | + @override_settings(**DISPATCH_SETTINGS) |
| 106 | + def test_just_args(self): |
| 107 | + body = b64encode(create_request_body('the_counter', 2).encode('ascii')) |
| 108 | + response = self.client.post(self.url, |
| 109 | + body, |
| 110 | + content_type='application/json') |
| 111 | + self.assertEquals(response.status_code, 200) |
| 112 | + self.assertEquals(json.loads(response.content.decode()), |
| 113 | + {}) |
| 114 | + self.assertEquals(CALL_COUNTER, 2) |
0 commit comments