diff --git a/.rubocop.yml b/.rubocop.yml index bda431a00..c92e19cdc 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -73,9 +73,11 @@ Metrics/ParameterLists: - app/models/jupiter_core/search.rb - app/models/jupiter_core/solr_services/deferred_faceted_solr_query.rb -# TODO: our tests are quite smelly. We should be working to get this lower! Minitest/MultipleAssertions: Max: 15 # default 3 + Exclude: + - test/system/admin_users_show_test.rb + - test/system/search_test.rb Naming/FileName: Exclude: diff --git a/CHANGELOG.md b/CHANGELOG.md index 27feb6cd0..5548131bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ New entries in this file should aim to provide a meaningful amount of informatio ## Unreleased +### Changed +- Refactored tests into smaller tests [PR#2563](https://github.com/ualbertalib/jupiter/pull/2563) + ## [2.4.4] - 2023-02-17 ### Added - Push collections and communities to preservation on save along with a rake task to do so [#255](https://github.com/ualbertalib/pushmi_pullyu/issues/255) diff --git a/test/integration/oaisys_list_identifiers_test.rb b/test/integration/oaisys_list_identifiers_test.rb deleted file mode 100644 index 4ac1d02d9..000000000 --- a/test/integration/oaisys_list_identifiers_test.rb +++ /dev/null @@ -1,536 +0,0 @@ -require 'test_helper' - -class OaisysListIdentifiersTest < ActionDispatch::IntegrationTest - - include Oaisys::Engine.routes.url_helpers - - setup do - @routes = Oaisys::Engine.routes - Oaisys::Engine.config.items_per_request = 2 - - @community = communities(:community_fancy) - @collection = collections(:collection_fancy) - - @thesis_collection = collections(:collection_thesis) - @embargoed_thesis_collection = collections(:collection_embargoed) - end - - # rubocop:disable Minitest/MultipleAssertions - # TODO: our tests are quite smelly. This one needs work! - test 'list identifers items xml' do - # TODO: Add tests for this which uses post requests. - skip('Skipping until bug regarding path helper is fixed. https://github.com/rails/rails/issues/40078') - get oaisys_path(verb: 'ListIdentifiers', metadataPrefix: 'oai_dc'), headers: { 'Accept' => 'application/xml' } - assert_response :success - - assert_valid_against_schema - - item_identifiers = Oaisys::Engine.config.oai_dc_model.public_items.page(1) - .per(Oaisys::Engine.config.items_per_request) - .pluck(:id, :updated_at, :member_of_paths) - assert_select 'OAI-PMH' do - assert_select 'responseDate' - assert_select 'request' - assert_select 'ListIdentifiers' do - item_identifiers.each do |identifier, date, sets| - assert_select 'header' do - assert_select 'identifier', "oai:era.library.ualberta.ca:#{identifier}" - assert_select 'datestamp', date.utc.xmlschema - sets.each do |set| - assert_select 'setSpec', set.tr('/', ':') - end - end - end - assert_select 'resumptionToken' - end - end - - resumption_token = document.css('OAI-PMH ListIdentifiers resumptionToken').text - # TODO: look into why every second request to Oaisys in the same test gives a 503. - get oaisys_path(verb: 'ListIdentifiers', resumptionToken: resumption_token), - headers: { 'Accept' => 'application/xml' } - - # Test use of resumption token. - get oaisys_path(verb: 'ListIdentifiers', resumptionToken: resumption_token), - headers: { 'Accept' => 'application/xml' } - assert_response :success - - document = Nokogiri::XML(@response.body) - assert_empty schema.validate(document) - item_identifiers = Oaisys::Engine.config.oai_dc_model.public_items.page(2) - .per(Oaisys::Engine.config.items_per_request) - .pluck(:id, :updated_at, :member_of_paths) - - assert_select 'OAI-PMH' do - assert_select 'responseDate' - assert_select 'request' - assert_select 'ListIdentifiers' do - item_identifiers.each do |identifier, date, sets| - assert_select 'header' do - assert_select 'identifier', "oai:era.library.ualberta.ca:#{identifier}" - assert_select 'datestamp', date.utc.xmlschema - sets.each do |set| - assert_select 'setSpec', set.tr('/', ':') - end - end - end - assert_select 'resumptionToken' - end - end - - resumption_token = document.css('OAI-PMH ListIdentifiers resumptionToken').text - # TODO: look into why every second request to Oaisys in the same test gives a 503. - get oaisys_path(verb: 'ListIdentifiers', resumptionToken: resumption_token), - headers: { 'Accept' => 'application/xml' } - - # Test expiration of resumption token when results change. - @item = Item.new(visibility: JupiterCore::VISIBILITY_PUBLIC, - owner_id: users(:user_admin).id, title: 'Fancy Item 1', - creators: ['Joe Blow'], - created: '1938-01-02', - languages: [ControlledVocabulary.era.language.english], - item_type: ControlledVocabulary.era.item_type.article, - publication_status: - [ControlledVocabulary.era.publication_status.published], - license: ControlledVocabulary.era.license.attribution_4_0_international, - subject: ['Items']).tap do |uo| - uo.add_to_path(@community.id, @collection.id) - uo.save! - end - - get oaisys_path(verb: 'ListIdentifiers', resumptionToken: resumption_token), - headers: { 'Accept' => 'application/xml' } - assert_response :success - - assert_select 'OAI-PMH' do - assert_select 'responseDate' - assert_select 'request' - assert_select 'error', I18n.t('error_messages.resumption_token_invalid') - end - end - # rubocop:enable Minitest/MultipleAssertions - - test 'list identifers theses xml' do - get oaisys_path(verb: 'ListIdentifiers', metadataPrefix: 'oai_etdms'), headers: { 'Accept' => 'application/xml' } - - list_identifiers_theses_xml - end - - test 'list identifers theses xml post' do - post oaisys_path, params: { verb: 'ListIdentifiers', metadataPrefix: 'oai_etdms' }, - headers: { 'Content-Type' => 'application/x-www-form-urlencoded', 'Content-Length' => 82 } - - list_identifiers_theses_xml - end - test 'list identifers item set xml' do - get oaisys_path(verb: 'ListIdentifiers', metadataPrefix: 'oai_dc', set: @collection.id), - headers: { 'Accept' => 'application/xml' } - - list_identifiers_item_set_xml - end - - test 'list identifers item set xml post' do - post oaisys_path, params: { verb: 'ListIdentifiers', metadataPrefix: 'oai_dc', set: @collection.id }, - headers: { 'Accept' => 'application/xml' } - - list_identifiers_item_set_xml - end - - test 'list identifers thesis set xml' do - get oaisys_path(verb: 'ListIdentifiers', metadataPrefix: 'oai_etdms', set: @thesis_collection.id), - headers: { 'Accept' => 'application/xml' } - - list_identifiers_thesis_set_xml - end - - test 'list identifers thesis set xml post' do - post oaisys_path, params: { verb: 'ListIdentifiers', metadataPrefix: 'oai_etdms', set: @thesis_collection.id }, - headers: { 'Accept' => 'application/xml' } - - list_identifiers_thesis_set_xml - end - - test 'list identifers embargo thesis xml' do - get oaisys_path(verb: 'ListIdentifiers', metadataPrefix: 'oai_etdms', set: @embargoed_thesis_collection.id), - headers: { 'Accept' => 'application/xml' } - - assert_no_records_found_response - end - - test 'list identifers embargo thesis xml post' do - post oaisys_path, params: { verb: 'ListIdentifiers', metadataPrefix: 'oai_etdms', - set: @embargoed_thesis_collection.id }, - headers: { 'Accept' => 'application/xml' } - - assert_no_records_found_response - end - - test 'list identifers item until date xml' do - just_after_current_time = (Time.current + 5).utc.xmlschema - get oaisys_path(verb: 'ListIdentifiers', metadataPrefix: 'oai_dc', set: @community.id, - until: just_after_current_time), headers: { 'Accept' => 'application/xml' } - - list_identifiers_item_until_date_xml(just_after_current_time) - end - - test 'list identifers item until date xml post' do - just_after_current_time = (Time.current + 5).utc.xmlschema - post oaisys_path, params: { verb: 'ListIdentifiers', metadataPrefix: 'oai_dc', set: @community.id, - until: just_after_current_time }, - headers: { 'Content-Type' => 'application/x-www-form-urlencoded', 'Content-Length' => 82 } - - list_identifiers_item_until_date_xml(just_after_current_time) - end - - test 'list identifers thesis until date xml' do - just_after_current_time = (Time.current + 5).utc.xmlschema - get oaisys_path(verb: 'ListIdentifiers', metadataPrefix: 'oai_etdms', set: @thesis_collection.id, - until: just_after_current_time), headers: { 'Accept' => 'application/xml' } - - list_identifiers_thesis_until_date_xml(just_after_current_time) - end - - test 'list identifers thesis until date xml post' do - just_after_current_time = (Time.current + 5).utc.xmlschema - post oaisys_path, params: { verb: 'ListIdentifiers', metadataPrefix: 'oai_etdms', set: @thesis_collection.id, - until: just_after_current_time }, - headers: { 'Content-Type' => 'application/x-www-form-urlencoded', 'Content-Length' => 82 } - - list_identifiers_thesis_until_date_xml(just_after_current_time) - end - - test 'list identifers thesis until date yyyy/mm/dd xml' do - current_date = Time.current.strftime('%Y-%m-%d') - get oaisys_path(verb: 'ListIdentifiers', metadataPrefix: 'oai_etdms', set: @thesis_collection.id, - until: current_date), headers: { 'Accept' => 'application/xml' } - - list_identifiers_thesis_until_date_yyy_mm_dd_xml(current_date) - end - - test 'list identifers thesis until date yyyy/mm/dd xml post' do - current_date = Time.current.strftime('%Y-%m-%d') - post oaisys_path, params: { verb: 'ListIdentifiers', metadataPrefix: 'oai_etdms', set: @thesis_collection.id, - until: current_date }, headers: { 'Content-Type' => 'application/x-www-form-urlencoded', - 'Content-Length' => 82 } - - list_identifiers_thesis_until_date_yyy_mm_dd_xml(current_date) - end - - test 'list identifers item from date xml' do - six_days_from_now = 6.days.from_now.strftime('%Y-%m-%d') - get oaisys_path(verb: 'ListIdentifiers', metadataPrefix: 'oai_dc', set: @community.id, - from: six_days_from_now), headers: { 'Accept' => 'application/xml' } - - assert_no_records_found_response - end - - test 'list identifers item from date xml post' do - six_days_from_now = 6.days.from_now.strftime('%Y-%m-%d') - post oaisys_path, params: { verb: 'ListIdentifiers', metadataPrefix: 'oai_dc', set: @community.id, - from: six_days_from_now }, - headers: { 'Content-Type' => 'application/x-www-form-urlencoded', 'Content-Length' => 82 } - - assert_no_records_found_response - end - - test 'list identifers item from date yyy_mm_dd xml' do - six_days_from_now = 6.days.from_now.utc.xmlschema - get oaisys_path(verb: 'ListIdentifiers', metadataPrefix: 'oai_dc', set: @community.id, - from: six_days_from_now), headers: { 'Accept' => 'application/xml' } - - assert_no_records_found_response - end - - test 'list identifers item from date yyy_mm_dd xml post' do - six_days_from_now = 6.days.from_now.utc.xmlschema - post oaisys_path, params: { verb: 'ListIdentifiers', metadataPrefix: 'oai_dc', set: @community.id, - from: six_days_from_now }, - headers: { 'Content-Type' => 'application/x-www-form-urlencoded', 'Content-Length' => 82 } - - assert_no_records_found_response - end - - test 'list identifers thesis from date xml' do - six_days_from_now = (Time.current + 5).utc.xmlschema - get oaisys_path(verb: 'ListIdentifiers', metadataPrefix: 'oai_etdms', set: @thesis_collection.id, - from: six_days_from_now), headers: { 'Accept' => 'application/xml' } - - assert_no_records_found_response - end - - test 'list identifers thesis from date xml post' do - six_days_from_now = (Time.current + 5).utc.xmlschema - post oaisys_path, params: { verb: 'ListIdentifiers', metadataPrefix: 'oai_etdms', set: @thesis_collection.id, - from: six_days_from_now }, - headers: { 'Content-Type' => 'application/x-www-form-urlencoded', 'Content-Length' => 82 } - - assert_no_records_found_response - end - - test 'list identifers item from until date xml' do - item = Oaisys::Engine.config.oai_dc_model.public_items.belongs_to_path(@community.id).first - item_creation_time = item[:updated_at].utc.xmlschema - just_after_item_creation_time = (item[:updated_at] + 5.seconds).utc.xmlschema - get oaisys_path(verb: 'ListIdentifiers', metadataPrefix: 'oai_dc', set: @community.id, from: item_creation_time, - until: just_after_item_creation_time), - headers: { 'Accept' => 'application/xml' } - - assert_item_is_displayed(item, item_creation_time) - end - - test 'list identifers item from until date xml post' do - item = Oaisys::Engine.config.oai_dc_model.public_items.belongs_to_path(@community.id).first - item_creation_time = item[:updated_at].utc.xmlschema - just_after_item_creation_time = (item[:updated_at] + 5.seconds).utc.xmlschema - post oaisys_path, params: { verb: 'ListIdentifiers', metadataPrefix: 'oai_dc', set: @community.id, - from: item_creation_time, until: just_after_item_creation_time }, - headers: { 'Accept' => 'application/xml' } - - assert_item_is_displayed(item, item_creation_time) - end - - test 'list identifers thesis from until date xml' do - thesis = Oaisys::Engine.config.oai_etdms_model.public_items.belongs_to_path(@thesis_collection.id).first - thesis_creation_time = thesis[:updated_at].utc.xmlschema - just_after_thesis_creation_time = (thesis[:updated_at] + 5.seconds).utc.xmlschema - get oaisys_path(verb: 'ListIdentifiers', metadataPrefix: 'oai_etdms', set: @thesis_collection.id, - from: thesis_creation_time, until: just_after_thesis_creation_time), - headers: { 'Accept' => 'application/xml' } - - assert_item_is_displayed(thesis, thesis_creation_time) - end - - test 'list identifers thesis from until date xml post' do - thesis = Oaisys::Engine.config.oai_etdms_model.public_items.belongs_to_path(@thesis_collection.id).first - thesis_creation_time = thesis[:updated_at].utc.xmlschema - just_after_thesis_creation_time = (thesis[:updated_at] + 5.seconds).utc.xmlschema - post oaisys_path, params: { verb: 'ListIdentifiers', metadataPrefix: 'oai_etdms', set: @thesis_collection.id, - from: thesis_creation_time, until: just_after_thesis_creation_time }, - headers: { 'Accept' => 'application/xml' } - - assert_item_is_displayed(thesis, thesis_creation_time) - end - - test 'list identifers thesis from until date yyy_mm_dd xml' do - thesis = Oaisys::Engine.config.oai_etdms_model.public_items.belongs_to_path(@thesis_collection.id).first - thesis_creation_time = thesis[:updated_at].strftime('%Y-%m-%d') - thesis_creation_time_utc = thesis[:updated_at].utc.xmlschema - just_after_thesis_creation_time = (thesis[:updated_at] + 5.seconds).strftime('%Y-%m-%d') - get oaisys_path(verb: 'ListIdentifiers', metadataPrefix: 'oai_etdms', set: @thesis_collection.id, - from: thesis_creation_time, until: just_after_thesis_creation_time), - headers: { 'Accept' => 'application/xml' } - - assert_item_is_displayed(thesis, thesis_creation_time_utc) - end - - test 'list identifers thesis from until date yyy_mm_dd xml post' do - thesis = Oaisys::Engine.config.oai_etdms_model.public_items.belongs_to_path(@thesis_collection.id).first - thesis_creation_time = thesis[:updated_at].strftime('%Y-%m-%d') - thesis_creation_time_utc = thesis[:updated_at].utc.xmlschema - just_after_thesis_creation_time = (thesis[:updated_at] + 5.seconds).strftime('%Y-%m-%d') - post oaisys_path, params: { verb: 'ListIdentifiers', metadataPrefix: 'oai_etdms', set: @thesis_collection.id, - from: thesis_creation_time, until: just_after_thesis_creation_time }, - headers: { 'Accept' => 'application/xml' } - - assert_item_is_displayed(thesis, thesis_creation_time_utc) - end - - test 'list identifers item invalid from date xml' do - get oaisys_path(verb: 'ListIdentifiers', metadataPrefix: 'oai_dc', set: @community.id, - from: 'junk'), headers: { 'Accept' => 'application/xml' } - assert_response :success - - assert_bad_argument_response - end - - test 'list identifers item invalid from date xml post' do - post oaisys_path, params: { verb: 'ListIdentifiers', metadataPrefix: 'oai_dc', set: @community.id, - from: 'junk' }, headers: { 'Content-Type' => 'application/x-www-form-urlencoded', - 'Content-Length' => 82 } - assert_response :success - - assert_bad_argument_response - end - - test 'list identifers item invalid until date xml' do - get oaisys_path(verb: 'ListIdentifiers', metadataPrefix: 'oai_dc', set: @community.id, - until: 'junk'), headers: { 'Accept' => 'application/xml' } - assert_response :success - - assert_bad_argument_response - end - - test 'list identifers item invalid until date xml post' do - post oaisys_path, params: { verb: 'ListIdentifiers', metadataPrefix: 'oai_dc', set: @community.id, - until: 'junk' }, headers: { 'Content-Type' => 'application/x-www-form-urlencoded', - 'Content-Length' => 82 } - assert_response :success - - assert_bad_argument_response - end - - test 'list identifers item invalid from until date xml' do - get oaisys_path(verb: 'ListIdentifiers', metadataPrefix: 'oai_dc', set: @community.id, - from: 'junk', until: 'junk'), headers: { 'Accept' => 'application/xml' } - assert_response :success - - assert_bad_argument_response - end - - test 'list identifers item invalid from until date xml post' do - post oaisys_path, params: { verb: 'ListIdentifiers', metadataPrefix: 'oai_dc', set: @community.id, from: 'junk', - until: 'junk' }, headers: { 'Content-Type' => 'application/x-www-form-urlencoded', - 'Content-Length' => 82 } - assert_response :success - - assert_bad_argument_response - end - - test 'list identifers item different granularities from until date xml' do - item = Oaisys::Engine.config.oai_dc_model.public_items.belongs_to_path(@community.id).first - just_after_item_creation_time = (item[:updated_at] + 5.seconds).utc.xmlschema - get oaisys_path(verb: 'ListIdentifiers', metadataPrefix: 'oai_dc', set: @community.id, from: '1000/01/01', - until: just_after_item_creation_time), - headers: { 'Accept' => 'application/xml' } - - assert_bad_argument_response - end - - test 'list identifers item different granularities from until date post xml' do - item = Oaisys::Engine.config.oai_dc_model.public_items.belongs_to_path(@community.id).first - just_after_item_creation_time = (item[:updated_at] + 5.seconds).utc.xmlschema - post oaisys_path, params: { verb: 'ListIdentifiers', metadataPrefix: 'oai_dc', set: @community.id, - from: '1000/01/01', until: just_after_item_creation_time }, - headers: { 'Accept' => 'application/xml' } - - assert_bad_argument_response - end - - private - - def list_identifiers_item_until_date_xml(just_after_current_time) - assert_valid_against_schema - - item_identifiers = Oaisys::Engine.config.oai_dc_model.public_items.updated_before(just_after_current_time) - .limit(Oaisys::Engine.config.items_per_request) - .belongs_to_path(@community.id).pluck(:id, :updated_at, :member_of_paths) - - assert_identifiers_response(item_identifiers) - end - - def list_identifiers_thesis_set_xml - assert_valid_against_schema - - thesis_identifiers = Oaisys::Engine.config.oai_etdms_model.public_items.belongs_to_path(@thesis_collection.id) - .limit(Oaisys::Engine.config.items_per_request) - .pluck(:id, :updated_at, :member_of_paths) - - assert_identifiers_response(thesis_identifiers) - end - - def list_identifiers_thesis_until_date_yyy_mm_dd_xml(current_date) - assert_valid_against_schema - - thesis_identifiers = Oaisys::Engine.config.oai_etdms_model - .public_items.updated_before(current_date) - .belongs_to_path(@thesis_collection.id) - .limit(Oaisys::Engine.config.items_per_request) - .pluck(:id, :updated_at, :member_of_paths) - - assert_identifiers_response(thesis_identifiers) - end - - def assert_item_is_displayed(item, item_creation_time) - assert_valid_against_schema - - assert_select 'OAI-PMH' do - assert_select 'responseDate' - assert_select 'request' - assert_select 'ListIdentifiers' do - assert_select 'header' do - assert_select 'identifier', "oai:era.library.ualberta.ca:#{item[:id]}" - assert_select 'datestamp', item_creation_time - item[:member_of_paths].each do |set| - assert_select 'setSpec', set.tr('/', ':') - end - end - end - end - end - - def list_identifiers_thesis_until_date_xml(just_after_current_time) - assert_valid_against_schema - - thesis_identifiers = Oaisys::Engine.config.oai_etdms_model - .public_items.updated_before(just_after_current_time) - .belongs_to_path(@thesis_collection.id) - .limit(Oaisys::Engine.config.items_per_request) - .pluck(:id, :updated_at, :member_of_paths) - assert_identifiers_response(thesis_identifiers) - end - - def list_identifiers_theses_xml - assert_valid_against_schema - - thesis_identifiers = Oaisys::Engine.config.oai_etdms_model.public_items - .limit(Oaisys::Engine.config.items_per_request) - .pluck(:id, :updated_at, :member_of_paths) - assert_identifiers_response(thesis_identifiers) - end - - def list_identifiers_item_set_xml - assert_valid_against_schema - - item_identifiers = Oaisys::Engine.config.oai_dc_model.public_items.belongs_to_path(@collection.id) - .limit(Oaisys::Engine.config.items_per_request) - .pluck(:id, :updated_at, :member_of_paths) - assert_identifiers_response(item_identifiers) - end - - def assert_no_records_found_response - assert_valid_against_schema - - assert_select 'OAI-PMH' do - assert_select 'responseDate' - assert_select 'request' - assert_select 'error', I18n.t('error_messages.no_record_found') - end - end - - def assert_bad_argument_response - assert_valid_against_schema - - assert_select 'OAI-PMH' do - assert_select 'responseDate' - assert_select 'request' - assert_select 'error', I18n.t('error_messages.illegal_or_missing_arguments') - end - end - - def assert_valid_against_schema - assert_response :success - - schema = Nokogiri::XML::Schema(File.open(file_fixture('OAI-PMH.xsd')), Nokogiri::XML::ParseOptions.new.nononet) - document = Nokogiri::XML(@response.body) - assert_empty schema.validate(document) - end - - def assert_identifiers_response(identifiers) - assert_select 'OAI-PMH' do - assert_select 'responseDate' - assert_select 'request' - assert_select 'ListIdentifiers' do - identifiers.each do |identifier, date, sets| - assert_select 'header' do - assert_select 'identifier', "oai:era.library.ualberta.ca:#{identifier}" - assert_select 'datestamp', date.utc.xmlschema - sets.each do |set| - assert_select 'setSpec', set.tr('/', ':') - end - end - end - end - end - end - -end diff --git a/test/integration/oaisys_list_sets_test.rb b/test/integration/oaisys_list_sets_test.rb deleted file mode 100644 index 97bbbaa0a..000000000 --- a/test/integration/oaisys_list_sets_test.rb +++ /dev/null @@ -1,110 +0,0 @@ -require 'test_helper' - -class OaisysListSetsTest < ActionDispatch::IntegrationTest - - include Oaisys::Engine.routes.url_helpers - - setup do - @routes = Oaisys::Engine.routes - Oaisys::Engine.config.items_per_request = 3 - end - - # rubocop:disable Minitest/MultipleAssertions - # TODO: our tests are quite smelly. This one needs work! - test 'test_list_sets_resumption_token_xml' do - # TODO: Add tests for this which uses post requests. - skip('Skipping until bug regarding path helper is fixed. https://github.com/rails/rails/issues/40078') - get oaisys_path(verb: 'ListSets'), headers: { 'Accept' => 'application/xml' } - assert_response :success - - schema = Nokogiri::XML::Schema(File.open(file_fixture('OAI-PMH.xsd'))) - document = Nokogiri::XML(@response.body) - assert_empty schema.validate(document) - - top_level_sets = Oaisys::Engine.config.top_level_sets_model.pluck(:id, :title) - sets = Oaisys::Engine.config.set_model.page(1) - .per(Oaisys::Engine.config.items_per_request) - .pluck(:community_id, :id, :title, :description) - assert_select 'OAI-PMH' do - assert_select 'responseDate' - assert_select 'request' - assert_select 'ListSets' do - sets.each do |community_id, id, title, description| - assert_select 'set' do - assert_select 'setSpec', "#{community_id}:#{id}" - assert_select 'setName', "#{top_level_sets.find { |a| a[0] == community_id }[1]} / #{title}" - if description.present? - assert_select 'setDescription' do - assert_select 'oai_dc|dc' do - assert_select 'dc|description', description - end - end - end - end - end - assert_select 'resumptionToken' - end - end - - resumption_token = document.css('OAI-PMH ListSets resumptionToken').text - # TODO: look into why every second request to Oaisys in the same test gives a 503. - get oaisys_path(verb: 'ListSets', resumptionToken: resumption_token), - headers: { 'Accept' => 'application/xml' } - - # Test use of resumption token. - get oaisys_path(verb: 'ListSets', resumptionToken: resumption_token), - headers: { 'Accept' => 'application/xml' } - assert_response :success - - schema = Nokogiri::XML::Schema(File.open(file_fixture('OAI-PMH.xsd'))) - document = Nokogiri::XML(@response.body) - assert_empty schema.validate(document) - - top_level_sets = Oaisys::Engine.config.top_level_sets_model.pluck(:id, :title) - sets = Oaisys::Engine.config.set_model.page(2) - .per(Oaisys::Engine.config.items_per_request) - .pluck(:community_id, :id, :title, :description) - assert_select 'OAI-PMH' do - assert_select 'responseDate' - assert_select 'request' - assert_select 'ListSets' do - sets.each do |community_id, id, title, description| - assert_select 'set' do - assert_select 'setSpec', "#{community_id}:#{id}" - assert_select 'setName', "#{top_level_sets.find { |a| a[0] == community_id }[1]} / #{title}" - if description.present? - assert_select 'setDescription' do - assert_select 'oai_dc|dc' do - assert_select 'dc|description', description - end - end - end - end - end - assert_select 'resumptionToken' - end - end - - resumption_token = document.css('OAI-PMH ListIdentifiers resumptionToken').text - # TODO: look into why every second request to Oaisys in the same test gives a 503. - get oaisys_path(verb: 'ListIdentifiers', resumptionToken: resumption_token), - headers: { 'Accept' => 'application/xml' } - - # Test expiration of resumption token when results change. - Collection.create!(community_id: communities(:community_books).id, - title: 'Fancy Collection 7', owner_id: users(:user_admin).id, - description: '') - - get oaisys_path(verb: 'ListSets', resumptionToken: resumption_token), - headers: { 'Accept' => 'application/xml' } - assert_response :success - - assert_select 'OAI-PMH' do - assert_select 'responseDate' - assert_select 'request' - assert_select 'error', I18n.t('error_messages.resumption_token_invalid') - end - end - # rubocop:enable Minitest/MultipleAssertions - -end diff --git a/test/integration/site_for_bots_test.rb b/test/integration/site_for_bots_test.rb index 0e5e1b6a5..36fe74066 100644 --- a/test/integration/site_for_bots_test.rb +++ b/test/integration/site_for_bots_test.rb @@ -111,7 +111,7 @@ class SiteForBotsTest < ActionDispatch::IntegrationTest assert_select "a[rel='nofollow']", text: @item.subject.first end - test 'structured data for google scholar' do + test 'structured thesis data for google scholar' do # TODO: can be a lot more complicated see https://scholar.google.com/intl/en/scholar/inclusion.html#indexing get item_path @thesis assert_select "meta[name='citation_title'][content='#{@thesis.title}']" @@ -125,7 +125,9 @@ class SiteForBotsTest < ActionDispatch::IntegrationTest )) assert_select "meta[name='dc.identifier'][content='#{@item.doi}']" assert_select "meta[name='citation_doi'][content='#{@item.doi}']" + end + test 'structured item data for google scholar' do get item_path @item assert_select "meta[name='citation_title'][content='#{@item.title}']" @item.creators.each do |author| diff --git a/test/jobs/update_user_activity_job_test.rb b/test/jobs/update_user_activity_job_test.rb new file mode 100644 index 000000000..cc32de789 --- /dev/null +++ b/test/jobs/update_user_activity_job_test.rb @@ -0,0 +1,20 @@ +require 'test_helper' + +class UpdateUserActivityJobTest < ActiveSupport::TestCase + + test 'should update the user activity (last seen at and IP) through the job' do + user = users(:user_admin) + ip = '4.73.73.73' + now = Time.now.utc.to_s + UpdateUserActivityJob.perform_now(user.id, now, ip) + user.reload + assert_equal user.last_seen_at.to_s, now + assert_equal user.last_seen_ip, ip + # Still does not change sign-in information + assert_predicate user.last_sign_in_at, :blank? + assert_predicate user.last_sign_in_ip, :blank? + assert_predicate user.previous_sign_in_at, :blank? + assert_predicate user.previous_sign_in_ip, :blank? + end + +end diff --git a/test/models/user_test.rb b/test/models/user_test.rb index c3b9d6876..3c943d5e5 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -23,83 +23,47 @@ class UserTest < ActiveSupport::TestCase assert_equal "User:#{user.id}", user.flipper_id end - # rubocop:disable Minitest/MultipleAssertions - # TODO: our tests are quite smelly. This one needs work! - test 'should update the activity columns when not signing-in' do - user = users(:user_regular) + test 'should update the activity columns when not signing-in for new user without activity update' do + user = users(:user_admin) assert_predicate user.last_seen_at, :blank? assert_predicate user.last_sign_in_at, :blank? assert_predicate user.previous_sign_in_at, :blank? + end + + test 'should update the activity columns when not signing-in' do + user = users(:user_regular) - ip1 = '4.26.50.50' - now1 = Time.now.utc.to_s + ip = '4.26.50.50' + now = Time.now.utc.to_s - user.update_activity!(now1, ip1) + user.update_activity!(now, ip) user.reload assert_predicate user.last_seen_at, :present? - assert_equal user.last_seen_at.to_s, now1 - assert_equal user.last_seen_ip, ip1 + assert_equal user.last_seen_at.to_s, now + assert_equal user.last_seen_ip, ip # Does not change sign-in information assert_predicate user.last_sign_in_at, :blank? assert_predicate user.last_sign_in_ip, :blank? assert_predicate user.previous_sign_in_at, :blank? assert_predicate user.previous_sign_in_ip, :blank? - - travel 1.hour do - ip2 = '4.73.73.73' - now2 = Time.now.utc.to_s - assert_not_equal now2, now1 - UpdateUserActivityJob.perform_now(user.id, now2, ip2) - user.reload - assert_equal user.last_seen_at.to_s, now2 - assert_equal user.last_seen_ip, ip2 - # Still does not change sign-in information - assert_predicate user.last_sign_in_at, :blank? - assert_predicate user.last_sign_in_ip, :blank? - assert_predicate user.previous_sign_in_at, :blank? - assert_predicate user.previous_sign_in_ip, :blank? - end end - # rubocop:enable Minitest/MultipleAssertions - # rubocop:disable Minitest/MultipleAssertions - # TODO: our tests are quite smelly. This one needs work! test 'should update the activity columns when signing-in' do user = users(:user_regular) - assert_predicate user.last_seen_at, :blank? - assert_predicate user.last_sign_in_at, :blank? - assert_predicate user.previous_sign_in_at, :blank? - - ip1 = '4.26.50.50' - now1 = Time.now.utc.to_s + ip = '4.26.50.50' + now = Time.now.utc.to_s - user.update_activity!(now1, ip1, sign_in: true) + user.update_activity!(now, ip, sign_in: true) user.reload assert_predicate user.last_seen_at, :present? - assert_equal user.last_seen_at.to_s, now1 - assert_equal user.last_seen_ip, ip1 - assert_equal user.last_sign_in_at.to_s, now1 - assert_equal user.last_sign_in_ip, ip1 + assert_equal user.last_seen_at.to_s, now + assert_equal user.last_seen_ip, ip + assert_equal user.last_sign_in_at.to_s, now + assert_equal user.last_sign_in_ip, ip assert_predicate user.previous_sign_in_at, :blank? assert_predicate user.previous_sign_in_ip, :blank? - - travel 1.hour do - ip2 = '4.73.73.73' - now2 = Time.now.utc.to_s - assert_not_equal now2, now1 - - user.update_activity!(now2, ip2, sign_in: true) - user.reload - assert_equal user.last_seen_at.to_s, now2 - assert_equal user.last_seen_ip, ip2 - assert_equal user.last_sign_in_at.to_s, now2 - assert_equal user.last_sign_in_ip, ip2 - assert_equal user.previous_sign_in_at, now1 - assert_equal user.previous_sign_in_ip, ip1 - end end - # rubocop:enable Minitest/MultipleAssertions test 'should validate if it does not have an api key and it is not a system account' do user = User.new( diff --git a/test/services/statistics_test.rb b/test/services/statistics_test.rb index b1f14b695..d214a4b86 100644 --- a/test/services/statistics_test.rb +++ b/test/services/statistics_test.rb @@ -2,6 +2,11 @@ class StatisticsTest < ActiveSupport::TestCase + setup do + @obj_id = generate_random_string + @test_ip = '192.168.0.1' + end + test 'counts are zero for unviewed objects' do obj_id = generate_random_string @@ -10,61 +15,74 @@ class StatisticsTest < ActiveSupport::TestCase assert_equal [0, 0], Statistics.for(item_id: obj_id) end - # rubocop:disable Minitest/MultipleAssertions - # TODO: our tests are quite smelly. This one needs work! - test 'counts increment correctly' do - obj_id = generate_random_string - test_ip = '192.168.0.1' - + test 'view counts increment correctly' do freeze_time do - Statistics.increment_view_count_for(item_id: obj_id, ip: test_ip) - assert_equal 1, Statistics.views_for(item_id: obj_id) - assert_equal [1, 0], Statistics.for(item_id: obj_id) + Statistics.increment_view_count_for(item_id: @obj_id, ip: @test_ip) + assert_equal 1, Statistics.views_for(item_id: @obj_id) + assert_equal 1, Statistics.for(item_id: @obj_id)[0] # a second view inside the same time period is ignored - Statistics.increment_view_count_for(item_id: obj_id, ip: test_ip) - assert_equal 1, Statistics.views_for(item_id: obj_id) - assert_equal [1, 0], Statistics.for(item_id: obj_id) - - # downloads work equally - - Statistics.increment_download_count_for(item_id: obj_id, ip: test_ip) - assert_equal 1, Statistics.downloads_for(item_id: obj_id) - assert_equal [1, 1], Statistics.for(item_id: obj_id) + Statistics.increment_view_count_for(item_id: @obj_id, ip: @test_ip) + assert_equal 1, Statistics.views_for(item_id: @obj_id) + assert_equal 1, Statistics.for(item_id: @obj_id)[0] + end + end - Statistics.increment_download_count_for(item_id: obj_id, ip: test_ip) - assert_equal 1, Statistics.downloads_for(item_id: obj_id) - assert_equal [1, 1], Statistics.for(item_id: obj_id) + test 'view counts increment correctly after key expiry' do + freeze_time do + previous_views = Statistics.views_for(item_id: @obj_id) # simulate expiring key at top of hour in order to test behaviour after ip filter rotation - views_key = Statistics.send(:uniques_key_for, :view, obj_id) - downloads_key = Statistics.send(:uniques_key_for, :download, obj_id) + views_key = Statistics.send(:uniques_key_for, :view, @obj_id) # clear current timeout RedisClient.current.persist views_key - RedisClient.current.persist downloads_key # expire immediately RedisClient.current.pexpire views_key, -1 - RedisClient.current.pexpire downloads_key, -1 # Susequent viewings outside the ip filter expiry period should now count - Statistics.increment_view_count_for(item_id: obj_id, ip: test_ip) - assert_equal 2, Statistics.views_for(item_id: obj_id) - assert_equal [2, 1], Statistics.for(item_id: obj_id) + Statistics.increment_view_count_for(item_id: @obj_id, ip: @test_ip) + views = Statistics.views_for(item_id: @obj_id) + assert_equal previous_views + 1, views # a second view inside the same time period is still ignored - Statistics.increment_view_count_for(item_id: obj_id, ip: test_ip) - assert_equal 2, Statistics.views_for(item_id: obj_id) - assert_equal [2, 1], Statistics.for(item_id: obj_id) + Statistics.increment_view_count_for(item_id: @obj_id, ip: @test_ip) + views = Statistics.views_for(item_id: @obj_id) + assert_equal previous_views + 1, views + end + end - Statistics.increment_download_count_for(item_id: obj_id, ip: test_ip) - assert_equal 2, Statistics.downloads_for(item_id: obj_id) - assert_equal [2, 2], Statistics.for(item_id: obj_id) + test 'download counts increment correctly' do + freeze_time do + Statistics.increment_download_count_for(item_id: @obj_id, ip: @test_ip) + assert_equal 1, Statistics.downloads_for(item_id: @obj_id) + assert_equal 1, Statistics.for(item_id: @obj_id)[1] - Statistics.increment_download_count_for(item_id: obj_id, ip: test_ip) - assert_equal 2, Statistics.downloads_for(item_id: obj_id) - assert_equal [2, 2], Statistics.for(item_id: obj_id) + # a second view inside the same time period is ignored + Statistics.increment_download_count_for(item_id: @obj_id, ip: @test_ip) + assert_equal 1, Statistics.downloads_for(item_id: @obj_id) + assert_equal 1, Statistics.for(item_id: @obj_id)[1] + end + end + + test 'download counts increment correctly after key expiry' do + freeze_time do + previous_downloads = Statistics.downloads_for(item_id: @obj_id) + # simulate expiring key at top of hour in order to test behaviour after ip filter rotation + downloads_key = Statistics.send(:uniques_key_for, :download, @obj_id) + # clear current timeout + RedisClient.current.persist downloads_key + # expire immediately + RedisClient.current.pexpire downloads_key, -1 + + Statistics.increment_download_count_for(item_id: @obj_id, ip: @test_ip) + downloads = Statistics.downloads_for(item_id: @obj_id) + assert_equal previous_downloads + 1, downloads + + # a second view inside the same time period is ignored + Statistics.increment_download_count_for(item_id: @obj_id, ip: @test_ip) + downloads = Statistics.downloads_for(item_id: @obj_id) + assert_equal previous_downloads + 1, downloads end end - # rubocop:enable Minitest/MultipleAssertions end diff --git a/test/system/admin_communities_index_test.rb b/test/system/admin_communities_index_test.rb index 53ce9f420..2f5613554 100644 --- a/test/system/admin_communities_index_test.rb +++ b/test/system/admin_communities_index_test.rb @@ -10,10 +10,7 @@ class AdminCommunitiesIndexTest < ApplicationSystemTestCase community_id: @community.id) .save! end - end - # TODO: add more tests - test 'should be able to expand the collection for a community in the list' do admin = users(:user_admin) login_user(admin) @@ -21,12 +18,21 @@ class AdminCommunitiesIndexTest < ApplicationSystemTestCase click_link admin.name # opens user dropdown which has the admin link click_link I18n.t('application.navbar.links.admin') click_link I18n.t('admin.communities.index.header') + end + + # TODO: add more tests + test 'ensure collections are not shown on community page' do assert_selector 'h1', text: I18n.t('admin.communities.index.header') - # Initially, collections aren't shown, but there is a 'Collections' link + # Initially, collections aren't shown refute_link 'Fancy Collection 0' refute_link 'Fancy Collection 1' refute_button 'Close' + + logout_user + end + + test 'should be able to expand the collection for a community in the list' do assert_selector 'a.btn', text: 'Collections' # After clicking 'Collections', collections and close button are shown diff --git a/test/system/admin_communities_test.rb b/test/system/admin_communities_test.rb index b24c33125..cc5becce2 100644 --- a/test/system/admin_communities_test.rb +++ b/test/system/admin_communities_test.rb @@ -1,6 +1,6 @@ require 'application_system_test_case' -class AdminCommunitiesIndexTest < ApplicationSystemTestCase +class AdminCommunitiesTest < ApplicationSystemTestCase test 'should be able to see rendered markdown of a description for a community and collection' do admin = users(:user_admin) diff --git a/test/system/admin_users_show_test.rb b/test/system/admin_users_show_test.rb index fb5d33609..e35febf4b 100644 --- a/test/system/admin_users_show_test.rb +++ b/test/system/admin_users_show_test.rb @@ -31,8 +31,6 @@ class AdminUsersShowTest < ApplicationSystemTestCase logout_user end - # rubocop:disable Minitest/MultipleAssertions - # TODO: our tests are quite smelly. This one needs work! test 'should be able to toggle suspended/admin a regular user' do admin = users(:user_admin) user = users(:user_regular) @@ -105,7 +103,6 @@ class AdminUsersShowTest < ApplicationSystemTestCase logout_user end - # rubocop:enable Minitest/MultipleAssertions test 'should be able to login as a regular user' do admin = users(:user_admin) diff --git a/test/system/collections_pagination_and_sort_test.rb b/test/system/collections_pagination_and_sort_test.rb index e45136c6a..27dc99ae0 100644 --- a/test/system/collections_pagination_and_sort_test.rb +++ b/test/system/collections_pagination_and_sort_test.rb @@ -12,8 +12,6 @@ class CollectionsPaginationAndSortTest < ApplicationSystemTestCase end end - # rubocop:disable Minitest/MultipleAssertions - # TODO: our tests are quite smelly. This one needs work! test 'anybody should be able to sort and paginate collections' do visit community_path(@community) assert_selector 'div', text: '1 - 10 of 11' @@ -30,8 +28,10 @@ class CollectionsPaginationAndSortTest < ApplicationSystemTestCase assert_equal URI.parse(current_url).request_uri, community_path(@community, page: '2') assert_selector 'div', text: '11 - 11 of 11' assert_selector 'ul.list-group li:first-child a', text: 'Nice Collection 09' + end - # Sort links + test 'sort by descending alphabetical order and paginate collections' do + visit community_path(@community) click_button 'Sort by' assert_selector 'a', text: 'Title (A-Z)' assert_selector 'a', text: 'Title (Z-A)' @@ -55,21 +55,12 @@ class CollectionsPaginationAndSortTest < ApplicationSystemTestCase page: '2') assert_selector 'div', text: '11 - 11 of 11' assert_selector 'ul.list-group li:first-child a', text: 'Fancy Collection 00' + end - # Sort the other way again - click_button 'Title (Z-A)' - click_link 'Title (A-Z)' - assert_equal URI.parse(current_url).request_uri, community_path(@community, sort: 'title', direction: 'asc') - assert_selector 'button', text: 'Title (A-Z)' - assert_selector 'div', text: '1 - 10 of 11' - # First 6 say 'Fancy', last 4 say 'Nice' - assert_selector 'ul.list-group li:first-child a', text: 'Fancy Collection 00' - assert_selector 'ul.list-group li:nth-child(2) a', text: 'Fancy Collection 02' - assert_selector 'ul.list-group li:nth-child(9) a', text: 'Nice Collection 05' - assert_selector 'ul.list-group li:nth-child(10) a', text: 'Nice Collection 07' - + test 'sort by newest and paginate collections' do + visit community_path(@community) # Sort with newest first - click_button 'Title (A-Z)' + click_button 'Sort by' click_link 'Date (newest first)' assert_equal URI.parse(current_url).request_uri, community_path(@community, sort: 'record_created_at', direction: 'desc') @@ -87,9 +78,27 @@ class CollectionsPaginationAndSortTest < ApplicationSystemTestCase direction: 'desc', page: '2') assert_selector 'div', text: '11 - 11 of 11' assert_selector 'ul.list-group li:first-child a', text: 'Fancy Collection 00' + end + test 'sort by ascending alphabetical order and paginate collections' do + visit community_path(@community) + # Sort the other way again + click_button 'Sort by' + click_link 'Title (A-Z)' + assert_equal URI.parse(current_url).request_uri, community_path(@community, sort: 'title', direction: 'asc') + assert_selector 'button', text: 'Title (A-Z)' + assert_selector 'div', text: '1 - 10 of 11' + # First 6 say 'Fancy', last 4 say 'Nice' + assert_selector 'ul.list-group li:first-child a', text: 'Fancy Collection 00' + assert_selector 'ul.list-group li:nth-child(2) a', text: 'Fancy Collection 02' + assert_selector 'ul.list-group li:nth-child(9) a', text: 'Nice Collection 05' + assert_selector 'ul.list-group li:nth-child(10) a', text: 'Nice Collection 07' + end + + test 'sort by oldest and paginate collections' do + visit community_path(@community) # Sort with oldest first - click_button 'Date (newest first)' + click_button 'Sort by' click_link 'Date (oldest first)' assert_equal URI.parse(current_url).request_uri, community_path(@community, sort: 'record_created_at', direction: 'asc') @@ -107,6 +116,5 @@ class CollectionsPaginationAndSortTest < ApplicationSystemTestCase assert_selector 'div', text: '11 - 11 of 11' assert_selector 'ul.list-group li:first-child a', text: 'Fancy Collection 10' end - # rubocop:enable Minitest/MultipleAssertions end diff --git a/test/system/search_test.rb b/test/system/search_test.rb index 41f83d02a..fd422d7fe 100644 --- a/test/system/search_test.rb +++ b/test/system/search_test.rb @@ -109,9 +109,7 @@ class SearchTest < ApplicationSystemTestCase end end - # rubocop:disable Minitest/MultipleAssertions - # TODO: our tests are quite smelly. This one needs work! - test 'anybody should be able to filter the public items' do + test 'anybody should be able to view search results' do visit root_path fill_in name: 'search', with: 'Fancy' click_button 'Search' @@ -151,6 +149,12 @@ class SearchTest < ApplicationSystemTestCase within '.list-group-item', text: 'Fancy CCID Item' do refute_selector 'a', text: 'Download' end + end + + test 'anybody should be able to filter the public items by collection' do + visit root_path + fill_in name: 'search', with: 'Fancy' + click_button 'Search' # A checkbox for the facet should be unchecked, and link should turn on facet within 'div.jupiter-filters a', text: 'Fancy Collection 1' do @@ -195,7 +199,6 @@ class SearchTest < ApplicationSystemTestCase badge = badges.find_link('a', text: 'Fancy Collection 1', href: search_path(search: 'Fancy')) badge.assert_selector 'span.badge', text: 'Fancy Collection 1' end - # rubocop:enable Minitest/MultipleAssertions test 'facet badge should have category when flipped' do Flipper.enable(:facet_badge_category_name) @@ -215,8 +218,6 @@ class SearchTest < ApplicationSystemTestCase Flipper.disable(:facet_badge_category_name) end - # rubocop:disable Minitest/MultipleAssertions - # TODO: our tests are quite smelly. This one needs work! test 'anybody should be able to view community/collection hits via tabs' do visit root_path fill_in name: 'search', with: 'Fancy' @@ -232,6 +233,13 @@ class SearchTest < ApplicationSystemTestCase assert_selector 'div.jupiter-results-list h3 a', text: 'Item', count: 6 assert_selector 'div.jupiter-results-list a', text: 'Community', count: 0 assert_selector 'div.jupiter-results-list a', text: 'Collection', count: 0 + end + + test 'anybody should be able to view community hits via tabs' do + visit root_path + fill_in name: 'search', with: 'Fancy' + click_button 'Search' + assert_equal URI.parse(current_url).request_uri, search_path(search: 'Fancy') # Visit community tab click_link 'Communities' @@ -243,6 +251,13 @@ class SearchTest < ApplicationSystemTestCase assert_selector 'div.jupiter-results-list h3 a', text: 'Item', count: 0 assert_selector 'div.jupiter-results-list a', text: 'Community', count: 1 assert_selector 'div.jupiter-results-list a', text: 'Collection', count: 0 + end + + test 'anybody should be able to view collection hits via tabs' do + visit root_path + fill_in name: 'search', with: 'Fancy' + click_button 'Search' + assert_equal URI.parse(current_url).request_uri, search_path(search: 'Fancy') # Visit collection tab within('.nav-tabs') do @@ -257,7 +272,6 @@ class SearchTest < ApplicationSystemTestCase assert_selector 'div.jupiter-results-list a', text: 'Community', count: 0 assert_selector 'div.jupiter-results-list a', text: 'Collection', count: 2 end - # rubocop:enable Minitest/MultipleAssertions test 'anybody should only see some facet results by default, with a "show more" button' do visit root_path @@ -287,9 +301,7 @@ class SearchTest < ApplicationSystemTestCase assert_selector 'li a', text: /Extra Community/, count: 6 end - # rubocop:disable Minitest/MultipleAssertions - # TODO: our tests are quite smelly. This one needs work! - test 'anybody should be able to sort results' do + test 'anybody should be able to view sort options and view items in default order (ascending alphabetical)' do visit root_path fill_in name: 'search', with: 'Fancy' click_button 'Search' @@ -307,42 +319,68 @@ class SearchTest < ApplicationSystemTestCase assert_selector 'a', text: 'Date (newest first)' assert_selector 'a', text: 'Date (oldest first)' assert_selector 'a', text: 'Relevance' + end + + test 'anybody should be able to sort results in descending alphabetical order' do + visit root_path + fill_in name: 'search', with: 'Fancy' + click_button 'Search' + assert_equal URI.parse(current_url).request_uri, search_path(search: 'Fancy') # Reverse sort + click_button 'Sort by' click_link 'Title (Z-A)' assert_equal URI.parse(current_url).request_uri, search_path(search: 'Fancy', sort: 'title', direction: 'desc') assert_selector 'button', text: 'Title (Z-A)' assert_match(/Fancy Item 8.*Fancy Item 6.*Fancy Item 4.*Fancy Item 2.*Fancy Item 0.*Fancy CCID Item/m, page.text) + end + + test 'anybody should be able to sort results in ascending alphabetical order' do + visit root_path + fill_in name: 'search', with: 'Fancy' + click_button 'Search' + assert_equal URI.parse(current_url).request_uri, search_path(search: 'Fancy') # Sort the other way again - click_button 'Title (Z-A)' + click_button 'Sort by' click_link 'Title (A-Z)' assert_equal URI.parse(current_url).request_uri, search_path(search: 'Fancy', sort: 'title', direction: 'asc') assert_selector 'button', text: 'Title (A-Z)' assert_match(/Fancy CCID Item.*Fancy Item 0.*Fancy Item 2.*Fancy Item 4.*Fancy Item 6.*Fancy Item 8/m, page.text) + end + + test 'anybody should be able to sort results by newest first' do + visit root_path + fill_in name: 'search', with: 'Fancy' + click_button 'Search' + assert_equal URI.parse(current_url).request_uri, search_path(search: 'Fancy') # Sort with newest first - click_button 'Title (A-Z)' + click_button 'Sort by' click_link 'Date (newest first)' assert_equal URI.parse(current_url).request_uri, search_path(search: 'Fancy', sort: 'sort_year', direction: 'desc') assert_selector 'button', text: 'Date (newest first)' assert_match(/Fancy Item 8.*Fancy Item 6.*Fancy Item 4.*Fancy Item 2.*Fancy Item 0/m, page.text) + end + + test 'anybody should be able to sort results by oldest first' do + visit root_path + fill_in name: 'search', with: 'Fancy' + click_button 'Search' + assert_equal URI.parse(current_url).request_uri, search_path(search: 'Fancy') # Sort with oldest first - click_button 'Date (newest first)' + click_button 'Sort by' click_link 'Date (oldest first)' assert_equal URI.parse(current_url).request_uri, search_path(search: 'Fancy', sort: 'sort_year', direction: 'asc') assert_selector 'button', text: 'Date (oldest first)' assert_match(/Fancy Item 0.*Fancy Item 2.*Fancy Item 4.*Fancy Item 6.*Fancy Item 8/m, page.text) end - # rubocop:enable Minitest/MultipleAssertions - # rubocop:disable Minitest/MultipleAssertions - # TODO: our tests are quite smelly. This one needs work! - # TODO: Slow Test, consistently around ~8-9 seconds - test 'admin should be able to filter the public and private items' do + # TODO: Slow Tests, next two consistently around ~8-9 seconds + test 'admin should be able to view public and private items' do admin = users(:user_admin) login_user(admin) @@ -395,6 +433,18 @@ class SearchTest < ApplicationSystemTestCase assert_selector 'i.far.fa-square', count: 1 end + logout_user + end + + test 'admin should be able to filter the public and private items' do + admin = users(:user_admin) + login_user(admin) + + # Search box should be on any page we happen to be on + fill_in name: 'search', with: 'Fancy' + click_button 'Search' + assert_equal URI.parse(current_url).request_uri, search_path(search: 'Fancy') + # Click on facet click_link 'Fancy Collection 1' @@ -431,7 +481,8 @@ class SearchTest < ApplicationSystemTestCase badges = find('div.jupiter-facet-badges') badge = badges.find_link('a', text: 'Fancy Collection 1', href: search_path(search: 'Fancy')) badge.assert_selector 'span.badge', text: 'Fancy Collection 1' + + logout_user end - # rubocop:enable Minitest/MultipleAssertions end