def test_add_trusted_host(self): # Leave a gap to test how the ordering is affected. trusted_hosts = ['host1', 'host3'] session = PipSession(trusted_hosts=trusted_hosts) insecure_adapter = session._insecure_adapter prefix2 = 'https://host2/' prefix3 = 'https://host3/' # Confirm some initial conditions as a baseline. assert session.pip_trusted_hosts == ['host1', 'host3'] assert session.adapters[prefix3] is insecure_adapter assert prefix2 not in session.adapters # Test adding a new host. session.add_trusted_host('host2') assert session.pip_trusted_hosts == ['host1', 'host3', 'host2'] # Check that prefix3 is still present. assert session.adapters[prefix3] is insecure_adapter assert session.adapters[prefix2] is insecure_adapter # Test that adding the same host doesn't create a duplicate. session.add_trusted_host('host3') assert session.pip_trusted_hosts == ['host1', 'host3', 'host2'], ( 'actual: {}'.format(session.pip_trusted_hosts) )
def test_add_trusted_host__logging(self, caplog): """ Test logging when add_trusted_host() is called. """ trusted_hosts = ['host0', 'host1'] session = PipSession(trusted_hosts=trusted_hosts) with caplog.at_level(logging.INFO): # Test adding an existing host. session.add_trusted_host('host1', source='somewhere') session.add_trusted_host('host2') # Test calling add_trusted_host() on the same host twice. session.add_trusted_host('host2') actual = [(r.levelname, r.message) for r in caplog.records] # Observe that "host0" isn't included in the logs. expected = [ ('INFO', "adding trusted host: 'host1' (from somewhere)"), ('INFO', "adding trusted host: 'host2'"), ('INFO', "adding trusted host: 'host2'"), ] assert actual == expected