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

Parallel Tests #5110

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions app/models/concerns/rubygem_searchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ module RubygemSearchable
extend ActiveSupport::Concern

included do
searchkick index_name: Gemcutter::SEARCH_INDEX_NAME,
callbacks: false,
searchkick callbacks: false,
settings: {
number_of_shards: 1,
number_of_replicas: Gemcutter::SEARCH_NUM_REPLICAS,
Expand Down
6 changes: 6 additions & 0 deletions test/factories/rubygem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@

name

trait :reindex do
after(:create) do |rubygem, _evaluator|
rubygem.reindex(refresh: true)
end
end

after(:build) do |rubygem, evaluator|
if evaluator.linkset
rubygem.linkset = evaluator.linkset
Expand Down
6 changes: 6 additions & 0 deletions test/factories/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
sha256 { "tdQEXD9Gb6kf4sxqvnkjKhpXzfEE96JucW4KHieJ33g=" }
spec_sha256 { Digest::SHA2.base64digest("#{rubygem.name}-#{number}-#{platform}") }

trait :reindex do
after(:create) do |version|
version.rubygem.reindex(refresh: true)
end
end

trait :yanked do
indexed { false }
end
Expand Down
12 changes: 5 additions & 7 deletions test/functional/api/v1/searches_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ def self.should_respond_to(format)
setup do
@match = create(:rubygem, name: "match")
@other = create(:rubygem, name: "other")
create(:version, rubygem: @match)
create(:version, rubygem: @other)
import_and_refresh
create(:version, :reindex, rubygem: @match)
create(:version, :reindex, rubygem: @other)
end

should_respond_to(:json) do |body|
Expand Down Expand Up @@ -79,10 +78,9 @@ def self.should_respond_to(format)
@match1 = create(:rubygem, name: "match1")
@match2 = create(:rubygem, name: "match2")
@other = create(:rubygem, name: "other")
create(:version, rubygem: @match1)
create(:version, rubygem: @match2)
create(:version, rubygem: @other)
import_and_refresh
create(:version, :reindex, rubygem: @match1)
create(:version, :reindex, rubygem: @match2)
create(:version, :reindex, rubygem: @other)
end

context "with elasticsearch up" do
Expand Down
30 changes: 13 additions & 17 deletions test/functional/searches_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ class SearchesControllerTest < ActionController::TestCase

context "on GET to show with search parameters for a rubygem without versions" do
setup do
@sinatra = create(:rubygem, name: "sinatra")
import_and_refresh
@sinatra = create(:rubygem, :reindex, name: "sinatra")

assert_nil @sinatra.most_recent_version
assert_predicate @sinatra.reload.versions.count, :zero?
Expand All @@ -35,10 +34,10 @@ class SearchesControllerTest < ActionController::TestCase
@sinatra = create(:rubygem, name: "sinatra")
@sinatra_redux = create(:rubygem, name: "sinatra-redux")
@brando = create(:rubygem, name: "brando")
create(:version, rubygem: @sinatra)
create(:version, rubygem: @sinatra_redux)
create(:version, rubygem: @brando)
import_and_refresh
create(:version, :reindex, rubygem: @sinatra)
create(:version, :reindex, rubygem: @sinatra_redux)
create(:version, :reindex, rubygem: @brando)

get :show, params: { query: "sinatra" }
end

Expand All @@ -61,10 +60,9 @@ class SearchesControllerTest < ActionController::TestCase
@sinatra = create(:rubygem, name: "sinatra")
@sinatra_redux = create(:rubygem, name: "sinatra-redux")
@brando = create(:rubygem, name: "brando")
create(:version, rubygem: @sinatra)
create(:version, rubygem: @sinatra_redux)
create(:version, rubygem: @brando)
import_and_refresh
create(:version, :reindex, rubygem: @sinatra)
create(:version, :reindex, rubygem: @sinatra_redux)
create(:version, :reindex, rubygem: @brando)
get :show, params: { query: "sinatra" }
end

Expand Down Expand Up @@ -103,10 +101,9 @@ class SearchesControllerTest < ActionController::TestCase
@sinatra = create(:rubygem, name: "sinatra")
@sinatra_redux = create(:rubygem, name: "sinatra-redux")
@brando = create(:rubygem, name: "brando")
create(:version, rubygem: @sinatra)
create(:version, rubygem: @sinatra_redux)
create(:version, rubygem: @brando)
import_and_refresh
create(:version, :reindex, rubygem: @sinatra)
create(:version, :reindex, rubygem: @sinatra_redux)
create(:version, :reindex, rubygem: @brando)
get :show, params: { query: "sinatre" }
end

Expand All @@ -132,9 +129,8 @@ class SearchesControllerTest < ActionController::TestCase
setup do
@sinatra = create(:rubygem, name: "sinatra")
@sinatra_redux = create(:rubygem, name: "sinatra-redux")
create(:version, rubygem: @sinatra)
create(:version, :yanked, rubygem: @sinatra_redux)
import_and_refresh
create(:version, :reindex, rubygem: @sinatra)
create(:version, :yanked, :reindex, rubygem: @sinatra_redux)
get :show, params: { query: @sinatra_redux.name.to_s, yanked: true }
end

Expand Down
10 changes: 1 addition & 9 deletions test/helpers/es_helper.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
module SearchKickHelper
def import_and_refresh
Rubygem.searchkick_reindex

# wait for indexing to finish
Searchkick.client.cluster.health wait_for_status: "yellow"
end

def es_downloads(id)
response = get_response(id)
response["_source"]["downloads"]
Expand All @@ -17,7 +10,6 @@ def es_version_downloads(id)
end

def get_response(id)
Rubygem.searchkick_index.refresh
Searchkick.client.get index: "rubygems-#{Rails.env}", id: id
Searchkick.client.get index: Rubygem.search_index.name, id: id
end
end
179 changes: 1 addition & 178 deletions test/integration/gems_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,181 +66,4 @@ class GemsTest < ActionDispatch::IntegrationTest

assert_response :success
end
end

class GemsSystemTest < SystemTest
setup do
@user = create(:user)
@rubygem = create(:rubygem, name: "sandworm", number: "1.0.0")
@version = create(:version, rubygem: @rubygem, number: "1.1.1")
end

test "version navigation" do
visit rubygem_version_path(@rubygem.slug, "1.0.0")
click_link "Next version →"

assert_equal page.current_path, rubygem_version_path(@rubygem.slug, "1.1.1")
click_link "← Previous version"

assert_equal page.current_path, rubygem_version_path(@rubygem.slug, "1.0.0")
end

test "subscribe to a gem" do
visit rubygem_path(@rubygem.slug, as: @user.id)

assert page.has_css?("a#subscribe")

click_link "Subscribe"

assert page.has_content? "Unsubscribe"
assert_equal @user.subscribed_gems.first, @rubygem
end

test "unsubscribe to a gem" do
create(:subscription, rubygem: @rubygem, user: @user)

visit rubygem_path(@rubygem.slug, as: @user.id)

assert page.has_css?("a#unsubscribe")

click_link "Unsubscribe"

assert page.has_content? "Subscribe"
assert_empty @user.subscribed_gems
end

test "shows enable MFA instructions when logged in as owner with MFA disabled" do
create(:ownership, rubygem: @rubygem, user: @user)

visit rubygem_path(@rubygem.slug, as: @user.id)

assert page.has_selector?(".gem__users__mfa-disabled .gem__users a")
assert page.has_content? "Please consider enabling multi-factor"
end

test "shows owners without mfa when logged in as owner" do
@user.enable_totp!("some-seed", "ui_and_api")
user_without_mfa = create(:user)

create(:ownership, rubygem: @rubygem, user: @user)
create(:ownership, rubygem: @rubygem, user: user_without_mfa)

visit rubygem_path(@rubygem.slug, as: @user.id)

assert page.has_selector?(".gem__users__mfa-disabled .gem__users a")
assert page.has_selector?(".gem__users__mfa-text.mfa-warn")
end

test "show mfa enabled when logged in as owner but everyone has mfa enabled" do
@user.enable_totp!("some-seed", "ui_and_api")
user_with_mfa = create(:user)
user_with_mfa.enable_totp!("some-seed", "ui_and_api")

create(:ownership, rubygem: @rubygem, user: @user)
create(:ownership, rubygem: @rubygem, user: user_with_mfa)

visit rubygem_path(@rubygem.slug, as: @user.id)

assert page.has_no_selector?(".gem__users__mfa-text.mfa-warn")
assert page.has_selector?(".gem__users__mfa-text.mfa-info")
end

test "does not show owners without mfa when not logged in as owner" do
@user.enable_totp!("some-seed", "ui_and_api")
user_without_mfa = create(:user)

create(:ownership, rubygem: @rubygem, user: @user)
create(:ownership, rubygem: @rubygem, user: user_without_mfa)

visit rubygem_path(@rubygem.slug)

assert page.has_no_selector?(".gem__users__mfa-disabled .gem__users a")
assert page.has_no_selector?(".gem__users__mfa-text.mfa-warn")
assert page.has_no_selector?(".gem__users__mfa-text.mfa-info")
end

test "shows github link when source_code_uri is set" do
github_link = "http://github.com/user/project"
create(:version, number: "3.0.1", rubygem: @rubygem, metadata: { "source_code_uri" => github_link })

visit rubygem_path(@rubygem.slug)

assert page.has_selector?(".github-btn")
end

test "shows github link when homepage_uri is set" do
github_link = "http://github.com/user/project"
create(:version, number: "3.0.1", rubygem: @rubygem, metadata: { "homepage_uri" => github_link })

visit rubygem_path(@rubygem.slug)

assert page.has_selector?(".github-btn")
end

test "does not show github link when homepage_uri is not github" do
notgithub_link = "http://notgithub.com/user/project"
create(:version, number: "3.0.1", rubygem: @rubygem, metadata: { "homepage_uri" => notgithub_link })

visit rubygem_path(@rubygem.slug)

assert page.has_no_selector?(".github-btn")
end

test "shows both mfa headers if latest AND viewed version require MFA" do
@version.update_attribute :metadata, { "rubygems_mfa_required" => "true" }
create(:version, :mfa_required, rubygem: @rubygem, number: "0.1.1")

visit rubygem_version_path(@rubygem.slug, "0.1.1")

assert page.has_content? "New versions require MFA"
assert page.has_content? "Version published with MFA"
end

test "shows 'new' mfa header only if latest requires MFA but viewed version doesn't" do
@version.update_attribute :metadata, { "rubygems_mfa_required" => "true" }
create(:version, rubygem: @rubygem, number: "0.1.1")

visit rubygem_version_path(@rubygem.slug, "0.1.1")

assert page.has_content? "New versions require MFA"
refute page.has_content? "Version published with MFA"
end

test "shows 'version' mfa header only if latest does not require MFA but viewed version does" do
@version.update_attribute :metadata, { "rubygems_mfa_required" => "false" }
create(:version, :mfa_required, rubygem: @rubygem, number: "0.1.1")

visit rubygem_version_path(@rubygem.slug, "0.1.1")

refute page.has_content? "New versions require MFA"
assert page.has_content? "Version published with MFA"
end

test "does not show either mfa header if neither latest or viewed version require MFA" do
@version.update_attribute :metadata, { "rubygems_mfa_required" => "false" }
create(:version, rubygem: @rubygem, number: "0.1.1")

visit rubygem_version_path(@rubygem.slug, "0.1.1")

refute page.has_content? "New versions require MFA"
refute page.has_content? "Version published with MFA"
end

test "shows both mfa headers if MFA enabled for latest version and viewing latest version" do
@version.update_attribute :metadata, { "rubygems_mfa_required" => "true" }

visit rubygem_path(@rubygem.slug)

assert page.has_content? "New versions require MFA"
assert page.has_content? "Version published with MFA"
end

test "shows neither mfa header if MFA disabled for latest version and viewing latest version" do
@version.update_attribute :metadata, { "rubygems_mfa_required" => "false" }

visit rubygem_path(@rubygem.slug)

refute page.has_content? "New versions require MFA"
refute page.has_content? "Version published with MFA"
end
end
end
2 changes: 1 addition & 1 deletion test/integration/locale_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "test_helper"

class LocaleTest < SystemTest
class LocaleTest < ActionDispatch::IntegrationTest
test "html lang attribute is set from locale" do
I18n.available_locales.each do |locale|
visit root_path(locale: locale)
Expand Down
8 changes: 5 additions & 3 deletions test/jobs/fastly_log_processor_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class FastlyLogProcessorJobTest < ActiveJob::TestCase
@processor = FastlyLogProcessor.new("test-bucket", "fastly-fake.log")
@job = FastlyLogProcessorJob.new(bucket: "test-bucket", key: "fastly-fake.log")
create(:gem_download)
import_and_refresh
end

teardown do
Expand Down Expand Up @@ -58,8 +57,6 @@ class FastlyLogProcessorJobTest < ActiveJob::TestCase
create(:version, rubygem: json, number: "1.8.3", platform: "java")
create(:version, rubygem: json, number: "1.8.3")
create(:version, rubygem: json, number: "1.8.2")

import_and_refresh
end

context "#perform" do
Expand All @@ -69,6 +66,8 @@ class FastlyLogProcessorJobTest < ActiveJob::TestCase
assert_equal 0, GemDownload.count_for_rubygem(json.id)
3.times { @job.perform_now }

json.reindex(refresh: true)

assert_equal 7, es_downloads(json.id)
assert_equal 7, GemDownload.count_for_rubygem(json.id)
end
Expand All @@ -85,6 +84,7 @@ class FastlyLogProcessorJobTest < ActiveJob::TestCase
end

json = Rubygem.find_by_name("json")
json.reindex(refresh: true)

assert_equal 7, GemDownload.count_for_rubygem(json.id)
assert_equal 7, es_downloads(json.id)
Expand All @@ -93,6 +93,7 @@ class FastlyLogProcessorJobTest < ActiveJob::TestCase

should "not run if already processed" do
json = Rubygem.find_by_name("json")
json.reindex(refresh: true)

assert_equal 0, json.downloads
assert_equal 0, es_downloads(json.id)
Expand All @@ -119,6 +120,7 @@ class FastlyLogProcessorJobTest < ActiveJob::TestCase

@job.perform_now
json = Rubygem.find_by_name("json")
json.reindex(refresh: true)

assert_equal 0, json.downloads
assert_equal 0, es_downloads(json.id)
Expand Down
Loading
Loading