Esempio n. 1
0
def test_set_camera_data(client, enabled):
    """
    Verifies that setting the enabled state of the camera sets the state correctly.

    :param FlaskClient client: The flask client to use for the test.
    :param str enabled: The camera enabled state
    """

    camera_name = 'Test-Camera'
    expected_enabled = enabled in ('true', 'True')

    with client:
        response = client.post('/api/camera/',
                               data={
                                   'api_key':
                                   Config.get_user_config('test')['api_key'],
                                   'name':
                                   camera_name,
                                   'enabled':
                                   enabled
                               })

        assert response.status_code == HTTPStatus.OK

        response = client.get('/api/camera/',
                              data={
                                  'api_key':
                                  Config.get_user_config('test')['api_key'],
                                  'name':
                                  camera_name
                              })

        assert response.status_code == HTTPStatus.OK
        assert response.json[
            'camera_enabled'] == expected_enabled  # The current state of the camera
Esempio n. 2
0
def test_post_two_camera_names(client):
    """
    Verifies that posting two different camera names works also fine and that two cameras are then stored
    in config.

    :param FlaskClient client: The flask client to use for the test.
    """
    with client:
        response = client.post('/api/camera/',
                               data={
                                   'api_key':
                                   Config.get_user_config('test')['api_key'],
                                   'name':
                                   'Camera1',
                                   'enabled':
                                   True
                               })

        assert response.status_code == HTTPStatus.OK

        response = client.post('/api/camera/',
                               data={
                                   'api_key':
                                   Config.get_user_config('test')['api_key'],
                                   'name':
                                   'Camera2',
                                   'enabled':
                                   True
                               })

        assert response.status_code == HTTPStatus.OK

        assert len(Config.get_connected_cameras()) == 2
Esempio n. 3
0
def test_api_key_regeneration(client, auth):
    """
    Tests that logged in user sees a proper page if no images are available.

    :param FlaskClient client: The client to run the test with.
    :param AuthActions auth: The authentication object to use for login.
    """
    auth.login()

    with client:
        # Get the api key before update
        old_api_key = Config.get_user_config('test')['api_key']

        response = client.get('/api_key/regenerate_key')

        assert response.status_code == HTTPStatus.FOUND
        assert urlparse(response.location).path == '/api_key/'

        # Check update of configuration
        new_api_key = Config.get_user_config('test')['api_key']
        assert new_api_key != old_api_key
        assert g.user['api_key'] == new_api_key

        # Check that new api key is also shown on redirected page
        response = client.get(response.location)
        assert new_api_key.encode('utf-8') in response.data
Esempio n. 4
0
    def post(self):
        """
        :return: "Success" on success
        :rtype: str
        """
        # This will also check that mandatory file key is available in arguments and stop execution if not.
        args = camera_enabled_parser.parse_args()

        Config.set_camera_info(args.name, args.enabled in ("true", "True"))
        return "Success"
Esempio n. 5
0
    def get(self):
        # This will also check that mandatory file key is available in arguments and stop execution if not.
        args = camera_name_parser.parse_args()

        cameras = Config.get_connected_cameras()
        if args.name in cameras:
            return cameras[args.name]

        return {"enabled": False}
def test_delete_camera(client, auth):
    """
    Verifies that deletion of a camera removes the camera.

    :param FlaskClient client: The client to run the test with.
    :param AuthActions auth: The authentication object to use for login.
    """
    auth.login()

    with client:
        Config.set_camera_info(CAMERA_NAME, True)

        response = client.get('/cameras/?name=' + CAMERA_NAME + '&action=delete',
                              follow_redirects=True)

        assert response.status_code == HTTPStatus.OK
        assert CAMERA_NAME.encode() not in response.data
        assert CAMERA_NAME not in Config.get_connected_cameras()
def test_disable_camera(client, auth):
    """
    Verifies that disabling a camera sets the correct settings.

    :param FlaskClient client: The client to run the test with.
    :param AuthActions auth: The authentication object to use for login.
    """
    auth.login()

    with client:
        Config.set_camera_info(CAMERA_NAME, True)

        response = client.get('/cameras/?name=' + CAMERA_NAME + '&enable=false',
                              follow_redirects=True)

        assert response.status_code == HTTPStatus.OK
        assert CAMERA_NAME.encode() in response.data
        assert b'enabled' in response.data.lower()

        assert Config.get_connected_cameras()[CAMERA_NAME]['enabled'] == False
def test_camera_available_enabled_connection_pending(client, auth):
    """
    Verifies that if a camera is enabled and there was no connection for
    quite a long time, it is displayed properly.

    :param FlaskClient client: The client to run the test with.
    :param AuthActions auth: The authentication object to use for login.
    """
    auth.login()

    with client:
        Config.set_camera_info(CAMERA_NAME, True)
        test_timestamp = datetime(2020, 2, 1, 10, 15, 0, tzinfo=timezone.utc)
        update_camera_timestamp(test_timestamp)

        response = client.get('/cameras/')

        assert CAMERA_NAME.encode() in response.data
        assert test_timestamp.strftime('%Y-%m-%d %H:%M:%S UTC').encode() in response.data
        assert b'enabled' in response.data.lower()
        assert b'connection_pending' in response.data.lower()
def test_camera_available_disabled_connection_up_to_date(client, auth):
    """
    Verifies that if a camera is disabled and there was a recent connection,
    it is displayed properly.

    :param FlaskClient client: The client to run the test with.
    :param AuthActions auth: The authentication object to use for login.
    """
    auth.login()

    with client:
        Config.set_camera_info(CAMERA_NAME, False)
        test_timestamp = datetime.now(tz=timezone.utc)
        update_camera_timestamp(test_timestamp)

        response = client.get('/cameras/')

        assert CAMERA_NAME.encode() in response.data
        assert test_timestamp.strftime('%Y-%m-%d %H:%M:%S UTC').encode() in response.data
        assert b'disabled' in response.data.lower()
        assert b'connection_pending' not in response.data.lower()
Esempio n. 10
0
def test_upload_missing_file(client):
    """
    Verifies that the correct error message is shown if the api key is valid, but the uploaded file is missing.

    :param FlaskClient client: The flask client to use for the test.
    """

    with client:
        response = client.post(
            '/api/picture/', data={'api_key': Config.get_user_config('test')['api_key']})

        assert response.status_code == HTTPStatus.BAD_REQUEST
        assert b'missing' in response.data.lower() and b'file' in response.data
Esempio n. 11
0
def test_update_camera_data(client):
    """
    Verifies that updating the camera data for a single camera updates the data correctly.

    :param FlaskClient client: The flask client to use for the test.
    """

    camera_name = 'Test-Camera'

    with client:
        response = client.post('/api/camera/',
                               data={
                                   'api_key':
                                   Config.get_user_config('test')['api_key'],
                                   'name':
                                   camera_name,
                                   'enabled':
                                   False
                               })

        assert response.status_code == HTTPStatus.OK
        assert Config.get_connected_cameras(
        )[camera_name]['camera_enabled'] == False

        response = client.post('/api/camera/',
                               data={
                                   'api_key':
                                   Config.get_user_config('test')['api_key'],
                                   'name':
                                   camera_name,
                                   'enabled':
                                   True
                               })

        assert response.status_code == HTTPStatus.OK
        assert Config.get_connected_cameras(
        )[camera_name]['camera_enabled'] == True
Esempio n. 12
0
def test_upload_invalid_file(client):
    """
    Verifies that the correct error message is shown if the api key is valid, but the uploaded file is of wrong type.

    :param FlaskClient client: The flask client to use for the test.
    """

    with client:
        response = client.post('/api/picture/', data={
            'api_key': Config.get_user_config('test')['api_key'],
            'file': (io.BytesIO(b"test"), 'test.txt')
        })

        assert response.status_code == HTTPStatus.BAD_REQUEST
        assert b'invalid file type' in response.data.lower() and b'file' in response.data
Esempio n. 13
0
def test_upload_thumbnail_creation_failure(client):
    """
    Test thumbnail creation with an invalid byte stream that is not a valid picture.
    The creation should fail.

    :param FlaskClient client: The flask client to use for the test.
    """
    with client:
        response = client.post('/api/picture/', data={
            'api_key': Config.get_user_config('test')['api_key'],
            'file': (io.BytesIO(b"test"), 'test.jpg')
        })

        assert response.status_code == HTTPStatus.INTERNAL_SERVER_ERROR
        assert b'thumbnail' in response.data.lower()
Esempio n. 14
0
def test_get_missing_name(client):
    """
    Verifies that the correct error message is shown if camera name is missing for get requests.

    :param FlaskClient client: The flask client to use for the test.
    """

    with client:
        response = client.get(
            '/api/camera/',
            data={'api_key': Config.get_user_config('test')['api_key']})

        assert response.status_code == HTTPStatus.BAD_REQUEST
        assert b'name' in response.data.lower()
        assert b'camera' in response.data.lower()
        assert b'missing' in response.data.lower()
Esempio n. 15
0
def test_api_key_login(client, auth):
    """
    Tests that logged in user sees a proper page if no images are available.

    :param FlaskClient client: The client to run the test with.
    :param AuthActions auth: The authentication object to use for login.
    """
    auth.login()

    with client:
        response = client.get('/api_key/')

        # Now we should see the api key and a regeneration information
        api_key = Config.get_user_config('test')["api_key"]

        assert api_key.encode('utf-8') in response.data
        assert b'regenerate' in response.data.lower()
Esempio n. 16
0
def test_upload_valid_file(client, file_type):
    """
    Tests uploading of a valid jpg and png file. Both should succeed.

    :param FlaskClient client: The flask client to use for the test.
    :param str file_type: The file type to upload.
    """
    with client:
        test_file = os.path.join(os.path.dirname(
            __file__), 'test_data', 'test.{}'.format(file_type))
        response = client.post('/api/picture/', data={
            'api_key': Config.get_user_config('test')['api_key'],
            'file': (open(test_file, 'rb'), 'test.{}'.format(file_type))
        })

        assert response.status_code == HTTPStatus.OK
        assert response.data.decode('utf-8').strip() == '"Success"'
Esempio n. 17
0
def run_upload_test(client):
    """
    Will execute the uploading of a single file and return the response.
    Used in test_upload_verify_fast.

    :param FlaskClient client: The flask client to use for the test.
    :returns: The upload response.
    """
    test_file = os.path.join(os.path.dirname(__file__),
                             'test_data', 'test.jpg')

    with open(test_file, 'rb') as bin_data:
        # First request should succeed
        response = client.post('/api/picture/', data={
            'api_key': Config.get_user_config('test')['api_key'],
            'file': (bin_data, 'test.jpg')
        })
        return response
Esempio n. 18
0
def test_get_unknown_camera(client):
    """
    Verifies that the correct error message is shown if an unknown camera should be requested.

    :param FlaskClient client: The flask client to use for the test.
    """

    with client:
        response = client.get('/api/camera/',
                              data={
                                  'api_key':
                                  Config.get_user_config('test')['api_key'],
                                  'name':
                                  'Unknown_Camera'
                              })

        assert response.status_code == HTTPStatus.OK
        assert response.json[
            'enabled'] == False  # The 'should' state sent by the server