Пример #1
0
    def test_post_with_error(self, m):
        payload = {"foo": ["a", "b", "c"]}
        result = {"id": 1}
        m.post('http://iotile.test/api/v1/test/',
               status_code=400,
               text=json.dumps(result))

        api = Api(domain='http://iotile.test')
        with self.assertRaises(HttpClientError):
            api.test.post(payload)

        m.post('http://iotile.test/api/v1/test/',
               status_code=404,
               text=json.dumps(result))

        api = Api(domain='http://iotile.test')
        with self.assertRaises(HttpClientError):
            api.test.post(payload)

        m.post('http://iotile.test/api/v1/test/',
               status_code=500,
               text=json.dumps(result))

        api = Api(domain='http://iotile.test')
        with self.assertRaises(HttpServerError):
            api.test.post(payload)
Пример #2
0
def test_quick_add_functionality(mock_cloud_private_nossl):
    """Make sure quick add functions work."""

    domain, cloud = mock_cloud_private_nossl
    api = Api(domain=domain)

    res = api.login('test', '*****@*****.**')
    assert res is False

    cloud.quick_add_user('*****@*****.**', 'test')
    res = api.login('test', '*****@*****.**')
    assert res is True

    proj_id, slug = cloud.quick_add_project()

    proj_data = api.project(proj_id).get()
    assert proj_data['id'] == proj_id
    assert proj_data['slug'] == slug

    org_data = api.org(proj_data['org']).get()
    assert org_data['slug'] == "quick-test-org"

    # Make sure quick add streamer works (Issue 28)
    slug = cloud.quick_add_streamer(1, 0, 10)
    resp = api.streamer(slug).get()
    print(resp)
    assert resp['last_id'] == 10
Пример #3
0
    def test_get_list(self, m):
        payload = {"result": ["a", "b", "c"]}
        m.get('http://iotile.test/api/v1/test/', text=json.dumps(payload))

        api = Api(domain='http://iotile.test')
        resp = api.test.get()
        self.assertEqual(resp['result'], ['a', 'b', 'c'])
Пример #4
0
def test_deny_unverified_by_default(httpsserver):
    """Ensure that we throw an error by default for self-signed servers."""

    api = Api(domain=httpsserver.url)

    with pytest.raises(HttpCouldNotVerifyServerError):
        api.login('*****@*****.**', 'test')

    with pytest.raises(HttpCouldNotVerifyServerError):
        api.logout()

    with pytest.raises(HttpCouldNotVerifyServerError):
        api.refresh_token()

    # Also ensure that the RestResource works as well
    resource = api.event

    with pytest.raises(HttpCouldNotVerifyServerError):
        resource.get()

    with pytest.raises(HttpCouldNotVerifyServerError):
        resource.put()

    with pytest.raises(HttpCouldNotVerifyServerError):
        resource.patch()

    with pytest.raises(HttpCouldNotVerifyServerError):
        resource.post()

    with pytest.raises(HttpCouldNotVerifyServerError):
        resource.delete()
Пример #5
0
    def __init__(self, domain=None, username=None, **kwargs):
        reg = ComponentRegistry()
        conf = ConfigManager()

        if domain is None:
            domain = conf.get('cloud:server')

        self.api = Api(domain=domain, **kwargs)
        self._domain = self.api.domain

        try:
            token = reg.get_config('arch:cloud_token')
            token_type = reg.get_config('arch:cloud_token_type', default='jwt')
            self.api.set_token(token, token_type=token_type)
        except ArgumentError:
            # If we are interactive, try to get the user to login for a single
            # session rather than making them call link_cloud to store a cloud token
            if type_system.interactive:
                username, password = self._prompt_user_pass(username, domain)
                ok_resp = self.api.login(email=username, password=password)

                if not ok_resp:
                    raise ExternalError("Could not login to %s as user %s" %
                                        (domain, username))
            else:
                raise ExternalError(
                    "No stored iotile cloud authentication information",
                    suggestion=
                    'Call iotile config link_cloud with your username and password'
                )

        self.token = self.api.token
        self.token_type = self.api.token_type
Пример #6
0
def test_list_devices(mock_cloud_private_nossl):
    """Make sure we can list and filter devices."""

    domain, cloud = mock_cloud_private_nossl
    api = Api(domain=domain)
    cloud.quick_add_user('*****@*****.**', 'test')
    api.login('test', '*****@*****.**')

    proj_id1, _slug1 = cloud.quick_add_project()
    device_slug1_1 = cloud.quick_add_device(proj_id1, 15)
    device_slug1_2 = cloud.quick_add_device(proj_id1, 20)

    proj_id2, _slug2 = cloud.quick_add_project()
    device_slug2_1 = cloud.quick_add_device(proj_id2)
    device_slug2_2 = cloud.quick_add_device(proj_id2)
    device_slug2_3 = cloud.quick_add_device(proj_id2)

    res = api.device.get()
    assert len(res['results']) == 5

    res = api.device.get(project=proj_id1)
    devs = set([x['slug'] for x in res['results']])
    assert device_slug1_1 in devs and device_slug1_2 in devs and len(devs) == 2

    res = api.device.get(project=proj_id2)
    devs = set([x['slug'] for x in res['results']])
    assert device_slug2_1 in devs and device_slug2_2 in devs and device_slug2_3 in devs and len(
        devs) == 3
Пример #7
0
    def test_init(self):

        api = Api()
        self.assertEqual(api.domain, 'https://iotile.cloud')
        self.assertEqual(api.base_url, 'https://iotile.cloud/api/v1')
        self.assertTrue(api.use_token)
        self.assertEqual(api.token_type, 'jwt')
Пример #8
0
def test_mock_cloud_login(water_meter):
    """Make sure the mock cloud is working."""

    domain, cloud = water_meter
    api = Api(domain=domain, verify=False)

    res = api.login('test', '*****@*****.**')
    assert res

    acc = api.account.get()
    print(acc)
    assert len(acc['results']) == 1
    assert acc['results'][0] == {
        "id": 1,
        "email": "unknown email",
        "username": "******",
        "name": "unknown name",
        "slug": "unknown slug"
    }

    res = api.login('test2', '*****@*****.**')
    assert not res

    res = api.login('test', '[email protected]')
    assert not res
Пример #9
0
def link_cloud(self, username=None, password=None, device_id=None):
    """Create and store a token for interacting with the IOTile Cloud API.

    You will need to call link_cloud once for each virtualenv that
    you create and want to use with any api calls that touch iotile cloud.

    Note that this method is called on a ConfigManager instance

    If you do not pass your username or password it will be prompted from
    you securely on stdin.

    If you are logging in for a user, the token will expire periodically and you
    will have to relogin.

    If you pass a device_id, you can obtain a limited token for that device
    that will never expire, assuming you have access to that device.

    Args:
        username (string): Your iotile.cloud username.  This is prompted
            from stdin if not provided.
        password (string): Your iotile.cloud password.  This is prompted
            from stdin if not provided.
        device_id (int): Optional device id to obtain permanent credentials
            for a device.
    """

    reg = ComponentRegistry()

    domain = self.get('cloud:server')

    if username is None:
        # Both python 2 and 3 require native strings to be passed into getpass
        prompt_str = "Please enter your IOTile.cloud email: "
        if sys.version_info.major < 3:
            prompt_str = prompt_str.encode('utf-8')

        username = input(prompt_str)

    if password is None:
        # Both python 2 and 3 require native strings to be passed into getpass
        prompt_str = "Please enter your IOTile.cloud password: "******"Could not login to iotile.cloud as user %s" %
                            username)

    reg.set_config('arch:cloud_user', cloud.username)
    reg.set_config('arch:cloud_token', cloud.token)
    reg.set_config('arch:cloud_token_type', cloud.token_type)

    if device_id is not None:
        cloud = IOTileCloud()
        cloud.impersonate_device(device_id)
Пример #10
0
    def test_get_detail(self, m):
        payload = {"a": "b", "c": "d"}
        m.get('http://iotile.test/api/v1/test/my-detail/',
              text=json.dumps(payload))

        api = Api(domain='http://iotile.test')
        resp = api.test('my-detail').get()
        self.assertEqual(resp, {'a': 'b', 'c': 'd'})
Пример #11
0
def test_http_support(water_meter_http):
    """Make sure we can use the cloud over http as well."""

    domain, cloud = water_meter_http
    api = Api(domain=domain, verify=False)

    res = api.login('test', '*****@*****.**')
    assert res
Пример #12
0
    def test_delete(self, m):
        result = {"id": 1}
        m.delete('http://iotile.test/api/v1/test/my-detail/',
                 text=json.dumps(result))

        api = Api(domain='http://iotile.test')
        deleted = api.test('my-detail').delete()
        self.assertTrue(deleted)
Пример #13
0
    def test_post_with_extra_args(self, m):
        payload = {"foo": ["a", "b", "c"]}
        result = {"id": 1}
        m.post('http://iotile.test/api/v1/test/', text=json.dumps(result))

        api = Api(domain='http://iotile.test')
        resp = api.test.post(payload, foo='bar')
        self.assertEqual(resp['id'], 1)
Пример #14
0
    def test_put(self, m):
        payload = {"foo": ["a", "b", "c"]}
        result = {"id": 1}
        m.put('http://iotile.test/api/v1/test/my-detail/',
              text=json.dumps(result))

        api = Api(domain='http://iotile.test')
        resp = api.test('my-detail').put(payload)
        self.assertEqual(resp['id'], 1)
Пример #15
0
    def test_token_refresh(self, m):
        payload = {'token': 'new-token'}
        m.post('http://iotile.test/api/v1/auth/api-jwt-refresh/',
               text=json.dumps(payload))

        api = Api(domain='http://iotile.test')
        api.set_token('old-token')
        self.assertEqual(api.token, 'old-token')
        api.refresh_token()
        self.assertEqual(api.token, 'new-token')
Пример #16
0
    def test_login(self, m):
        payload = {'jwt': 'big-token', 'username': '******'}
        m.post('http://iotile.test/api/v1/auth/login/',
               text=json.dumps(payload))

        api = Api(domain='http://iotile.test')
        ok = api.login(email='*****@*****.**', password='******')
        self.assertTrue(ok)
        self.assertEqual(api.username, 'user1')
        self.assertEqual(api.token, 'big-token')
Пример #17
0
def test_get_sg_dt(mock_cloud_private_nossl):
    domain, cloud = mock_cloud_private_nossl
    api = Api(domain=domain)
    cloud.quick_add_user('*****@*****.**', 'test')
    api.login('test', '*****@*****.**')

    sg_slug = cloud.quick_add_sg(slug="test-sg", app_tag=1027)
    dt_slug = cloud.quick_add_dt(slug="test-dt", os_tag=1027)

    assert api.sg(sg_slug).get()['slug'] == "test-sg"
    assert api.dt(dt_slug).get()['slug'] == "test-dt"
Пример #18
0
def test_upload_report(mock_cloud_private_nossl):
    """Make sure we can upload data."""

    domain, cloud = mock_cloud_private_nossl
    api = Api(domain=domain)
    cloud.quick_add_user('*****@*****.**', 'test')
    api.login('test', '*****@*****.**')

    inpath = os.path.join(os.path.dirname(__file__), 'reports',
                          'report_100readings_10dev.bin')
    with open(inpath, "rb") as infile:
        data = infile.read()

    timestamp = '{}'.format(cloud._fixed_utc_timestr())
    payload = {'file': BytesIO(data)}

    resource = api.streamer.report

    headers = {}
    authorization_str = '{0} {1}'.format(api.token_type, api.token)
    headers['Authorization'] = authorization_str

    # Make sure there are no reports
    resp = api.streamer.report.get()
    assert len(resp['results']) == 0

    resp = requests.post(resource.url(),
                         files=payload,
                         headers=headers,
                         params={'timestamp': timestamp})
    resp = resource._process_response(resp)

    # Verify that the response contains a count record
    assert resp['count'] == 100

    # Verify the streamer record exists
    resp = api.streamer('t--0000-0000-0000-000a--0001').get()
    assert resp['last_id'] == 100
    assert resp['selector'] == 0xABCD

    # Verify the report record exists and that the raw file is saved
    resp = api.streamer.report.get()
    assert len(resp['results']) == 1
    rep_id = resp['results'][0]['id']

    report = api.streamer.report(rep_id).get()
    assert report['id'] == rep_id
    assert cloud.raw_report_files[rep_id] == data
Пример #19
0
def test_data_access(water_meter):
    """Make sure we can load and access data."""

    domain, _cloud = water_meter

    api = Api(domain=domain, verify=False)
    api.login('test', '*****@*****.**')

    data = api.device('d--0000-0000-0000-00d2').get()
    assert data['slug'] == 'd--0000-0000-0000-00d2'

    data = api.datablock('b--0001-0000-0000-04e7').get()
    assert data['slug'] == 'b--0001-0000-0000-04e7'

    stream = api.stream('s--0000-0077--0000-0000-0000-00d2--5002').get()
    assert stream['slug'] == 's--0000-0077--0000-0000-0000-00d2--5002'

    proj = api.project('1c07fdd0-3fad-4549-bd56-5af2aca18d5b').get()
    assert proj['slug'] == 'p--0000-0077'

    events = api.event.get(filter="s--0000-0077--0000-0000-0000-00d2--5001")
    res = events['results']
    assert len(res) == 3

    raw1 = api.event(1).data.get()
    assert raw1 == {"test": 1, "hello": 2}

    raw2 = api.event(2).data.get()
    assert raw2 == {"test": 1, "goodbye": 15}

    api.vartype('water-meter-volume').get()

    extra = api.device('d--0000-0000-0000-00d2').extra.get()
    counts = extra.get('stream_counts')

    print(counts)
    assert counts is not None
    assert len(counts) == 3
    assert counts['s--0000-0077--0000-0000-0000-00d2--5001']['data_cnt'] == 11
    assert counts['s--0000-0077--0000-0000-0000-00d2--5001'][
        'has_streamid'] is True
    assert counts['s--0000-0077--0000-0000-0000-00d2--5002']['data_cnt'] == 3
    assert counts['s--0000-0077--0000-0000-0000-00d2--5002'][
        'has_streamid'] is True
    assert counts['s--0000-0077--0000-0000-0000-00d2--5c00']['data_cnt'] == 4
    assert counts['s--0000-0077--0000-0000-0000-00d2--5c00'][
        'has_streamid'] is False
Пример #20
0
def test_allow_unverified_option(httpsserver):
    """Ensure that we allow unverified servers if the user passes a flag."""

    api = Api(domain=httpsserver.url, verify=False)

    api.login('*****@*****.**', 'test')
    api.logout()
    api.refresh_token()

    # Also ensure that the RestResource works as well
    resource = api.event

    resource.get()
    resource.put()
    resource.patch()
    resource.post()
    resource.delete()
Пример #21
0
def test_device_patch(mock_cloud_private_nossl):
    """Make sure we can patch device data"""
    domain, cloud = mock_cloud_private_nossl
    api = Api(domain=domain)
    cloud.quick_add_user('*****@*****.**', 'test')
    api.login('test', '*****@*****.**')

    proj_id, _slug = cloud.quick_add_project()
    device_slug = cloud.quick_add_device(proj_id, 15)

    assert api.device(
        'd--0000-0000-0000-000f').get()['sg'] == 'water-meter-v1-1-0'
    payload = {'sg': 'water-meter-v1-1-1'}
    api.device('d--0000-0000-0000-000f').patch(payload)

    assert api.device(
        'd--0000-0000-0000-000f').get()['sg'] == 'water-meter-v1-1-1'
Пример #22
0
 def main(self):
     """
     Main function to call to initiate execution.
     1. Get domain name and use to instantiate Api object
     2. Call before_login to allow for work before logging in
     3. Logging into the server
     4. Call after_loging to do actual work with server data
     5. Logout
     6. Call after_logout to do work at end of script
     :return: Nothing
     """
     self.domain = self.get_domain()
     self.api = Api(self.domain)
     self.before_login()
     ok = self.login()
     if ok:
         self.after_login()
         self.logout()
         self.after_logout()
Пример #23
0
def test_data_stream(water_meter):
    """Make sure we can load and access data."""

    domain, _cloud = water_meter

    api = Api(domain=domain, verify=False)
    api.login('test', '*****@*****.**')

    data = api.data.get(filter='s--0000-0077--0000-0000-0000-00d2--5001')

    assert data['count'] == 11
    first = data['results'][0]
    assert first['project'] == 'p--0000-0077'
    assert first['device'] == 'd--0000-0000-0000-00d2'
    assert first['stream'] == 's--0000-0077--0000-0000-0000-00d2--5001'
    assert first['value'] == 37854.1
    assert first['int_value'] == 100

    data2 = api.data.get(filter='s--0000-0077--0000-0000-0000-00d2--5c00')
    assert data2['count'] == 4
Пример #24
0
def test_data_frame(water_meter):
    """Make sure we can load and access data."""

    domain, _cloud = water_meter

    api = Api(domain=domain, verify=False)
    api.login('test', '*****@*****.**')

    data = api.df.get(filter='s--0000-0077--0000-0000-0000-00d2--5001',
                      format='csv')

    data = data.decode('utf-8')
    lines = data.split('\n')
    assert len(lines) == 12

    data = [x.split(',') for x in lines]
    assert data[0] == ['row', 'int_value', 'stream_slug']
    assert data[1] == [
        '2017-04-11T20:37:29.608972Z', '37854.1',
        's--0000-0077--0000-0000-0000-00d2--5001'
    ]
Пример #25
0
def test_quick_add_fleet(mock_cloud_private_nossl):
    """Make sure we can add, list and filter fleets."""

    domain, cloud = mock_cloud_private_nossl
    api = Api(domain=domain)
    cloud.quick_add_user('*****@*****.**', 'test')
    api.login('test', '*****@*****.**')

    proj, _slug = cloud.quick_add_project()
    dev1 = cloud.quick_add_device(proj, 1)
    dev2 = cloud.quick_add_device(proj)
    dev3 = cloud.quick_add_device(proj)
    dev4 = cloud.quick_add_device(proj)

    fleet = cloud.quick_add_fleet([1, (dev2, False, False), (dev3, True)],
                                  True)
    fleet2 = cloud.quick_add_fleet([dev2])

    # Make sure we can list
    res = api.fleet.get()
    assert len(res['results']) == 2

    # Make sure we can get a single fleet
    fleet_data = api.fleet(fleet).get()
    assert fleet_data == cloud.fleets[fleet]

    # Make sure we can get all fleets containing a device
    res = api.fleet().get(device=dev1)
    assert len(res['results']) == 1
    assert res['results'][0]['slug'] == fleet

    res = api.fleet().get(device=dev4)
    assert len(res['results']) == 0

    res = api.fleet().get(device=dev2)
    assert len(res['results']) == 2

    # Make sure we can get all devices in a fleet
    res = api.fleet(fleet).devices.get()
    assert len(res['results']) == 3
Пример #26
0
    def test_logout(self, m):
        payload = {'jwt': 'big-token', 'username': '******'}

        # login works only if there is no Authorization header in the request
        def match_request_headers(request):
            return 'Authorization' not in request.headers

        m.post('http://iotile.test/api/v1/auth/login/',
               additional_matcher=match_request_headers,
               text=json.dumps(payload))
        m.post('http://iotile.test/api/v1/auth/logout/', status_code=204)

        api = Api(domain='http://iotile.test')
        ok = api.login(email='*****@*****.**', password='******')
        self.assertTrue(ok)

        api.logout()
        self.assertEqual(api.username, None)
        self.assertEqual(api.token, None)

        # can log in again
        ok = api.login(email='*****@*****.**', password='******')
        self.assertTrue(ok)
Пример #27
0
def test_quick_add_device(mock_cloud_private_nossl):
    """Make sure quick_add_device works."""

    domain, cloud = mock_cloud_private_nossl
    api = Api(domain=domain)

    cloud.quick_add_user('*****@*****.**', 'test')
    api.login('test', '*****@*****.**')

    res = api.streamer.get()
    assert len(res['results']) == 0

    proj_id, slug = cloud.quick_add_project()
    device_slug15 = cloud.quick_add_device(proj_id, 15, streamers=[10, 15])
    device_slug20 = cloud.quick_add_device(proj_id, 20, streamers=[1])
    device_slug = cloud.quick_add_device(proj_id)

    res = api.streamer.get()
    assert len(res['results']) == 3

    res = api.streamer.get(device=device_slug15)
    assert len(res['results']) == 2

    res = api.streamer('t--0000-0000-0000-000f--0001').get()
    assert res['last_id'] == 15
    assert res['device'] == device_slug15
    assert res['is_system'] is True

    with pytest.raises(HttpNotFoundError):
        api.streamer('t--0000-0000-0000-0015--0001').get()

    res = api.device(device_slug15).get()
    assert res['slug'] == device_slug15

    res = api.device(device_slug20).get()
    assert res['slug'] == device_slug20
Пример #28
0
    def test_set_token(self):

        api = Api()
        self.assertEqual(api.token, None)
        api.set_token('big-token')
        self.assertEqual(api.token, 'big-token')
Пример #29
0
 def test_timeout(self, m):
     m.get('http://iotile.test/api/v1/timeout/',
           exc=requests.exceptions.ConnectTimeout)
     api = Api(domain='http://iotile.test', timeout=0.01)
     with self.assertRaises(requests.exceptions.ConnectTimeout):
         api.timeout.get()
Пример #30
0
 def setUp(self):
     api = Api(domain='http://iotile.test')
     self.stream_data = StreamData('s--0001', api)
     self.stream_data._data = []