<commit_before>from indy import ledger
from indy.error import ErrorCode, IndyError

import json
import pytest


@pytest.mark.asyncio
async def test_build_nym_request_works_for_invalid_identifier():
    identifier = "invalid_base58_identifier"
    dest = "<KEY>"

    with pytest.raises(IndyError) as e:
        await ledger.build_nym_request(identifier, dest, None, None, None)
    assert ErrorCode.CommonInvalidStructure == e.value.error_code


@pytest.mark.asyncio
async def test_build_nym_request_works_for_only_required_fields():
    identifier = "Th7MpTaRZVRYnPiabds81Y"
    destination = "FY<KEY>"

    expected_response = {
        "identifier": identifier,
        "operation": {
            "type": "1",
            "dest": destination
        }
    }

    response = json.loads((await ledger.build_nym_request(identifier, destination, None, None, None)))
    assert expected_response.items() <= response.items()


@pytest.mark.asyncio
async def test_build_nym_request_works_with_option_fields():
    identifier = "Th7MpTaRZVRYnPiabds81Y"
    destination = "<KEY>"
    ver_key = "<KEY>"
    role = "STEWARD"
    alias = "some_alias"

    expected_response = {
        "identifier": identifier,
        "operation": {
            "type": "1",
            "dest": destination,
            "verkey": ver_key,
            "alias": alias,
            "role": "2"
        }
    }

    response = json.loads(await ledger.build_nym_request(identifier, destination, ver_key, alias, role))
    assert expected_response.items() <= response.items()<commit_msg>Added couple of tests for nym request<commit_after>from indy import ledger
from indy.error import ErrorCode, IndyError

import json
import pytest


@pytest.mark.asyncio
async def test_build_nym_request_works_for_invalid_identifier():
    identifier = "invalid_base58_identifier"
    dest = "<KEY>"

    with pytest.raises(IndyError) as e:
        await ledger.build_nym_request(identifier, dest, None, None, None)
    assert ErrorCode.CommonInvalidStructure == e.value.error_code


@pytest.mark.asyncio
async def test_build_nym_request_works_for_only_required_fields():
    identifier = "Th7MpTaRZVRYnPiabds81Y"
    destination = "<KEY>"

    expected_response = {
        "identifier": identifier,
        "operation": {
            "type": "1",
            "dest": destination
        }
    }

    response = json.loads((await ledger.build_nym_request(identifier, destination, None, None, None)))
    assert expected_response.items() <= response.items()


@pytest.mark.asyncio
async def test_build_nym_request_works_with_option_fields():
    identifier = "Th7MpTaRZVRYnPiabds81Y"
    destination = "<KEY>"
    ver_key = "<KEY>"
    role = "STEWARD"
    alias = "some_alias"

    expected_response = {
        "identifier": identifier,
        "operation": {
            "type": "1",
            "dest": destination,
            "verkey": ver_key,
            "alias": alias,
            "role": "2"
        }
    }

    response = json.loads(await ledger.build_nym_request(identifier, destination, ver_key, alias, role))
    assert expected_response.items() <= response.items()


@pytest.mark.asyncio
async def test_nym_request_works_for_different_roles(wallet_handle, pool_handle, identity_trustee1):
    (trustee_did, _) = identity_trustee1

    await check_for_role(pool_handle, wallet_handle, trustee_did, 'TRUST_ANCHOR', '101')
    await check_for_role(pool_handle, wallet_handle, trustee_did, 'TRUSTEE', '0')
    await check_for_role(pool_handle, wallet_handle, trustee_did, 'STEWARD', '2')


@pytest.mark.asyncio
async def check_for_role(pool_handle, wallet_handle, trustee_did, role, expected_role_value):
    (my_did, my_verkey, _) = await signus.create_and_store_my_did(wallet_handle, "{}")

    nym_request = await ledger.build_nym_request(trustee_did, my_did, my_verkey, None, role)
    await ledger.sign_and_submit_request(pool_handle, wallet_handle, trustee_did, nym_request)

    get_nym_request = await ledger.build_get_nym_request(my_did, my_did)
    get_nym_response = json.loads(await ledger.submit_request(pool_handle, get_nym_request))
    assert expected_role_value == json.loads(get_nym_response['result']['data'])['role']


@pytest.mark.asyncio
async def test_nym_request_works_for_invalid_role(identity_trustee1, identity_my1):
    (trustee_did, _) = identity_trustee1
    (my_did, _) = identity_my1

    with pytest.raises(IndyError) as e:
        await ledger.build_nym_request(trustee_did, my_did, None, None, "WRONG_ROLE")
    assert ErrorCode.CommonInvalidStructure == e.value.error_code
{'fraction_non_alphanumeric': 0.10533216783216784, 'fraction_numerical': 0.007211538461538462, 'mean_word_length': 3.900428265524625, 'pattern_counts': {'":': 22, '<': 15, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6739618', 'n_tokens_mistral': 1515, 'n_tokens_neox': 1514, 'n_words': 284}