"Fossies" - the Fresh Open Source Software Archive

Member "zaqar-9.0.0/zaqar/tests/unit/common/test_urls.py" (16 Oct 2019, 4427 Bytes) of package /linux/misc/openstack/zaqar-9.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.

    1 # Copyright (c) 2015 Red Hat, 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 datetime
   17 import hashlib
   18 import hmac
   19 
   20 from oslo_utils import timeutils
   21 import six
   22 
   23 from zaqar.common import urls
   24 from zaqar.tests import base
   25 
   26 
   27 class TestURLs(base.TestBase):
   28 
   29     def test_create_signed_url(self):
   30         timeutils.set_time_override()
   31         self.addCleanup(timeutils.clear_time_override)
   32 
   33         key = six.b('test')
   34         methods = ['POST']
   35         project = 'my-project'
   36         paths = ['/v2/queues/shared/messages']
   37         expires = timeutils.utcnow() + datetime.timedelta(days=1)
   38         expires_str = expires.strftime(urls._DATE_FORMAT)
   39 
   40         hmac_body = six.b(r'%(paths)s\n%(methods)s\n'
   41                           r'%(project)s\n%(expires)s' %
   42                           {'paths': ','.join(paths),
   43                            'methods': ','.join(methods),
   44                            'project': project, 'expires': expires_str})
   45 
   46         expected = hmac.new(key, hmac_body, hashlib.sha256).hexdigest()
   47         actual = urls.create_signed_url(key, paths, methods=['POST'],
   48                                         project=project)
   49         self.assertEqual(expected, actual['signature'])
   50 
   51     def test_create_signed_url_multiple_paths(self):
   52         timeutils.set_time_override()
   53         self.addCleanup(timeutils.clear_time_override)
   54 
   55         key = six.b('test')
   56         methods = ['POST']
   57         project = 'my-project'
   58         paths = ['/v2/queues/shared/messages',
   59                  '/v2/queues/shared/subscriptions']
   60         expires = timeutils.utcnow() + datetime.timedelta(days=1)
   61         expires_str = expires.strftime(urls._DATE_FORMAT)
   62 
   63         hmac_body = six.b(r'%(paths)s\n%(methods)s\n'
   64                           r'%(project)s\n%(expires)s' %
   65                           {'paths': ','.join(paths),
   66                            'methods': ','.join(methods),
   67                            'project': project, 'expires': expires_str})
   68 
   69         expected = hmac.new(key, hmac_body, hashlib.sha256).hexdigest()
   70         actual = urls.create_signed_url(key, paths, methods=['POST'],
   71                                         project=project)
   72         self.assertEqual(expected, actual['signature'])
   73 
   74     def test_create_signed_url_utc(self):
   75         """Test that the method converts the TZ to UTC."""
   76         date_str = '2100-05-31T19:00:17+02'
   77         date_str_utc = '2100-05-31T17:00:17'
   78 
   79         key = six.b('test')
   80         project = None
   81         methods = ['GET']
   82         paths = ['/v2/queues/shared/messages']
   83         parsed = timeutils.parse_isotime(date_str_utc)
   84         expires = timeutils.normalize_time(parsed)
   85         expires_str = expires.strftime(urls._DATE_FORMAT)
   86 
   87         hmac_body = six.b('%(paths)s\\n%(methods)s\\n'
   88                           '%(project)s\\n%(expires)s' %
   89                           {'paths': ','.join(paths),
   90                            'methods': ','.join(methods),
   91                            'project': project, 'expires': expires_str})
   92 
   93         expected = hmac.new(key, hmac_body, hashlib.sha256).hexdigest()
   94         actual = urls.create_signed_url(key, paths, expires=date_str)
   95         self.assertEqual(expected, actual['signature'])
   96 
   97     def test_create_signed_urls_validation(self):
   98         self.assertRaises(ValueError, urls.create_signed_url, None, ['/test'])
   99         self.assertRaises(ValueError, urls.create_signed_url, 'test', None)
  100         self.assertRaises(ValueError, urls.create_signed_url, 'test',
  101                           ['/test'], methods='not list')
  102         self.assertRaises(ValueError, urls.create_signed_url, 'test', [])
  103         self.assertRaises(ValueError, urls.create_signed_url, 'test', '/test')
  104         self.assertRaises(ValueError, urls.create_signed_url, 'test',
  105                           ['/test'], expires='wrong date format')
  106         self.assertRaises(ValueError, urls.create_signed_url, 'test',
  107                           ['/test'], expires='3600')