def test_index(self):
        # index page is visible to users who are not logged in
        rv = self.client.get('/', headers=self.get_headers())
        self.assertEqual(rv.status_code, 200)

        # generate user
        fake_user = {
            'username': '******',
            'email': '*****@*****.**',
            'password': '******'
        }

        User.generate_fake_user(fake_user['username'], fake_user['email'],
                                fake_user['password'])

        # login
        login_data = {
            'username': fake_user['username'],
            'password': fake_user['password']
        }
        self.client.post(path='/forms/login',
                         data=json.dumps(login_data),
                         headers=self.get_headers())

        # index page redirects to userpage
        rv = self.client.get('/', headers=self.get_headers())
        self.assertEqual(rv.status_code, 302)

        # logout
        self.client.get(path='/logout', headers=self.get_headers())

        # index page is visible again after logout
        rv = self.client.get('/', headers=self.get_headers())
        self.assertEqual(rv.status_code, 200)
    def test_chartconfigs(self):
        # create a user
        user = self.get_users()[0]
        User.generate_fake_user(user['username'], user['email'],
                                user['password'])

        # create chartconfigs
        create_chartconfigs()

        locations = []

        # fetch all chartconfigs
        rv = self.client.get('/chartconfigs',
                             headers=self.get_headers(user['username'],
                                                      user['password']))
        data = json.loads(rv.get_data())['chartconfigs']

        from data_visualization.chartjs import chart_types

        self.assertEqual(len(data), len(chart_types))
        for i in xrange(len(data)):
            self.assertTrue(data[i]['type'], chart_types[i])
            locations.append(data[i]['links']['self'])

        # check the returned 'Location' links
        for i in xrange(len(locations)):
            rv = self.client.get(locations[i],
                                 headers=self.get_headers(
                                     user['username'], user['password']))
            data = json.loads(rv.get_data())
            self.assertEqual(data['type'], chart_types[i])
Beispiel #3
0
def debug_with_user():
    with app.app_context():
        db.drop_all()
        db.create_all()
        create_chartconfigs()
        from data_visualization.models import User
        User.generate_fake_user('Fake User', '*****@*****.**',
                                'fakepassword')
    app.run(debug=True)
Beispiel #4
0
    def test_sensor_form(self):
        # add a user
        user = self.get_users()[0]
        User.generate_fake_user(user['username'], user['email'], user['password'])
        self.client.post(path='/forms/login', data=json.dumps({'username': user['username'], 'password': user['password']}), headers=self.get_headers())

        # add category
        category = self.get_categories()[0]
        rv = self.client.post(path='/category', data=json.dumps(category), headers=self.get_headers(user['username'], user['password']))

        # add a sensor with form
        sensor = {
            'name': 'virtsen',
            'category_name': category['name'],
            'location': 'London',
            'ipv4_addr': '192.168.0.2'
        }
        rv = self.client.post(path='/forms/sensors/add', data=json.dumps(sensor), headers=self.get_headers())
        self.assertEqual(rv.status_code, 302)

        # edit category with form
        sensor['location'] = 'Paris'
        sensor['ipv4_addr'] = '192.168.0.1'
        rv = self.client.post(path='/forms/sensors/edit/1', data=json.dumps(sensor), headers=self.get_headers())
        self.assertEqual(rv.status_code, 302)

        # invalid 'name' length on form
        sensor['name'] = 'asd'
        rv = self.client.post(path='/forms/sensors/edit/1', data=json.dumps(sensor), headers=self.get_headers())
        self.assertEqual(rv.status_code, 200)
        sensor['name'] = 'qwertyuiopasdfghjklzx'
        rv = self.client.post(path='/forms/sensors/edit/1', data=json.dumps(sensor), headers=self.get_headers())
        self.assertEqual(rv.status_code, 200)

        sensor['name'] = 'sens'

        # invalid 'location' length on form
        sensor['location'] = 'as'
        rv = self.client.post(path='/forms/sensors/edit/1', data=json.dumps(sensor), headers=self.get_headers())
        self.assertEqual(rv.status_code, 200)
        sensor['location'] = 'qwertyuiopasdfghjklzxqwertyuiopasdfghjklz'
        rv = self.client.post(path='/forms/sensors/edit/1', data=json.dumps(sensor), headers=self.get_headers())
        self.assertEqual(rv.status_code, 200)

        sensor['location'] = 'location'

        # invalid 'ipv4_addr' on form
        sensor['ipv4_addr'] = '192.168.1'
        rv = self.client.post(path='/forms/sensors/edit/1', data=json.dumps(sensor), headers=self.get_headers())
        self.assertEqual(rv.status_code, 200)

        sensor['ipv4_addr'] = '192.168.0.1'

        # check if the set valid datas after the invalid ones are accepted
        rv = self.client.post(path='/forms/sensors/edit/1', data=json.dumps(sensor), headers=self.get_headers())
        self.assertEqual(rv.status_code, 302)
    def test_verify_password(self):
        # create user
        user = self.get_users()[0]
        User.generate_fake_user(user['username'], user['email'], user['password'])

        # get user with API
        rv = self.client.get('/user', headers=self.get_headers(user['username'], user['password']))

        # verify response code is 200
        self.assertEqual(rv.status_code, 200)
Beispiel #6
0
    def test_view_form(self):
        # add a user
        user = self.get_users()[0]
        User.generate_fake_user(user['username'], user['email'], user['password'])
        self.client.post(path='/forms/login', data=json.dumps({'username': user['username'], 'password': user['password']}), headers=self.get_headers())

        view = {
            'name': 'view1',
            'count': 1,
            'refresh_time': 10
        }

        # add a view with form
        rv = self.client.post(path='/forms/views/add', data=json.dumps(view), headers=self.get_headers())
        self.assertEqual(rv.status_code, 302)

        # edit view with form
        view['count'] = 2
        view['refresh_time'] = 20
        rv = self.client.post(path='/forms/views/edit/1', data=json.dumps(view), headers=self.get_headers())
        self.assertEqual(rv.status_code, 302)

        # invalid 'name' length on form
        view['name'] = 'asd'
        rv = self.client.post(path='/forms/views/edit/1', data=json.dumps(view), headers=self.get_headers())
        self.assertEqual(rv.status_code, 200)
        view['name'] = 'qwertyuiopasdfghjklzx'
        rv = self.client.post(path='/forms/views/edit/1', data=json.dumps(view), headers=self.get_headers())
        self.assertEqual(rv.status_code, 200)

        view['name'] = 'view'

        # invalid 'count' on form
        view['count'] = 3
        rv = self.client.post(path='/forms/views/edit/1', data=json.dumps(view), headers=self.get_headers())
        self.assertEqual(rv.status_code, 200)
        view['count'] = 5
        rv = self.client.post(path='/forms/views/edit/1', data=json.dumps(view), headers=self.get_headers())
        self.assertEqual(rv.status_code, 200)

        view['count'] = 2

        # invalid 'refresh_time' on form
        view['refresh_time'] = 9
        rv = self.client.post(path='/forms/views/edit/1', data=json.dumps(view), headers=self.get_headers())
        self.assertEqual(rv.status_code, 200)
        view['refresh_time'] = 61
        rv = self.client.post(path='/forms/views/edit/1', data=json.dumps(view), headers=self.get_headers())
        self.assertEqual(rv.status_code, 200)

        view['refresh_time'] = 22

        # check if the set valid datas after the invalid ones are accepted
        rv = self.client.post(path='/forms/views/edit/1', data=json.dumps(view), headers=self.get_headers())
        self.assertEqual(rv.status_code, 302)
Beispiel #7
0
def debug_with_datafactory():
    with app.app_context():
        db.drop_all()
        db.create_all()
        create_chartconfigs()
        from data_visualization.models import User
        User.generate_fake_user('Fake User', '*****@*****.**',
                                'fakepassword')
    import subprocess
    datafactory_proc = subprocess.Popen(['python', './datafactory.py'])
    try:
        app.run(debug=True)
    except KeyboardInterrupt:
        datafactory_proc.kill()
Beispiel #8
0
    def test_subview_form(self):
        # add a user
        user = self.get_users()[0]
        User.generate_fake_user(user['username'], user['email'], user['password'])
        self.client.post(path='/forms/login', data=json.dumps({'username': user['username'], 'password': user['password']}), headers=self.get_headers())

        # add categories
        categories = self.get_categories()
        for category in categories:
            self.client.post(path='/category', data=json.dumps(category), headers=self.get_headers(user['username'], user['password']))

        # add sensors
        sensors = self.get_sensors()
        for sensor in sensors:
            self.client.post(path='/sensor', data=json.dumps(sensor), headers=self.get_headers(user['username'], user['password']))

        # add views
        views = self.get_views()
        for view in views:
            self.client.post(path='/view', data=json.dumps(view), headers=self.get_headers(user['username'], user['password']))

        # create chartconfigs
        create_chartconfigs()
        rv = self.client.get(path='/chartconfigs', headers=self.get_headers(user['username'], user['password']))
        chartconfig_types = [chartconf['type'] for chartconf in json.loads(rv.get_data())['chartconfigs']]

        # sensor_names for user
        sensor_names = []
        rv = self.client.get(path='/user', headers=self.get_headers(user['username'], user['password']))
        category_links = json.loads(rv.get_data())['links']['categories']
        for category_link in category_links:
            rv = self.client.get(path=category_link, headers=self.get_headers(user['username'], user['password']))
            sensor_links = json.loads(rv.get_data())['links']['sensors']
            for sensor_link in sensor_links:
                rv = self.client.get(path=sensor_link, headers=self.get_headers(user['username'], user['password']))
                sensor_names.append(json.loads(rv.get_data())['name'])

        # get selection for sensor names and chartconfig types from subview form
        rv = self.client.get(path='/forms/subviews/add/1', headers=self.get_headers())
        data = rv.get_data()

        import re
        matches = re.findall('">(.*?)</option>', data)

        self.assertTrue('sensor' in matches[0])
        for i in xrange(len(sensor_names)):
            self.assertEqual(matches[i+1], sensor_names[i])
        self.assertTrue('chart' in matches[i+2])
        for j in xrange(len(chartconfig_types)):
            self.assertEqual(matches[i+j+3], chartconfig_types[j])
Beispiel #9
0
    def test_category_form(self):
        # add a user
        user = self.get_users()[0]
        User.generate_fake_user(user['username'], user['email'], user['password'])
        self.client.post(path='/forms/login', data=json.dumps({'username': user['username'], 'password': user['password']}), headers=self.get_headers())

        # add a category with form
        category = {
            'name': 'virtcateg',
            'min_value': 10,
            'max_value': 40,
            'user_id': 1
        }
        rv = self.client.post(path='/forms/categories/add/{0}'.format(category['user_id']), data=json.dumps(category), headers=self.get_headers())
        self.assertEqual(rv.status_code, 302)

        # edit category with form
        category['min_value'] = 30
        category['max_value'] = 70
        rv = self.client.post(path='/forms/categories/edit/1', data=json.dumps(category), headers=self.get_headers())
        self.assertEqual(rv.status_code, 302)

        # invalid 'name' length on form
        category['name'] = 'asd'
        rv = self.client.post(path='/forms/categories/edit/1', data=json.dumps(category), headers=self.get_headers())
        self.assertEqual(rv.status_code, 200)
        category['name'] = 'qwertyuiopasdfghjklzx'
        rv = self.client.post(path='/forms/categories/edit/1', data=json.dumps(category), headers=self.get_headers())
        self.assertEqual(rv.status_code, 200)

        category['name'] = 'categ'

        # without 'min_value' on form
        category['min_value'] = ''
        rv = self.client.post(path='/forms/categories/edit/1', data=json.dumps(category), headers=self.get_headers())
        self.assertEqual(rv.status_code, 200)

        category['min_value'] = 10

        # without 'max_value' on form
        category['max_value'] = ''
        rv = self.client.post(path='/forms/categories/edit/1', data=json.dumps(category), headers=self.get_headers())
        self.assertEqual(rv.status_code, 200)

        category['max_value'] = 20

        # check if the set valid datas after the invalid ones are accepted
        rv = self.client.post(path='/forms/categories/edit/1', data=json.dumps(category), headers=self.get_headers())
        self.assertEqual(rv.status_code, 302)
    def test_get_token(self):
        # create user
        user = self.get_users()[0]
        User.generate_fake_user(user['username'], user['email'], user['password'])

        # get token
        rv = self.client.get('/token', headers=self.get_headers(user['username'], user['password']))
        token = json.loads(rv.get_data())['token']

        # get user with API, then wait for 1 second
        for request_counter in xrange(19):
            rv = self.client.get('/user', headers=self.get_headers(token=token))
            self.assertEqual(rv.status_code, 200)
            time.sleep(1)

        # verify the token expires after 20 seconds when testing
        rv = self.client.get('/token', headers=self.get_headers(token=token))
        self.assertEqual(rv.status_code, 403)
Beispiel #11
0
def debug_with_datareplay():
    import subprocess
    subprocess.call([
        'python', './generate_parkingspace_datadefinitions.py', '-c', '20',
        '-t', '60', '-n', '4', '-x', '8', '-a', '47.475382', '-o', '19.056040',
        '-r', '0.0025'
    ])
    with app.app_context():
        db.drop_all()
        db.create_all()
        create_chartconfigs()
        from data_visualization.models import User
        User.generate_fake_user('Fake User', '*****@*****.**',
                                'fakepassword')
    datareplay_proc = subprocess.Popen(['python', './datareplay.py'])
    try:
        app.run(debug=True)
    except KeyboardInterrupt:
        datareplay_proc.kill()
    def test_userpage(self):
        # generate two fake users
        fake_user_1 = {
            'username': '******',
            'email': '*****@*****.**',
            'password': '******'
        }

        fake_user_2 = {
            'username': '******',
            'email': '*****@*****.**',
            'password': '******'
        }

        User.generate_fake_user(fake_user_1['username'], fake_user_1['email'],
                                fake_user_1['password'])
        User.generate_fake_user(fake_user_2['username'], fake_user_2['email'],
                                fake_user_2['password'])

        # try to access userpage without being logged in
        rv = self.client.get('/FakeUser1', headers=self.get_headers())
        self.assertEqual(rv.status_code, 302)

        # login FakeUser1
        login_data = {
            'username': fake_user_1['username'],
            'password': fake_user_1['password']
        }
        self.client.post(path='/forms/login',
                         data=json.dumps(login_data),
                         headers=self.get_headers())

        # try to access userpage after login
        rv = self.client.get('/Fake.User.1', headers=self.get_headers())
        self.assertEqual(rv.status_code, 200)

        # try to access non-exisiting userpage
        rv = self.client.get('/nonexisting', headers=self.get_headers())
        self.assertEqual(rv.status_code, 302)

        # try to access FakeUser2's userpage
        rv = self.client.get('/Fake.User.2', headers=self.get_headers())
        self.assertEqual(rv.status_code, 302)
    def test_user(self):
        # create users
        users = self.get_users()

        for user in users:
            User.generate_fake_user(user['username'], user['email'],
                                    user['password'])
            rv = self.client.get(path='/user',
                                 data=json.dumps(user),
                                 headers=self.get_headers(
                                     user['username'], user['password']))
            self.assertEqual(rv.status_code, 200)

        # create categories
        categories_for_user = {}
        categories = self.get_categories()
        for category in categories:
            categories_for_user[str(category['user_id'])] = []
        for category in categories:
            user = users[category['user_id'] - 1]
            rv = self.client.post('/category',
                                  data=json.dumps(category),
                                  headers=self.get_headers(
                                      user['username'], user['password']))
            categories_for_user[str(category['user_id'])].append(
                rv.headers['Location'])

        # check the created users
        for i in xrange(len(users)):
            user = users[i]
            rv = self.client.get(path='/user',
                                 headers=self.get_headers(
                                     user['username'], user['password']))
            data = json.loads(rv.get_data())
            self.assertEqual(data['username'], user['username'])
            self.assertTrue('email' not in data)
            self.assertTrue('password' not in data)
            self.assertEqual(len(data['links']['categories']),
                             len(categories_for_user[str(i + 1)]))
            for j in xrange(len(categories_for_user[str(i + 1)])):
                self.assertEqual(
                    '/'.join(data['links']['categories'][j].split('/')[-2:]),
                    '/'.join(categories_for_user[str(i +
                                                     1)][j].split('/')[-2:]))

        # modify users[0]
        username = users[0]['username']
        users[0]['username'] = '******'
        rv = self.client.put(path='/user',
                             data=json.dumps(users[0]),
                             headers=self.get_headers(username,
                                                      users[0]['password']))
        data = json.loads(rv.get_data())
        self.assertEqual(rv.status_code, 201)
        self.assertEqual(data['username'], users[0]['username'])
        self.assertTrue('email' not in data)
        self.assertTrue('password' not in data)

        # delete users
        for i in xrange(len(users)):
            rv = self.client.delete(path='/user',
                                    headers=self.get_headers(
                                        users[i]['username'],
                                        users[i]['password']))
            self.assertEqual(rv.status_code, 204)

        # try to get users
        for i in xrange(len(users)):
            rv = self.client.get(path='/user',
                                 headers=self.get_headers(
                                     users[i]['username'],
                                     users[i]['password']))
            self.assertEqual(rv.status_code, 302)
    def test_categories(self):
        # create users
        users = self.get_users()
        for user in users:
            User.generate_fake_user(user['username'], user['email'],
                                    user['password'])

        # create categories
        categories = self.get_categories()
        locations = []
        for category in categories:
            user = users[category['user_id'] - 1]
            rv = self.client.post(path='/category',
                                  data=json.dumps(category),
                                  headers=self.get_headers(
                                      user['username'], user['password']))
            self.assertEqual(rv.status_code, 201)
            locations.append(rv.headers['Location'])

        # try to add one of the categories once more
        user = users[categories[0]['user_id'] - 1]
        rv = self.client.post('/category',
                              data=json.dumps(categories[0]),
                              headers=self.get_headers(user['username'],
                                                       user['password']))
        self.assertEqual(rv.status_code, 400)

        # check the returned 'Location' links
        for i in xrange(len(locations)):
            user = users[categories[i]['user_id'] - 1]
            rv = self.client.get(path=locations[i],
                                 headers=self.get_headers(
                                     user['username'], user['password']))
            self.assertEqual(
                json.loads(rv.get_data())['name'], categories[i]['name'])

        # create a sensor with foreign key to categories[0]
        user = users[categories[0]['user_id'] - 1]
        sensor = self.get_sensors()[0]
        rv = self.client.post(path='/sensor',
                              data=json.dumps(sensor),
                              headers=self.get_headers(user['username'],
                                                       user['password']))
        self.assertEqual(rv.status_code, 201)
        sensor_location = rv.headers['Location']

        # get the sensors for categories[0]
        rv = self.client.get(path=locations[0],
                             headers=self.get_headers(user['username'],
                                                      user['password']))
        sensors_location = json.loads(rv.get_data())['links']['sensors']
        self.assertTrue(len(sensors_location), 1)
        self.assertEqual(sensors_location[0].split('/')[-2:],
                         sensor_location.split('/')[-2:])
        rv = self.client.get(path=sensor_location,
                             headers=self.get_headers(user['username'],
                                                      user['password']))
        data = json.loads(rv.get_data())
        self.assertEqual(data['name'], sensor['name'])

        # modify categories[0]
        category_mod = {'name': 'humi'}
        rv = self.client.put(path=locations[0],
                             data=json.dumps(category_mod),
                             headers=self.get_headers(user['username'],
                                                      user['password']))
        data = json.loads(rv.get_data())
        self.assertEqual(rv.status_code, 201)
        self.assertEqual(data['id'], int(locations[0].split('/')[-1]))
        self.assertEqual(data['name'], category_mod['name'])

        # delete categories
        username = user['username']
        password = user['password']
        for i in xrange(len(locations)):
            user = users[categories[i]['user_id'] - 1]
            rv = self.client.delete(path=locations[i],
                                    headers=self.get_headers(
                                        user['username'], user['password']))
            self.assertEqual(rv.status_code, 204)

        # try to get sensor
        rv = self.client.get(path=sensor_location,
                             headers=self.get_headers(username, password))
        self.assertEqual(rv.status_code, 400)
    def test_views(self):
        # create users
        users = self.get_users()
        for user in users:
            User.generate_fake_user(user['username'], user['email'],
                                    user['password'])

        # create categories
        categories = self.get_categories()
        for category in categories:
            user = users[category['user_id'] - 1]
            self.client.post(path='/category',
                             data=json.dumps(category),
                             headers=self.get_headers(user['username'],
                                                      user['password']))

        # create sensors
        sensors = self.get_sensors()
        for sensor in sensors:
            user = users[categories[sensor['category_id'] - 1]['user_id'] - 1]
            self.client.post(path='/sensor',
                             data=json.dumps(sensor),
                             headers=self.get_headers(user['username'],
                                                      user['password']))

        # create chartconfigs
        create_chartconfigs()

        # create views
        views = self.get_views()
        locations = []

        for view in views:
            user = users[view['user_id'] - 1]
            rv = self.client.post(path='/view',
                                  data=json.dumps(view),
                                  headers=self.get_headers(
                                      user['username'], user['password']))
            self.assertEqual(rv.status_code, 201)
            locations.append(rv.headers['Location'])

        # try to create view with 'count' > 4
        user = users[0]
        rv = self.client.post(path='/view',
                              data=json.dumps({'count': 5}),
                              headers=self.get_headers(user['username'],
                                                       user['password']))
        self.assertEqual(rv.status_code, 400)

        # try to create view with 'count' not in [1, 2, 4]
        rv = self.client.post(path='/view',
                              data=json.dumps({'count': 3}),
                              headers=self.get_headers(user['username'],
                                                       user['password']))
        self.assertEqual(rv.status_code, 400)

        # create subviews
        subviews = self.get_subviews()
        subview_locations = []

        for subview in subviews:
            user = users[categories[sensors[subview['sensor_id'] -
                                            1]['category_id'] - 1]['user_id'] -
                         1]
            rv = self.client.post(path='/subview',
                                  data=json.dumps(subview),
                                  headers=self.get_headers(
                                      user['username'], user['password']))
            subview_locations.append(rv.headers['Location'])

        # check the returned 'Location' links
        expected_icons = ['2x1', '1x1']

        for i in xrange(len(locations)):
            user = users[views[i]['user_id'] - 1]
            rv = self.client.get(path=locations[i],
                                 headers=self.get_headers(
                                     user['username'], user['password']))
            data = json.loads(rv.get_data())
            self.assertEqual(data['count'], views[i]['count'])
            self.assertEqual(len(data['links']['subviews']), views[i]['count'])
            self.assertTrue(expected_icons[i] in data['links']['icon'])

        # modify views[0]
        user = users[views[0]['user_id'] - 1]
        views[0]['name'] = 'modifiedview'
        views[0]['count'] = 4
        rv = self.client.put(path=locations[0],
                             data=json.dumps(views[0]),
                             headers=self.get_headers(user['username'],
                                                      user['password']))
        self.assertEqual(rv.status_code, 201)
        data = json.loads(rv.get_data())
        self.assertEqual(data['name'], 'modifiedview')
        self.assertEqual(data['count'], 4)
        self.assertTrue('2x2' in data['links']['icon'])

        # try to modify the 'count' in views[1]
        user = users[views[1]['user_id'] - 1]
        views[1]['count'] = 1
        rv = self.client.put(path=locations[1],
                             data=json.dumps(views[1]),
                             headers=self.get_headers(user['username'],
                                                      user['password']))
        self.assertEqual(rv.status_code, 400)

        views[1]['count'] = 3
        rv = self.client.put(path=locations[1],
                             data=json.dumps(views[1]),
                             headers=self.get_headers(user['username'],
                                                      user['password']))
        self.assertEqual(rv.status_code, 400)

        views[1]['count'] = 5
        rv = self.client.put(path=locations[1],
                             data=json.dumps(views[1]),
                             headers=self.get_headers(user['username'],
                                                      user['password']))
        self.assertEqual(rv.status_code, 400)

        # try to modify the 'refresh_time' in views[0]
        user = users[views[0]['user_id'] - 1]
        views[0]['refresh_time'] = 9
        rv = self.client.put(path=locations[0],
                             data=json.dumps(views[0]),
                             headers=self.get_headers(user['username'],
                                                      user['password']))
        self.assertEqual(rv.status_code, 400)

        views[0]['refresh_time'] = 61
        rv = self.client.put(path=locations[0],
                             data=json.dumps(views[0]),
                             headers=self.get_headers(user['username'],
                                                      user['password']))
        self.assertEqual(rv.status_code, 400)

        # remove views
        for i in xrange(len(locations)):
            user = users[views[i]['user_id'] - 1]
            rv = self.client.delete(path=locations[i],
                                    headers=self.get_headers(
                                        user['username'], user['password']))
            self.assertEqual(rv.status_code, 204)

        # check if subviews were removed
        for i in xrange(len(subview_locations)):
            user = users[categories[sensors[subviews[i]['sensor_id'] -
                                            1]['category_id'] - 1]['user_id'] -
                         1]
            rv = self.client.get(path=subview_locations[i],
                                 headers=self.get_headers(
                                     user['username'], user['password']))
            self.assertEqual(rv.status_code, 400)
    def test_subviews(self):
        # create users
        users = self.get_users()
        for user in users:
            User.generate_fake_user(user['username'], user['email'],
                                    user['password'])

        # create categories
        categories = self.get_categories()
        for category in categories:
            user = users[category['user_id'] - 1]
            self.client.post(path='/category',
                             data=json.dumps(category),
                             headers=self.get_headers(user['username'],
                                                      user['password']))

        # create sensors
        sensors = self.get_sensors()
        for sensor in sensors:
            user = users[categories[sensor['category_id'] - 1]['user_id'] - 1]
            self.client.post(path='/sensor',
                             data=json.dumps(sensor),
                             headers=self.get_headers(user['username'],
                                                      user['password']))

        # create chartconfigs
        create_chartconfigs()

        # create views
        views = self.get_views()
        for view in views:
            user = users[view['user_id'] - 1]
            self.client.post(path='/view',
                             data=json.dumps(view),
                             headers=self.get_headers(user['username'],
                                                      user['password']))

        # create subviews
        subviews = self.get_subviews()
        locations = []

        for subview in subviews:
            user = users[categories[sensors[subview['sensor_id'] -
                                            1]['category_id'] - 1]['user_id'] -
                         1]
            rv = self.client.post(path='/subview',
                                  data=json.dumps(subview),
                                  headers=self.get_headers(
                                      user['username'], user['password']))
            self.assertEqual(rv.status_code, 201)
            locations.append(rv.headers['Location'])

        # check the returned 'Location' links
        for i in xrange(len(locations)):
            user = users[categories[sensors[subviews[i]['sensor_id'] -
                                            1]['category_id'] - 1]['user_id'] -
                         1]
            rv = self.client.get(path=locations[i],
                                 headers=self.get_headers(
                                     user['username'], user['password']))
            data = json.loads(rv.get_data())
            self.assertEqual(data['sensor_id'], subviews[i]['sensor_id'])
            self.assertEqual(data['chartconfig_id'],
                             subviews[i]['chartconfig_id'])

        # modify subviews[0]
        user = users[categories[sensors[subviews[0]['sensor_id'] -
                                        1]['category_id'] - 1]['user_id'] - 1]
        subviews[0]['sensor_id'] = 2
        subviews[0]['chartconfig_id'] = 4
        rv = self.client.put(path=locations[0],
                             data=json.dumps(subviews[0]),
                             headers=self.get_headers(user['username'],
                                                      user['password']))
        self.assertEqual(rv.status_code, 201)
        data = json.loads(rv.get_data())
        self.assertEqual(data['sensor_id'], 2)
        self.assertEqual(data['chartconfig_id'], 4)
        sensor_location = data['links']['sensor']
        chartconfig_location = data['links']['chartconfig']

        # remove subviews[0]
        rv = self.client.delete(path=locations[0],
                                data=json.dumps(subviews[0]),
                                headers=self.get_headers(
                                    user['username'], user['password']))
        self.assertEqual(rv.status_code, 204)
        rv = self.client.get(path=locations[0],
                             data=json.dumps(subviews[0]),
                             headers=self.get_headers(user['username'],
                                                      user['password']))
        self.assertEqual(rv.status_code, 400)

        # check if sensor and chartconfig are still there
        rv = self.client.get(path=sensor_location,
                             headers=self.get_headers(user['username'],
                                                      user['password']))
        self.assertEqual(rv.status_code, 200)
        rv = self.client.get(path=chartconfig_location,
                             headers=self.get_headers(user['username'],
                                                      user['password']))
        self.assertEqual(rv.status_code, 200)
    def test_sensors(self):
        users = self.get_users()
        for user in users:
            User.generate_fake_user(user['username'], user['email'],
                                    user['password'])

        # create categories
        categories = self.get_categories()
        for category in categories:
            user = users[category['user_id'] - 1]
            self.client.post(path='/category',
                             data=json.dumps(category),
                             headers=self.get_headers(user['username'],
                                                      user['password']))

        # create sensors
        sensors = self.get_sensors()
        locations = []
        category_locations = []

        for sensor in sensors:
            user = users[categories[sensor['category_id'] - 1]['user_id'] - 1]
            rv = self.client.post(path='/sensor',
                                  data=json.dumps(sensor),
                                  headers=self.get_headers(
                                      user['username'], user['password']))
            self.assertEqual(rv.status_code, 201)
            locations.append(rv.headers['Location'])
            category_locations.append(
                json.loads(rv.get_data())['links']['category'])

        # check the returned 'Location' links
        for i in xrange(len(locations)):
            user = users[categories[sensors[i]['category_id'] - 1]['user_id'] -
                         1]
            rv = self.client.get(path=locations[i],
                                 headers=self.get_headers(
                                     user['username'], user['password']))
            data = json.loads(rv.get_data())
            for key in sensors[i].iterkeys():
                self.assertTrue(key in data.keys())
                self.assertEqual(data[key], sensors[i][key])

        # fetch sensors in every category
        for i in xrange(len(category_locations)):
            user = users[categories[i]['user_id'] - 1]
            rv = self.client.get(path=category_locations[i],
                                 headers=self.get_headers(
                                     user['username'], user['password']))
            data = json.loads(rv.get_data())['links']['sensors']
            self.assertEqual(len(data), 1)
            self.assertTrue(('/').join(data[0].split('/')[-2:]) in [(
                '/').join(location.split('/')[-2:]) for location in locations])

        # create a data with foreign key to sensors[0]
        user = users[categories[sensors[0]['category_id'] - 1]['user_id'] - 1]
        data = self.get_datas()[0]
        rv = self.client.post(path='/data',
                              data=json.dumps(data),
                              headers=self.get_headers(user['username'],
                                                       user['password']))
        self.assertEqual(rv.status_code, 201)
        data_location = rv.headers['Location']

        # modify sensors[0]
        username = user['username']
        password = user['password']
        sensors[0]['name'] = 'humi'
        sensors[0]['location'] = 'New York'
        rv = self.client.put(path=locations[0],
                             data=json.dumps(sensors[0]),
                             headers=self.get_headers(user['username'],
                                                      user['password']))
        data = json.loads(rv.get_data())
        self.assertEqual(rv.status_code, 201)
        self.assertEqual(data['id'], int(locations[0].split('/')[-1]))
        self.assertEqual(data['name'], 'humi')
        self.assertEqual(data['location'], 'New York')
        self.assertEqual(data['ipv4_addr'], sensors[0]['ipv4_addr'])

        # delete sensors
        for i in xrange(len(locations)):
            user = users[categories[sensors[i]['category_id'] - 1]['user_id'] -
                         1]
            rv = self.client.delete(path=locations[i],
                                    headers=self.get_headers(
                                        user['username'], user['password']))
            self.assertEqual(rv.status_code, 204)

        # try to get data
        rv = self.client.get(path=data_location,
                             headers=self.get_headers(username, password))
        self.assertEqual(rv.status_code, 400)
    def test_datas(self):
        # create users
        users = self.get_users()
        for user in users:
            User.generate_fake_user(user['username'], user['email'],
                                    user['password'])

        # create categories
        categories = self.get_categories()
        for category in categories:
            user = users[category['user_id'] - 1]
            self.client.post(path='/category',
                             data=json.dumps(category),
                             headers=self.get_headers(user['username'],
                                                      user['password']))

        # create sensors
        sensors = self.get_sensors()
        for sensor in sensors:
            user = users[categories[sensor['category_id'] - 1]['user_id'] - 1]
            self.client.post(path='/sensor',
                             data=json.dumps(sensor),
                             headers=self.get_headers(user['username'],
                                                      user['password']))

        # create datas
        datas = self.get_datas()
        locations = []
        sensor_locations = []

        for data in datas:
            user = users[categories[sensors[data['sensor_id'] -
                                            1]['category_id'] - 1]['user_id'] -
                         1]
            rv = self.client.post(path='/data',
                                  data=json.dumps(data),
                                  headers=self.get_headers(
                                      user['username'], user['password']))
            self.assertEqual(rv.status_code, 201)
            locations.append(rv.headers['Location'])
            sensor_locations.append(
                json.loads(rv.get_data())['links']['sensor'])

        # check the returned 'Location' links
        for i in xrange(len(locations)):
            user = users[categories[sensors[datas[i]['sensor_id'] -
                                            1]['category_id'] - 1]['user_id'] -
                         1]
            rv = self.client.get(path=locations[i],
                                 headers=self.get_headers(
                                     user['username'], user['password']))
            data = json.loads(rv.get_data())
            for key in datas[i].iterkeys():
                self.assertTrue(key in data.keys())
                self.assertEqual(data[key], datas[i][key])

        # fetch datas for every sensor
        for i in xrange(len(sensor_locations)):
            user = users[categories[sensors[datas[i]['sensor_id'] -
                                            1]['category_id'] - 1]['user_id'] -
                         1]
            rv = self.client.get(path=sensor_locations[i],
                                 headers=self.get_headers(
                                     user['username'], user['password']))
            data = json.loads(rv.get_data())['links']['datas']
            self.assertEqual(len(data), 1)
            self.assertTrue(('/').join(data[0].split('/')[-2:]) in [(
                '/').join(location.split('/')[-2:]) for location in locations])