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

Mutlitenancy in test #1105

Merged
merged 6 commits into from May 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions .travis.yml
@@ -1,3 +1,5 @@
sudo: false
language: ruby
notifications:
email: false
dist: trusty
Expand All @@ -12,15 +14,18 @@ env:
- secure: BnmjWPatxrNOpYKw05B54q+CUiDRw16bnL0YMvBU6XDotOTNnGjG24oul3et0wmOx0TfgLXLQ3twyWnOkW5HEIUGzjX825Pd/Ur8YaHZI9KeQc9FrAn43aDIVAtbnkg7lxMqsJZUmfSHzUXkbyPmMtLc+LK9wteQ0kdzIplsenyLV7VRTP8jINxkUnt/TYj5poah1RU9NemvGcUKW2hC3aW0d8/yjAV7boZodgBSiMSHSLQvScNNvS3+Goymlq15hl26ZjMqddLMJgRZwskZhDyAgUQuZpSHymXlzRSiSzSshJW1ST0MLmECImRdJ6t/JP2cf0ZHN/sR2XBG0ABc9dQfKB/aLITbZ5IbSHyjOoBzlilSixNVT7+VoW6MfbnXFlg2cwFfKgKPkXElADcIUT7lRcTiR5EO7oJILJ8C1psOFtC1XtbKVT1P4zHXoFqvaNQ8iWg0sMdSbYfDxfG9ObAA0SYA4d7ZvbZ0bt8XUhWFsBoMEAi6bzDxsp4jPkKIAjboDar/iol/rEIF2FwaRIjoTsM+MzN+p+P1bqjMFIJsFRX7DqzJ/6XP+vm+z437oEWKbTGnPlCWCwBw2qOpQNwe9o5wzP2H3KN2u0AcVs2UUcyE/pPObvWFKiGXC5FAljdyjv1jjHmuvBnnsuAkB3siqbEpvT7S/c4Qi8L8/To=
- secure: FknMcFAQQAZMZZeUOgm1pyYI7s2/I+JYpKzCDI4y7MjwdGjRWs1vM1suud2sEtBEtCajeedvXDhrENHPEwVaBwKwgOkQxb6WjcVHLm9MBzrG5apWOZre9hAdbZXcs5QVpoA/xj3ASLF/XV4wndyN03eex0cAYRekh/rTsHPhItdEOc1PuZKuLb+3UNWKp9tyQ1oHA7ZqeEvyEK6EqOMBBW9EBdnkYN6JT5x1NwAtm0FPTia58UQyUdqjSLv7oWUAutACyPCS5zNsBwpzJWaQ2x7qhCDuRB8X0J0lz5lReuBx9j2FW0dWLEdj11gam0osdYux85Tk5fPOOGtKRmNwWFqSPrvzIvcuR/C3pzlfr35d+KduK+OD3EnsKL2Cp6mkgQPlBDGvKGl6GH0aiOenhHxjNIsG5eK29Gyit0EQRLosHy+eDZymzvT97WNheali+PKTFmopatmFILmID2Gnk66yvtdeNFFBUCAMVH3t0C/8hsSt484HP1AiwaPR4aeucMOJmK+H7xvwQRPs+vYemGYL7bBRoQE7F6tr9UQFMyw7/WuKPNSx1KV8bhcmoCOrOpjxzVvMjhokX83CGxok2hA/vhhHMHjh3D+Z+uhJzLja/gDJAa5rPHkqQMUdUssJ0/R9IiW+tEGHdUM8hPrzyFU5V1DmcO1DEBFvSeTvL/4=

sudo: false
language: ruby
cache:
bundler: true
directories:
- "travis_phantomjs"

jdk:
- oraclejdk8
addons:
hosts:
- tenant1.localhost
- tenant2.localhost
- test.host

before_install:
- gem update --system
Expand Down
2 changes: 1 addition & 1 deletion config/settings/test.yml
Expand Up @@ -2,6 +2,6 @@ zookeeper:
connection_str: 'localhost:9985/configs'

multitenancy:
enabled: false
enabled: true
# admin_host: 'localhost'
# default_host: "%{tenant}.localhost"
2 changes: 1 addition & 1 deletion spec/controllers/account_sign_up_controller_spec.rb
@@ -1,4 +1,4 @@
RSpec.describe AccountSignUpController, type: :controller, multitenant: true do
RSpec.describe AccountSignUpController, type: :controller do
let(:user) {}

before do
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/admin/group_users_controller_spec.rb
@@ -1,4 +1,4 @@
RSpec.describe Admin::GroupUsersController do
RSpec.describe Admin::GroupUsersController, faketenant: true do
let(:group) { FactoryGirl.create(:group) }

context 'as an anonymous user' do
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/admin/groups_controller_spec.rb
@@ -1,4 +1,4 @@
RSpec.describe Admin::GroupsController do
RSpec.describe Admin::GroupsController, singletenant: true do
context 'as an anonymous user' do
describe 'GET #index' do
subject { get :index }
Expand Down
5 changes: 3 additions & 2 deletions spec/controllers/hyrax/admin/appearances_controller_spec.rb
@@ -1,7 +1,6 @@
RSpec.describe Hyrax::Admin::AppearancesController, type: :controller do
RSpec.describe Hyrax::Admin::AppearancesController, type: :controller, singletenant: true do
before { sign_in user }
routes { Hyrax::Engine.routes }
let(:hyrax) { routes.url_helpers }

context 'with an unprivileged user' do
let(:user) { create(:user) }
Expand Down Expand Up @@ -32,6 +31,8 @@
end

describe "PUT #update" do
let(:hyrax) { routes.url_helpers }

context "with valid params" do
let(:valid_attributes) do
{ banner_image: "image.jpg" }
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/splash_controller_spec.rb
@@ -1,4 +1,4 @@
RSpec.describe SplashController, multitenant: true do
RSpec.describe SplashController do
describe 'get index' do
it 'is successful' do
get :index
Expand Down
12 changes: 12 additions & 0 deletions spec/factories/accounts.rb
@@ -1,6 +1,18 @@
FactoryGirl.define do
factory :solr_endpoint do
options Hash.new(url: 'http://fakesolr.localhost:9876/solr/', collection: 'fakecore')
end
factory :fcrepo_endpoint do
options Hash.new(url: 'http://fakefedora.localhost:8765/', base_path: 'fakebase')
end
factory :redis_endpoint do
options Hash.new(namespace: 'fakeNS')
end
factory :account do
sequence(:cname) { |_n| srand }
solr_endpoint
redis_endpoint
fcrepo_endpoint
end
factory :sign_up_account, class: Account do
sequence(:name) { |_n| srand }
Expand Down
9 changes: 4 additions & 5 deletions spec/models/account_spec.rb
Expand Up @@ -87,15 +87,14 @@
end

describe '#switch' do
let!(:previous_solr_connection) { Blacklight.default_index }
let!(:previous_fcrepo_connection) { ActiveFedora.fedora }
let!(:previous_solr_url) { ActiveFedora::SolrService.instance.conn.uri.to_s }
let!(:previous_redis_namespace) { 'hyku' }
let!(:previous_fedora_host) { ActiveFedora.fedora.host }

before do
subject.build_solr_endpoint.update(url: 'http://example.com/solr/')
subject.build_fcrepo_endpoint.update(url: 'http://example.com/fedora', base_path: '/dev')
subject.build_redis_endpoint.update(namespace: 'foobaz')
subject.switch!
end

after do
Expand All @@ -115,8 +114,8 @@
subject.switch do
# no-op
end
expect(ActiveFedora::SolrService.instance.conn.uri.to_s).to eq 'http://127.0.0.1:8985/solr/hydra-test/'
expect(ActiveFedora.fedora.host).to eq 'http://127.0.0.1:8986/rest'
expect(ActiveFedora::SolrService.instance.conn.uri.to_s).to eq previous_solr_url
expect(ActiveFedora.fedora.host).to eq previous_fedora_host
expect(Hyrax.config.redis_namespace).to eq previous_redis_namespace
end

Expand Down
22 changes: 10 additions & 12 deletions spec/requests/home_spec.rb
@@ -1,31 +1,29 @@
RSpec.describe 'Home page', type: :request do
context 'without a current tenant', multitenant: true do
before do
allow(Settings.multitenancy).to receive(:admin_host).and_return('localhost')
end

context 'with multitenancy' do
describe 'GET /' do
context "on the primary host" do
context 'on the primary host' do
before { host! 'localhost' }
it 'redirects to the accounts landing page' do
get root_path
expect(response).to have_http_status(200)
end
end

context "on a subhost" do
before { host! 'foo.bar.com' }
context 'on an unknown subhost' do
before { host! 'mystery.localhost' }
it 'raises a 404' do
expect { get root_path }.to raise_error(ActionController::RoutingError)
end
end
end
end

describe 'GET /' do
it 'works! (now write some real specs)' do
get root_path
expect(response).to have_http_status(200)
context 'with singletenancy', singletenant: true do
describe 'GET /' do
it 'fields the request' do
get root_path
expect(response).to have_http_status(200)
end
end
end
end
2 changes: 1 addition & 1 deletion spec/requests/riiif_spec.rb
@@ -1,4 +1,4 @@
RSpec.describe 'IIIF image API' do
RSpec.describe 'IIIF image API', type: :request, singletenant: true do
let(:user) { create(:user) }
let(:work) { create(:work_with_one_file, user: user) }
let(:file_set) { work.ordered_members.to_a.first }
Expand Down
2 changes: 1 addition & 1 deletion spec/routing/admin/accounts_routing_spec.rb
@@ -1,4 +1,4 @@
RSpec.describe Admin::AccountsController, type: :routing, multitenant: true do
RSpec.describe Admin::AccountsController, type: :routing do
describe "routing" do
it "routes to #edit" do
expect(get: "/admin/account/edit").to route_to("admin/accounts#edit")
Expand Down
2 changes: 1 addition & 1 deletion spec/routing/proprietor/accounts_routing_spec.rb
@@ -1,4 +1,4 @@
RSpec.describe Proprietor::AccountsController, type: :routing, multitenant: true do
RSpec.describe Proprietor::AccountsController, type: :routing do
let(:admin_host) { Account.admin_host }
let(:admin_host_url) { "http://#{admin_host}" }
describe "routing" do
Expand Down
24 changes: 23 additions & 1 deletion spec/support/multitenancy_metadata.rb
Expand Up @@ -3,16 +3,38 @@
# might not actually be used in all examples where it's included.
config.after do
example = RSpec.current_example
if example.metadata[:multitenant]
if example.metadata[:multitenant] || example.metadata[:singletenant]
allow(Settings.multitenancy).to receive(:enabled).and_call_original
Rails.application.reload_routes!
end
end

# There are 3 optional flags available to a test block. Only ONE will be active
# at any given time. They are (with areas of likely use):
# :multitenant - general case default, only needs to be explicit for types described below
# :singletenant - For tests explicitly written for singletenancy, in particular routing
# :faketenant - Ignoring multitenancy, but pretending *some* tenant is always active
#
# Spec types:
# :feature - Because multitenancy affects routing, all :feature tests default to :singletenant.
# Similarly, :feature tests cannot use :faketenant (would fail anyway).
# :controller - default to :faketenant, since most resource controllers can be tested
# without routing as long as they get some account.

config.before do
example = RSpec.current_example
if example.metadata[:multitenant]
allow(Settings.multitenancy).to receive(:enabled).and_return(true)
Rails.application.reload_routes!
elsif example.metadata[:singletenant] || example.metadata[:type] == :feature
example.metadata[:singletenant] = true if example.metadata[:type] == :feature # flag for cleanup later
allow(Settings.multitenancy).to receive(:enabled).and_return(false)
Rails.application.reload_routes!
elsif example.metadata[:faketenant] || example.metadata[:type] == :controller
example.metadata[:faketenant] = true if example.metadata[:type] == :controller # flag for cleanup later
acct = FactoryGirl.build(:account, tenant: 'FakeTenant', cname: 'tenant1')
allow(acct).to receive(:persisted?).and_return true # nevertheless
allow(Account).to receive(:from_request).and_return(acct)
end
end
end
2 changes: 1 addition & 1 deletion spec/views/account_sign_up/new.html.erb_spec.rb
@@ -1,4 +1,4 @@
RSpec.describe "account_sign_up/new", type: :view, multitenant: true do
RSpec.describe "account_sign_up/new", type: :view do
before do
assign(:account, Account.new(
name: "MyString"
Expand Down
2 changes: 1 addition & 1 deletion spec/views/proprietor/accounts/edit.html.erb_spec.rb
@@ -1,4 +1,4 @@
RSpec.describe "proprietor/accounts/edit", type: :view, multitenant: true do
RSpec.describe "proprietor/accounts/edit", type: :view do
let(:account) { FactoryGirl.create(:account) }

before do
Expand Down
2 changes: 1 addition & 1 deletion spec/views/proprietor/accounts/index.html.erb_spec.rb
@@ -1,4 +1,4 @@
RSpec.describe "proprietor/accounts/index", type: :view, multitenant: true do
RSpec.describe "proprietor/accounts/index", type: :view do
let(:account_a) { FactoryGirl.create(:account) }
let(:account_b) { FactoryGirl.create(:account) }

Expand Down
2 changes: 1 addition & 1 deletion spec/views/proprietor/accounts/new.html.erb_spec.rb
@@ -1,4 +1,4 @@
RSpec.describe "proprietor/accounts/new", type: :view, multitenant: true do
RSpec.describe "proprietor/accounts/new", type: :view do
before do
assign(:account, Account.new(
name: "MyString"
Expand Down
2 changes: 1 addition & 1 deletion spec/views/proprietor/accounts/show.html.erb_spec.rb
@@ -1,4 +1,4 @@
RSpec.describe "proprietor/accounts/show", type: :view, multitenant: true do
RSpec.describe "proprietor/accounts/show", type: :view do
let(:account) { FactoryGirl.create(:account) }

before do
Expand Down