def test_serialize_preprint_file(self, preprint, primary_file): req = make_drf_request_with_version(version='2.2') data = FileSerializer(primary_file, context={ 'request': req }).data['data'] mfr_url = get_mfr_url(preprint, 'osfstorage') # Check render file link with path download_link = data['links']['download'] assert data['links']['render'] == build_expected_render_link( mfr_url, download_link, with_version=False) # Check render file link with guid primary_file.get_guid(create=True)._id req = make_drf_request_with_version() data = FileSerializer(primary_file, context={ 'request': req }).data['data'] download_link = data['links']['download'] assert data['links']['render'] == build_expected_render_link( mfr_url, download_link, with_version=False) # Check html link assert data['links']['html'] == '{}{}/files/osfstorage/{}'.format( settings.DOMAIN, preprint._id, primary_file._id)
def test_file_serializer(self, file_one): date_created = file_one.versions.first().date_created date_modified = file_one.versions.last().date_created date_created_tz_aware = date_created.replace(tzinfo=utc) date_modified_tz_aware = date_modified.replace(tzinfo=utc) new_format = '%Y-%m-%dT%H:%M:%S.%fZ' # test_date_modified_formats_to_old_format req = make_drf_request_with_version(version='2.0') data = FileSerializer(file_one, context={'request': req}).data['data'] assert date_modified_tz_aware == data['attributes']['date_modified'] # test_date_modified_formats_to_new_format req = make_drf_request_with_version(version='2.2') data = FileSerializer(file_one, context={'request': req}).data['data'] assert datetime.strftime( date_modified, new_format) == data['attributes']['date_modified'] # test_date_created_formats_to_old_format req = make_drf_request_with_version(version='2.0') data = FileSerializer(file_one, context={'request': req}).data['data'] assert date_created_tz_aware == data['attributes']['date_created'] # test_date_created_formats_to_new_format req = make_drf_request_with_version(version='2.2') data = FileSerializer(file_one, context={'request': req}).data['data'] assert datetime.strftime( date_created, new_format) == data['attributes']['date_created']
def to_representation(self, data, envelope='data'): if isinstance(data, AbstractNode): if data.is_registration: serializer = RegistrationSerializer(data, context=self.context) return RegistrationSerializer.to_representation( serializer, data) serializer = NodeSerializer(data, context=self.context) return NodeSerializer.to_representation(serializer, data) if isinstance(data, OSFUser): serializer = UserSerializer(data, context=self.context) return UserSerializer.to_representation(serializer, data) if isinstance(data, BaseFileNode): serializer = FileSerializer(data, context=self.context) return FileSerializer.to_representation(serializer, data) if isinstance(data, Institution): serializer = InstitutionSerializer(data, context=self.context) return InstitutionSerializer.to_representation(serializer, data) if isinstance(data, CollectionSubmission): serializer = CollectionSubmissionSerializer(data, context=self.context) return CollectionSubmissionSerializer.to_representation( serializer, data) return None
def test_no_node_relationship_after_version_2_7(self, file_one): req_2_7 = make_drf_request_with_version(version='2.7') data_2_7 = FileSerializer(file_one, context={ 'request': req_2_7 }).data['data'] assert 'node' in data_2_7['relationships'].keys() req_2_8 = make_drf_request_with_version(version='2.8') data_2_8 = FileSerializer(file_one, context={ 'request': req_2_8 }).data['data'] assert 'node' not in data_2_8['relationships'].keys()
def to_representation(self, data, envelope='data'): if isinstance(data, AbstractNode): if data.is_registration: serializer = RegistrationSerializer(data, context=self.context) return RegistrationSerializer.to_representation(serializer, data) serializer = NodeSerializer(data, context=self.context) return NodeSerializer.to_representation(serializer, data) if isinstance(data, OSFUser): serializer = UserSerializer(data, context=self.context) return UserSerializer.to_representation(serializer, data) if isinstance(data, BaseFileNode): serializer = FileSerializer(data, context=self.context) return FileSerializer.to_representation(serializer, data) if isinstance(data, Institution): serializer = InstitutionSerializer(data, context=self.context) return InstitutionSerializer.to_representation(serializer, data) if isinstance(data, CollectedGuidMetadata): serializer = CollectedMetaSerializer(data, context=self.context) return CollectedMetaSerializer.to_representation(serializer, data) return None
def test_draft_node_relationships(self, draft_node, draft_node_folder): # Ensure that the files relationship link is pointing to the correct root endpoint req = make_drf_request_with_version() data = FileSerializer(draft_node_folder, context={ 'request': req }).data['data'] assert 'draft_nodes' in data['relationships']['files']['links'][ 'related']['href']
def to_representation(self, data, envelope='data'): if isinstance(data, AbstractNode): if data.is_registration: serializer = RegistrationSerializer(data, context=self.context) return RegistrationSerializer.to_representation( serializer, data) serializer = NodeSerializer(data, context=self.context) return NodeSerializer.to_representation(serializer, data) if isinstance(data, User): serializer = UserSerializer(data, context=self.context) return UserSerializer.to_representation(serializer, data) if isinstance(data, FileNode): serializer = FileSerializer(data, context=self.context) return FileSerializer.to_representation(serializer, data) return None
def test_file_serializer(self, file_one): created = file_one.versions.last().created modified = file_one.versions.first().created created_tz_aware = created.replace(tzinfo=utc) modified_tz_aware = modified.replace(tzinfo=utc) new_format = '%Y-%m-%dT%H:%M:%S.%fZ' download_base = '/download/{}' path = file_one._id # test_date_modified_formats_to_old_format req = make_drf_request_with_version(version='2.0') data = FileSerializer(file_one, context={'request': req}).data['data'] assert modified_tz_aware == data['attributes']['date_modified'] # test_date_modified_formats_to_new_format req = make_drf_request_with_version(version='2.2') data = FileSerializer(file_one, context={'request': req}).data['data'] assert datetime.strftime( modified, new_format) == data['attributes']['date_modified'] # test_date_created_formats_to_old_format req = make_drf_request_with_version(version='2.0') data = FileSerializer(file_one, context={'request': req}).data['data'] assert created_tz_aware == data['attributes']['date_created'] # test_date_created_formats_to_new_format req = make_drf_request_with_version(version='2.2') data = FileSerializer(file_one, context={'request': req}).data['data'] assert datetime.strftime( created, new_format) == data['attributes']['date_created'] # check download file link with path assert download_base.format(path) in data['links']['download'] # check download file link with guid guid = file_one.get_guid(create=True)._id req = make_drf_request_with_version() data = FileSerializer(file_one, context={'request': req}).data['data'] assert download_base.format(guid) in data['links']['download']
def to_representation(self, data, envelope="data"): if isinstance(data, Node): if data.is_registration: serializer = RegistrationSerializer(data, context=self.context) return RegistrationSerializer.to_representation(serializer, data) serializer = NodeSerializer(data, context=self.context) return NodeSerializer.to_representation(serializer, data) if isinstance(data, User): serializer = UserSerializer(data, context=self.context) return UserSerializer.to_representation(serializer, data) if isinstance(data, FileNode): serializer = FileSerializer(data, context=self.context) return FileSerializer.to_representation(serializer, data) return None
def test_file_serializer(self, file_one, node): created = file_one.versions.last().created modified = file_one.versions.first().created created_tz_aware = created.replace(tzinfo=utc) modified_tz_aware = modified.replace(tzinfo=utc) new_format = '%Y-%m-%dT%H:%M:%S.%fZ' download_base = '/download/{}' path = file_one._id mfr_url = get_mfr_url(file_one, 'osfstorage') # test_date_modified_formats_to_old_format req = make_drf_request_with_version(version='2.0') data = FileSerializer(file_one, context={'request': req}).data['data'] assert modified_tz_aware == data['attributes']['date_modified'] # test_date_modified_formats_to_new_format req = make_drf_request_with_version(version='2.2') data = FileSerializer(file_one, context={'request': req}).data['data'] assert datetime.strftime( modified, new_format) == data['attributes']['date_modified'] # test_date_created_formats_to_old_format req = make_drf_request_with_version(version='2.0') data = FileSerializer(file_one, context={'request': req}).data['data'] assert created_tz_aware == data['attributes']['date_created'] # test_date_created_formats_to_new_format req = make_drf_request_with_version(version='2.2') data = FileSerializer(file_one, context={'request': req}).data['data'] assert datetime.strftime( created, new_format) == data['attributes']['date_created'] # check download file link with path assert download_base.format(path) in data['links']['download'] # check render file link with path assert download_base.format(path) in data['links']['render'] assert mfr_url in data['links']['render'] # check download file link with guid guid = file_one.get_guid(create=True)._id req = make_drf_request_with_version() data = FileSerializer(file_one, context={'request': req}).data['data'] assert download_base.format(guid) in data['links']['download'] # check render file link with guid assert download_base.format(guid) in data['links']['render'] assert mfr_url in data['links']['render'] # check html link in file serializer assert data['links']['html'] == '{}{}/files/osfstorage/{}'.format( settings.DOMAIN, node._id, file_one._id) # check download/render/html link for folder folder = node.get_addon('osfstorage').get_root().append_folder( 'Test_folder') folder.save() req = make_drf_request_with_version(version='2.2') data = FileSerializer(folder, context={'request': req}).data['data'] assert 'render' not in data['links'] assert 'download' not in data['links'] assert 'html' not in data['links']
def test_date_created_formats_to_new_format(self): req = make_drf_request_with_version(version='2.2') data = FileSerializer(self.file, context={'request': req}).data['data'] assert_equal(datetime.strftime(self.date_created, self.new_format), data['attributes']['date_created'])
def test_date_created_formats_to_old_format(self): req = make_drf_request_with_version(version='2.0') data = FileSerializer(self.file, context={'request': req}).data['data'] assert_equal(self.date_created_tz_aware, data['attributes']['date_created'])