def test_coexisting_configurations(click_runner, custom_filepath, testerchain,
                                   agency_local_registry):
    #
    # Setup
    #

    if custom_filepath.exists():
        shutil.rmtree(str(custom_filepath), ignore_errors=True)
    assert not custom_filepath.exists()

    # Parse node addresses
    # TODO: Is testerchain & Full contract deployment needed here (causes massive slowdown)?
    alice, ursula, another_ursula, felix, staker, *all_yall = testerchain.unassigned_accounts

    envvars = {
        NUCYPHER_ENVVAR_KEYRING_PASSWORD: INSECURE_DEVELOPMENT_PASSWORD,
        NUCYPHER_ENVVAR_ALICE_ETH_PASSWORD: INSECURE_DEVELOPMENT_PASSWORD,
        NUCYPHER_ENVVAR_BOB_ETH_PASSWORD: INSECURE_DEVELOPMENT_PASSWORD
    }

    # Future configuration filepaths for assertions...
    public_keys_dir = custom_filepath / 'keyring' / 'public'
    known_nodes_dir = custom_filepath / 'known_nodes'

    # ... Ensure they do not exist to begin with.

    # No keys have been generated...
    assert not public_keys_dir.exists()

    # No known nodes exist...
    assert not known_nodes_dir.exists()

    # Not the configuration root...
    assert not os.path.isdir(custom_filepath)

    # ... nothing
    None

    #
    # Create
    #

    # Expected config files
    felix_file_location = custom_filepath / FelixConfiguration.generate_filename(
    )
    alice_file_location = custom_filepath / AliceConfiguration.generate_filename(
    )
    ursula_file_location = custom_filepath / UrsulaConfiguration.generate_filename(
    )
    another_ursula_configuration_file_location = custom_filepath / UrsulaConfiguration.generate_filename(
        modifier=another_ursula)

    # Felix creates a system configuration
    felix_init_args = ('felix', 'init', '--config-root', custom_filepath,
                       '--network', TEMPORARY_DOMAIN, '--provider',
                       TEST_PROVIDER_URI, '--checksum-address', felix,
                       '--registry-filepath', agency_local_registry.filepath,
                       '--debug')

    result = click_runner.invoke(nucypher_cli,
                                 felix_init_args,
                                 catch_exceptions=False,
                                 env=envvars)
    assert result.exit_code == 0

    # All configuration files still exist.
    assert os.path.isdir(custom_filepath)
    assert os.path.isfile(felix_file_location)
    assert os.path.isdir(public_keys_dir)
    assert len(os.listdir(public_keys_dir)) == 3

    # Use a custom local filepath to init a persistent Alice
    alice_init_args = ('alice', 'init', '--network', TEMPORARY_DOMAIN,
                       '--provider', TEST_PROVIDER_URI, '--pay-with', alice,
                       '--registry-filepath', agency_local_registry.filepath,
                       '--config-root', custom_filepath)

    result = click_runner.invoke(nucypher_cli,
                                 alice_init_args,
                                 catch_exceptions=False,
                                 env=envvars)
    assert result.exit_code == 0

    # All configuration files still exist.
    assert os.path.isfile(felix_file_location)
    assert os.path.isfile(alice_file_location)
    assert len(os.listdir(public_keys_dir)) == 5

    # Use the same local filepath to init a persistent Ursula
    init_args = ('ursula', 'init', '--network', TEMPORARY_DOMAIN, '--provider',
                 TEST_PROVIDER_URI, '--worker-address', ursula, '--rest-host',
                 MOCK_IP_ADDRESS, '--registry-filepath',
                 agency_local_registry.filepath, '--config-root',
                 custom_filepath)

    result = click_runner.invoke(nucypher_cli,
                                 init_args,
                                 catch_exceptions=False,
                                 env=envvars)
    assert result.exit_code == 0, result.output

    # All configuration files still exist.
    assert len(os.listdir(public_keys_dir)) == 8
    assert os.path.isfile(felix_file_location)
    assert os.path.isfile(alice_file_location)
    assert os.path.isfile(ursula_file_location)

    # keyring signing key
    signing_public_key = SecretKey.random().public_key()
    with patch('nucypher.config.keyring.NucypherKeyring.signing_public_key',
               PropertyMock(return_value=signing_public_key)):
        # Use the same local filepath to init another persistent Ursula
        init_args = ('ursula', 'init', '--network', TEMPORARY_DOMAIN,
                     '--worker-address', another_ursula, '--rest-host',
                     MOCK_IP_ADDRESS_2, '--registry-filepath',
                     agency_local_registry.filepath, '--provider',
                     TEST_PROVIDER_URI, '--config-root', custom_filepath)

        result = click_runner.invoke(nucypher_cli,
                                     init_args,
                                     catch_exceptions=False,
                                     env=envvars)
        assert result.exit_code == 0

    another_ursula_configuration_file_location = custom_filepath / UrsulaConfiguration.generate_filename(
        modifier=bytes(signing_public_key).hex()[:8])

    # All configuration files still exist.
    assert os.path.isfile(felix_file_location)
    assert os.path.isfile(alice_file_location)
    assert os.path.isfile(another_ursula_configuration_file_location)
    assert os.path.isfile(ursula_file_location)
    assert len(os.listdir(public_keys_dir)) == 11

    #
    # Run
    #

    # Run an Ursula amidst the other configuration files
    run_args = ('ursula', 'run', '--dry-run', '--no-ip-checkup',
                '--config-file', another_ursula_configuration_file_location)

    user_input = f'{INSECURE_DEVELOPMENT_PASSWORD}\n' * 2

    Worker.READY_POLL_RATE = 1
    Worker.READY_TIMEOUT = 1
    with pytest.raises(Teacher.UnbondedWorker):
        # Worker init success, but not bonded.
        result = click_runner.invoke(nucypher_cli,
                                     run_args,
                                     input=user_input,
                                     catch_exceptions=False)
    assert result.exit_code == 0
    Worker.READY_TIMEOUT = None

    # All configuration files still exist.
    assert os.path.isfile(felix_file_location)
    assert os.path.isfile(alice_file_location)
    assert os.path.isfile(another_ursula_configuration_file_location)
    assert os.path.isfile(ursula_file_location)
    assert len(os.listdir(public_keys_dir)) == 11

    # Check that the proper Ursula console is attached
    assert another_ursula in result.output

    #
    # Destroy
    #

    another_ursula_destruction_args = (
        'ursula', 'destroy', '--force', '--config-file',
        another_ursula_configuration_file_location)
    result = click_runner.invoke(nucypher_cli,
                                 another_ursula_destruction_args,
                                 catch_exceptions=False,
                                 env=envvars)
    assert result.exit_code == 0
    assert len(os.listdir(public_keys_dir)) == 8
    assert not os.path.isfile(another_ursula_configuration_file_location)

    ursula_destruction_args = ('ursula', 'destroy', '--config-file',
                               ursula_file_location)
    result = click_runner.invoke(nucypher_cli,
                                 ursula_destruction_args,
                                 input='Y',
                                 catch_exceptions=False,
                                 env=envvars)
    assert result.exit_code == 0
    assert 'y/N' in result.output
    assert len(os.listdir(public_keys_dir)) == 5
    assert not os.path.isfile(ursula_file_location)

    alice_destruction_args = ('alice', 'destroy', '--force', '--config-file',
                              alice_file_location)
    result = click_runner.invoke(nucypher_cli,
                                 alice_destruction_args,
                                 catch_exceptions=False,
                                 env=envvars)
    assert result.exit_code == 0
    assert len(os.listdir(public_keys_dir)) == 3
    assert not os.path.isfile(alice_file_location)

    felix_destruction_args = ('felix', 'destroy', '--force', '--config-file',
                              felix_file_location)
    result = click_runner.invoke(nucypher_cli,
                                 felix_destruction_args,
                                 catch_exceptions=False,
                                 env=envvars)
    assert result.exit_code == 0
    assert len(os.listdir(public_keys_dir)) == 0
    assert not os.path.isfile(felix_file_location)
Esempio n. 2
0
def test_keypair_serialization():
    umbral_pubkey = SecretKey.random().public_key()
    new_keypair = keypairs.Keypair(public_key=umbral_pubkey)

    pubkey_bytes = bytes(new_keypair.pubkey)
    assert pubkey_bytes == bytes(umbral_pubkey)