Skip to content

Commit

Permalink
Add more automated tests to the filebrowser app
Browse files Browse the repository at this point in the history
  • Loading branch information
jbernal0019 committed Mar 14, 2024
1 parent 09d1c63 commit c491903
Showing 1 changed file with 232 additions and 3 deletions.
235 changes: 232 additions & 3 deletions chris_backend/filebrowser/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,67 @@ def test_filebrowserfolderfile_list_file_folder_failure_not_found_unauthenticate
response = self.client.get(read_url)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

def test_fileBrowserfile_list_success_shared_feed(self):
other_user = User.objects.get(username=self.other_username)
plugin = self.plugin

# create a feed by creating a "fs" plugin instance
pl_inst = PluginInstance.objects.create(plugin=plugin, owner=other_user,
title='test',
compute_resource=
plugin.compute_resources.all()[0])
pl_inst.feed.name = 'shared_feed'
pl_inst.feed.save()
pl_inst.feed.owner.add(User.objects.get(username=self.username))

file_path = f'{pl_inst.output_folder.path}/file3.txt'
with io.StringIO("test file") as file3:
self.storage_manager.upload_obj(file_path, file3.read(),
content_type='text/plain')

userfile = UserFile(owner=other_user, parent_folder=pl_inst.output_folder)
userfile.fname.name = file_path
userfile.save()

read_url = reverse("chrisfolder-file-list",
kwargs={"pk": pl_inst.output_folder.id})
self.client.login(username=self.username, password=self.password)
response = self.client.get(read_url)
self.assertContains(response, file_path)

self.storage_manager.delete_obj(file_path)

def test_fileBrowserfile_list_failure_not_found_shared_feed_unauthenticated(self):
other_user = User.objects.get(username=self.other_username)
plugin = self.plugin

# create a feed by creating a "fs" plugin instance
pl_inst = PluginInstance.objects.create(plugin=plugin, owner=other_user,
title='test',
compute_resource=
plugin.compute_resources.all()[0])
pl_inst.feed.name = 'shared_feed'
pl_inst.feed.save()
pl_inst.feed.owner.add(User.objects.get(username=self.username))

file_path = f'{pl_inst.output_folder.path}/file3.txt'
with io.StringIO("test file") as file3:
self.storage_manager.upload_obj(file_path, file3.read(),
content_type='text/plain')

userfile = UserFile(owner=other_user, parent_folder=pl_inst.output_folder)
userfile.fname.name = file_path
userfile.save()

read_url = reverse("chrisfolder-file-list",
kwargs={"pk": pl_inst.output_folder.id})

response = self.client.get(read_url)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

self.storage_manager.delete_obj(file_path)


class FileBrowserFolderLinkFileListViewTests(FileBrowserViewTests):
"""
Test the 'chrisfolder-linkfile-list' view.
Expand All @@ -812,6 +873,8 @@ class FileBrowserFolderLinkFileListViewTests(FileBrowserViewTests):
def setUp(self):
super(FileBrowserFolderLinkFileListViewTests, self).setUp()

self.storage_manager = connect_storage(settings)

# create compute resource
(compute_resource, tf) = ComputeResource.objects.get_or_create(
name="host", compute_url=COMPUTE_RESOURCE_URL)
Expand Down Expand Up @@ -839,7 +902,6 @@ def setUp(self):

def tearDown(self):
super(FileBrowserFolderLinkFileListViewTests, self).tearDown()
self.storage_manager = connect_storage(settings)
self.storage_manager.delete_obj(self.link_path)

def test_filebrowserfolderlinkfile_list_success(self):
Expand Down Expand Up @@ -902,6 +964,59 @@ def test_filebrowserfolderlinkfile_list_file_folder_failure_not_found_unauthenti
response = self.client.get(read_url)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

def test_fileBrowserlinkfile_list_success_shared_feed(self):
other_user = User.objects.get(username=self.other_username)
plugin = self.plugin

# create a feed by creating a "fs" plugin instance
pl_inst = PluginInstance.objects.create(plugin=plugin, owner=other_user,
title='test',
compute_resource=
plugin.compute_resources.all()[0])
pl_inst.feed.name = 'shared_feed'
pl_inst.feed.save()
pl_inst.feed.owner.add(User.objects.get(username=self.username))

# create link file in the output folder
link_path = f'{pl_inst.output_folder.path}/SERVICES_PACS.chrislink'

link_file = ChrisLinkFile(path='SERVICES/PACS', owner=other_user,
parent_folder=pl_inst.output_folder)
link_file.save(name='SERVICES_PACS')

read_url = reverse("chrisfolder-linkfile-list", kwargs={"pk": pl_inst.output_folder.id})
self.client.login(username=self.username, password=self.password)
response = self.client.get(read_url)
self.assertContains(response, link_path)

self.storage_manager.delete_obj(link_path)

def test_fileBrowserlinkfile_list_failure_not_found_shared_feed_unauthenticated(self):
other_user = User.objects.get(username=self.other_username)
plugin = self.plugin

# create a feed by creating a "fs" plugin instance
pl_inst = PluginInstance.objects.create(plugin=plugin, owner=other_user,
title='test',
compute_resource=
plugin.compute_resources.all()[0])
pl_inst.feed.name = 'shared_feed'
pl_inst.feed.save()
pl_inst.feed.owner.add(User.objects.get(username=self.username))

# create link file in the output folder
link_path = f'{pl_inst.output_folder.path}/SERVICES_PACS.chrislink'

link_file = ChrisLinkFile(path='SERVICES/PACS', owner=other_user,
parent_folder=pl_inst.output_folder)
link_file.save(name='SERVICES_PACS')

read_url = reverse("chrisfolder-linkfile-list", kwargs={"pk": pl_inst.output_folder.id})
response = self.client.get(read_url)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

self.storage_manager.delete_obj(link_path)

class FileBrowserLinkFileDetailViewTests(FileBrowserViewTests):
"""
Test the chrislinkfile-detail view.
Expand All @@ -910,6 +1025,8 @@ class FileBrowserLinkFileDetailViewTests(FileBrowserViewTests):
def setUp(self):
super(FileBrowserLinkFileDetailViewTests, self).setUp()

self.storage_manager = connect_storage(settings)

# create compute resource
(compute_resource, tf) = ComputeResource.objects.get_or_create(
name="host", compute_url=COMPUTE_RESOURCE_URL)
Expand Down Expand Up @@ -940,7 +1057,6 @@ def setUp(self):

def tearDown(self):
super(FileBrowserLinkFileDetailViewTests, self).tearDown()
self.storage_manager = connect_storage(settings)
self.storage_manager.delete_obj(self.link_path)

def test_fileBrowserlinkfile_detail_success(self):
Expand All @@ -967,6 +1083,59 @@ def test_fileBrowserlinkfile_detail_success_public_feed_unauthenticated(self):
response = self.client.get(self.read_url)
self.assertContains(response, self.link_path)

def test_fileBrowserlinkfile_detail_success_shared_feed(self):
other_user = User.objects.get(username=self.other_username)
plugin = self.plugin

# create a feed by creating a "fs" plugin instance
pl_inst = PluginInstance.objects.create(plugin=plugin, owner=other_user,
title='test',
compute_resource=
plugin.compute_resources.all()[0])
pl_inst.feed.name = 'shared_feed'
pl_inst.feed.save()
pl_inst.feed.owner.add(User.objects.get(username=self.username))

# create link file in the output folder
link_path = f'{pl_inst.output_folder.path}/SERVICES_PACS.chrislink'

link_file = ChrisLinkFile(path='SERVICES/PACS', owner=other_user,
parent_folder=pl_inst.output_folder)
link_file.save(name='SERVICES_PACS')

read_url = reverse("chrislinkfile-detail", kwargs={"pk": link_file.id})
self.client.login(username=self.username, password=self.password)
response = self.client.get(read_url)
self.assertContains(response, link_path)

self.storage_manager.delete_obj(link_path)

def test_fileBrowserlinkfile_detail_failure_unauthorized_shared_feed_unauthenticated(self):
other_user = User.objects.get(username=self.other_username)
plugin = self.plugin

# create a feed by creating a "fs" plugin instance
pl_inst = PluginInstance.objects.create(plugin=plugin, owner=other_user,
title='test',
compute_resource=
plugin.compute_resources.all()[0])
pl_inst.feed.name = 'shared_feed'
pl_inst.feed.save()
pl_inst.feed.owner.add(User.objects.get(username=self.username))

# create link file in the output folder
link_path = f'{pl_inst.output_folder.path}/SERVICES_PACS.chrislink'

link_file = ChrisLinkFile(path='SERVICES/PACS', owner=other_user,
parent_folder=pl_inst.output_folder)
link_file.save(name='SERVICES_PACS')

read_url = reverse("chrislinkfile-detail", kwargs={"pk": link_file.id})
response = self.client.get(read_url)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

self.storage_manager.delete_obj(link_path)


class FileBrowserLinkFileResourceViewTests(FileBrowserViewTests):
"""
Expand All @@ -976,6 +1145,8 @@ class FileBrowserLinkFileResourceViewTests(FileBrowserViewTests):
def setUp(self):
super(FileBrowserLinkFileResourceViewTests, self).setUp()

self.storage_manager = connect_storage(settings)

# create compute resource
(compute_resource, tf) = ComputeResource.objects.get_or_create(
name="host", compute_url=COMPUTE_RESOURCE_URL)
Expand Down Expand Up @@ -1007,7 +1178,6 @@ def setUp(self):

def tearDown(self):
super(FileBrowserLinkFileResourceViewTests, self).tearDown()
self.storage_manager = connect_storage(settings)
self.storage_manager.delete_obj(self.link_path)

def test_fileBrowserlinkfile_resource_success(self):
Expand Down Expand Up @@ -1039,3 +1209,62 @@ def test_fileBrowserlinkfile_resource_success_public_feed_unauthenticated(self):
self.assertEqual(response.status_code, 200)
content = [c for c in response.streaming_content][0].decode('utf-8')
self.assertEqual(content, 'SERVICES/PACS')

def test_fileBrowserlinkfile_resource_success_shared_feed(self):
other_user = User.objects.get(username=self.other_username)
plugin = self.plugin

# create a feed by creating a "fs" plugin instance
pl_inst = PluginInstance.objects.create(plugin=plugin, owner=other_user,
title='test',
compute_resource=
plugin.compute_resources.all()[0])
pl_inst.feed.name = 'shared_feed'
pl_inst.feed.save()
pl_inst.feed.owner.add(User.objects.get(username=self.username))

# create link file in the output folder
link_path = f'{pl_inst.output_folder.path}/SERVICES_PACS.chrislink'

link_file = ChrisLinkFile(path='SERVICES/PACS', owner=other_user,
parent_folder=pl_inst.output_folder)
link_file.save(name='SERVICES_PACS')

download_url = reverse("chrislinkfile-resource",
kwargs={"pk": link_file.id}) + 'SERVICES_PACS.chrislink'

self.client.login(username=self.username, password=self.password)
response = self.client.get(download_url)
self.assertEqual(response.status_code, 200)
content = [c for c in response.streaming_content][0].decode('utf-8')
self.assertEqual(content, 'SERVICES/PACS')

self.storage_manager.delete_obj(link_path)

def test_fileBrowserlinkfile_resource_failure_unauthorized_shared_feed_unauthenticated(self):
other_user = User.objects.get(username=self.other_username)
plugin = self.plugin

# create a feed by creating a "fs" plugin instance
pl_inst = PluginInstance.objects.create(plugin=plugin, owner=other_user,
title='test',
compute_resource=
plugin.compute_resources.all()[0])
pl_inst.feed.name = 'shared_feed'
pl_inst.feed.save()
pl_inst.feed.owner.add(User.objects.get(username=self.username))

# create link file in the output folder
link_path = f'{pl_inst.output_folder.path}/SERVICES_PACS.chrislink'

link_file = ChrisLinkFile(path='SERVICES/PACS', owner=other_user,
parent_folder=pl_inst.output_folder)
link_file.save(name='SERVICES_PACS')

download_url = reverse("chrislinkfile-resource",
kwargs={"pk": link_file.id}) + 'SERVICES_PACS.chrislink'

response = self.client.get(download_url)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

self.storage_manager.delete_obj(link_path)

0 comments on commit c491903

Please sign in to comment.