github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/families/identity/tests/test_tp_identity.py (about)

     1  # Copyright 2017 Intel Corporation
     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 implied.
    12  # See the License for the specific language governing permissions and
    13  # limitations under the License.
    14  # ------------------------------------------------------------------------------
    15  
    16  from sawtooth_identity_test.identity_message_factory \
    17      import IdentityMessageFactory
    18  
    19  from sawtooth_processor_test.transaction_processor_test_case \
    20      import TransactionProcessorTestCase
    21  
    22  # Address for Setting: "sawtooth.identity.allowed_keys"
    23  ALLOWED_SIGNER_ADDRESS = \
    24      "000000a87cb5eafdcca6a8689f6a627384c7dcf91e6901b1da081ee3b0c44298fc1c14"
    25  
    26  
    27  class TestIdentity(TransactionProcessorTestCase):
    28  
    29      @classmethod
    30      def setUpClass(cls):
    31          super().setUpClass()
    32          cls.factory = IdentityMessageFactory()
    33  
    34      def _expect_setting_get(self, key, allowed=True):
    35          recieved = self.validator.expect(
    36              self.factory.create_get_setting_request(key))
    37  
    38          self.validator.respond(
    39              self.factory.create_get_setting_response(key, allowed),
    40              recieved)
    41  
    42      def _expect_policy_get(self, key, value=None):
    43          recieved = self.validator.expect(
    44              self.factory.create_get_policy_request(key))
    45  
    46          self.validator.respond(
    47              self.factory.create_get_policy_response(key, value),
    48              recieved)
    49  
    50      def _expect_role_get(self, key=None, value=None):
    51          recieved = self.validator.expect(
    52              self.factory.create_get_role_request(key))
    53  
    54          self.validator.respond(
    55              self.factory.create_get_role_response(key, value),
    56              recieved)
    57  
    58      def _expect_policy_set(self, key, expected_value):
    59          recieved = self.validator.expect(
    60              self.factory.create_set_policy_request(key, expected_value))
    61  
    62          self.validator.respond(
    63              self.factory.create_set_policy_response(key),
    64              recieved)
    65  
    66      def _expect_role_set(self, key, value):
    67          recieved = self.validator.expect(
    68              self.factory.create_set_role_request(key, value))
    69  
    70          self.validator.respond(
    71              self.factory.create_set_role_response(key),
    72              recieved)
    73  
    74      def _expect_add_event(self, key):
    75          recieved = self.validator.expect(
    76              self.factory.create_add_event_request(key))
    77  
    78          self.validator.respond(
    79              self.factory.create_add_event_response(),
    80              recieved)
    81  
    82      def _expect_ok(self):
    83          self.validator.expect(self.factory.create_tp_response("OK"))
    84  
    85      def _expect_invalid_transaction(self):
    86          self.validator.expect(
    87              self.factory.create_tp_response("INVALID_TRANSACTION"))
    88  
    89      def _expect_internal_error(self):
    90          self.validator.expect(
    91              self.factory.create_tp_response("INTERNAL_ERROR"))
    92  
    93      def _role(self, name, policy_name):
    94          self.validator.send(self.factory.create_role_transaction(
    95              name, policy_name))
    96  
    97      def _policy(self, name, declarations):
    98          self.validator.send(
    99              self.factory.create_policy_transaction(name, declarations))
   100  
   101      @property
   102      def _public_key(self):
   103          return self.factory.public_key
   104  
   105      def test_set_policy(self):
   106          """
   107          Tests setting a valid policy.
   108          """
   109          self._policy("policy1", "PERMIT_KEY *")
   110          self._expect_setting_get(ALLOWED_SIGNER_ADDRESS)
   111          self._expect_policy_get("policy1")
   112          self._expect_policy_set("policy1", "PERMIT_KEY *")
   113          self._expect_add_event("policy1")
   114          self._expect_ok()
   115  
   116      def test_set_role(self):
   117          """
   118          Tests setting a valid role.
   119          """
   120          self._role("role1", "policy1")
   121          self._expect_setting_get(ALLOWED_SIGNER_ADDRESS)
   122          self._expect_policy_get("policy1", "PERMIT_KEY *")
   123          self._expect_role_get("role1")
   124          self._expect_role_set("role1", "policy1")
   125          self._expect_add_event("role1")
   126          self._expect_ok()
   127  
   128      def test_set_role_bad_signer(self):
   129          self._role("role1", "policy1")
   130          self._expect_setting_get(ALLOWED_SIGNER_ADDRESS, False)
   131          self._expect_invalid_transaction()
   132  
   133      def test_set_role_without_policy(self):
   134          """
   135          Tests setting a invalid role, where the policy does not exist. This
   136          should return an invalid transaction.
   137          """
   138          self._role("role1", "policy1")
   139          self._expect_setting_get(ALLOWED_SIGNER_ADDRESS)
   140          self._expect_policy_get("policy1")
   141          self._expect_invalid_transaction()
   142  
   143      def test_set_role_without_policy_name(self):
   144          """
   145          Tests setting a invalid role, where no policy name is set. This should
   146          return an invalid transaction.
   147          """
   148          self._role("role1", "")
   149          self._expect_setting_get(ALLOWED_SIGNER_ADDRESS)
   150          self._expect_invalid_transaction()
   151  
   152      def test_set_role_without_name(self):
   153          """
   154          Tests setting a invalid role, where no role name is set. This should
   155          return an invalid transaction.
   156          """
   157          self._role("", "policy1")
   158          self._expect_setting_get(ALLOWED_SIGNER_ADDRESS)
   159          self._expect_invalid_transaction()
   160  
   161      def test_set_policy_without_entries(self):
   162          """
   163          Tests setting a invalid policy, where no entries are set. This
   164          should return an invalid transaction.
   165          """
   166          self._policy("policy1", "")
   167          self._expect_setting_get(ALLOWED_SIGNER_ADDRESS)
   168          self._expect_invalid_transaction()
   169  
   170      def test_set_policy_without_name(self):
   171          """
   172          Tests setting a invalid role, where no policy name is set. This should
   173          return an invalid transaction.
   174          """
   175          self._policy("", "PERMIT_KEY *")
   176          self._expect_setting_get(ALLOWED_SIGNER_ADDRESS)
   177          self._expect_invalid_transaction()