async def test_save_and_deploy_resource_sends_resource_request_message( self, send_resource_request_mock, resource_template_repo, resource_repo, operations_repo, basic_resource_template): resource = sample_resource() operation = sample_resource_operation(resource_id=resource.id, operation_id=str(uuid.uuid4())) resource_repo.save_item = MagicMock(return_value=None) operations_repo.create_operations_item = MagicMock( return_value=operation) user = create_test_user() await save_and_deploy_resource( resource=resource, resource_repo=resource_repo, operations_repo=operations_repo, resource_template_repo=resource_template_repo, user=create_test_user(), resource_template=basic_resource_template) send_resource_request_mock.assert_called_once_with( resource=resource, operations_repo=operations_repo, resource_repo=resource_repo, user=user, resource_template_repo=resource_template_repo, resource_template=basic_resource_template, action=RequestAction.Install)
async def test_resource_request_message_generated_correctly( resource_template_repo, resource_repo, service_bus_client_mock, operations_repo_mock, request_action): service_bus_client_mock().get_queue_sender().send_messages = AsyncMock() resource = create_test_resource() operation = create_sample_operation(resource.id, request_action) template = get_sample_workspace_template_object() operations_repo_mock.create_operation_item.return_value = operation resource_repo.get_resource_by_id.return_value = resource await send_resource_request_message( resource=resource, operations_repo=operations_repo_mock, resource_repo=resource_repo, user=create_test_user(), resource_template=template, resource_template_repo=resource_template_repo, action=request_action) args = service_bus_client_mock().get_queue_sender( ).send_messages.call_args.args assert len(args) == 1 assert isinstance(args[0], ServiceBusMessage) sent_message = args[0] assert sent_message.correlation_id == operation.id sent_message_as_json = json.loads(str(sent_message)) assert sent_message_as_json["id"] == resource.id assert sent_message_as_json["action"] == request_action
def sample_resource(workspace_id=WORKSPACE_ID): workspace = Workspace(id=workspace_id, templateName="tre-workspace-base", templateVersion="0.1.0", etag="", properties={"client_id": "12345"}, resourcePath=f'/workspaces/{workspace_id}', user=create_test_user(), updatedWhen=FAKE_CREATE_TIMESTAMP) return workspace
def sample_resource_operation(resource_id: str, operation_id: str): operation = Operation(id=operation_id, resourceId=resource_id, resourcePath=f'/workspaces/{resource_id}', resourceVersion=0, action="install", message="test", Status=Status.Deployed, createdWhen=FAKE_CREATE_TIMESTAMP, updatedWhen=FAKE_CREATE_TIMESTAMP, user=create_test_user()) return operation
async def test_send_uninstall_message_raises_503_on_service_bus_exception( self, operations_repo, _, resource_template_repo, resource_repo, basic_resource_template): with pytest.raises(HTTPException) as ex: await send_uninstall_message( resource=sample_resource(), resource_repo=resource_repo, operations_repo=operations_repo, resource_type=ResourceType.Workspace, resource_template_repo=resource_template_repo, user=create_test_user(), resource_template=basic_resource_template) assert ex.value.status_code == status.HTTP_503_SERVICE_UNAVAILABLE
def test_patch_resource_preserves_property_history(_, __, resource_repo): """ Tests that properties are copied into a history array and only certain values in the root are updated """ resource_repo.update_item_with_etag = MagicMock(return_value=None) resource_patch = ResourcePatch(isEnabled=True, properties={'display_name': 'updated name'}) etag = "some-etag-value" user = create_test_user() resource = sample_resource() expected_resource = sample_resource() expected_resource.history = [ ResourceHistoryItem( isEnabled=True, resourceVersion=0, updatedWhen=FAKE_CREATE_TIMESTAMP, properties={'display_name': 'initial display name', 'description': 'initial description', 'computed_prop': 'computed_val'}, user=user)] expected_resource.properties['display_name'] = 'updated name' expected_resource.resourceVersion = 1 expected_resource.user = user expected_resource.updatedWhen = FAKE_UPDATE_TIMESTAMP resource_repo.patch_resource(resource, resource_patch, None, etag, None, user) resource_repo.update_item_with_etag.assert_called_once_with(expected_resource, etag) # now patch again new_resource = copy.deepcopy(expected_resource) # new_resource is after the first patch new_patch = ResourcePatch(isEnabled=False, properties={'display_name': 'updated name 2'}) expected_resource.history.append( ResourceHistoryItem( isEnabled=True, resourceVersion=1, updatedWhen=FAKE_UPDATE_TIMESTAMP, properties={'display_name': 'updated name', 'description': 'initial description', 'computed_prop': 'computed_val'}, user=user ) ) expected_resource.resourceVersion = 2 expected_resource.properties['display_name'] = "updated name 2" expected_resource.isEnabled = False expected_resource.user = user resource_repo.patch_resource(new_resource, new_patch, None, etag, None, user) resource_repo.update_item_with_etag.assert_called_with(expected_resource, etag)
async def test_save_and_deploy_resource_raises_503_if_save_to_db_fails( self, resource_template_repo, resource_repo, operations_repo, basic_resource_template): resource = sample_resource() resource_repo.save_item = MagicMock(side_effect=Exception) with pytest.raises(HTTPException) as ex: await save_and_deploy_resource( resource=resource, resource_repo=resource_repo, operations_repo=operations_repo, resource_template_repo=resource_template_repo, user=create_test_user(), resource_template=basic_resource_template) assert ex.value.status_code == status.HTTP_503_SERVICE_UNAVAILABLE
def sample_resource() -> Resource: return Resource( id=RESOURCE_ID, isActive=True, isEnabled=True, resourcePath="/resource/path", templateName="template_name", templateVersion="template_version", properties={ 'display_name': 'initial display name', 'description': 'initial description', 'computed_prop': 'computed_val' }, resourceType=ResourceType.Workspace, etag="some-etag-value", resourceVersion=0, updatedWhen=FAKE_CREATE_TIMESTAMP, user=create_test_user() )
async def test_save_and_deploy_resource_deletes_item_from_db_if_send_request_fails( self, _, resource_template_repo, resource_repo, operations_repo, basic_resource_template): resource = sample_resource() resource_repo.save_item = MagicMock(return_value=None) resource_repo.delete_item = MagicMock(return_value=None) operations_repo.create_operation_item = MagicMock(return_value=None) with pytest.raises(HTTPException): await save_and_deploy_resource( resource=resource, resource_repo=resource_repo, operations_repo=operations_repo, resource_template_repo=resource_template_repo, user=create_test_user(), resource_template=basic_resource_template) resource_repo.delete_item.assert_called_once_with(resource.id)
async def test_save_and_deploy_resource_saves_item( self, _, resource_template_repo, resource_repo, operations_repo, basic_resource_template): resource = sample_resource() operation = sample_resource_operation(resource_id=resource.id, operation_id=str(uuid.uuid4())) resource_repo.save_item = MagicMock(return_value=None) operations_repo.create_operation_item = MagicMock( return_value=operation) await save_and_deploy_resource( resource=resource, resource_repo=resource_repo, operations_repo=operations_repo, resource_template_repo=resource_template_repo, user=create_test_user(), resource_template=basic_resource_template) resource_repo.save_item.assert_called_once_with(resource)
async def test_send_uninstall_message_sends_uninstall_message( self, operations_repo, send_request_mock, resource_template_repo, resource_repo, basic_resource_template): resource = sample_resource() user = create_test_user() await send_uninstall_message( resource=resource, resource_repo=resource_repo, operations_repo=operations_repo, resource_type=ResourceType.Workspace, resource_template_repo=resource_template_repo, user=user, resource_template=basic_resource_template) send_request_mock.assert_called_once_with( resource=resource, operations_repo=operations_repo, resource_repo=resource_repo, user=user, resource_template_repo=resource_template_repo, resource_template=basic_resource_template, action=RequestAction.UnInstall)