github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/bddtests/steps/chaincode_rbac_impl.py (about)

     1  import os
     2  import re
     3  import time
     4  import copy
     5  import base64
     6  from datetime import datetime, timedelta
     7  
     8  import sys, requests, json
     9  
    10  import bdd_test_util
    11  
    12  import bdd_grpc_util
    13  from grpc.beta import implementations
    14  
    15  import fabric_pb2
    16  import chaincode_pb2
    17  import devops_pb2
    18  
    19  LAST_REQUESTED_TCERT="lastRequestedTCert"
    20  
    21  
    22  @when(u'user "{enrollId}" requests a new application TCert')
    23  def step_impl(context, enrollId):
    24  	assert 'users' in context, "users not found in context. Did you register a user?"
    25  	(channel, userRegistration) = bdd_grpc_util.getGRPCChannelAndUser(context, enrollId)
    26  	
    27  	stub = devops_pb2.beta_create_Devops_stub(channel)
    28  
    29  	secret =  bdd_grpc_util.getSecretForUserRegistration(userRegistration)
    30  	response = stub.EXP_GetApplicationTCert(secret,2)
    31  	assert response.status == fabric_pb2.Response.SUCCESS, 'Failure getting TCert from {0}, for user "{1}":  {2}'.format(userRegistration.composeService,enrollId, response.msg)
    32  	tcert = response.msg
    33  
    34  	userRegistration.lastResult = tcert
    35  
    36  @when(u'user "{enrollId}" stores their last result as "{tagName}"')
    37  def step_impl(context, enrollId, tagName):
    38  	assert 'users' in context, "users not found in context. Did you register a user?"
    39  	# Retrieve the userRegistration from the context
    40  	userRegistration = bdd_test_util.getUserRegistration(context, enrollId)
    41  	userRegistration.tags[tagName] = userRegistration.lastResult
    42  
    43  @when(u'user "{enrollId}" sets metadata to their stored value "{tagName}"')
    44  def step_impl(context, enrollId, tagName):
    45  	assert 'users' in context, "users not found in context. Did you register a user?"
    46  	# Retrieve the userRegistration from the context
    47  	userRegistration = bdd_test_util.getUserRegistration(context, enrollId)
    48  	assert tagName in userRegistration.tags, 'Tag "{0}" not found in user "{1}" tags'.format(tagName, enrollId)
    49  	context.metadata = userRegistration.tags[tagName] 
    50  
    51  
    52  @when(u'user "{enrollId}" deploys chaincode "{chaincodePath}" aliased as "{ccAlias}" with ctor "{ctor}" and args')
    53  def step_impl(context, enrollId, chaincodePath, ccAlias, ctor):
    54  	bdd_grpc_util.deployChaincode(context, enrollId, chaincodePath, ccAlias, ctor)
    55  
    56  
    57  @when(u'user "{enrollId}" gives stored value "{tagName}" to "{recipientEnrollId}"')
    58  def step_impl(context, enrollId, tagName, recipientEnrollId):
    59  	assert 'users' in context, "users not found in context. Did you register a user?"
    60  	# Retrieve the userRegistration from the context
    61  	userRegistration = bdd_test_util.getUserRegistration(context, enrollId)
    62  	recipientUserRegistration = bdd_test_util.getUserRegistration(context, recipientEnrollId)
    63  	# Copy value from target to recipient
    64  	recipientUserRegistration.tags[tagName] = userRegistration.tags[tagName]
    65  
    66  
    67  @when(u'"{enrollId}" uses application TCert "{assignerAppTCert}" to assign role "{role}" to application TCert "{assigneeAppTCert}"')
    68  def step_impl(context, enrollId, assignerAppTCert, role, assigneeAppTCert):
    69  	assert 'users' in context, "users not found in context. Did you register a user?"
    70  	assert 'compose_containers' in context, "compose_containers not found in context"
    71  
    72  	(channel, userRegistration) = bdd_grpc_util.getGRPCChannelAndUser(context, enrollId)
    73  
    74  	stub = devops_pb2.beta_create_Devops_stub(channel)
    75  
    76  	# First get binding with EXP_PrepareForTx
    77  	secret = bdd_grpc_util.getSecretForUserRegistration(userRegistration)
    78  	response = stub.EXP_PrepareForTx(secret,2)
    79  	assert response.status == fabric_pb2.Response.SUCCESS, 'Failure getting Binding from {0}, for user "{1}":  {2}'.format(userRegistration.composeService,enrollId, response.msg)
    80  	binding = response.msg
    81  
    82  	# Now produce the sigma EXP_ProduceSigma
    83  	chaincodeInput = chaincode_pb2.ChaincodeInput(function = "addRole", args = (base64.b64encode(userRegistration.tags[assigneeAppTCert]), role) ) 
    84  	chaincodeInputRaw = chaincodeInput.SerializeToString()
    85  	appTCert = userRegistration.tags[assignerAppTCert]
    86  	sigmaInput = devops_pb2.SigmaInput(secret = secret, appTCert = appTCert,  data = chaincodeInputRaw + binding)
    87  	response = stub.EXP_ProduceSigma(sigmaInput,2)
    88  	assert response.status == fabric_pb2.Response.SUCCESS, 'Failure prducing sigma from {0}, for user "{1}":  {2}'.format(userRegistration.composeService,enrollId, response.msg)
    89  	sigmaOutputBytes = response.msg
    90  	# Parse the msg bytes as a SigmaOutput message
    91  	sigmaOutput = devops_pb2.SigmaOutput()
    92  	sigmaOutput.ParseFromString(sigmaOutputBytes)
    93  	print('Length of sigma = {0}'.format(len(sigmaOutput.sigma)))
    94  	
    95  	# Now execute the transaction with the saved binding, EXP_ExecuteWithBinding
    96  	assert "grpcChaincodeSpec" in context, "grpcChaincodeSpec NOT found in context"
    97  	newChaincodeSpec = chaincode_pb2.ChaincodeSpec()
    98  	newChaincodeSpec.CopyFrom(context.grpcChaincodeSpec)
    99  	newChaincodeSpec.metadata = sigmaOutput.asn1Encoding
   100  	newChaincodeSpec.ctorMsg.CopyFrom(chaincodeInput)
   101  
   102  	ccInvocationSpec = chaincode_pb2.ChaincodeInvocationSpec(chaincodeSpec = newChaincodeSpec)
   103  
   104  	executeWithBinding = devops_pb2.ExecuteWithBinding(chaincodeInvocationSpec = ccInvocationSpec, binding = binding)
   105  
   106  	response = stub.EXP_ExecuteWithBinding(executeWithBinding,60)
   107  	assert response.status == fabric_pb2.Response.SUCCESS, 'Failure executeWithBinding from {0}, for user "{1}":  {2}'.format(userRegistration.composeService,enrollId, response.msg)
   108  	context.response = response
   109  	context.transactionID = response.msg
   110  
   111  
   112  @then(u'"{enrollId}"\'s last transaction should have failed with message that contains "{msg}"')
   113  def step_impl(context, enrollId, msg):
   114  	assert 'users' in context, "users not found in context. Did you register a user?"
   115  	assert 'compose_containers' in context, "compose_containers not found in context"
   116  	txResult = bdd_grpc_util.getTxResult(context, enrollId)
   117  	assert txResult.errorCode > 0, "Expected failure (errorCode > 0), instead found errorCode={0}".format(txResult.errorCode)
   118  	assert msg in txResult.error, "Expected error to contain'{0}', instead found '{1}".format(msg, txResult.error)
   119  
   120  @then(u'"{enrollId}"\'s last transaction should have succeeded')
   121  def step_impl(context, enrollId):
   122  	txResult = bdd_grpc_util.getTxResult(context, enrollId)
   123  	assert txResult.errorCode == 0, "Expected success (errorCode == 0), instead found errorCode={0}, error={1}".format(txResult.errorCode, txResult.error)
   124  
   125  @when(u'user "{enrollId}" invokes chaincode "{ccAlias}" function name "{functionName}" with args')
   126  def step_impl(context, enrollId, ccAlias, functionName):
   127  	response = bdd_grpc_util.invokeChaincode(context, enrollId, ccAlias, functionName)
   128  	context.response = response
   129  	context.transactionID = response.msg
   130  	#assert response.status == fabric_pb2.Response.SUCCESS, 'Failure invoking chaincode {0} on {1}, for user "{2}":  {3}'.format(ccAlias, userRegistration.composeService,enrollId, response.msg)
   131  
   132  @given(u'user "{enrollId}" stores a reference to chaincode "{ccAlias}" as "{tagName}"')
   133  def step_impl(context, enrollId, ccAlias, tagName):
   134  	# Retrieve the userRegistration from the context
   135  	userRegistration = bdd_test_util.getUserRegistration(context, enrollId)
   136  	deployedCcSpec = bdd_grpc_util.getDeployment(context, ccAlias)
   137  	assert deployedCcSpec != None, "Deployment NOT found for chaincode alias '{0}'".format(ccAlias)
   138  	userRegistration.tags[tagName] = deployedCcSpec.chaincodeID.name