Skip to content

Commit

Permalink
Merge pull request #1205 from dradis/liquid-assigns-service
Browse files Browse the repository at this point in the history
Add LiquidAssignsService
  • Loading branch information
caitmich authored Dec 1, 2023
2 parents 8d5d98d + ec605eb commit 9a10384
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
- Upgrade to Rails 7.0.8
- Replace unicorn with puma in production
- Add importmap-rails to handle js libraries
- Add LiquidAssignsService
- Revision history: Improve version history for content with carriage return
- Upgraded gems:
- [gem]
Expand Down
6 changes: 1 addition & 5 deletions app/controllers/concerns/liquid_enabled_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,11 @@ def default_liquid_assigns
end

def project_assigns
result = {}

# This is required because we may be in Markup#preview that's passing
# :project_id for Tylium rendered editors
project = Project.find(params[:project_id])
authorize! :use, project

result['project'] = ProjectDrop.new(project)

result
LiquidAssignsService.new(project).assigns
end
end
4 changes: 2 additions & 2 deletions app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Project
attr_reader :id, :name

# -- Class Methods --------------------------------------------------------
def self.create(args={})
def self.create(args = {})
new(**args.merge(id: 1))
end

Expand All @@ -17,7 +17,7 @@ def self.find(id)
end

# This tricks NamingService.name_project (used by KitImportJob)
def self.exists?(args={})
def self.exists?(args = {})
false
end

Expand Down
18 changes: 18 additions & 0 deletions app/services/liquid_assigns_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class LiquidAssignsService
attr_accessor :project

def initialize(project)
@project = project
end

def assigns
result = { 'project' => ProjectDrop.new(project) }
result.merge!(assigns_pro) if defined?(Dradis::Pro)
result
end

private

def assigns_pro
end
end
26 changes: 26 additions & 0 deletions spec/services/liquid_assigns_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'rails_helper'

RSpec.describe LiquidAssignsService do
let!(:project) { create(:project) }

let(:liquid_assigns) { described_class.new(project).assigns }

it 'builds a hash of liquid assigns' do
expect(liquid_assigns['project'].name).to eq(project.name)
end

context 'with pro records', skip: !defined?(Dradis::Pro) do
let!(:project) { create(:project, :with_team) }

before do
report_content = project.content_library
report_content.properties = { 'dradis.project' => project.name }
report_content.save
end

it 'builds a hash with Dradis::Pro assigns' do
expect(liquid_assigns['document_properties'].available_properties).to eq({ 'dradis.project' => project.name })
expect(liquid_assigns['team'].name).to eq(project.team.name)
end
end
end
19 changes: 12 additions & 7 deletions spec/support/revision_shared_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#
# let(:submit_form) { click_link "delete" }
#
shared_examples "deleted item is listed in Trash" do |item_type|
it "deletes the item and destroy revision is shown in Trash" do
shared_examples 'deleted item is listed in Trash' do |item_type|
it 'deletes the item and destroy revision is shown in Trash' do
with_versioning do
submit_form
visit project_trash_path(current_project)
Expand All @@ -17,8 +17,8 @@

# Apart from the 'submit_form' let variable described above, another let variable
# called 'model' should be defined, which will be the object to recover.
shared_examples "recover deleted item" do |item_type|
it "should recover item listed in Trash", js: true do
shared_examples 'recover deleted item' do |item_type|
it 'should recover item listed in Trash', js: true do
with_versioning do
submit_form
visit project_trash_path(current_project)
Expand Down Expand Up @@ -48,8 +48,8 @@

# Apart from the 'submit_form' let variable described above, another let variable
# called 'model' should be defined, which will be the object to recover.
shared_examples "recover deleted item without node" do |item_type|
it "should recover item listed in Trash even if its node has been destroyed", js: true do
shared_examples 'recover deleted item without node' do |item_type|
it 'should recover item listed in Trash even if its node has been destroyed', js: true do
with_versioning do
submit_form
visit project_node_path(model.node.project, model.node.id)
Expand All @@ -61,6 +61,11 @@

expect do
visit project_trash_path(current_project)

within '#trash' do
@item_count = page.all('td', text: item_type.to_s).count
end

rr_path = recover_project_revision_path(current_project, model.versions.last)
accept_confirm do
find(:xpath, "//a[@href='#{rr_path}']").click
Expand All @@ -75,7 +80,7 @@
)

within '#trash' do
expect(page).not_to have_content item_type.to_s
expect(page.all('td', text: item_type.to_s).count).to eq(@item_count - 1)
end
expect(model.class.find_by_id(model.id)).not_to be_nil
expect(page).to have_content /Recovered/i
Expand Down

0 comments on commit 9a10384

Please sign in to comment.