"Fossies" - the Fresh Open Source Software Archive

Member "keystone-17.0.0/keystone/tests/unit/receipt/test_receipt_serialization.py" (13 May 2020, 2506 Bytes) of package /linux/misc/openstack/keystone-17.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_receipt_serialization.py": 16.0.1_vs_17.0.0.

    1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
    2 # not use this file except in compliance with the License. You may obtain
    3 # 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, WITHOUT
    9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   10 # License for the specific language governing permissions and limitations
   11 # under the License.
   12 
   13 import datetime
   14 from unittest import mock
   15 import uuid
   16 
   17 from keystone.common.cache import _context_cache
   18 from keystone.common import utils as ks_utils
   19 from keystone import exception
   20 from keystone.models import receipt_model
   21 from keystone.tests.unit import base_classes
   22 
   23 
   24 class TestReceiptSerialization(base_classes.TestCaseWithBootstrap):
   25 
   26     def setUp(self):
   27         super(TestReceiptSerialization, self).setUp()
   28         self.admin_user_id = self.bootstrapper.admin_user_id
   29 
   30         self.receipt_id = uuid.uuid4().hex
   31         issued_at = datetime.datetime.utcnow()
   32         self.issued_at = ks_utils.isotime(at=issued_at, subsecond=True)
   33 
   34         # Reach into the cache registry and pull out an instance of the
   35         # _ReceiptModelHandler so that we can interact and test it directly (as
   36         # opposed to using PROVIDERS or managers to invoke it).
   37         receipt_handler_id = receipt_model._ReceiptModelHandler.identity
   38         self.receipt_handler = _context_cache._registry.get(receipt_handler_id)
   39 
   40         self.exp_receipt = receipt_model.ReceiptModel()
   41         self.exp_receipt.user_id = self.admin_user_id
   42         self.exp_receipt.mint(self.receipt_id, self.issued_at)
   43 
   44     def test_serialize_and_deserialize_receipt_model(self):
   45         serialized = self.receipt_handler.serialize(self.exp_receipt)
   46         receipt = self.receipt_handler.deserialize(serialized)
   47 
   48         self.assertEqual(self.exp_receipt.user_id, receipt.user_id)
   49         self.assertEqual(self.exp_receipt.id, receipt.id)
   50         self.assertEqual(self.exp_receipt.issued_at, receipt.issued_at)
   51 
   52     @mock.patch.object(
   53         receipt_model.ReceiptModel, '__init__', side_effect=Exception)
   54     def test_error_handling_in_deserialize(self, handler_mock):
   55         serialized = self.receipt_handler.serialize(self.exp_receipt)
   56         self.assertRaises(
   57             exception.CacheDeserializationError,
   58             self.receipt_handler.deserialize,
   59             serialized
   60         )