Skip to content

Commit c40535d

Browse files
authored
Merge pull request #2446 from seek4science/citation-style-error-fix
Validate citation styles
2 parents 7034ad3 + 50e8097 commit c40535d

File tree

9 files changed

+67
-14
lines changed

9 files changed

+67
-14
lines changed

app/controllers/citations_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ def fetch
1111
private
1212

1313
def set_citation_style
14-
session[:citation_style] = params[:style] if params[:style].present?
14+
session[:citation_style] = params[:style] if params[:style].present? && Seek::Citations.valid_style?(params[:style])
1515
end
1616
end

app/helpers/citations_helper.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@
44
module CitationsHelper
55
def render_doi_citation(doi, style)
66
Seek::Citations.from_doi(doi, style)
7+
rescue Seek::Citations::InvalidStyleException
8+
t('citations.errors.invalid_style')
79
rescue JSON::ParserError, RestClient::Exception
8-
'An error occurred whilst fetching the citation'
10+
t('citations.errors.general')
911
end
1012

1113
def render_cff_citation(blob, style)
1214
Seek::Citations.from_cff(blob, style)
13-
rescue StandardError
14-
'An error occurred whilst fetching the citation'
15+
rescue Seek::Citations::InvalidStyleException
16+
t('citations.errors.invalid_style')
17+
rescue JSON::ParserError, RestClient::Exception
18+
t('citations.errors.general')
1519
end
1620

1721
def citation_style_options(selected = nil)
@@ -20,6 +24,6 @@ def citation_style_options(selected = nil)
2024
end
2125

2226
def selected_citation_style
23-
session[:citation_style] || Seek::Citations::DEFAULT
27+
session[:citation_style] || Seek::Config.default_citation_style
2428
end
2529
end

config/initializers/seek_configuration.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ def load_seek_config_defaults!
287287
]
288288

289289
Seek::Config.default :reindex_all_batch_size, 50
290+
Seek::Config.default :default_citation_style, 'apa'
290291

291292
load_seek_testing_defaults! if Rails.env.test?
292293
end

config/locales/en.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,4 +351,8 @@ you can choose to have a %{project} linked to a site managed %{programme} instea
351351

352352
tooltips:
353353
batch_permission_changes_button: "You can change the sharing policy and permissions for your items as a batch. A preview of selected items will be given before you choose new permissions for them."
354-
publish_your_items_button: "Publish your owned items as a batch. A preview will be given before publishing"
354+
publish_your_items_button: "Publish your owned items as a batch. A preview will be given before publishing"
355+
citations:
356+
errors:
357+
general: An error occurred whilst fetching the citation
358+
invalid_style: Invalid citation style

lib/seek/citations.rb

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44

55
module Seek
66
class Citations
7-
DEFAULT = 'apa' # This could be a setting one day
8-
97
def self.from_doi(doi, style)
10-
generate(doi_to_csl(doi), style)
8+
validate_style(style)
9+
csl = doi_to_csl(doi)
10+
generate(csl, style)
1111
end
1212

1313
def self.from_cff(blob, style)
14-
generate(cff_to_csl(blob), style)
14+
validate_style(style)
15+
csl = cff_to_csl(blob)
16+
generate(csl, style)
1517
end
1618

1719
def self.generate(csl, style)
@@ -26,6 +28,10 @@ def self.style_pairs
2628
end
2729
end
2830

31+
def self.valid_styles
32+
@valid_styles ||= Set.new(style_pairs.map(&:last))
33+
end
34+
2935
def self.doi_to_csl(doi)
3036
Rails.cache.fetch("citation-#{doi}") do
3137
resp = RestClient.get("https://doi.org/#{Addressable::URI.escape(doi)}", accept: 'application/vnd.citationstyles.csl+json')
@@ -47,5 +53,15 @@ def self.generate_style_pairs
4753
def self.style_dictionary_path
4854
Rails.root.join('config/default_data/csl_styles.yml')
4955
end
56+
57+
def self.valid_style?(style)
58+
valid_styles.include?(style)
59+
end
60+
61+
private
62+
63+
def self.validate_style(style)
64+
raise Seek::Citations::InvalidStyleException unless valid_style?(style)
65+
end
5066
end
5167
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class Seek::Citations::InvalidStyleException < StandardError; end

lib/seek/config_setting_attributes.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,4 @@ max_filters:
278278
convert: :to_i
279279
reindex_all_batch_size:
280280
convert: :to_i
281+
default_citation_style:

lib/seek/renderers/citation_renderer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def render_content
1212
private
1313

1414
def style
15-
params[:style] || Seek::Citations::DEFAULT
15+
params[:style] || Seek::Config.default_citation_style
1616
end
1717
end
1818
end

test/integration/citation_style_test.rb

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,52 @@ def setup
1515
get model_path(@model)
1616
# APA by default
1717
assert_select '#citation' do
18-
assert_select 'div[data-citation-style=?]', Seek::Citations::DEFAULT, text: /Bacall, F/, count: 1
18+
assert_select 'div[data-citation-style=?]', Seek::Config.default_citation_style, text: /Bacall, F/, count: 1
1919
end
2020
assert_select '#citation-style-select' do
21-
assert_select "option[selected='selected'][value=?]", Seek::Citations::DEFAULT
21+
assert_select "option[selected='selected'][value=?]", Seek::Config.default_citation_style
2222
end
2323

2424
new_style = 'journal-of-infectious-diseases'
2525

26+
assert_nil session[:citation_style]
2627
get citation_path(@doi, style: new_style, format: :js), xhr: true
28+
assert_equal new_style, session[:citation_style]
2729

2830
get model_path(@model)
2931
assert_select '#citation' do
30-
assert_select 'div[data-citation-style=?]', Seek::Citations::DEFAULT, count: 0
32+
assert_select 'div[data-citation-style=?]', Seek::Config.default_citation_style, count: 0
3133
assert_select 'div[data-citation-style=?]', new_style, text: /Bacall F/, count: 1
3234
end
3335
assert_select '#citation-style-select' do
3436
assert_select "option[selected='selected'][value=?]", new_style
3537
end
3638
end
3739

40+
test 'does not remember style if invalid style chosen' do
41+
assert_nil session[:citation_style]
42+
get citation_path(@doi, style: 'ghdfgkhdfjkghdkj', format: :js), xhr: true
43+
assert_nil session[:citation_style]
44+
end
45+
46+
test 'handles invalid style selection' do
47+
doi_citation_mock
48+
49+
get citation_path(@doi, style: 'fjkgdfhgkjdf123', format: :js), xhr: true
50+
51+
assert_includes response.body, 'Invalid citation style'
52+
end
53+
54+
test 'handles CSL fetch error' do
55+
stub_request(:get, /(https?:\/\/)?(dx\.)?doi\.org\/.+/)
56+
.with(headers: { 'Accept' => 'application/vnd.citationstyles.csl+json' })
57+
.to_return(body: 'Error!', status: 500)
58+
59+
get citation_path(@doi, style: 'apa', format: :js), xhr: true
60+
61+
assert_includes response.body, 'An error occurred whilst fetching the citation'
62+
end
63+
3864
private
3965

4066
def doi_citation_mock

0 commit comments

Comments
 (0)