2020-07-17 22:59:25 +00:00
|
|
|
from github import UnknownObjectException
|
|
|
|
from sqlalchemy import desc
|
2020-05-23 19:08:17 +00:00
|
|
|
from tests.base_test import BaseTest
|
2020-07-17 22:59:25 +00:00
|
|
|
from unittest.mock import patch, Mock
|
2020-06-04 13:49:42 +00:00
|
|
|
|
|
|
|
from crc import db
|
2020-07-17 22:59:25 +00:00
|
|
|
from crc.models.file import FileDataModel
|
2020-05-23 19:08:17 +00:00
|
|
|
from crc.services.file_service import FileService
|
|
|
|
from crc.services.workflow_processor import WorkflowProcessor
|
|
|
|
|
2020-07-17 22:59:25 +00:00
|
|
|
|
|
|
|
class FakeGithubCreates(Mock):
|
|
|
|
def get_user(var):
|
|
|
|
class FakeUser(Mock):
|
|
|
|
def get_repo(var, name):
|
|
|
|
class FakeRepo(Mock):
|
2020-08-14 17:04:22 +00:00
|
|
|
def get_contents(var, filename, ref):
|
2021-05-04 17:39:49 +00:00
|
|
|
raise UnknownObjectException(status='Failure', data='Failed data', headers=[])
|
2020-08-14 17:04:22 +00:00
|
|
|
def update_file(var, path, message, content, sha, branch):
|
2020-07-17 22:59:25 +00:00
|
|
|
pass
|
|
|
|
return FakeRepo()
|
|
|
|
return FakeUser()
|
|
|
|
|
|
|
|
|
|
|
|
class FakeGithub(Mock):
|
|
|
|
def get_user(var):
|
|
|
|
class FakeUser(Mock):
|
|
|
|
def get_repo(var, name):
|
|
|
|
class FakeRepo(Mock):
|
2020-08-14 17:04:22 +00:00
|
|
|
def get_contents(var, filename, ref):
|
2020-07-17 22:59:25 +00:00
|
|
|
fake_file = Mock()
|
|
|
|
fake_file.decoded_content = b'Some bytes'
|
|
|
|
fake_file.path = '/el/path/'
|
|
|
|
fake_file.data = 'Serious data'
|
|
|
|
fake_file.sha = 'Sha'
|
|
|
|
return fake_file
|
2020-08-14 17:04:22 +00:00
|
|
|
def get_branches(var):
|
|
|
|
branch1 = Mock()
|
|
|
|
branch1.name = 'branch1'
|
|
|
|
branch2 = Mock()
|
|
|
|
branch2.name = 'branch2'
|
|
|
|
master = Mock()
|
|
|
|
master.name = 'master'
|
|
|
|
return [branch1, branch2, master]
|
|
|
|
def update_file(var, path, message, content, sha, branch):
|
2020-07-17 22:59:25 +00:00
|
|
|
pass
|
|
|
|
return FakeRepo()
|
|
|
|
return FakeUser()
|
|
|
|
|
|
|
|
|
2020-05-23 19:08:17 +00:00
|
|
|
class TestFileService(BaseTest):
|
|
|
|
"""Largely tested via the test_file_api, and time is tight, but adding new tests here."""
|
|
|
|
|
|
|
|
def test_add_file_from_task_increments_version_and_replaces_on_subsequent_add(self):
|
|
|
|
self.load_example_data()
|
|
|
|
self.create_reference_document()
|
|
|
|
workflow = self.create_workflow('file_upload_form')
|
|
|
|
processor = WorkflowProcessor(workflow)
|
|
|
|
task = processor.next_task()
|
|
|
|
irb_code = "UVACompl_PRCAppr" # The first file referenced in pb required docs.
|
A major refactor of how we search and store files, as there was a lot of confusing bits in here.
From an API point of view you can do the following (and only the following)
/files?workflow_spec_id=x
* You can find all files associated with a workflow_spec_id, and add a file with a workflow_spec_id
/files?workflow_id=x
* You can find all files associated with a workflow_id, and add a file that is directly associated with the workflow
/files?workflow_id=x&form_field_key=y
* You can find all files associated with a form element on a running workflow, and add a new file.
Note: you can add multiple files to the same form_field_key, IF they have different file names. If the same name, the original file is archived,
and the new file takes its place.
The study endpoints always return a list of the file metadata associated with the study. Removed /studies-files, but there is an
endpoint called
/studies/all - that returns all the studies in the system, and does include their files.
On a deeper level:
The File model no longer contains:
- study_id,
- task_id,
- form_field_key
Instead, if the file is associated with workflow - then that is the one way it is connected to the study, and we use this relationship to find files for a study.
A file is never associated with a task_id, as these change when the workflow is reloaded.
The form_field_key must match the irb_doc_code, so when requesting files for a form field, we just look up the irb_doc_code.
2020-05-28 12:27:26 +00:00
|
|
|
FileService.add_workflow_file(workflow_id=workflow.id,
|
|
|
|
name="anything.png", content_type="text",
|
|
|
|
binary_data=b'1234', irb_doc_code=irb_code)
|
2020-05-23 19:08:17 +00:00
|
|
|
# Add the file again with different data
|
A major refactor of how we search and store files, as there was a lot of confusing bits in here.
From an API point of view you can do the following (and only the following)
/files?workflow_spec_id=x
* You can find all files associated with a workflow_spec_id, and add a file with a workflow_spec_id
/files?workflow_id=x
* You can find all files associated with a workflow_id, and add a file that is directly associated with the workflow
/files?workflow_id=x&form_field_key=y
* You can find all files associated with a form element on a running workflow, and add a new file.
Note: you can add multiple files to the same form_field_key, IF they have different file names. If the same name, the original file is archived,
and the new file takes its place.
The study endpoints always return a list of the file metadata associated with the study. Removed /studies-files, but there is an
endpoint called
/studies/all - that returns all the studies in the system, and does include their files.
On a deeper level:
The File model no longer contains:
- study_id,
- task_id,
- form_field_key
Instead, if the file is associated with workflow - then that is the one way it is connected to the study, and we use this relationship to find files for a study.
A file is never associated with a task_id, as these change when the workflow is reloaded.
The form_field_key must match the irb_doc_code, so when requesting files for a form field, we just look up the irb_doc_code.
2020-05-28 12:27:26 +00:00
|
|
|
FileService.add_workflow_file(workflow_id=workflow.id,
|
|
|
|
name="anything.png", content_type="text",
|
|
|
|
binary_data=b'5678', irb_doc_code=irb_code)
|
2020-05-23 19:08:17 +00:00
|
|
|
|
|
|
|
file_models = FileService.get_workflow_files(workflow_id=workflow.id)
|
2020-06-05 18:08:46 +00:00
|
|
|
self.assertEqual(1, len(file_models))
|
2020-05-29 00:03:50 +00:00
|
|
|
|
|
|
|
file_data = FileService.get_workflow_data_files(workflow_id=workflow.id)
|
2020-06-05 18:08:46 +00:00
|
|
|
self.assertEqual(1, len(file_data))
|
|
|
|
self.assertEqual(2, file_data[0].version)
|
2021-04-30 15:55:12 +00:00
|
|
|
self.assertEquals(4, file_data[0].size) # File dat size is included.
|
2020-05-23 19:08:17 +00:00
|
|
|
|
|
|
|
def test_add_file_from_form_increments_version_and_replaces_on_subsequent_add_with_same_name(self):
|
|
|
|
self.load_example_data()
|
|
|
|
self.create_reference_document()
|
|
|
|
workflow = self.create_workflow('file_upload_form')
|
|
|
|
processor = WorkflowProcessor(workflow)
|
|
|
|
task = processor.next_task()
|
|
|
|
irb_code = "UVACompl_PRCAppr" # The first file referenced in pb required docs.
|
A major refactor of how we search and store files, as there was a lot of confusing bits in here.
From an API point of view you can do the following (and only the following)
/files?workflow_spec_id=x
* You can find all files associated with a workflow_spec_id, and add a file with a workflow_spec_id
/files?workflow_id=x
* You can find all files associated with a workflow_id, and add a file that is directly associated with the workflow
/files?workflow_id=x&form_field_key=y
* You can find all files associated with a form element on a running workflow, and add a new file.
Note: you can add multiple files to the same form_field_key, IF they have different file names. If the same name, the original file is archived,
and the new file takes its place.
The study endpoints always return a list of the file metadata associated with the study. Removed /studies-files, but there is an
endpoint called
/studies/all - that returns all the studies in the system, and does include their files.
On a deeper level:
The File model no longer contains:
- study_id,
- task_id,
- form_field_key
Instead, if the file is associated with workflow - then that is the one way it is connected to the study, and we use this relationship to find files for a study.
A file is never associated with a task_id, as these change when the workflow is reloaded.
The form_field_key must match the irb_doc_code, so when requesting files for a form field, we just look up the irb_doc_code.
2020-05-28 12:27:26 +00:00
|
|
|
FileService.add_workflow_file(workflow_id=workflow.id,
|
|
|
|
irb_doc_code=irb_code,
|
|
|
|
name="anything.png", content_type="text",
|
|
|
|
binary_data=b'1234')
|
2020-05-23 19:08:17 +00:00
|
|
|
# Add the file again with different data
|
A major refactor of how we search and store files, as there was a lot of confusing bits in here.
From an API point of view you can do the following (and only the following)
/files?workflow_spec_id=x
* You can find all files associated with a workflow_spec_id, and add a file with a workflow_spec_id
/files?workflow_id=x
* You can find all files associated with a workflow_id, and add a file that is directly associated with the workflow
/files?workflow_id=x&form_field_key=y
* You can find all files associated with a form element on a running workflow, and add a new file.
Note: you can add multiple files to the same form_field_key, IF they have different file names. If the same name, the original file is archived,
and the new file takes its place.
The study endpoints always return a list of the file metadata associated with the study. Removed /studies-files, but there is an
endpoint called
/studies/all - that returns all the studies in the system, and does include their files.
On a deeper level:
The File model no longer contains:
- study_id,
- task_id,
- form_field_key
Instead, if the file is associated with workflow - then that is the one way it is connected to the study, and we use this relationship to find files for a study.
A file is never associated with a task_id, as these change when the workflow is reloaded.
The form_field_key must match the irb_doc_code, so when requesting files for a form field, we just look up the irb_doc_code.
2020-05-28 12:27:26 +00:00
|
|
|
FileService.add_workflow_file(workflow_id=workflow.id,
|
|
|
|
irb_doc_code=irb_code,
|
|
|
|
name="anything.png", content_type="text",
|
|
|
|
binary_data=b'5678')
|
2020-05-23 19:08:17 +00:00
|
|
|
|
2020-06-04 13:49:42 +00:00
|
|
|
def test_replace_archive_file_unarchives_the_file_and_updates(self):
|
|
|
|
self.load_example_data()
|
|
|
|
self.create_reference_document()
|
|
|
|
workflow = self.create_workflow('file_upload_form')
|
|
|
|
processor = WorkflowProcessor(workflow)
|
|
|
|
task = processor.next_task()
|
|
|
|
irb_code = "UVACompl_PRCAppr" # The first file referenced in pb required docs.
|
|
|
|
FileService.add_workflow_file(workflow_id=workflow.id,
|
|
|
|
irb_doc_code=irb_code,
|
|
|
|
name="anything.png", content_type="text",
|
|
|
|
binary_data=b'1234')
|
|
|
|
|
|
|
|
# Archive the file
|
|
|
|
file_models = FileService.get_workflow_files(workflow_id=workflow.id)
|
2020-06-18 03:11:47 +00:00
|
|
|
self.assertEqual(1, len(file_models))
|
2020-06-04 13:49:42 +00:00
|
|
|
file_model = file_models[0]
|
|
|
|
file_model.archived = True
|
|
|
|
db.session.add(file_model)
|
|
|
|
|
|
|
|
# Assure that the file no longer comes back.
|
|
|
|
file_models = FileService.get_workflow_files(workflow_id=workflow.id)
|
2020-06-18 03:11:47 +00:00
|
|
|
self.assertEqual(0, len(file_models))
|
2020-06-04 13:49:42 +00:00
|
|
|
|
|
|
|
# Add the file again with different data
|
|
|
|
FileService.add_workflow_file(workflow_id=workflow.id,
|
|
|
|
irb_doc_code=irb_code,
|
|
|
|
name="anything.png", content_type="text",
|
|
|
|
binary_data=b'5678')
|
|
|
|
|
2020-05-23 19:08:17 +00:00
|
|
|
file_models = FileService.get_workflow_files(workflow_id=workflow.id)
|
2020-06-05 18:08:46 +00:00
|
|
|
self.assertEqual(1, len(file_models))
|
2020-05-29 00:03:50 +00:00
|
|
|
|
|
|
|
file_data = FileService.get_workflow_data_files(workflow_id=workflow.id)
|
2020-06-05 18:35:05 +00:00
|
|
|
|
2020-06-05 18:08:46 +00:00
|
|
|
self.assertEqual(1, len(file_data))
|
|
|
|
self.assertEqual(2, file_data[0].version)
|
2020-06-05 18:35:05 +00:00
|
|
|
self.assertEqual(b'5678', file_data[0].data)
|
2020-05-23 19:08:17 +00:00
|
|
|
|
|
|
|
def test_add_file_from_form_allows_multiple_files_with_different_names(self):
|
|
|
|
self.load_example_data()
|
|
|
|
self.create_reference_document()
|
|
|
|
workflow = self.create_workflow('file_upload_form')
|
|
|
|
processor = WorkflowProcessor(workflow)
|
|
|
|
task = processor.next_task()
|
|
|
|
irb_code = "UVACompl_PRCAppr" # The first file referenced in pb required docs.
|
A major refactor of how we search and store files, as there was a lot of confusing bits in here.
From an API point of view you can do the following (and only the following)
/files?workflow_spec_id=x
* You can find all files associated with a workflow_spec_id, and add a file with a workflow_spec_id
/files?workflow_id=x
* You can find all files associated with a workflow_id, and add a file that is directly associated with the workflow
/files?workflow_id=x&form_field_key=y
* You can find all files associated with a form element on a running workflow, and add a new file.
Note: you can add multiple files to the same form_field_key, IF they have different file names. If the same name, the original file is archived,
and the new file takes its place.
The study endpoints always return a list of the file metadata associated with the study. Removed /studies-files, but there is an
endpoint called
/studies/all - that returns all the studies in the system, and does include their files.
On a deeper level:
The File model no longer contains:
- study_id,
- task_id,
- form_field_key
Instead, if the file is associated with workflow - then that is the one way it is connected to the study, and we use this relationship to find files for a study.
A file is never associated with a task_id, as these change when the workflow is reloaded.
The form_field_key must match the irb_doc_code, so when requesting files for a form field, we just look up the irb_doc_code.
2020-05-28 12:27:26 +00:00
|
|
|
FileService.add_workflow_file(workflow_id=workflow.id,
|
|
|
|
irb_doc_code=irb_code,
|
|
|
|
name="anything.png", content_type="text",
|
|
|
|
binary_data=b'1234')
|
2020-05-23 19:08:17 +00:00
|
|
|
# Add the file again with different data
|
A major refactor of how we search and store files, as there was a lot of confusing bits in here.
From an API point of view you can do the following (and only the following)
/files?workflow_spec_id=x
* You can find all files associated with a workflow_spec_id, and add a file with a workflow_spec_id
/files?workflow_id=x
* You can find all files associated with a workflow_id, and add a file that is directly associated with the workflow
/files?workflow_id=x&form_field_key=y
* You can find all files associated with a form element on a running workflow, and add a new file.
Note: you can add multiple files to the same form_field_key, IF they have different file names. If the same name, the original file is archived,
and the new file takes its place.
The study endpoints always return a list of the file metadata associated with the study. Removed /studies-files, but there is an
endpoint called
/studies/all - that returns all the studies in the system, and does include their files.
On a deeper level:
The File model no longer contains:
- study_id,
- task_id,
- form_field_key
Instead, if the file is associated with workflow - then that is the one way it is connected to the study, and we use this relationship to find files for a study.
A file is never associated with a task_id, as these change when the workflow is reloaded.
The form_field_key must match the irb_doc_code, so when requesting files for a form field, we just look up the irb_doc_code.
2020-05-28 12:27:26 +00:00
|
|
|
FileService.add_workflow_file(workflow_id=workflow.id,
|
|
|
|
irb_doc_code=irb_code,
|
|
|
|
name="a_different_thing.png", content_type="text",
|
|
|
|
binary_data=b'5678')
|
2020-05-23 19:08:17 +00:00
|
|
|
file_models = FileService.get_workflow_files(workflow_id=workflow.id)
|
2020-06-05 18:08:46 +00:00
|
|
|
self.assertEqual(2, len(file_models))
|
2020-07-17 22:59:25 +00:00
|
|
|
|
|
|
|
@patch('crc.services.file_service.Github')
|
|
|
|
def test_update_from_github(self, mock_github):
|
|
|
|
mock_github.return_value = FakeGithub()
|
|
|
|
|
|
|
|
self.load_example_data()
|
|
|
|
self.create_reference_document()
|
|
|
|
workflow = self.create_workflow('file_upload_form')
|
|
|
|
processor = WorkflowProcessor(workflow)
|
|
|
|
task = processor.next_task()
|
|
|
|
irb_code = "UVACompl_PRCAppr" # The first file referenced in pb required docs.
|
|
|
|
file_model = FileService.add_workflow_file(workflow_id=workflow.id,
|
|
|
|
irb_doc_code=irb_code,
|
|
|
|
name="anything.png", content_type="text",
|
|
|
|
binary_data=b'1234')
|
|
|
|
FileService.update_from_github([file_model.id])
|
|
|
|
|
|
|
|
file_model_data = FileDataModel.query.filter_by(
|
|
|
|
file_model_id=file_model.id
|
|
|
|
).order_by(
|
|
|
|
desc(FileDataModel.version)
|
|
|
|
).first()
|
|
|
|
self.assertEqual(file_model_data.data, b'Some bytes')
|
|
|
|
|
|
|
|
@patch('crc.services.file_service.Github')
|
|
|
|
def test_publish_to_github_creates(self, mock_github):
|
|
|
|
mock_github.return_value = FakeGithubCreates()
|
|
|
|
|
|
|
|
self.load_example_data()
|
|
|
|
self.create_reference_document()
|
|
|
|
workflow = self.create_workflow('file_upload_form')
|
|
|
|
processor = WorkflowProcessor(workflow)
|
|
|
|
task = processor.next_task()
|
|
|
|
irb_code = "UVACompl_PRCAppr" # The first file referenced in pb required docs.
|
|
|
|
file_model = FileService.add_workflow_file(workflow_id=workflow.id,
|
|
|
|
irb_doc_code=irb_code,
|
|
|
|
name="anything.png", content_type="text",
|
|
|
|
binary_data=b'1234')
|
|
|
|
result = FileService.publish_to_github([file_model.id])
|
|
|
|
|
|
|
|
self.assertEqual(result['created'], True)
|
|
|
|
|
|
|
|
@patch('crc.services.file_service.Github')
|
|
|
|
def test_publish_to_github_updates(self, mock_github):
|
|
|
|
mock_github.return_value = FakeGithub()
|
|
|
|
|
|
|
|
self.load_example_data()
|
|
|
|
self.create_reference_document()
|
|
|
|
workflow = self.create_workflow('file_upload_form')
|
|
|
|
processor = WorkflowProcessor(workflow)
|
|
|
|
task = processor.next_task()
|
|
|
|
irb_code = "UVACompl_PRCAppr" # The first file referenced in pb required docs.
|
|
|
|
file_model = FileService.add_workflow_file(workflow_id=workflow.id,
|
|
|
|
irb_doc_code=irb_code,
|
|
|
|
name="anything.png", content_type="text",
|
|
|
|
binary_data=b'1234')
|
|
|
|
result = FileService.publish_to_github([file_model.id])
|
|
|
|
|
|
|
|
self.assertEqual(result['updated'], True)
|
2020-08-14 17:04:22 +00:00
|
|
|
|
|
|
|
@patch('crc.services.file_service.Github')
|
|
|
|
def test_get_repo_branches(self, mock_github):
|
|
|
|
mock_github.return_value = FakeGithub()
|
|
|
|
|
|
|
|
branches = FileService.get_repo_branches()
|
|
|
|
|
|
|
|
self.assertIsInstance(branches, list)
|