Ejemplo n.º 1
0
    def test_validate_path_failure_does_not_exist(self):
        """
        Test whether overriden validate_path method validates that submitted path exists
        in internal storage.
        """
        pacsfiles_serializer = PACSFileSerializer()
        path = 'SERVICES/PACS/MyPACS/123456-Jorge/brain/brain_mri/file1.dcm'
        object_list = []
        container_data = ['', object_list]

        with mock.patch.object(swiftclient.Connection,
                               '__init__',
                               return_value=None) as conn_init_mock:
            with mock.patch.object(
                    swiftclient.Connection,
                    'get_container',
                    return_value=container_data) as conn_get_container_mock:
                with self.assertRaises(serializers.ValidationError):
                    pacsfiles_serializer.validate_path(path)
                conn_init_mock.assert_called_with(
                    user=settings.SWIFT_USERNAME,
                    key=settings.SWIFT_KEY,
                    authurl=settings.SWIFT_AUTH_URL)
                conn_get_container_mock.assert_called_with(
                    settings.SWIFT_CONTAINER_NAME, prefix=path)
 def test_integration_validate_path_failure_does_not_exist(self):
     """
     Test whether overriden validate_path method validates that submitted path exists
     in internal storage.
     """
     pacsfiles_serializer = PACSFileSerializer()
     path = 'SERVICES/PACS/MyPACS/123456-crazy/brain_crazy_study/SAG_T1_MPRAGE/file1.dcm'
     with self.assertRaises(serializers.ValidationError):
         pacsfiles_serializer.validate_path(path)
 def test_validate_path_failure_does_not_start_with_SERVICES_PACS(self):
     """
     Test whether overriden validate_path method validates submitted path must start
     with the 'SERVICES/PACS/' string.
     """
     pacsfiles_serializer = PACSFileSerializer()
     path = 'cube/123456-Jorge/brain/brain_mri/file1.dcm'
     with self.assertRaises(serializers.ValidationError):
         pacsfiles_serializer.validate_path(path)
     path = 'SERVICES/123456-Jorge/brain/brain_mri/file1.dcm'
     with self.assertRaises(serializers.ValidationError):
         pacsfiles_serializer.validate_path(path)
    def test_validate_path_failure_does_not_exist(self):
        """
        Test whether overriden validate_path method validates that submitted path exists
        in internal storage.
        """
        pacsfiles_serializer = PACSFileSerializer()
        path = 'SERVICES/PACS/MyPACS/123456-Jorge/brain/brain_mri/file1.dcm'

        with mock.patch.object(SwiftManager, 'obj_exists',
                               return_value=False) as obj_exists_mock:
            with self.assertRaises(serializers.ValidationError):
                pacsfiles_serializer.validate_path(path)
            obj_exists_mock.assert_called_with(path.strip(' ').strip('/'))
Ejemplo n.º 5
0
    def test_integration_validate_path_success(self):
        """
        Test whether overriden validate_path method validates submitted path.
        """
        pacsfiles_serializer = PACSFileSerializer()
        path = 'SERVICES/PACS/MyPACS/123456-crazy/brain_crazy_study/SAG_T1_MPRAGE/file1.dcm'
        # initiate a Swift service connection
        conn = swiftclient.Connection(
            user=settings.SWIFT_USERNAME,
            key=settings.SWIFT_KEY,
            authurl=settings.SWIFT_AUTH_URL,
        )
        # create container in case it doesn't already exist
        conn.put_container(settings.SWIFT_CONTAINER_NAME)

        # upload file to Swift storage
        with io.StringIO("test file") as file1:
            conn.put_object(settings.SWIFT_CONTAINER_NAME,
                            path,
                            contents=file1.read(),
                            content_type='text/plain')
        for _ in range(20):
            object_list = conn.get_container(settings.SWIFT_CONTAINER_NAME,
                                             prefix=path)[1]
            if object_list:
                break
            time.sleep(0.2)
        self.assertEqual(pacsfiles_serializer.validate_path(path), path)

        # delete file from Swift storage
        conn.delete_object(settings.SWIFT_CONTAINER_NAME, path)
    def test_integration_validate_path_success(self):
        """
        Test whether overriden validate_path method validates submitted path.
        """
        pacsfiles_serializer = PACSFileSerializer()
        path = 'SERVICES/PACS/MyPACS/123456-crazy/brain_crazy_study/SAG_T1_MPRAGE/file1.dcm'
        swift_manager = SwiftManager(settings.SWIFT_CONTAINER_NAME,
                                     settings.SWIFT_CONNECTION_PARAMS)
        # upload file to Swift storage
        with io.StringIO("test file") as file1:
            swift_manager.upload_obj(path,
                                     file1.read(),
                                     content_type='text/plain')

        for _ in range(20):
            if swift_manager.obj_exists(path):
                break
            time.sleep(0.2)
        self.assertEqual(pacsfiles_serializer.validate_path(path), path)

        # delete file from Swift storage
        swift_manager.delete_obj(path)