Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve our smelliest tests [Minitest/MultipleAssertions] #2419

Open
2 of 13 tasks
pgwillia opened this issue Jul 5, 2021 · 0 comments
Open
2 of 13 tasks

Improve our smelliest tests [Minitest/MultipleAssertions] #2419

pgwillia opened this issue Jul 5, 2021 · 0 comments

Comments

@pgwillia
Copy link
Member

pgwillia commented Jul 5, 2021

Is your feature request related to a problem? Please describe.
Looks like rubocop-minitest was fixed [changelog says they changed the code so it starts working on “Test” blocks] and it's calling us out on too many assertions in our tests. I moved the max Minitest/MultipleAssertions to 15 which is kind of smelly. Additionally I disabled the cop on a number of our worst violations. This is the ticket to do the work to clean up these tests.

#2416

Describe the solution you'd like
Take a good look at these tests and ensure that they are testing one thing. Also think about the setup of the tests: use the fixtures, change the config of dependencies to focus the test, don't test the dependencies.

Additional context

  • # 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
  • # 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
  • # 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)
    assert user.last_seen_at.blank?
    assert user.last_sign_in_at.blank?
    assert user.previous_sign_in_at.blank?
    ip1 = '4.26.50.50'
    now1 = Time.now.utc.to_s
    user.update_activity!(now1, ip1)
    user.reload
    assert user.last_seen_at.present?
    assert_equal user.last_seen_at.to_s, now1
    assert_equal user.last_seen_ip, ip1
    # Does not change sign-in information
    assert user.last_sign_in_at.blank?
    assert user.last_sign_in_ip.blank?
    assert user.previous_sign_in_at.blank?
    assert 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 user.last_sign_in_at.blank?
    assert user.last_sign_in_ip.blank?
    assert user.previous_sign_in_at.blank?
    assert 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 user.last_seen_at.blank?
    assert user.last_sign_in_at.blank?
    assert user.previous_sign_in_at.blank?
    ip1 = '4.26.50.50'
    now1 = Time.now.utc.to_s
    user.update_activity!(now1, ip1, sign_in: true)
    user.reload
    assert 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 user.previous_sign_in_at.blank?
    assert 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
  • # rubocop:disable Minitest/MultipleAssertions
    # TODO: our tests are quite smelly. This one needs work!
    test 'DOI state transitions' do
    @admin = users(:user_admin)
    assert_no_enqueued_jobs only: DOIRemoveJob
    Rails.application.secrets.doi_minting_enabled = true
    community = Community.new(title: 'Community', owner_id: @admin.id,
    visibility: JupiterCore::VISIBILITY_PUBLIC)
    community.save!
    collection = Collection.new(title: 'Collection', owner_id: @admin.id,
    visibility: JupiterCore::VISIBILITY_PUBLIC,
    community_id: community.id)
    collection.save!
    item = Item.new(title: 'Test Title', owner_id: @admin.id, visibility: JupiterCore::VISIBILITY_PUBLIC,
    created: '2017-02-02',
    languages: [ControlledVocabulary.era.language.english],
    creators: ['Joe Blow'],
    subject: ['Things'],
    license: ControlledVocabulary.era.license.attribution_4_0_international,
    item_type: ControlledVocabulary.era.item_type.book)
    item.tap do |unlocked_item|
    unlocked_item.add_to_path(community.id, collection.id)
    unlocked_item.save!
    end
    assert_nil item.doi
    assert_enqueued_jobs 1, only: DOICreateJob
    clear_enqueued_jobs
    VCR.use_cassette('ezid_minting', erb: { id: item.id }, record: :none) do
    assert_equal 'unminted', item.aasm_state
    ezid_identifer = DOIService.new(item).create
    assert_not_nil ezid_identifer
    assert_equal EXAMPLE_DOI, ezid_identifer.id
    assert_equal 'University of Alberta Libraries', ezid_identifer.datacite_publisher
    assert_equal 'Test Title', ezid_identifer.datacite_title
    assert_equal 'Text/Book', ezid_identifer.datacite_resourcetype
    assert_equal '2017', ezid_identifer.datacite_publicationyear
    assert_equal Ezid::Status::PUBLIC, ezid_identifer.status
    assert_equal 'yes', ezid_identifer.export
    assert_not_nil item.doi
    assert_equal 'available', item.aasm_state
    end
    VCR.use_cassette('ezid_updating', erb: { id: item.id }, record: :none) do
    assert_no_enqueued_jobs only: DOIRemoveJob
    item.tap do |uo|
    uo.title = 'Different Title'
    uo.save!
    end
    assert_enqueued_jobs 1, only: DOIUpdateJob
    clear_enqueued_jobs
    ezid_identifer = DOIService.new(item).update
    assert_not_nil ezid_identifer
    assert_equal EXAMPLE_DOI, ezid_identifer.id
    assert_equal Ezid::Status::PUBLIC, ezid_identifer.status
    assert_equal 'Different Title', ezid_identifer.datacite_title
    assert_equal 'yes', ezid_identifer.export
    assert_equal 'available', item.aasm_state
    end
    VCR.use_cassette('ezid_updating_unavailable', erb: { id: item.id }, record: :none) do
    assert_no_enqueued_jobs only: DOIRemoveJob
    item.tap do |uo|
    uo.visibility = JupiterCore::VISIBILITY_PRIVATE
    uo.save!
    end
    assert_enqueued_jobs 1, only: DOIUpdateJob
    clear_enqueued_jobs
    ezid_identifer = DOIService.new(item).update
    assert_not_nil ezid_identifer
    assert_equal EXAMPLE_DOI, ezid_identifer.id
    assert_equal 'unavailable | not publicly released', ezid_identifer.status
    assert_equal 'not_available', item.aasm_state
    end
    VCR.use_cassette('ezid_removal', erb: { id: item.id }, record: :none, allow_unused_http_interactions: false) do
    assert_no_enqueued_jobs only: DOIRemoveJob
    item.tap(&:destroy)
    assert_enqueued_jobs 1, only: DOIRemoveJob
    clear_enqueued_jobs
    ezid_identifer = DOIService.remove(item.doi)
    assert_not_nil ezid_identifer
    assert_equal EXAMPLE_DOI, ezid_identifer.id
    assert_equal 'unavailable | withdrawn', ezid_identifer.status
    assert_equal 'no', ezid_identifer.export
    end
    Rails.application.secrets.doi_minting_enabled = false
    end
    # rubocop:enable Minitest/MultipleAssertions
  • # 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'
    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)
    # 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_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)
    # 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)
    # clear current timeout
    Redis.current.persist views_key
    Redis.current.persist downloads_key
    # expire immediately
    Redis.current.pexpire views_key, -1
    Redis.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)
    # 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_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)
    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)
    end
    end
    # rubocop:enable Minitest/MultipleAssertions
  • # 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)
    login_user(admin)
    click_link admin.name # opens user dropdown which has the admin link
    click_link I18n.t('application.navbar.links.admin')
    assert_selector 'h1', text: I18n.t('admin.dashboard.index.header')
    click_link I18n.t('admin.users.index.header')
    assert_selector 'h1', text: I18n.t('admin.users.index.header')
    assert_selector 'tbody tr', count: 6
    assert_selector 'tbody tr:first-child th[scope="row"]', text: admin.email
    click_link user.email
    assert_selector 'h1', text: user.name
    assert_selector :link, text: I18n.t('admin.users.show.suspend_link_text')
    assert_selector :link, text: I18n.t('admin.users.show.grant_admin_link_text')
    assert_selector :link, text: I18n.t('admin.users.show.login_as_user_link_text')
    refute_selector :link, text: I18n.t('admin.users.show.unsuspend_link_text')
    refute_selector :link, text: I18n.t('admin.users.show.revoke_admin_link_text')
    accept_confirm do
    click_link I18n.t('admin.users.show.grant_admin_link_text')
    end
    assert_text I18n.t('admin.users.show.grant_admin_flash', user: user.name)
    assert_selector :link, text: I18n.t('admin.users.show.revoke_admin_link_text')
    refute_selector :link, text: I18n.t('admin.users.show.suspend_link_text')
    refute_selector :link, text: I18n.t('admin.users.show.unsuspend_link_text')
    refute_selector :link, text: I18n.t('admin.users.show.grant_admin_link_text')
    refute_selector :link, text: I18n.t('admin.users.show.login_as_user_link_text')
    accept_confirm do
    click_link I18n.t('admin.users.show.revoke_admin_link_text')
    end
    assert_text I18n.t('admin.users.show.revoke_admin_flash', user: user.name)
    assert_selector :link, text: I18n.t('admin.users.show.suspend_link_text')
    assert_selector :link, text: I18n.t('admin.users.show.grant_admin_link_text')
    assert_selector :link, text: I18n.t('admin.users.show.login_as_user_link_text')
    refute_selector :link, text: I18n.t('admin.users.show.unsuspend_link_text')
    refute_selector :link, text: I18n.t('admin.users.show.revoke_admin_link_text')
    accept_confirm do
    click_link I18n.t('admin.users.show.suspend_link_text')
    end
    assert_text I18n.t('admin.users.show.suspend_flash', user: user.name)
    assert_selector :link, text: I18n.t('admin.users.show.unsuspend_link_text')
    refute_selector :link, text: I18n.t('admin.users.show.suspend_link_text')
    refute_selector :link, text: I18n.t('admin.users.show.grant_admin_link_text')
    refute_selector :link, text: I18n.t('admin.users.show.revoke_admin_link_text')
    refute_selector :link, text: I18n.t('admin.users.show.login_as_user_link_text')
    accept_confirm do
    click_link I18n.t('admin.users.show.unsuspend_link_text')
    end
    assert_text I18n.t('admin.users.show.unsuspend_flash', user: user.name)
    assert_selector :link, text: I18n.t('admin.users.show.suspend_link_text')
    assert_selector :link, text: I18n.t('admin.users.show.grant_admin_link_text')
    assert_selector :link, text: I18n.t('admin.users.show.login_as_user_link_text')
    refute_selector :link, text: I18n.t('admin.users.show.unsuspend_link_text')
    refute_selector :link, text: I18n.t('admin.users.show.revoke_admin_link_text')
    logout_user
    end
    # rubocop:enable Minitest/MultipleAssertions
  • # 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'
    # Default sort is by title. 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'
    # Would like to use 'last-child' here, but for some reason it's not working
    assert_selector 'ul.list-group li:nth-child(10) a', text: 'Nice Collection 07'
    # The last one should be on next page
    refute_selector 'a', text: 'Nice Collection 09'
    click_link 'Next'
    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'
    # Sort links
    click_button 'Sort by'
    assert_selector 'a', text: 'Title (A-Z)'
    assert_selector 'a', text: 'Title (Z-A)'
    assert_selector 'a', text: 'Date (newest first)'
    assert_selector 'a', text: 'Date (oldest first)'
    # Reverse sort
    click_link 'Title (Z-A)'
    assert_equal URI.parse(current_url).request_uri, community_path(@community, sort: 'title', direction: 'desc')
    assert_selector 'div', text: '1 - 10 of 11'
    assert_selector 'button', text: 'Title (Z-A)'
    assert_selector 'ul.list-group li:first-child a', text: 'Nice Collection 09'
    assert_selector 'ul.list-group li:nth-child(2) a', text: 'Nice Collection 07'
    assert_selector 'ul.list-group li:nth-child(9) a', text: 'Fancy Collection 04'
    assert_selector 'ul.list-group li:nth-child(10) a', text: 'Fancy Collection 02'
    # The first 'Fancy' collection should be on next page
    refute_selector 'a', text: 'Fancy Collection 00'
    click_link 'Next'
    assert_equal URI.parse(current_url).request_uri, community_path(@community, sort: 'title', 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'
    # 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'
    # Sort with newest first
    click_button 'Title (A-Z)'
    click_link 'Date (newest first)'
    assert_equal URI.parse(current_url).request_uri, community_path(@community, sort: 'record_created_at',
    direction: 'desc')
    assert_selector 'button', text: 'Date (newest first)'
    assert_selector 'div', text: '1 - 10 of 11'
    assert_selector 'ul.list-group li:first-child a', text: 'Fancy Collection 10'
    assert_selector 'ul.list-group li:nth-child(2) a', text: 'Nice Collection 09'
    assert_selector 'ul.list-group li:nth-child(9) a', text: 'Fancy Collection 02'
    assert_selector 'ul.list-group li:nth-child(10) a', text: 'Nice Collection 01'
    # The first 'Fancy' collection should be on next page
    refute_selector 'a', text: 'Fancy Collection 00'
    click_link 'Next'
    assert_equal URI.parse(current_url).request_uri, community_path(@community, sort: 'record_created_at',
    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'
    # Sort with oldest first
    click_button 'Date (newest first)'
    click_link 'Date (oldest first)'
    assert_equal URI.parse(current_url).request_uri, community_path(@community, sort: 'record_created_at',
    direction: 'asc')
    assert_selector 'button', text: 'Date (oldest first)'
    assert_selector 'div', text: '1 - 10 of 11'
    assert_selector 'ul.list-group li:first-child a', text: 'Fancy Collection 00'
    assert_selector 'ul.list-group li:nth-child(2) a', text: 'Nice Collection 01'
    assert_selector 'ul.list-group li:nth-child(9) a', text: 'Fancy Collection 08'
    assert_selector 'ul.list-group li:nth-child(10) a', text: 'Nice Collection 09'
    # The Last 'Nice' collection should be on next page
    refute_selector 'a', text: 'Fancy Collection 10'
    click_link 'Next'
    assert_equal URI.parse(current_url).request_uri, community_path(@community, sort: 'record_created_at',
    direction: 'asc', page: '2')
    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
  • # rubocop:disable Minitest/MultipleAssertions
    # TODO: our tests are quite smelly. This one needs work!
    # TODO: Slow test
    test 'anybody should be able to sort and paginate communities' do
    # for some runs/seeds (like SEED=1099), stale Communities are left over from other tests. We can't assume the
    # communities created here are the only ones! This test needs to be re-written
    # For sorting, creation order is 'Fancy Community 00', 'Nice Community 01', 'Fancy Community 02', etc. ...
    #
    # Try SEED=8921
    visit communities_path
    assert_selector 'div', text: '1 - 10 of 11'
    # Default sort is by title. First 6 say 'Fancy', last 4 say 'Nice'
    assert_selector 'li:first-child a', text: 'Fancy Community 00'
    assert_selector 'li:nth-child(2) a', text: 'Fancy Community 02'
    assert_selector 'li:nth-child(9) a', text: 'Nice Community 05'
    assert_selector 'li:last-child a', text: 'Nice Community 07'
    # The last one should be on next page
    refute_selector 'a', text: 'Nice Community 09'
    click_link 'Next'
    assert_equal URI.parse(current_url).request_uri, communities_path(page: '2')
    assert_selector 'div', text: '11 - 11 of 11'
    assert_selector 'li:first-child a', text: 'Nice Community 09'
    # Sort links
    click_button 'Sort by'
    assert_selector 'a', text: 'Title (A-Z)'
    assert_selector 'a', text: 'Title (Z-A)'
    assert_selector 'a', text: 'Date (newest first)'
    assert_selector 'a', text: 'Date (oldest first)'
    # Reverse sort
    click_link 'Title (Z-A)'
    assert_equal URI.parse(current_url).request_uri, communities_path(sort: 'title', direction: 'desc')
    assert_selector 'div', text: '1 - 10 of 11'
    assert_selector 'button', text: 'Title (Z-A)'
    assert_selector 'li:first-child a', text: 'Nice Community 09'
    assert_selector 'li:nth-child(2) a', text: 'Nice Community 07'
    assert_selector 'li:nth-child(9) a', text: 'Fancy Community 04'
    assert_selector 'li:last-child a', text: 'Fancy Community 02'
    # The first 'Fancy' community should be on next page
    refute_selector 'a', text: 'Fancy Community 00'
    click_link 'Next'
    assert_equal URI.parse(current_url).request_uri, communities_path(sort: 'title', direction: 'desc', page: '2')
    assert_selector 'div', text: '11 - 11 of 11'
    assert_selector 'li:first-child a', text: 'Fancy Community 00'
    # Sort the other way again
    click_button 'Title (Z-A)'
    click_link 'Title (A-Z)'
    assert_equal URI.parse(current_url).request_uri, communities_path(sort: 'title', direction: 'asc')
    assert_selector 'button', text: 'Title (A-Z)'
    assert_selector 'div', text: '1 - 10 of 11'
    # Default sort is by title. First 6 say 'Fancy', last 4 say 'Nice'
    assert_selector 'li:first-child a', text: 'Fancy Community 00'
    assert_selector 'li:nth-child(2) a', text: 'Fancy Community 02'
    assert_selector 'li:nth-child(9) a', text: 'Nice Community 05'
    assert_selector 'li:last-child a', text: 'Nice Community 07'
    # Sort with newest first
    click_button 'Title (A-Z)'
    click_link 'Date (newest first)'
    assert_equal URI.parse(current_url).request_uri, communities_path(sort: 'record_created_at', direction: 'desc')
    assert_selector 'button', text: 'Date (newest first)'
    assert_selector 'div', text: '1 - 10 of 11'
    assert_selector 'li:first-child a', text: 'Fancy Community 10'
    assert_selector 'li:nth-child(2) a', text: 'Nice Community 09'
    assert_selector 'li:nth-child(9) a', text: 'Fancy Community 02'
    assert_selector 'li:last-child a', text: 'Nice Community 01'
    # The first 'Fancy' community should be on next page
    refute_selector 'a', text: 'Fancy Community 00'
    click_link 'Next'
    assert_equal URI.parse(current_url).request_uri, communities_path(sort: 'record_created_at',
    direction: 'desc', page: '2')
    assert_selector 'div', text: '11 - 11 of 11'
    assert_selector 'li:first-child a', text: 'Fancy Community 00'
    # Sort with oldest first
    click_button 'Date (newest first)'
    click_link 'Date (oldest first)'
    assert_equal URI.parse(current_url).request_uri, communities_path(sort: 'record_created_at', direction: 'asc')
    assert_selector 'button', text: 'Date (oldest first)'
    assert_selector 'div', text: '1 - 10 of 11'
    assert_selector 'li:first-child a', text: 'Fancy Community 00'
    assert_selector 'li:nth-child(2) a', text: 'Nice Community 01'
    assert_selector 'li:nth-child(9) a', text: 'Fancy Community 08'
    assert_selector 'li:last-child a', text: 'Nice Community 09'
    # The Last 'Fancy' community should be on next page
    refute_selector 'a', text: 'Fancy Community 10'
    click_link 'Next'
    assert_equal URI.parse(current_url).request_uri, communities_path(sort: 'record_created_at',
    direction: 'asc', page: '2')
    assert_selector 'div', text: '11 - 11 of 11'
    assert_selector 'li:first-child a', text: 'Fancy Community 10'
    end
    # rubocop:enable Minitest/MultipleAssertions
  • # 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
    visit root_path
    fill_in name: 'search', with: 'Fancy'
    click_button 'Search'
    assert_equal URI.parse(current_url).request_uri, search_path(search: 'Fancy')
    # Tabs
    assert_selector 'a.nav-link.active', text: 'Items (6)'
    assert_selector 'a.nav-link', text: 'Collections'
    assert_selector 'a.nav-link', text: 'Communities'
    # Facets and counts
    refute_selector 'div.card-header', text: 'Visibility'
    refute_selector 'li a', text: /5\nPublic/
    refute_selector 'li a', text: /1\nAuthenticated/
    # Should not be a facet for 'private'
    refute_selector 'li a', text: /Private/
    # TODO: The 'Member of paths' text will likely change
    assert_selector 'div.card-header', text: 'Collections'
    assert_selector 'li a', text: /6\nFancy Community/
    assert_selector 'li a', text: /4\nFancy Community\/Fancy Collection 0/
    assert_selector 'li a', text: /2\nFancy Community\/Fancy Collection 1/
    assert_selector 'div.card-header', text: I18n.t('facets.sort_year')
    sort_year_facet = Item.solr_exporter_class.solr_name_for(:sort_year, role: :range_facet)
    assert_selector "#ranges_#{sort_year_facet}_begin"
    assert_selector "#ranges_#{sort_year_facet}_end"
    assert_selector 'input.btn'
    # Exactly 6 items shown
    assert_selector 'div.jupiter-results-list li.list-group-item', count: 6
    assert_selector 'a', text: 'Fancy Item 0'
    assert_selector 'a', text: 'Fancy Item 2'
    assert_selector 'a', text: 'Fancy Item 4'
    assert_selector 'a', text: 'Fancy Item 6'
    assert_selector 'a', text: 'Fancy Item 8'
    assert_selector 'a', text: 'Fancy CCID Item'
    # Should not show CCID download
    within '.list-group-item', text: 'Fancy CCID Item' do
    refute_selector 'a', text: 'Download'
    end
    # A checkbox for the facet should be unchecked, and link should turn on facet
    within 'div.jupiter-filters a', text: 'Fancy Collection 1' do
    assert_selector 'i.far.fa-square', count: 1
    end
    # Click on facet
    click_link 'Fancy Collection 1'
    path = "#{@community.id}/#{@collections[1].id}"
    facet_path = search_path(search: 'Fancy', facets: { member_of_paths_dpsim: [path] })
    assert_equal URI.parse(current_url).request_uri, facet_path
    # Tab counts should only change for this tab
    assert_selector 'a.nav-link.active', text: 'Items (2)'
    assert_selector 'a.nav-link', text: 'Collections'
    assert_selector 'a.nav-link', text: 'Communities'
    # Some facets are now gone, some with changed counts
    refute_selector 'div.card-header', text: 'Visibility'
    refute_selector 'li a', text: '2 Public'
    assert_selector 'div.card-header', text: 'Collections'
    assert_selector 'li a', text: /2\nFancy Community/
    assert_selector 'li a', text: 'Fancy Community/Fancy Collection 0', count: 0
    assert_selector 'li a', text: /2\nFancy Community\/Fancy Collection 1/
    # A checkbox for the selected facet should be checked, and link should turn off facet
    within 'div.jupiter-filters a', text: 'Fancy Collection 1' do
    assert_selector 'i.far.fa-check-square', count: 1
    end
    # 2 items shown, 3 not shown
    assert_selector 'div.jupiter-results-list li.list-group-item', count: 2
    assert_selector 'a', text: 'Fancy Item 6'
    assert_selector 'a', text: 'Fancy Item 8'
    assert_selector 'a', text: 'Fancy Item 0', count: 0
    assert_selector 'a', text: 'Fancy Item 2', count: 0
    assert_selector 'a', text: 'Fancy Item 4', count: 0
    # A badge should be displayed for the enabled facet as a link that turns off facet
    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'
    end
    # rubocop:enable Minitest/MultipleAssertions
  • # 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'
    click_button 'Search'
    assert_equal URI.parse(current_url).request_uri, search_path(search: 'Fancy')
    # Tabs
    assert_selector 'a.nav-link.active', text: 'Items (6)'
    assert_selector 'a.nav-link', text: 'Collections'
    assert_selector 'a.nav-link', text: 'Communities'
    # No community/collection results initially shown
    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
    # Visit community tab
    click_link 'Communities'
    assert_equal URI.parse(current_url).request_uri, search_path(search: 'Fancy', tab: 'community')
    assert_selector 'a.nav-link', text: 'Items'
    assert_selector 'a.nav-link', text: 'Collections'
    assert_selector 'a.nav-link.active', text: 'Communities (1)'
    # Only community hits shown
    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
    # Visit collection tab
    within('.nav-tabs') do
    click_link 'Collections'
    end
    assert_equal URI.parse(current_url).request_uri, search_path(search: 'Fancy', tab: 'collection')
    assert_selector 'a.nav-link', text: 'Items'
    assert_selector 'a.nav-link.active', text: 'Collections (2)'
    assert_selector 'a.nav-link', text: 'Communities'
    # Only collection hits shown
    assert_selector 'div.jupiter-results-list h3 a', text: 'Item', count: 0
    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
  • # rubocop:disable Minitest/MultipleAssertions
    # TODO: our tests are quite smelly. This one needs work!
    test 'anybody should be able to sort results' 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')
    # Default sort is by relevance
    # TODO this test has flapped in the past because the score of each document is equal
    # TODO could go through this section and add the Fancy CCID Item, regex allows tests to pass without this change
    assert_match(/Fancy CCID Item.*Fancy Item 0.*Fancy Item 2.*Fancy Item 4.*Fancy Item 6.*Fancy Item 8/m, page.text)
    # Sort sort links
    click_button 'Sort by'
    assert_selector 'a', text: 'Title (A-Z)'
    assert_selector 'a', text: 'Title (Z-A)'
    assert_selector 'a', text: 'Date (newest first)'
    assert_selector 'a', text: 'Date (oldest first)'
    assert_selector 'a', text: 'Relevance'
    # Reverse sort
    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)
    # Sort the other way again
    click_button 'Title (Z-A)'
    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)
    # Sort with newest first
    click_button 'Title (A-Z)'
    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)
    # Sort with oldest first
    click_button 'Date (newest first)'
    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
    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')
    # Tabs
    assert_selector 'a.nav-link.active', text: 'Items (11)'
    assert_selector 'a.nav-link', text: 'Collections'
    assert_selector 'a.nav-link', text: 'Communities'
    # Facets and counts
    assert_selector 'div.card-header', text: 'Visibility'
    assert_selector 'li a', text: /5\nPublic/
    assert_selector 'li a', text: /1\nAuthenticated/
    # Should be a facet for 'private'
    assert_selector 'li a', text: /5\nPrivate/
    # TODO: The 'Member of paths' text will likely change
    assert_selector 'div.card-header', text: 'Collections'
    assert_selector 'li a', text: /11\nFancy Community/
    assert_selector 'li a', text: /7\nFancy Community\/Fancy Collection 0/
    assert_selector 'li a', text: /4\nFancy Community\/Fancy Collection 1/
    # Exactly 10 items shown
    assert_selector 'div.jupiter-results-list li.list-group-item', count: 10
    assert_selector 'a', text: 'Fancy Item 0'
    assert_selector 'a', text: 'Fancy Item 2'
    assert_selector 'a', text: 'Fancy Item 4'
    assert_selector 'a', text: 'Fancy Item 6'
    assert_selector 'a', text: 'Fancy Item 8'
    assert_selector 'a', text: 'Fancy CCID Item'
    assert_selector 'a', text: 'Fancy Private Item 10'
    assert_selector 'a', text: 'Fancy Private Item 12'
    assert_selector 'a', text: 'Fancy Private Item 14'
    assert_selector 'a', text: 'Fancy Private Item 16'
    # Should see the download link for CCID item
    within '.list-group-item', text: 'Fancy CCID Item' do
    assert_selector 'a', text: 'Download'
    assert_link href: /download/
    assert_no_link href: /active_storage/
    end
    # A checkbox for the facet should be unchecked, and link should turn on facet
    within 'div.jupiter-filters a', text: 'Fancy Collection 1' do
    assert_selector 'i.far.fa-square', count: 1
    end
    # Click on facet
    click_link 'Fancy Collection 1'
    path = "#{@community.id}/#{@collections[1].id}"
    facet_path = search_path(search: 'Fancy', facets: { member_of_paths_dpsim: [path] })
    assert_equal URI.parse(current_url).request_uri, facet_path
    # Tab counts should only change for this tab
    assert_selector 'a.nav-link.active', text: 'Items (4)'
    assert_selector 'a.nav-link', text: 'Collections'
    assert_selector 'a.nav-link', text: 'Communities'
    # Some facets are now gone, some with changed counts
    assert_selector 'div.card-header', text: 'Visibility'
    assert_selector 'li a', text: /2\nPublic/
    assert_selector 'div.card-header', text: 'Collections'
    assert_selector 'li a', text: /4\nFancy Community/
    assert_selector 'li a', text: 'Fancy Community/Fancy Collection 0', count: 0
    assert_selector 'li a', text: /4\nFancy Community\/Fancy Collection 1/
    # A checkbox for the selected facet should be checked, and link should turn off facet
    within 'div.jupiter-filters a', text: 'Fancy Collection 1' do
    assert_selector 'i.far.fa-check-square', count: 1
    end
    # 2 items shown, 3 not shown
    assert_selector 'a', text: 'Fancy Item 6'
    assert_selector 'a', text: 'Fancy Item 8'
    assert_selector 'a', text: 'Fancy Item 0', count: 0
    assert_selector 'a', text: 'Fancy Item 2', count: 0
    assert_selector 'a', text: 'Fancy Item 4', count: 0
    # A badge should be displayed for the enabled facet as a link that turns off facet
    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'
    end
    # rubocop:enable Minitest/MultipleAssertions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant