Beispiel #1
0
def test_bad_html(monkeypatch, caplog):
    def urlopen(_):
        return StringIO.StringIO('This is not HTML.')
    monkeypatch.setattr('urllib2.urlopen', urlopen)

    autoloader = Autoloader('host', '', '')
    with pytest.raises(HandledError):
        autoloader.update_inventory()

    log = caplog.records()[-1].message
    assert log == 'Invalid HTML, found no regex matches.'
Beispiel #2
0
def test_bad_attrs(monkeypatch, caplog):
    def urlopen(_):
        html = '<img src="ign" onclick="from_to(slot1)"><center><img src="" title="" onclick="x">'
        return StringIO.StringIO(html)
    monkeypatch.setattr('urllib2.urlopen', urlopen)

    autoloader = Autoloader('host', '', '')
    with pytest.raises(HandledError):
        autoloader.update_inventory()

    log = caplog.records()[-1].message
    assert log == 'Attribute "onclick" in img tag is invalid: x'
def test(monkeypatch, caplog):
    def urlopen(handler):
        raise urllib2.HTTPError(handler.get_full_url(), 401, '', None, None)
    monkeypatch.setattr('urllib2.urlopen', urlopen)

    autoloader = Autoloader('124t.local', 'user', 'pw')
    with pytest.raises(HandledError):
        autoloader.check_creds()
    log = caplog.records()[-1].message
    assert log.endswith('Possibly rate limiting or invalid credentials.')

    def urlopen(_):
        return StringIO.StringIO('test67')
    monkeypatch.setattr('urllib2.urlopen', urlopen)
    autoloader.check_creds()
Beispiel #4
0
def test_bad_host(caplog, host):
    autoloader = Autoloader(host, 'user', 'pw')
    request = urllib2.Request(autoloader.url)
    with pytest.raises(HandledError):
        getattr(autoloader, '_query')(request)
    log = caplog.records()[-1].message
    assert log.startswith('URL "http://{}/" is invalid: '.format(host))
    assert log.endswith('not known>')
Beispiel #5
0
def test(monkeypatch, caplog, error):
    def urlopen(request):
        if not hasattr(urlopen, '___raised'):
            setattr(urlopen, '___raised', True)
            if error == 'Error while ejecting.':
                raise urllib2.HTTPError(request.get_full_url(), 401, '', None, None)
            elif error == 'drive locked?':
                html = '<center><img src="" title="00008FA" onclick="from_to(drive)" /></center>'
            else:
                html = '<center><img src="" title="00008FA" onclick="from_to(slot1)" /></center>'
        else:
            html = '<center><img src="" title="00008FA" onclick="from_to(mailslot)" /></center>'
        return StringIO.StringIO(html)
    monkeypatch.setattr('urllib2.urlopen', urlopen)
    monkeypatch.setattr(Autoloader, 'DELAY', 0.01)
    monkeypatch.setattr(Autoloader, 'DELAY_ERROR', 0.01)

    autoloader = Autoloader('124t.local', '', '')
    autoloader.inventory['00008FA'] = '16'  # Doesn't matter where it is here.
    autoloader.eject('00008FA')
    assert autoloader.inventory == {'00008FA': 'mailslot'}

    records = caplog.records()
    assert [r for r in records if error in r.message]
Beispiel #6
0
def test_rate_limiting(monkeypatch):
    def urlopen(_):
        return StringIO.StringIO('test67')

    monkeypatch.setattr('urllib2.urlopen', urlopen)

    autoloader = Autoloader('124t.local', 'user', 'pw')
    request = urllib2.Request(autoloader.url)
    start_time = time.time()
    monkeypatch.setattr(Autoloader, 'DELAY', 1)

    assert getattr(autoloader, '_query')(request) == 'test67'
    assert time.time() - start_time < 0.1
    assert getattr(autoloader, '_query')(request) == 'test67'
    assert time.time() - start_time > 0.9
Beispiel #7
0
def test_http_errors(monkeypatch, caplog, code):
    def urlopen(handler):
        raise urllib2.HTTPError(handler.get_full_url(), code, '', None, None)

    monkeypatch.setattr('urllib2.urlopen', urlopen)

    autoloader = Autoloader('124t.local', 'user', 'pw')
    request = urllib2.Request(autoloader.url)
    with pytest.raises(AutoloaderError if code == 401 else HandledError):
        getattr(autoloader, '_query')(request)
    log = caplog.records()[-1].message
    if code == 404:
        expected = '404 Not Found on: http://124t.local/'
    elif code == 401:
        expected = '401 Unauthorized on: http://124t.local/'
    else:
        expected = 'http://124t.local/ returned HTTP {} instead of 200.'.format(
            code)
    assert log == expected
Beispiel #8
0
def test(monkeypatch, caplog):
    def urlopen(handler):
        raise urllib2.HTTPError(handler.get_full_url(), 401, '', None, None)

    monkeypatch.setattr('urllib2.urlopen', urlopen)

    autoloader = Autoloader('124t.local', 'user', 'pw')
    with pytest.raises(HandledError):
        autoloader.check_creds()
    log = caplog.records()[-1].message
    assert log.endswith('Possibly rate limiting or invalid credentials.')

    def urlopen(_):
        return StringIO.StringIO('test67')

    monkeypatch.setattr('urllib2.urlopen', urlopen)
    autoloader.check_creds()
Beispiel #9
0
def test_valid(monkeypatch):
    def urlopen(_):
        html = """
            <img src="ignore.me" title="ignore" onclick="from_to(slot1)" />
            <center>
                <img src="ignore_me.too" onclick="from_to(slot1)" />
                <img src="tape.gif" title="00001FA" onclick="from_to(slot1)" />
                <img src="tape.gif" title="00002FA" onclick="from_to(slot2)" />
                <img src="tape.gif" title="00003FA" onclick="from_to(slot3)" />
                <img src="tape.gif" title="00004FA" onclick="from_to(slot4)" />
                <img src="tape.gif" title="00005FA" onclick="from_to(slot5)" />
                <img src="tape.gif" title="00006FA" onclick="from_to(slot6)" />
                <img src="tape.gif" title="00007FA" onclick="from_to(slot7)" />
                <img src="tape.gif" title="00008FA" onclick="from_to(slot8)" />
                <img src="tape.gif" title="000016FA" onclick="from_to(slot16)" />
                <img src="tape.gif" title="000017FA" onclick="from_to(mailslot)" />
                <img src="tape.gif" title="000018FA" onclick="from_to(picker)" />
                <img src="tape.gif" title="000019FA" onclick="from_to(drive)" />
            </center>
        """
        return StringIO.StringIO(html)
    monkeypatch.setattr('urllib2.urlopen', urlopen)
    monkeypatch.setattr(Autoloader, 'DELAY', 0.01)
    autoloader = Autoloader('host', '', '')
    autoloader.update_inventory()
    expected = {
        '00001FA': '1',
        '00002FA': '2',
        '00003FA': '3',
        '00004FA': '4',
        '00005FA': '5',
        '00006FA': '6',
        '00007FA': '7',
        '00008FA': '8',
        '000016FA': '16',
        '000019FA': 'drive',
        '000018FA': 'picker',
        '000017FA': 'mailslot',
    }
    assert autoloader.inventory == expected

    def urlopen(_):
        html = """
            <center>
                <img src="tape.gif" title="00001FA" onclick="from_to(slot1)" />
                <img src="tape.gif" title="00002FA" onclick="from_to(slot2)" />
                <img src="tape.gif" title="00003FA" onclick="from_to(slot3)" />
                <img src="tape.gif" title="00004FA" onclick="from_to(slot4)" />
                <img src="tape.gif" title="00005FA" onclick="from_to(slot5)" />
                <img src="tape.gif" title="00006FA" onclick="from_to(slot6)" />
                <img src="tape.gif" title="00007FA" onclick="from_to(slot7)" />
                <img src="tape.gif" title="00008FA" onclick="from_to(slot8)" />
                <img src="tape.gif" title="10016FA" onclick="from_to(slot16)" />
                <img src="tape.gif" title="Empty" onclick="from_to(mailslot)" />
                <img src="tape.gif" title="Empty" onclick="from_to(picker)" />
                <img src="tape.gif" title="Empty" onclick="from_to(drive)" />
            </center>
        """
        return StringIO.StringIO(html)
    monkeypatch.setattr('urllib2.urlopen', urlopen)
    autoloader.update_inventory()
    expected = {
        '00001FA': '1',
        '00002FA': '2',
        '00003FA': '3',
        '00004FA': '4',
        '00005FA': '5',
        '00006FA': '6',
        '00007FA': '7',
        '00008FA': '8',
        '10016FA': '16',
    }
    assert autoloader.inventory == expected