"Fossies" - the Fresh Open Source Software Archive

Member "zaqar-7.0.0/zaqar/tests/unit/transport/wsgi/v2_0/test_validation.py" (30 Aug 2018, 9557 Bytes) of package /linux/misc/openstack/zaqar-7.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test_validation.py": 5.0.0_vs_7.0.0.

    1 # Copyright (c) 2013 Rackspace, Inc.
    2 #
    3 # Licensed under the Apache License, Version 2.0 (the "License");
    4 # you may not use this file except in compliance with the License.
    5 # You may obtain a copy of the License at
    6 #
    7 #    http://www.apache.org/licenses/LICENSE-2.0
    8 #
    9 # Unless required by applicable law or agreed to in writing, software
   10 # distributed under the License is distributed on an "AS IS" BASIS,
   11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
   12 # implied.
   13 # See the License for the specific language governing permissions and
   14 # limitations under the License.
   15 
   16 import json
   17 
   18 import falcon
   19 
   20 from oslo_utils import uuidutils
   21 from zaqar.tests.unit.transport.wsgi import base
   22 
   23 
   24 class TestValidation(base.V2Base):
   25 
   26     config_file = 'wsgi_mongodb_validation.conf'
   27 
   28     def setUp(self):
   29         super(TestValidation, self).setUp()
   30 
   31         self.project_id = '7e55e1a7e'
   32 
   33         self.queue_path = self.url_prefix + '/queues/noein'
   34         self.simulate_put(self.queue_path, self.project_id)
   35 
   36         self.headers = {
   37             'Client-ID': uuidutils.generate_uuid(),
   38         }
   39 
   40     def tearDown(self):
   41         self.simulate_delete(self.queue_path, self.project_id)
   42         super(TestValidation, self).tearDown()
   43 
   44     def test_metadata_deserialization(self):
   45         # Normal case
   46         self.simulate_put(self.queue_path,
   47                           self.project_id,
   48                           body='{"timespace": "Shangri-la"}')
   49 
   50         self.assertEqual(falcon.HTTP_204, self.srmock.status)
   51 
   52         # Too long
   53         max_queue_metadata = 64
   54 
   55         doc_tmpl = '{{"Dragon Torc":"{0}"}}'
   56         doc_tmpl_ws = '{{ "Dragon Torc" : "{0}" }}'  # with whitespace
   57         envelope_length = len(doc_tmpl.format(''))
   58 
   59         for tmpl in doc_tmpl, doc_tmpl_ws:
   60             gen = '0' * (max_queue_metadata - envelope_length + 1)
   61             doc = tmpl.format(gen)
   62             self.simulate_put(self.queue_path,
   63                               self.project_id,
   64                               body=doc)
   65 
   66             self.assertEqual(falcon.HTTP_400, self.srmock.status)
   67 
   68     def test_message_deserialization(self):
   69         # Normal case
   70         body = '{"messages": [{"body": "Dragon Knights", "ttl": 100}]}'
   71         self.simulate_post(self.queue_path + '/messages',
   72                            self.project_id, body=body,
   73                            headers=self.headers)
   74 
   75         self.assertEqual(falcon.HTTP_201, self.srmock.status)
   76 
   77         # Both messages' size are too long
   78         max_messages_post_size = 256
   79 
   80         obj = {'a': 0, 'b': ''}
   81         envelope_length = len(json.dumps(obj, separators=(',', ':')))
   82         obj['b'] = 'x' * (max_messages_post_size - envelope_length + 1)
   83 
   84         for long_body in ('a' * (max_messages_post_size - 2 + 1), obj):
   85             doc = json.dumps([{'body': long_body, 'ttl': 100}])
   86             self.simulate_post(self.queue_path + '/messages',
   87                                self.project_id,
   88                                body=doc,
   89                                headers=self.headers)
   90 
   91             self.assertEqual(falcon.HTTP_400, self.srmock.status)
   92 
   93     def test_request_without_client_id(self):
   94         # No Client-ID in headers, it will raise 400 error.
   95         empty_headers = {}
   96         self.simulate_put(self.queue_path,
   97                           self.project_id,
   98                           body='{"timespace": "Shangri-la"}',
   99                           headers=empty_headers)
  100 
  101     def test_subscription_ttl(self):
  102         # Normal case
  103         body = '{"subscriber": "http://trigger.she", "ttl": 100, "options":{}}'
  104         self.simulate_post(self.queue_path + '/subscriptions',
  105                            self.project_id, body=body,
  106                            headers=self.headers)
  107         self.assertEqual(falcon.HTTP_201, self.srmock.status)
  108 
  109         # Very big TTL
  110         body = ('{"subscriber": "http://a.c", "ttl": 99999999999999999'
  111                 ', "options":{}}')
  112         self.simulate_post(self.queue_path + '/subscriptions',
  113                            self.project_id, body=body,
  114                            headers=self.headers)
  115         self.assertEqual(falcon.HTTP_400, self.srmock.status)
  116 
  117     def test_queue_metadata_putting(self):
  118         # Test _default_message_ttl
  119         # TTL normal case
  120         queue_1 = self.url_prefix + '/queues/queue1'
  121         self.simulate_put(queue_1,
  122                           self.project_id,
  123                           body='{"_default_message_ttl": 60}')
  124         self.addCleanup(self.simulate_delete, queue_1, self.project_id,
  125                         headers=self.headers)
  126         self.assertEqual(falcon.HTTP_201, self.srmock.status)
  127 
  128         # TTL value is zero
  129         self.simulate_put(queue_1,
  130                           self.project_id,
  131                           body='{"_default_message_ttl": 0}')
  132         self.assertEqual(falcon.HTTP_400, self.srmock.status)
  133 
  134         # TTL under min
  135         self.simulate_put(queue_1,
  136                           self.project_id,
  137                           body='{"_default_message_ttl": 59}')
  138         self.assertEqual(falcon.HTTP_400, self.srmock.status)
  139 
  140         # TTL over max
  141         self.simulate_put(queue_1,
  142                           self.project_id,
  143                           body='{"_default_message_ttl": 1209601}')
  144         self.assertEqual(falcon.HTTP_400, self.srmock.status)
  145 
  146         # Delay TTL over max
  147         self.simulate_put(queue_1,
  148                           self.project_id,
  149                           body='{"_default_message_delay": 901}')
  150         self.assertEqual(falcon.HTTP_400, self.srmock.status)
  151 
  152         # Test _max_messages_post_size
  153         # Size normal case
  154         queue_2 = self.url_prefix + '/queues/queue2'
  155         self.simulate_put(queue_2,
  156                           self.project_id,
  157                           body='{"_max_messages_post_size": 255}')
  158         self.addCleanup(self.simulate_delete, queue_2, self.project_id,
  159                         headers=self.headers)
  160         self.assertEqual(falcon.HTTP_201, self.srmock.status)
  161 
  162         # Size over max
  163         self.simulate_put(queue_2,
  164                           self.project_id,
  165                           body='{"_max_messages_post_size": 257}')
  166         self.assertEqual(falcon.HTTP_400, self.srmock.status)
  167 
  168         # Size value is zero
  169         self.simulate_put(queue_2,
  170                           self.project_id,
  171                           body='{"_max_messages_post_size": 0}')
  172         self.assertEqual(falcon.HTTP_400, self.srmock.status)
  173 
  174         # _dead_letter_queue_messages_ttl is not integer
  175         self.simulate_put(queue_2,
  176                           self.project_id,
  177                           body='{"_dead_letter_queue_messages_ttl": "123"}')
  178         self.assertEqual(falcon.HTTP_400, self.srmock.status)
  179 
  180         # _max_claim_count is not integer
  181         self.simulate_put(queue_2,
  182                           self.project_id,
  183                           body='{"_max_claim_count": "123"}')
  184         self.assertEqual(falcon.HTTP_400, self.srmock.status)
  185 
  186         # _dead_letter_queue_messages_ttl is out of range
  187         self.simulate_put(queue_2,
  188                           self.project_id,
  189                           body='{"_dead_letter_queue_messages_ttl": 59}')
  190         self.assertEqual(falcon.HTTP_400, self.srmock.status)
  191 
  192         # _dead_letter_queue_messages_ttl value is zero
  193         self.simulate_put(queue_2,
  194                           self.project_id,
  195                           body='{"_dead_letter_queue_messages_ttl": 0}')
  196         self.assertEqual(falcon.HTTP_400, self.srmock.status)
  197 
  198     def test_queue_patching(self):
  199         headers = {
  200             'Client-ID': uuidutils.generate_uuid(),
  201             'Content-Type': "application/openstack-messaging-v2.0-json-patch"
  202         }
  203 
  204         # Wrong JSON pointer
  205         self.simulate_patch(self.queue_path,
  206                             self.project_id,
  207                             headers=headers,
  208                             body='[{"op":"add","path":"/a","value":2}]')
  209         self.assertEqual(falcon.HTTP_400, self.srmock.status)
  210 
  211         # Wrong op
  212         self.simulate_patch(self.queue_path,
  213                             self.project_id,
  214                             headers=headers,
  215                             body='[{"op":"a","path":"/metadata/a","value":2}]')
  216         self.assertEqual(falcon.HTTP_400, self.srmock.status)
  217 
  218         self.simulate_patch(self.queue_path,
  219                             self.project_id,
  220                             headers=headers,
  221                             body='[{"op":"add","path":"/metadata/a",'
  222                             '"value":2}]')
  223         self.assertEqual(falcon.HTTP_200, self.srmock.status)
  224 
  225     def test_queue_purge(self):
  226         # Wrong key
  227         queue_1 = self.url_prefix + '/queues/queue1/purge'
  228         self.simulate_post(queue_1,
  229                            self.project_id,
  230                            body='{"wrong_key": ["messages"]}')
  231         self.addCleanup(self.simulate_delete, queue_1, self.project_id,
  232                         headers=self.headers)
  233         self.assertEqual(falcon.HTTP_400, self.srmock.status)
  234 
  235         # Wrong value
  236         self.simulate_post(queue_1,
  237                            self.project_id,
  238                            body='{"resource_types": ["wrong_value"]}')
  239         self.assertEqual(falcon.HTTP_400, self.srmock.status)
  240 
  241         # Correct input
  242         self.simulate_post(queue_1,
  243                            self.project_id,
  244                            body='{"resource_types": ["messages"]}')
  245         self.assertEqual(falcon.HTTP_204, self.srmock.status)