Пример #1
0
def test_get_devices_ok():
    responses.add(responses.POST,
                  'https://user-field.aylanetworks.com/users/sign_in.json',
                  json=LOGIN_PAYLOAD,
                  status=200)

    api = OwletAPI()
    api.set_email("*****@*****.**")
    api.set_password("moped")
    api.login()

    responses.add(responses.GET,
                  'https://ads-field.aylanetworks.com/apiv1/devices.json',
                  json=DEVICES_PAYLOAD,
                  status=200)
    api.get_devices()

    assert Owlet.__init__.called_once

    # Check if Owlet has been properly called
    args, kwargs = Owlet.__init__.call_args
    instance, arguments = args
    assert instance is api
    assert arguments == devices_payload[0]['device']

    # When calling get_devices again, no new instances of Owlet should be created
    api.get_devices()
    assert Owlet.__init__.called_once
Пример #2
0
def test_get_auth_token_ok():
    responses.add(responses.POST,
                  'https://user-field.aylanetworks.com/users/sign_in.json',
                  json=LOGIN_PAYLOAD,
                  status=200)

    api = OwletAPI()
    api.set_email("*****@*****.**")
    api.set_password("moped")
    api.login()
    # If no exception occurred, everything seems to be fine

    assert api.get_auth_token() == "testtoken"
Пример #3
0
def test_login_fail_noconnection():
    api = OwletAPI()
    api.set_email("*****@*****.**")
    api.set_password("moped")

    with pytest.raises(OwletTemporaryCommunicationException) as info:
        api.login()

    assert 'Login request failed - no response' in str(info.value)
    assert api._email == "*****@*****.**"
    assert api._password == "moped"
    assert api._auth_token == None
    assert api.get_auth_token() == None
Пример #4
0
def test_get_request_headers_ok():
    responses.add(responses.POST,
                  'https://user-field.aylanetworks.com/users/sign_in.json',
                  json=LOGIN_PAYLOAD,
                  status=200)

    api = OwletAPI()
    api.set_email("*****@*****.**")
    api.set_password("moped")
    api.login()

    assert api.get_request_headers()['Content-Type'] == "application/json"
    assert api.get_request_headers()['Accept'] == "application/json"
    assert api.get_request_headers()['Authorization'] == "testtoken"
Пример #5
0
def test_update_devices_fail_noresponse():
    responses.add(responses.POST,
                  'https://user-field.aylanetworks.com/users/sign_in.json',
                  json=LOGIN_PAYLOAD,
                  status=200)

    api = OwletAPI()
    api.set_email("*****@*****.**")
    api.set_password("moped")
    api.login()

    with pytest.raises(OwletTemporaryCommunicationException) as info:
        api.update_devices()

    assert 'Server request failed - no response' in str(info.value)
Пример #6
0
def test_login_fail_temporary():
    responses.add(responses.POST,
                  'https://user-field.aylanetworks.com/users/sign_in.json',
                  json=LOGIN_PAYLOAD,
                  status=500)

    api = OwletAPI()
    api.set_email("*****@*****.**")
    api.set_password("moped")

    with pytest.raises(OwletTemporaryCommunicationException) as info:
        api.login()

    assert 'Login request failed - status code' in str(info.value)
    assert api._email == "*****@*****.**"
    assert api._password == "moped"
    assert api._auth_token == None
    assert api.get_auth_token() == None
Пример #7
0
def test_login_fail():
    responses.add(responses.POST,
                  'https://user-field.aylanetworks.com/users/sign_in.json',
                  json=LOGIN_PAYLOAD,
                  status=401)

    api = OwletAPI()
    api.set_email("*****@*****.**")
    api.set_password("moped")

    with pytest.raises(OwletPermanentCommunicationException) as info:
        api.login()

    assert 'Login failed, check username and password' in str(info.value)
    assert api._email == "*****@*****.**"
    assert api._password == "moped"
    assert api._auth_token == None
    assert api.get_auth_token() == None
Пример #8
0
def test_get_devices_ok():
    responses.add(responses.POST,
                  'https://user-field.aylanetworks.com/users/sign_in.json',
                  json=LOGIN_PAYLOAD,
                  status=200)

    api = OwletAPI()
    api.set_email("*****@*****.**")
    api.set_password("moped")
    api.login()

    responses.add(responses.GET,
                  'https://ads-field.aylanetworks.com/apiv1/devices.json',
                  json=DEVICES_PAYLOAD,
                  status=200)
    api.get_devices()

    assert api.get_update_interval() == 177
Пример #9
0
def test_login_fail_invalidjson():
    responses.add(responses.POST,
                  'https://user-field.aylanetworks.com/users/sign_in.json',
                  body="broken",
                  status=200)

    api = OwletAPI()
    api.set_email("*****@*****.**")
    api.set_password("moped")

    with pytest.raises(OwletTemporaryCommunicationException) as info:
        api.login()

    assert 'Server did not send valid json' in str(info.value)
    assert api._email == "*****@*****.**"
    assert api._password == "moped"
    assert api._auth_token == None
    assert api.get_auth_token() == None
Пример #10
0
def test_update_devices_fail_invalidjson():
    responses.add(responses.POST,
                  'https://user-field.aylanetworks.com/users/sign_in.json',
                  json=LOGIN_PAYLOAD,
                  status=200)

    api = OwletAPI()
    api.set_email("*****@*****.**")
    api.set_password("moped")
    api.login()

    responses.add(responses.GET,
                  'https://ads-field.aylanetworks.com/apiv1/devices.json',
                  body="invalid",
                  status=200)

    with pytest.raises(OwletTemporaryCommunicationException) as info:
        api.update_devices()

    assert 'Server did not send valid json' in str(info.value)
Пример #11
0
def test_get_auth_token_relogin():
    responses.add(responses.POST,
                  'https://user-field.aylanetworks.com/users/sign_in.json',
                  json=LOGIN_PAYLOAD,
                  status=200)

    login_payload2 = copy.deepcopy(LOGIN_PAYLOAD)
    login_payload2['access_token'] = 'newtoken'
    responses.add(responses.POST,
                  'https://user-field.aylanetworks.com/users/sign_in.json',
                  json=login_payload2,
                  status=200)

    api = OwletAPI()
    api.set_email("*****@*****.**")
    api.set_password("moped")

    # Login happens at 2018-12-30 and lasts 1 day
    with freeze_time("2018-12-30"):
        api.login()
        assert api.get_auth_token() == "testtoken"

    with freeze_time("2019-12-30"):
        assert api.get_auth_token() == "newtoken"
Пример #12
0
def cli():
    """Command Line Interface for Owletapi."""
    parser = argparse.ArgumentParser(
        description='Owlet API Command Line Interface')
    parser.add_argument('email', help='Specify Email Address')
    parser.add_argument('password', help='Specify Password')
    parser.add_argument(
        'actions',
        help='Specify the actions',
        nargs='+',
        choices=["token", "devices", "attributes", "stream", 'download'])
    parser.add_argument('--device',
                        dest='device',
                        help='Specify DSN for device filter')
    parser.add_argument('--stream',
                        dest='attributes',
                        action='append',
                        help='Specify attributes for stream filter')
    parser.add_argument('--timeout',
                        dest='timeout',
                        help='Specify streaming timeout in seconds')
    # Parse arguments
    args = parser.parse_args()

    if args.timeout:
        timeout = time.time() + float(args.timeout)
    else:
        timeout = None

    # Initialize Owlet api
    api = OwletAPI()

    # Provide Login data
    api.set_email(args.email)
    api.set_password(args.password)

    # Login
    try:
        api.login()
    except OwletPermanentCommunicationException:
        print("Login failed, username or passwort might be wrong")
        sys.exit(1)
    except OwletTemporaryCommunicationException:
        print("Login failed, server might be down")
        sys.exit(1)

    # Print token
    if "token" in args.actions:
        print("Token: %s" % api.get_auth_token())

    # print Devices
    if "devices" in args.actions:
        for device in api.get_devices():
            if args.device is None or args.device == device.dsn:
                print("%15s %-7s %2.4f %2.4f" %
                      (device.dsn, device.connection_status, device.lat,
                       device.lon))

    # print Attributes
    if "attributes" in args.actions:
        for device in api.get_devices():
            if args.device is None or args.device == device.dsn:
                device.update()
                for name, myproperty in device.get_properties().items():
                    print("%-19s %-21s %-20s %s" %
                          (myproperty.name, myproperty.display_name,
                           myproperty.last_update, myproperty.value))

    if "download" in args.actions:
        for device in api.get_devices():
            if args.device is None or args.device == device.dsn:
                device.update()
                print(device.download_logged_data())

    # Stream Attributes
    if "stream" in args.actions:
        # If no attributes for streaming have been defined, we stream
        # everything
        if args.attributes is None:
            args.attributes = []
            for device in api.get_devices():
                if args.device is None or args.device == device.dsn:
                    device.update()
                    for name, myproperty in device.get_properties().items():
                        args.attributes.append(name)

        # CSV header
        header = "TIMESTAMP;DSN;"
        for attribute in args.attributes:
            header = header + attribute + ";"
        print(header)

        # Stream forever
        while timeout is None or time.time() < timeout:
            start = time.time()

            for device in api.get_devices():
                try:
                    device.update()
                except OwletTemporaryCommunicationException:
                    continue

                try:
                    device.reactivate()
                except OwletTemporaryCommunicationException:
                    continue

                if args.device is None or args.device == device.dsn:
                    line = str(time.time()) + ";" + device.dsn + ";"
                    properties = device.get_properties()

                    for attribute in args.attributes:
                        if attribute in properties:
                            line = line + \
                                str(properties[attribute].value) + ";"

                    print(line)
                    sys.stdout.flush()

            wait_time = api.get_update_interval() - (time.time() - start)
            try:
                time.sleep(max(0, wait_time))
            except (KeyboardInterrupt, SystemExit):
                sys.exit(0)