Skip to content

Commit

Permalink
Merge pull request #363 from alphagov/tokens-on-guides
Browse files Browse the repository at this point in the history
Append draft access token to parts links on guides
  • Loading branch information
fofr authored May 17, 2017
2 parents 61a6da8 + 2f9800b commit 547c407
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/controllers/content_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@ def show
set_up_education_navigation_ab_testing
set_expiry
set_access_control_allow_origin_header if request.format.atom?
set_guide_draft_access_token if @content_item.is_a?(GuidePresenter)
render_template
end

private

# Allow guides to pass access token to each part to allow
# fact checking of all content
def set_guide_draft_access_token
@content_item.draft_access_token = params[:token]
end

def load_content_item
content_item = content_store.content_item(content_item_path)
@content_item = present(content_item)
Expand Down
19 changes: 19 additions & 0 deletions app/presenters/guide_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class GuidePresenter < ContentItemPresenter
include Parts
include UkviABTest

attr_accessor :draft_access_token

def page_title
if @part_slug
"#{title}: #{current_part_title}"
Expand Down Expand Up @@ -33,4 +35,21 @@ def print_link
def related_items
@nav_helper.related_items
end

# Append token parameters to part paths to allow fact-checkers to fact-check all pages
def parts
if draft_access_token
super.each do |part|
part['full_path'] = "#{part['full_path']}?#{draft_access_token_param}"
end
else
super
end
end

private

def draft_access_token_param
"token=#{draft_access_token}" if draft_access_token
end
end
8 changes: 8 additions & 0 deletions test/integration/guide_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ class GuideTest < ActionDispatch::IntegrationTest
assert page.has_css?('.print-link a[href$="/print"]')
end

test "draft access tokens are appended to part links within navigation" do
@content_item = JSON.parse(get_content_example("guide")).tap do |item|
visit("#{item['base_path']}?token=some_token")
end

assert page.has_css?('.part-navigation a[href$="?token=some_token"]')
end

test "does not show part navigation, print link or part title when only one part" do
setup_and_visit_content_item('single-page-guide')

Expand Down
19 changes: 19 additions & 0 deletions test/presenters/guide_presenter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,25 @@ def format_name
presented_item('no-part-guide').has_parts?
end

test "presents access tokens in part links when provided" do
presented = presented_item('guide')
presented.draft_access_token = 'some_token'
expected_param = '?token=some_token'

presented.parts.each do |part|
assert part['full_path'].ends_with?(expected_param)
end

presented.parts_navigation.flatten.each_with_index do |link, i|
if i > 0
assert expected_param.in?(link)
end
end

nav = presented.previous_and_next_navigation
assert nav[:next_page][:url].ends_with?(expected_param)
end

private

def presented_item(type = format_name, part_slug = nil, overrides = {})
Expand Down

0 comments on commit 547c407

Please sign in to comment.