Example #1
0
 def test_empty_data(self):
     random_data = generate_nonce(0).encode()
     attachment = self.member.create_blob(self.member.member_id,
                                          self.file_type, self.file_name,
                                          random_data)
     out_blob = self.member.get_blob(attachment.blob_id)
     assert out_blob.id == attachment.blob_id
     assert out_blob.payload.data == random_data
     assert out_blob.payload.owner_id == self.member.member_id
Example #2
0
 def test_default_access(self):
     random_data = generate_nonce(50).encode()
     attachment = self.member.create_blob(self.member.member_id,
                                          self.file_type, self.file_name,
                                          random_data, Blob.DEFAULT)
     with pytest.raises(RequestError) as exec_info:
         self.other_member.get_blob(attachment.blob_id)
     assert exec_info.value.details == "BlobId: {}".format(
         attachment.blob_id)
     assert exec_info.value.code == StatusCode.NOT_FOUND
Example #3
0
 def test_get_access_token(self):
     member1 = self.client.create_member(
         utils.generate_alias(type=Alias.DOMAIN))
     address = member1.add_address(generate_nonce(),
                                   utils.generate_address())
     payload = AccessTokenBuilder.create_with_alias(
         member1.get_first_alias()).for_address(address.id).build()
     access_token = member1.create_access_token(payload)
     result = member1.get_token(access_token.id)
     assert access_token == result
Example #4
0
    def create_member_id(self,
                         member_type,
                         token_request_id: Union[str, None] = None):
        nonce = utils.generate_nonce()
        request = CreateMemberRequest(nonce=nonce, member_type=member_type)
        if token_request_id is not None:
            request.token_request_id = token_request_id

        with self._unauthenticated_channel as channel:
            response = channel.stub.CreateMember(request)
        return response.member_id
Example #5
0
 def test_get_access_token_id(self):
     member1 = self.client.create_member(
         utils.generate_alias(type=Alias.DOMAIN))
     member2 = self.client.create_member(
         utils.generate_alias(type=Alias.DOMAIN))
     address = member1.add_address(generate_nonce(),
                                   utils.generate_address())
     payload = AccessTokenBuilder.create_with_alias(
         member2.get_first_alias()).for_address(address.id).build()
     request = TokenRequest.builder(payload).build()
     token_request_id = member2.store_token_request(request)
     access_token = member1.create_access_token(payload)
     member1.endorse_token(access_token, Key.STANDARD)
     signature = member1.sign_token_request_state(token_request_id,
                                                  access_token.id,
                                                  generate_nonce())
     assert signature.ByteSize != 0
     result = self.client.get_token_request_result(token_request_id)
     assert result.token_id == access_token.id
     assert result.signature.signature == signature.signature
Example #6
0
 def test_create_access_token_idempotent(self):
     member1 = self.client.create_member(
         utils.generate_alias(type=Alias.DOMAIN))
     address = member1.add_address(generate_nonce(),
                                   utils.generate_address())
     payload = AccessTokenBuilder.create_with_alias(
         member1.get_first_alias()).for_address(address.id).build()
     member1.endorse_token(member1.create_access_token(payload),
                           Key.STANDARD)
     member1.endorse_token(member1.create_access_token(payload),
                           Key.STANDARD)
     time.sleep(self.TOKEN_LOOKUP_POLL_FREQUENCY * 2)
     result = member1.get_access_tokens(2, None)
     assert len(result.tokens) == 1
Example #7
0
 def test_only_one_access_token_allowed(self):
     member = self.client.create_member(
         utils.generate_alias(type=Alias.DOMAIN))
     address = member.add_address(generate_nonce(),
                                  utils.generate_address())
     payload = AccessTokenBuilder.create_with_alias(
         member.get_first_alias()).for_address(address.id).build()
     member.create_access_token(payload)
     payload = AccessTokenBuilder.create_with_alias(
         member.get_first_alias()).for_address(address.id).build()
     with pytest.raises(RequestError) as exec_info:
         member.create_access_token(payload)
     assert exec_info.value.details == "Token from {} to {} already exists".format(
         member.member_id, member.member_id)
     assert exec_info.value.code == StatusCode.ALREADY_EXISTS
Example #8
0
 def test_get_access_tokens(self):
     # TODO: cleanup
     member1 = self.client.create_member(
         utils.generate_alias(type=Alias.DOMAIN))
     member2 = self.client.create_member(
         utils.generate_alias(type=Alias.DOMAIN))
     address = member1.add_address(generate_nonce(),
                                   utils.generate_address())
     payload = AccessTokenBuilder.create_with_alias(
         member1.get_first_alias()).for_address(address.id).build()
     member1.create_access_token(payload)
     payload = AccessTokenBuilder.create_with_alias(
         member2.get_first_alias()).for_address(address.id).build()
     access_token = member1.create_access_token(payload)
     member1.endorse_token(access_token, Key.STANDARD)
     time.sleep(self.TOKEN_LOOKUP_POLL_FREQUENCY * 2)
     result = member1.get_access_tokens(limit=2, offset=None)
     token_ids = [item.id for item in result.tokens]
     assert access_token.id in token_ids
Example #9
0
    def test_create_and_get(self):
        random_data = generate_nonce(100).encode()
        attachment = self.member.create_blob(self.member.member_id,
                                             self.file_type, self.file_name,
                                             random_data)
        blob_payload = Blob.Payload(owner_id=self.member.member_id,
                                    name=self.file_name,
                                    type=self.file_type,
                                    data=random_data,
                                    access_mode=Blob.DEFAULT)
        blob_hash = hash_proto_message(blob_payload)
        assert attachment.blob_id == "b:{}:{}".format(
            blob_hash,
            blob_payload.owner_id.split(':')[-1])
        assert attachment.type == self.file_type
        assert attachment.name == self.file_name

        out_blob = self.member.get_blob(attachment.blob_id)
        assert out_blob.id == attachment.blob_id
        assert out_blob.payload.data == random_data
        assert out_blob.payload.owner_id == self.member.member_id
Example #10
0
    def build_with_blob_attachments(self) -> TokenRequestPayload:
        self.build()

        redeemer = self.payload.to
        if len(redeemer.id) == 0 and redeemer.alias.ByteSize() == 0:
            raise IllegalArgumentException('No redeemer on token')

        if len(self.payload.ref_id) == 0:
            self.payload.ref_id = utils.generate_nonce()

        attachment_uploads = []
        for blob_payload in self.blob_payloads:
            attachment = self.member.create_blob(
                blob_payload.owner_id, blob_payload.type, blob_payload.name,
                blob_payload.data
            )
            if attachment.ByteSize() != 0:
                attachment_uploads.append(attachment)

        self.payload.transfer_body.attachments.extend(attachment_uploads)
        return self
Example #11
0
 def from_payload(cls, payload: TokenRequestPayload):
     new_payload = TokenRequestPayload()
     new_payload.CopyFrom(payload)
     new_payload.access.CopyFrom(AccessBody())
     new_payload.ref_id = utils.generate_nonce()
     return cls(new_payload)
Example #12
0
 def __init__(self, payload=None):
     self.payload = payload
     if payload is None:
         self.payload = TokenRequestPayload(ref_id=utils.generate_nonce(),
                                            access_body=AccessBody(),
                                            to=TokenMember())
Example #13
0
def generate_alias(value=None, type=Alias.EMAIL):
    if value is None:
        value = "python-sdk-test-{}[email protected]".format(generate_nonce())
    alias = Alias(value=value, type=type)
    return alias
Example #14
0
 def remove_trusted_beneficiary(self, member_id: str) -> bool:
     payload = TrustedBeneficiary.Payload(member_id=member_id,
                                          nonce=utils.generate_nonce())
     return self.client.remove_trusted_beneficiary(payload)