diff --git a/app/controllers/content_item_signups_controller.rb b/app/controllers/content_item_signups_controller.rb new file mode 100644 index 00000000..ad4eb866 --- /dev/null +++ b/app/controllers/content_item_signups_controller.rb @@ -0,0 +1,67 @@ +# This controller takes any content item and makes it possible to subscribe +# to alerts when documents linked to them are changed (taxons, orgs etc). +# This is in contrast to EmailAlertSignupsController, which takes a +# finder_email_signup content item. +class ContentItemSignupsController < ApplicationController + protect_from_forgery except: [:create] + before_action :require_content_item_param + before_action :validate_document_type + helper_method :estimated_email_frequency + + def new + @subscription = ContentItemSubscriptionPresenter.new(@content_item) + end + + def confirm; end + + def create + signup = ContentItemSubscriberList.new(content_item.to_h) + + if signup.has_content_item? + redirect_to signup.subscription_management_url + else + redirect_to confirm_content_item_signup_path(link: content_item_path) + end + end + +private + + PERMITTED_CONTENT_ITEMS = %w(taxon organisation).freeze + + def require_content_item_param + unless valid_content_item_param? + redirect_to '/' + false + end + end + + def valid_content_item_param? + content_item_path.to_s.starts_with?('/') && URI.parse(content_item_path).relative? + rescue URI::InvalidURIError + false + end + + def content_item_path + # Topic param left in for backwards compatibility. + # Topic is the user-facing terminology for taxons. Expect the taxon base + # path to be provided in a param of this name. + params[:link] || params[:topic] + end + + def content_item + @content_item ||= EmailAlertFrontend + .services(:content_store) + .content_item(content_item_path) + end + + def validate_document_type + unless PERMITTED_CONTENT_ITEMS.include?(content_item['document_type']) + redirect_to '/' + false + end + end + + def estimated_email_frequency + EmailVolume::WeeklyEmailVolume.new(content_item).estimate + end +end diff --git a/app/controllers/taxonomy_signups_controller.rb b/app/controllers/taxonomy_signups_controller.rb deleted file mode 100644 index 7f350457..00000000 --- a/app/controllers/taxonomy_signups_controller.rb +++ /dev/null @@ -1,66 +0,0 @@ -class TaxonomySignupsController < ApplicationController - protect_from_forgery except: [:create] - before_action :require_taxon_param - before_action :validate_taxon_document_type - helper_method :child_taxons - - def new; end - - def confirm - load_estimated_email_frequency - end - - def create - signup = TaxonomySignup.new(taxon.to_h) - - if signup.save - redirect_to signup.subscription_management_url - else - redirect_to confirm_taxonomy_signup_path(topic: taxon_path) - end - end - - def child_taxons - taxon['links'] - .fetch('https://github.com/child_taxons', []) - .reject { |taxon| taxon['phase'] == 'alpha' } - end - -private - - def require_taxon_param - unless valid_taxon_param? - redirect_to '/' - false - end - end - - def valid_taxon_param? - taxon_path.to_s.starts_with?('/') && URI.parse(taxon_path).relative? - rescue URI::InvalidURIError - false - end - - def taxon_path - # Topic is the user-facing terminology for taxons. Expect the taxon base - # path to be provided in a param of this name. - params[:topic] - end - - def taxon - @taxon ||= EmailAlertFrontend - .services(:content_store) - .content_item(taxon_path) - end - - def validate_taxon_document_type - unless taxon['document_type'] == 'taxon' - redirect_to '/' - false - end - end - - def load_estimated_email_frequency - @estimated_email_frequency = WeeklyEmailVolume.new(taxon).estimate - end -end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 636cb171..aecca5d2 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -9,7 +9,7 @@ def title(text, params = {}) render 'govuk_publishing_components/components/title', { title: text }.merge(params) end - def live_taxon?(taxon) - taxon['phase'] == 'live' + def live_content_item?(content_item) + content_item['phase'] == 'live' end end diff --git a/app/models/content_item_subscriber_list.rb b/app/models/content_item_subscriber_list.rb new file mode 100644 index 00000000..eac1bb9c --- /dev/null +++ b/app/models/content_item_subscriber_list.rb @@ -0,0 +1,61 @@ +class ContentItemSubscriberList + def initialize(content_item) + @content_item = content_item + end + + def subscription_management_url + subscriber_list.dig('subscriber_list', 'subscription_url') + end + + def has_content_item? + content_item.present? + end + +private + + attr_accessor :content_item + + class UnsupportedContentItemError < StandardError; end + + def subscriber_list + EmailAlertFrontend.services(:email_alert_api) + .find_or_create_subscriber_list(subscription_params) + end + + def subscription_params + { + 'title' => content_item['title'], + 'links' => link_hash + } + end + + def link_hash + case content_item_type + when 'taxon' + taxon_links + when 'organisation' + organisation_links + else + message = "No link hash available for content items of type #{content_item_type}!" + raise UnsupportedContentItemError, message + end + end + + def taxon_links + { + # 'taxon_tree' is the key used in email-alert-service for + # notifications, so create a subscriber list with this key. + 'taxon_tree' => [content_item['content_id']] + } + end + + def organisation_links + { + 'organisations' => [content_item['content_id']] + } + end + + def content_item_type + content_item.dig('document_type') + end +end diff --git a/app/models/email_volume/organisation_weekly_email_volume.rb b/app/models/email_volume/organisation_weekly_email_volume.rb new file mode 100644 index 00000000..1563323b --- /dev/null +++ b/app/models/email_volume/organisation_weekly_email_volume.rb @@ -0,0 +1,37 @@ +module EmailVolume + class OrganisationWeeklyEmailVolume + HIGH = '40 - 60'.freeze + MEDIUM = '0 - 20'.freeze + LOW = '0 - 5'.freeze + + def initialize(organisation) + @organisation = organisation + end + + def estimate + parent_organisation = extract_parent_from(@organisation) + + # It's a top-level organisation like HM Treasury + return HIGH if parent_organisation.blank? + + # Is a 2nd level organisation like UK Debt Management Office + grandparent_organisation = extract_parent_from( + fetch_content_item(parent_organisation.fetch('https://github.com/base_path')) + ) + return MEDIUM if grandparent_organisation.blank? + + # Is a 3rd level organisation + LOW + end + + private + + def extract_parent_from(content_item) + Array(content_item.dig('links', 'ordered_parent_organisations')).first + end + + def fetch_content_item(base_path) + EmailAlertFrontend.services(:content_store).content_item(base_path) + end + end +end diff --git a/app/models/email_volume/taxon_weekly_email_volume.rb b/app/models/email_volume/taxon_weekly_email_volume.rb new file mode 100644 index 00000000..60053527 --- /dev/null +++ b/app/models/email_volume/taxon_weekly_email_volume.rb @@ -0,0 +1,37 @@ +module EmailVolume + class TaxonWeeklyEmailVolume + HIGH = '40 - 60'.freeze + MEDIUM = '0 - 20'.freeze + LOW = '0 - 5'.freeze + + def initialize(taxon) + @taxon = taxon + end + + def estimate + parent_taxon = extract_parent_from(@taxon) + + # Is at the top of the taxonomy + return HIGH if parent_taxon.blank? + + # Is a 2nd level taxon + grandparent_taxon = extract_parent_from( + fetch_content_item(parent_taxon.fetch('https://github.com/base_path')) + ) + return MEDIUM if grandparent_taxon.blank? + + # Is a 3rd level taxon or below + LOW + end + + private + + def extract_parent_from(content_item) + Array(content_item.dig('links', 'parent_taxons')).first + end + + def fetch_content_item(base_path) + EmailAlertFrontend.services(:content_store).content_item(base_path) + end + end +end diff --git a/app/models/email_volume/weekly_email_volume.rb b/app/models/email_volume/weekly_email_volume.rb new file mode 100644 index 00000000..e6282731 --- /dev/null +++ b/app/models/email_volume/weekly_email_volume.rb @@ -0,0 +1,31 @@ +module EmailVolume + class WeeklyEmailVolume + def initialize(content_item) + @content_item = content_item + end + + def estimate + volume_estimator.estimate + end + + private + + class ContentItemNotEstimatableError < StandardError; end + + def volume_estimator + case content_item_type + when 'taxon' + TaxonWeeklyEmailVolume.new(@content_item) + when 'organisation' + OrganisationWeeklyEmailVolume.new(@content_item) + else + error_message = "Volume estimate not possible for content items of type #{content_item_type}!" + raise ContentItemNotEstimatableError, error_message + end + end + + def content_item_type + @content_item.dig('document_type') + end + end +end diff --git a/app/models/taxonomy_signup.rb b/app/models/taxonomy_signup.rb deleted file mode 100644 index cb6c91b2..00000000 --- a/app/models/taxonomy_signup.rb +++ /dev/null @@ -1,34 +0,0 @@ -class TaxonomySignup - attr_accessor :taxon, :subscription_management_url - - def initialize(taxon) - @taxon = taxon - end - - def save - return false unless taxon.present? - - self.subscription_management_url = update_subscription.dig( - 'subscriber_list', 'subscription_url' - ) - true - end - -private - - def update_subscription - EmailAlertFrontend.services(:email_alert_api) - .find_or_create_subscriber_list(subscription_params) - end - - def subscription_params - { - 'title' => taxon['title'], - 'links' => { - # 'taxon_tree' is the key used in email-alert-service for - # notifications, so create a subscriber list with this key. - 'taxon_tree' => [taxon['content_id']] - } - } - end -end diff --git a/app/models/weekly_email_volume.rb b/app/models/weekly_email_volume.rb deleted file mode 100644 index 3bd20a13..00000000 --- a/app/models/weekly_email_volume.rb +++ /dev/null @@ -1,35 +0,0 @@ -class WeeklyEmailVolume - HIGH = '40 - 60'.freeze - MEDIUM = '0 - 20'.freeze - LOW = '0 - 5'.freeze - - def initialize(taxon) - @taxon = taxon - end - - def estimate - parent_taxon = extract_parent_from(@taxon) - - # Is at the top of the taxonomy - return HIGH if parent_taxon.blank? - - # Is a 2nd level taxon - grandparent_taxon = extract_parent_from( - fetch_content_item(parent_taxon.fetch('https://github.com/base_path')) - ) - return MEDIUM if grandparent_taxon.blank? - - # Is a 3rd level taxon or below - LOW - end - -private - - def extract_parent_from(content_item) - Array(content_item.dig('links', 'parent_taxons')).first - end - - def fetch_content_item(base_path) - EmailAlertFrontend.services(:content_store).content_item(base_path) - end -end diff --git a/app/presenters/content_item_subscription_presenter.rb b/app/presenters/content_item_subscription_presenter.rb new file mode 100644 index 00000000..e75a2f83 --- /dev/null +++ b/app/presenters/content_item_subscription_presenter.rb @@ -0,0 +1,27 @@ +class ContentItemSubscriptionPresenter + def initialize(content_item) + @content_item = content_item + end + + def description + return "This will include: #{content_item['description']}" if is_taxon? + + content_item['description'] + end + + def child_taxons + return unless is_taxon? + + content_item['links'] + .fetch('https://github.com/child_taxons', []) + .reject { |taxon| taxon['phase'] == 'alpha' } + end + +private + + attr_accessor :content_item + + def is_taxon? + content_item['document_type'] == 'taxon' + end +end diff --git a/app/views/taxonomy_signups/confirm.html.erb b/app/views/content_item_signups/confirm.html.erb similarity index 80% rename from app/views/taxonomy_signups/confirm.html.erb rename to app/views/content_item_signups/confirm.html.erb index 6daab17b..88936acf 100644 --- a/app/views/taxonomy_signups/confirm.html.erb +++ b/app/views/content_item_signups/confirm.html.erb @@ -1,8 +1,8 @@ -<% content_for :title, @taxon['title'] %> +<% content_for :title, @content_item['title'] %> <% content_for :back_link do %> <%= render "govuk_publishing_components/components/back_link", { - href: @taxon['base_path'] + href: @content_item['base_path'] } %> <% end %> @@ -12,11 +12,11 @@

What you’ll get

- You'll get alerts when the government publishes or changes anything on GOV.UK about: <%= @taxon['title'] %>. + You'll get alerts when the government publishes or changes anything on GOV.UK about: <%= @content_item['title'] %>.

- This might be between <%= @estimated_email_frequency %> updates a week. + This might be between <%= estimated_email_frequency %> updates a week.

@@ -38,7 +38,7 @@ } %> <%= form_tag(:action => :create) do %> - <%= hidden_field_tag 'topic', @taxon['base_path'] %> + <%= hidden_field_tag 'link', @content_item['base_path'] %> <%= submit_tag('Sign up now', class: 'govuk-button') %> <% end %> diff --git a/app/views/taxonomy_signups/new.html.erb b/app/views/content_item_signups/new.html.erb similarity index 68% rename from app/views/taxonomy_signups/new.html.erb rename to app/views/content_item_signups/new.html.erb index 6efeb291..9493cf50 100644 --- a/app/views/taxonomy_signups/new.html.erb +++ b/app/views/content_item_signups/new.html.erb @@ -1,9 +1,9 @@ -<% content_for :title, @taxon['title'] %> +<% content_for :title, @content_item['title'] %> -<% if live_taxon?(@taxon) %> +<% if live_content_item?(@content_item) %> <% content_for :back_link do %> <%= render "govuk_publishing_components/components/back_link", { - href: @taxon['base_path'] + href: @content_item['base_path'] } %> <% end %> <% end %> @@ -11,7 +11,7 @@

- <% if child_taxons.present?%> + <% if @subscription.child_taxons.present?%>

What do you want to get alerts about?

<%= form_tag({action: "confirm"}, method: "get") do %> @@ -21,17 +21,17 @@ name: "topic", id_prefix: "all", items: [{ - value: @taxon['base_path'], - text: @taxon['title'], + value: @content_item['base_path'], + text: @content_item['title'], conditional: '

This will include alerts about all the topics below

'.html_safe }] } %> -

or only one of the following <%= child_taxons.count %> topics

+

or only one of the following <%= @subscription.child_taxons.count %> topics

<%= render "govuk_publishing_components/components/radio", { name: "topic", id_prefix: "topics", - items: child_taxons.each.map { |taxon| { + items: @subscription.child_taxons.each.map { |taxon| { :value => taxon['base_path'], :text => taxon['title'], :conditional => taxon['description'].present? ? ('

This will include: ' + taxon['description'] + '

').html_safe : nil @@ -48,22 +48,22 @@

Get email alerts

-

You are signing up for email alerts about: <%= @taxon['title']%>

- <% if @taxon['description'].present? %> -

This will include: <%= @taxon['description'] %>

+

You are signing up for email alerts about: <%= @content_item['title']%>

+ <% if @content_item['description'].present? %> +

<%= @subscription.description %>

<% end %> <%= render "govuk_publishing_components/components/button", { text: "Select", - href: confirm_taxonomy_signup_path(topic: @taxon['base_path']), + href: confirm_content_item_signup_path(link: @content_item['base_path']), margin_bottom: true } %> <% end %> - <% if live_taxon?(@taxon) %> + <% if live_content_item?(@content_item) %>

- <%= link_to 'Back to select a different topic', @taxon['base_path'], class: 'govuk-link' %> + <%= link_to 'Back to select a different topic', @content_item['base_path'], class: 'govuk-link' %>

<% end %> diff --git a/config/brakeman.ignore b/config/brakeman.ignore index 6357ce4b..1815849f 100644 --- a/config/brakeman.ignore +++ b/config/brakeman.ignore @@ -3,24 +3,24 @@ { "warning_type": "Redirect", "warning_code": 18, - "fingerprint": "8d684e0fecc43a48c8920e6b7855c91953ffffbc571fb73de4e02aaf6c889b7a", + "fingerprint": "59e3d7346526d83d64afe9efe3d30dc7856eed884dfbfe38ea371a6210df6abe", "check_name": "Redirect", "message": "Possible unprotected redirect", - "file": "app/controllers/taxonomy_signups_controller.rb", - "line": 16, + "file": "app/controllers/content_item_signups_controller.rb", + "line": 21, "link": "https://brakemanscanner.org/docs/warning_types/redirect/", - "code": "redirect_to(TaxonomySignup.new(taxon.to_h).subscription_management_url)", + "code": "redirect_to(ContentItemSubscriberList.new(content_item.to_h).subscription_management_url)", "render_path": null, "location": { "type": "method", - "class": "TaxonomySignupsController", + "class": "ContentItemSignupsController", "method": "create" }, - "user_input": "TaxonomySignup.new(taxon.to_h).subscription_management_url", + "user_input": "ContentItemSubscriberList.new(content_item.to_h).subscription_management_url", "confidence": "High", "note": "This URL comes from the Content Store and we trust all the data in the Content Store." } ], - "updated": "2018-09-11 12:08:33 +0100", - "brakeman_version": "4.3.1" + "updated": "2019-04-11 16:11:16 +0000", + "brakeman_version": "4.5.0" } diff --git a/config/routes.rb b/config/routes.rb index b43c4723..5cbf1c7b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,9 +4,9 @@ get '/*base_path' => 'email_alert_signups#new', as: :email_alert_signup, constraints: { base_path: %r|.*/email-signup| } post '/*base_path' => 'email_alert_signups#create', as: :email_alert_signups, constraints: { base_path: %r|.*/email-signup| } - get '/email-signup' => 'taxonomy_signups#new', as: :new_taxonomy_signup - get '/email-signup/confirm' => 'taxonomy_signups#confirm', as: :confirm_taxonomy_signup - post '/email-signup' => 'taxonomy_signups#create' + get '/email-signup' => 'content_item_signups#new', as: :new_content_item_signup + get '/email-signup/confirm' => 'content_item_signups#confirm', as: :confirm_content_item_signup + post '/email-signup' => 'content_item_signups#create' scope '/email' do get '/unsubscribe/:id' => 'unsubscriptions#confirm', as: :confirm_unsubscribe diff --git a/features/step_definitions/taxonomy_email_alert_steps.rb b/features/step_definitions/taxonomy_email_alert_steps.rb index d87198ad..1c18a9c8 100644 --- a/features/step_definitions/taxonomy_email_alert_steps.rb +++ b/features/step_definitions/taxonomy_email_alert_steps.rb @@ -43,7 +43,7 @@ end When(/^i visit its signup page$/) do - visit new_taxonomy_signup_path(topic: @taxon[:base_path]) + visit new_content_item_signup_path(topic: @taxon[:base_path]) end Then(/^i can subscribe to the taxon or one of its children$/) do diff --git a/spec/controllers/taxonomy_signups_controller_spec.rb b/spec/controllers/content_item_signups_controller_spec.rb similarity index 97% rename from spec/controllers/taxonomy_signups_controller_spec.rb rename to spec/controllers/content_item_signups_controller_spec.rb index 55b1b814..17dd8478 100644 --- a/spec/controllers/taxonomy_signups_controller_spec.rb +++ b/spec/controllers/content_item_signups_controller_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -RSpec.describe TaxonomySignupsController do +RSpec.describe ContentItemSignupsController do include GdsApi::TestHelpers::ContentStore shared_examples 'handles bad input data correctly' do diff --git a/spec/models/content_item_subscriber_list_spec.rb b/spec/models/content_item_subscriber_list_spec.rb new file mode 100644 index 00000000..9c72902f --- /dev/null +++ b/spec/models/content_item_subscriber_list_spec.rb @@ -0,0 +1,53 @@ +require 'rails_helper' + +RSpec.describe ContentItemSubscriberList do + describe "#subscription_management_url" do + let(:mock_email_alert_api) do + instance_double(EmailAlertFrontend.services(:email_alert_api).class) + end + + let(:fake_taxon) { { 'document_type' => 'taxon', 'title' => 'Foo', 'content_id' => 'foo-id' } } + let(:fake_organisation) { { 'document_type' => 'organisation', 'title' => 'Org', 'content_id' => 'org-id' } } + before do + allow(EmailAlertFrontend) + .to receive(:services) + .with(:email_alert_api) + .and_return(mock_email_alert_api) + allow(mock_email_alert_api) + .to receive(:find_or_create_subscriber_list) + .and_return('subscriber_list' => { 'subscription_url' => '/something' }) + end + + context "given a taxon" do + it 'asks email-alert-api to find or create a subscriber list' do + signup = described_class.new(fake_taxon) + + expect(signup.has_content_item?).to be + expect(signup.subscription_management_url).to eq '/something' + expect(mock_email_alert_api) + .to have_received(:find_or_create_subscriber_list) + .with('title' => 'Foo', 'links' => { 'taxon_tree' => ['foo-id'] }) + end + end + + context 'when no taxon is present' do + it 'does nothing' do + signup = described_class.new(nil) + + expect(signup.has_content_item?).to_not be + end + end + + context "given an organisation" do + it 'asks email-alert-api to find or create a subscriber list' do + signup = described_class.new(fake_organisation) + + expect(signup.has_content_item?).to be + expect(signup.subscription_management_url).to eq '/something' + expect(mock_email_alert_api) + .to have_received(:find_or_create_subscriber_list) + .with('title' => 'Org', 'links' => { 'organisations' => ['org-id'] }) + end + end + end +end diff --git a/spec/models/taxonomy_signup_spec.rb b/spec/models/taxonomy_signup_spec.rb deleted file mode 100644 index befe92ff..00000000 --- a/spec/models/taxonomy_signup_spec.rb +++ /dev/null @@ -1,47 +0,0 @@ -require 'rails_helper' - -RSpec.describe TaxonomySignup do - describe "#save" do - let(:mock_email_alert_api) do - instance_double(EmailAlertFrontend.services(:email_alert_api).class) - end - - let(:fake_taxon) { { 'title' => 'Foo', 'content_id' => 'foo-id' } } - - before do - allow(EmailAlertFrontend) - .to receive(:services) - .with(:email_alert_api) - .and_return(mock_email_alert_api) - allow(mock_email_alert_api) - .to receive(:find_or_create_subscriber_list) - .and_return('subscriber_list' => { 'subscription_url' => '/something' }) - end - - it 'asks email-alert-api to find or create a subscriber list' do - signup = TaxonomySignup.new(fake_taxon) - - expect(signup.save).to be - expect(mock_email_alert_api) - .to have_received(:find_or_create_subscriber_list) - .with('title' => 'Foo', 'links' => { 'taxon_tree' => ['foo-id'] }) - end - - it 'sets the subscription management url' do - signup = TaxonomySignup.new(fake_taxon) - - expect(signup.save).to be - expect(signup.subscription_management_url).to eq '/something' - end - - context 'when no taxon present' do - it 'does nothing' do - signup = TaxonomySignup.new(nil) - - expect(signup.save).to_not be - expect(mock_email_alert_api).to_not have_received(:find_or_create_subscriber_list) - expect(signup.subscription_management_url).to eq nil - end - end - end -end diff --git a/spec/models/weekly_email_volume_spec.rb b/spec/models/weekly_email_volume_spec.rb index 261eb58c..e39aec83 100644 --- a/spec/models/weekly_email_volume_spec.rb +++ b/spec/models/weekly_email_volume_spec.rb @@ -1,22 +1,22 @@ require 'rails_helper' -RSpec.describe WeeklyEmailVolume do +RSpec.describe EmailVolume::WeeklyEmailVolume do include GdsApi::TestHelpers::ContentStore describe "#estimate" do let(:top_taxon) do - { base_path: '/top', links: { parent_taxons: [] } }.deep_stringify_keys + { document_type: 'taxon', base_path: '/top', links: { parent_taxons: [] } }.deep_stringify_keys end let(:second_taxon) do { - base_path: '/second', links: { parent_taxons: [{ base_path: '/top' }] } + document_type: 'taxon', base_path: '/second', links: { parent_taxons: [{ base_path: '/top' }] } }.deep_stringify_keys end context 'given a top level taxon' do it 'returns a HIGH range' do - expect(WeeklyEmailVolume.new(top_taxon).estimate).to eq WeeklyEmailVolume::HIGH + expect(described_class.new(top_taxon).estimate).to eq EmailVolume::TaxonWeeklyEmailVolume::HIGH end end @@ -26,14 +26,14 @@ end it 'returns a MEDIUM range' do - expect(WeeklyEmailVolume.new(second_taxon).estimate).to eq WeeklyEmailVolume::MEDIUM + expect(described_class.new(second_taxon).estimate).to eq EmailVolume::TaxonWeeklyEmailVolume::MEDIUM end end context 'given a 3rd level taxon' do let(:third_taxon) do { - base_path: '/third', links: { parent_taxons: [{ base_path: '/second' }] } + document_type: 'taxon', base_path: '/third', links: { parent_taxons: [{ base_path: '/second' }] } }.deep_stringify_keys end @@ -42,7 +42,41 @@ end it 'returns a LOW range' do - expect(WeeklyEmailVolume.new(third_taxon).estimate).to eq WeeklyEmailVolume::LOW + expect(described_class.new(third_taxon).estimate).to eq EmailVolume::TaxonWeeklyEmailVolume::LOW + end + end + + context 'given a top-level organisation' do + let(:top_organisation) do + { document_type: 'organisation', base_path: '/ministry-of-funny-walks', links: { ordered_parent_organisations: [] } }.deep_stringify_keys + end + let(:second_organisation) do + { document_type: 'organisation', base_path: '/ministry-of-quite-funny-walks', links: { ordered_parent_organisations: [{ base_path: '/ministry-of-funny-walks' }] } }.deep_stringify_keys + end + let(:third_organisation) do + { document_type: 'organisation', base_path: '/ministry-of-normal-walks', links: { ordered_parent_organisations: [{ base_path: '/ministry-of-quite-funny-walks' }] } }.deep_stringify_keys + end + + context 'given a top level organisation' do + it 'returns a HIGH range' do + expect(described_class.new(top_organisation).estimate).to eq EmailVolume::OrganisationWeeklyEmailVolume::HIGH + end + end + context 'given a 2nd level organisation' do + before do + content_store_has_item(top_organisation['base_path'], top_organisation) + end + it 'returns a MEDIUM range' do + expect(described_class.new(second_organisation).estimate).to eq EmailVolume::OrganisationWeeklyEmailVolume::MEDIUM + end + end + context 'given a 3rd level organisation' do + before do + content_store_has_item(second_organisation['base_path'], second_organisation) + end + it 'returns a LOW range' do + expect(described_class.new(third_organisation).estimate).to eq EmailVolume::OrganisationWeeklyEmailVolume::LOW + end end end end diff --git a/spec/unit/presenters/content_item_subscription_presenter_spec.rb b/spec/unit/presenters/content_item_subscription_presenter_spec.rb new file mode 100644 index 00000000..b8e94a23 --- /dev/null +++ b/spec/unit/presenters/content_item_subscription_presenter_spec.rb @@ -0,0 +1,22 @@ +require 'rails_helper' + +RSpec.describe ContentItemSubscriptionPresenter do + describe "#description" do + let(:fake_taxon) { { 'document_type' => 'taxon', 'description' => 'description of foo-id' } } + let(:fake_organisation) { { 'document_type' => 'organisation', 'description' => 'description of magic-id' } } + + context "given a taxon" do + it "Prepends 'this will include:'" do + presenter = described_class.new(fake_taxon) + expect(presenter.description).to eq 'This will include: description of foo-id' + end + end + + context "given an organisation" do + it 'returns the content item description' do + presenter = described_class.new(fake_organisation) + expect(presenter.description).to eq 'description of magic-id' + end + end + end +end