def test_initial_gw_selection_depends_on_query_param():
    """If gateway param is given, that gateway is selected automatically."""
    user = get_e2e_user("e2e_user")
    assert user is not None
    # Extract the first device's ID
    devices = user.devices['items']
    devices = sorted(devices, key=lambda d: d['devConnectwareId'])
    devid = devices[0]['devConnectwareId']

    splinter_tests.visit("/#/xbee_network?gateway=" + devid)

    do_sleep()

    assert browser.is_text_present("XBee Network View")

    gw_select = browser.find_by_css("select#gateway")
    assert not gw_select.is_empty()
    # Check that the first gateway is selected
    assert_that(gw_select.first.value, is_("0"))

    # 'Fetch from' select box should be visible now
    assert browser.is_text_present("Fetch from:")
    # 'Device Cloud' is automatically selected
    fetch_from = browser.find_by_css('select#fetch_from')
    assert not fetch_from.is_empty()
    assert_that(fetch_from.first.value, is_("dc"))
def test_url_changes_with_gw_selection():
    """URL query param should change when gateway selection changes."""
    splinter_tests.visit("/#/xbee_network")

    assert browser.is_text_present("XBee Network View")

    # Since this user has more than one gateway, it shouldn't auto-select one.
    gw_select = browser.find_by_css('select#gateway')
    assert not gw_select.is_empty()  # Check that the select box is present
    assert_that(gw_select.first.value, is_("?"))

    # Pick the first gateway
    browser.select("gateway", "0")
    do_sleep()

    user = get_e2e_user("e2e_user")
    assert user is not None

    # Selected gateway should be first in order of devConnectwareId
    devices = user.devices['items']
    devices = sorted(devices, key=lambda d: d['devConnectwareId'])
    devid = devices[0]['devConnectwareId']
    assert_that(browser.url, ends_with("/xbee_network?gateway=" + devid))

    # Select the second gateway
    browser.select("gateway", "1")
    devid = devices[1]['devConnectwareId']
    assert_that(browser.url, ends_with("/xbee_network?gateway=" + devid))
def test_refresh_xbee_list():
    """Clicking the XBee 'Refresh List' button clears selection, etc."""
    # Pick this user's one gateway
    browser.select("inputDevice", "0")

    # Wait for XBee list to populate
    do_sleep()

    # Get the test_user's remote XBees, count them
    test_xbees = (x for x in get_e2e_user('test_user').xbees['items']
                  if int(x.get('xpNetAddr', 0)))
    num_xbees = len(list(test_xbees))

    xbee_count = len(browser.find_by_css('select#inputRadio option'))
    assert_that(xbee_count, is_(num_xbees + 1))  # some XBees, one empty option
    # The first option in the list is the empty option
    first = browser.find_by_css('select#inputRadio option:first-of-type')
    assert not first.is_empty()
    assert_that(first.value, is_(""))
    assert first.selected

    cbutton = browser.find_by_name("createDash").first

    # No XBees should have been selected yet, so no create dashboard button
    assert not cbutton.visible

    # Pick the first XBee in the list
    browser.select("inputRadio", "0")

    # Step 4, configuring the radio should show up.
    assert browser.is_text_present(STEP_TITLES[4])
    # Wait for configuration check to complete
    do_sleep()

    # If we click Refresh List, the selection should be cleared out
    button = browser.find_by_name("refreshXbees")
    assert not button.is_empty()
    button.click()
    # Step 4 should go away then
    assert not browser.is_text_present(STEP_TITLES[4])

    do_sleep()

    # Check XBee node list again, and the selection
    xbee_count = len(browser.find_by_css('select#inputRadio option'))
    assert_that(xbee_count, is_(num_xbees + 1))  # some XBees, one empty option
    # The first option in the list is the empty option
    first = browser.find_by_css('select#inputRadio option:first-of-type')
    assert not first.is_empty()
    assert_that(str(first.value), is_(""))
    assert first.selected
def test_refresh_xbee_list():
    """Clicking the XBee 'Refresh List' button clears selection, etc."""
    # Pick this user's one gateway
    browser.select("inputDevice", "0")

    # Wait for XBee list to populate
    do_sleep()

    # Get the test_user's remote XBees, count them
    test_xbees = (x for x in get_e2e_user('test_user').xbees['items']
                  if int(x.get('xpNetAddr', 0)))
    num_xbees = len(list(test_xbees))

    xbee_count = len(browser.find_by_css('select#inputRadio option'))
    assert_that(xbee_count, is_(num_xbees + 1))  # some XBees, one empty option
    # The first option in the list is the empty option
    first = browser.find_by_css('select#inputRadio option:first-of-type')
    assert not first.is_empty()
    assert_that(first.value, is_(""))
    assert first.selected

    cbutton = browser.find_by_name("createDash").first

    # No XBees should have been selected yet, so no create dashboard button
    assert not cbutton.visible

    # Pick the first XBee in the list
    browser.select("inputRadio", "0");

    # Step 4, configuring the radio should show up.
    assert browser.is_text_present(STEP_TITLES[4])
    # Wait for configuration check to complete
    do_sleep()

    # If we click Refresh List, the selection should be cleared out
    button = browser.find_by_name("refreshXbees")
    assert not button.is_empty()
    button.click()
    # Step 4 should go away then
    assert not browser.is_text_present(STEP_TITLES[4])

    do_sleep()

    # Check XBee node list again, and the selection
    xbee_count = len(browser.find_by_css('select#inputRadio option'))
    assert_that(xbee_count, is_(num_xbees + 1))  # some XBees, one empty option
    # The first option in the list is the empty option
    first = browser.find_by_css('select#inputRadio option:first-of-type')
    assert not first.is_empty()
    assert_that(str(first.value), is_(""))
    assert first.selected
def test_select_only_gateway():
    """If account has only one gateway, it is selected automatically."""
    splinter_tests.visit("/#/xbee_network")
    do_sleep()

    user = get_e2e_user("e2e_user_1device")
    assert user is not None
    # Extract the first device's ID
    devices = user.devices['items']
    devices = sorted(devices, key=lambda d: d['devConnectwareId'])
    devid = devices[0]['devConnectwareId']

    gw_select = browser.find_by_css('select#gateway')
    assert not gw_select.is_empty()
    assert_that(gw_select.first.value, is_("0"))
    assert_that(browser.url, ends_with("/xbee_network?gateway=" + devid))
def test_linked_to_from_devices_page():
    """Devices page should deep-link to XBee Network page."""
    user = get_e2e_user("test_user")
    assert user is not None
    # Extract the first device's ID
    devices = user.devices['items']
    devices = sorted(devices, key=lambda d: d['devConnectwareId'])
    devid = devices[0]['devConnectwareId']

    splinter_tests.visit("/#/devices")
    do_sleep()

    xpath = '//td[.="{}"]/..//a[.="View"]'.format(devid)
    view_btn = browser.find_by_xpath(xpath)
    assert not view_btn.is_empty()
    assert_that(view_btn.first['href'],
                ends_with("/xbee_network?gateway=" + devid))
def test_xbees_show_up():
    """XBees (but not coordinators) should be listed in the table correctly."""
    user = get_e2e_user("test_user")
    assert user is not None
    # Extract the first device's ID
    devices = user.devices['items']
    devices = sorted(devices, key=lambda d: d['devConnectwareId'])
    devid = devices[0]['devConnectwareId']

    splinter_tests.visit("/#/xbee_network?gateway=" + devid)
    do_sleep()

    xbees = user.xbees['items']

    for xbee in xbees:
        if str(xbee.get('xpNetAddr')) != "0":
            assert browser.is_text_present(xbee['xpExtAddr'])
            assert browser.is_text_present(xbee['xpNodeId'])
def get_user_device_option_labels(username='******'):
    user = get_e2e_user(username)

    if not user:
        raise ValueError("Failed to find user")

    options = []
    for device in user.devices['items']:
        template = "{id}"
        if device.get('dpDescription'):
            template = "{id} ({desc})"

        fmt = {
            'id': device['devConnectwareId'],
            'desc': device.get('dpDescription')
        }
        options.append(template.format(**fmt))

    return options
def get_user_device_option_labels(username='******'):
    user = get_e2e_user(username)

    if not user:
        raise ValueError("Failed to find user")

    options = []
    for device in user.devices['items']:
        template = "{id}"
        if device.get('dpDescription'):
            template = "{id} ({desc})"

        fmt = {
            'id': device['devConnectwareId'],
            'desc': device.get('dpDescription')
        }
        options.append(template.format(**fmt))

    return options