github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/validator/tests/test_permission_verifier/mocks.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_validator.protobuf.identity_pb2 import Policy
    17  from sawtooth_validator.protobuf.identity_pb2 import Role
    18  
    19  
    20  class MockIdentityViewFactory(object):
    21      def __init__(self):
    22          self.roles = {}
    23          self.policies = {}
    24  
    25      def create_identity_view(self, root):
    26          return MockIdentityView(self.roles, self.policies)
    27  
    28      def add_role(self, name, policy_name):
    29          role = Role(name=name, policy_name=policy_name)
    30          self.roles[name] = role
    31  
    32      def add_policy(self, name, rules):
    33          entries = []
    34          for rule in rules:
    35              rule = rule.split(" ")
    36              if rule[0] == "PERMIT_KEY":
    37                  entry = Policy.Entry(type=Policy.PERMIT_KEY,
    38                                       key=rule[1])
    39                  entries.append(entry)
    40              elif rule[0] == "DENY_KEY":
    41                  entry = Policy.Entry(type=Policy.DENY_KEY,
    42                                       key=rule[1])
    43                  entries.append(entry)
    44          policy = Policy(name=name, entries=entries)
    45          self.policies[name] = policy
    46  
    47  
    48  class MockIdentityView(object):
    49      def __init__(self, roles, policies):
    50          self.roles = roles
    51          self.policies = policies
    52  
    53      def get_role(self, role_name):
    54          if role_name in self.roles:
    55              return self.roles[role_name]
    56          return None
    57  
    58      def get_policy(self, policy_name):
    59          if policy_name in self.policies:
    60              return self.policies[policy_name]
    61          return None
    62  
    63  
    64  def make_policy(name, rules):
    65      entries = []
    66      for rule in rules:
    67          rule = rule.split(" ")
    68          if rule[0] == "PERMIT_KEY":
    69              entry = Policy.Entry(type=Policy.PERMIT_KEY,
    70                                   key=rule[1])
    71              entries.append(entry)
    72          elif rule[0] == "DENY_KEY":
    73              entry = Policy.Entry(type=Policy.DENY_KEY,
    74                                   key=rule[1])
    75              entries.append(entry)
    76      policy = Policy(name=name, entries=entries)
    77      return policy