Skip to content

Commit

Permalink
Merge pull request #783 from robinboening/master
Browse files Browse the repository at this point in the history
Removes duplicated test support auth helper
  • Loading branch information
tvdeyen committed Jun 15, 2015
2 parents 0e5cff8 + dde6291 commit 0f90048
Show file tree
Hide file tree
Showing 40 changed files with 90 additions and 122 deletions.
35 changes: 0 additions & 35 deletions lib/alchemy/test_support/auth_helpers.rb

This file was deleted.

26 changes: 16 additions & 10 deletions lib/alchemy/test_support/integration_helpers.rb
Expand Up @@ -3,24 +3,30 @@ module TestSupport

# Helpers for integration specs
#
# This file is included in rspec integration/request tests.
# This file is included in spec_helper.rb
#
module IntegrationHelpers

# Used in Capybara features specs. Stubs the current_alchemy_user
# Used to stub the current_alchemy_user
#
# It mocks an admin user, but you can pass in a user object that would be used as stub.
# Pass either a user object or a symbol in the format of ':as_admin'.
# The browser language is set to english ('en')
#
def authorize_as_admin(user=nil)
# Ensure that phantomjs has always the same browser language.
if Capybara.current_driver == :poltergeist
page.driver.headers = { 'Accept-Language' => 'en' }
end
if !user
user = mock_model('DummyUser', alchemy_roles: %w(admin), language: 'en')
def authorize_user(user_or_role = nil)
if user_or_role.is_a?(Alchemy.user_class)
user = user_or_role
else
user = build(:alchemy_dummy_user, user_or_role)
end
set_phantomjs_browser_language("en")
allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(user)
end

def set_phantomjs_browser_language(lang = nil)
if Capybara.current_driver == :poltergeist
page.driver.headers = {"Accept-Language" => lang}
end
end
end

end
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/admin/attachments_controller_spec.rb
Expand Up @@ -5,7 +5,7 @@ module Alchemy
let(:attachment) { build_stubbed(:attachment) }

before do
sign_in(admin_user)
authorize_user(:as_admin)
end

describe "#index" do
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/admin/clipboard_controller_spec.rb
Expand Up @@ -7,7 +7,7 @@ module Alchemy
let(:another_element) { build_stubbed(:element, page: public_page) }

before do
sign_in(admin_user)
authorize_user(:as_admin)
session[:alchemy_clipboard] = {}
end

Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/admin/contents_controller_spec.rb
Expand Up @@ -6,7 +6,7 @@ module Alchemy
let(:content) { build_stubbed(:content, element: element) }

before do
sign_in(admin_user)
authorize_user(:as_admin)
end

context 'with element_id parameter' do
Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/admin/dashboard_controller_spec.rb
Expand Up @@ -2,9 +2,9 @@

module Alchemy
describe Admin::DashboardController do
let(:user) { admin_user }
let(:user) { build(:alchemy_dummy_user, :as_admin) }

before { sign_in(user) }
before { authorize_user(user) }

describe '#index' do
before do
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/admin/elements_controller_spec.rb
Expand Up @@ -7,7 +7,7 @@ module Alchemy
let(:element_in_clipboard) { create(:element, :page_id => alchemy_page.id) }
let(:clipboard) { session[:alchemy_clipboard] = {} }

before { sign_in(author_user) }
before { authorize_user(:as_author) }

describe '#index' do
let(:alchemy_page) { build_stubbed(:page) }
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/admin/essence_files_controller_spec.rb
Expand Up @@ -3,7 +3,7 @@
module Alchemy
describe Admin::EssenceFilesController do
before do
sign_in(admin_user)
authorize_user(:as_admin)
end

let(:essence_file) { mock_model('EssenceFile', :attachment= => nil, content: content) }
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/admin/essence_pictures_controller_spec.rb
Expand Up @@ -2,7 +2,7 @@

module Alchemy
describe Admin::EssencePicturesController do
before { sign_in(admin_user) }
before { authorize_user(:as_admin) }

let(:essence) { EssencePicture.new }
let(:content) { Content.new }
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/admin/languages_controller_spec.rb
Expand Up @@ -3,7 +3,7 @@
describe Alchemy::Admin::LanguagesController do

before do
sign_in(admin_user)
authorize_user(:as_admin)
end

describe "#new" do
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/admin/layoutpages_controller_spec.rb
Expand Up @@ -4,7 +4,7 @@ module Alchemy
describe Admin::LayoutpagesController do

before(:each) do
sign_in(admin_user)
authorize_user(:as_admin)
end

describe "#index" do
Expand Down
6 changes: 3 additions & 3 deletions spec/controllers/admin/pages_controller_spec.rb
Expand Up @@ -12,7 +12,7 @@ module Alchemy
end

context 'a member' do
before { sign_in(member_user) }
before { authorize_user(build(:alchemy_dummy_user)) }

it 'can not access page tree' do
alchemy_get :index
Expand All @@ -21,9 +21,9 @@ module Alchemy
end

context 'with logged in editor user' do
let(:user) { editor_user }
let(:user) { build(:alchemy_dummy_user, :as_editor) }

before { sign_in(user) }
before { authorize_user(user) }

describe '#index' do
let(:language) { build_stubbed(:language) }
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/admin/pictures_controller_spec.rb
Expand Up @@ -4,7 +4,7 @@ module Alchemy
describe Admin::PicturesController do

before do
sign_in(admin_user)
authorize_user(:as_admin)
end

describe "#index" do
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/admin/resources_controller_spec.rb
Expand Up @@ -14,7 +14,7 @@ class Admin::EventsController < Alchemy::Admin::ResourcesController
let(:lustig) { Event.create(name: 'Lustig') }

before do
sign_in(admin_user)
authorize_user(:as_admin)
peter; lustig
end

Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/admin/trash_controller_spec.rb
Expand Up @@ -9,7 +9,7 @@ module Admin
let(:element) { FactoryGirl.create(:element, :public => false, :page => alchemy_page) }

before {
sign_in(admin_user)
authorize_user(:as_admin)
element.trash!
}

Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/alchemy/admin/tags_controller_spec.rb
Expand Up @@ -3,7 +3,7 @@
module Alchemy
module Admin
describe TagsController do
before { sign_in(admin_user) }
before { authorize_user(:as_admin) }

describe '#create' do
context 'without required params' do
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/attachments_controller_spec.rb
Expand Up @@ -47,7 +47,7 @@ module Alchemy
end

context "as member user" do
before { sign_in(member_user) }
before { authorize_user(build(:alchemy_dummy_user)) }

it "should be possible to download attachments from restricted pages" do
alchemy_get :download, :id => attachment.id
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/elements_controller_spec.rb
Expand Up @@ -36,7 +36,7 @@ module Alchemy
end

context "for member user" do
before { sign_in(member_user) }
before { authorize_user(build(:alchemy_dummy_user)) }

it "should render elements of restricted pages" do
alchemy_get :show, id: restricted_element.id
Expand Down
6 changes: 2 additions & 4 deletions spec/controllers/pages_controller_spec.rb
Expand Up @@ -12,7 +12,7 @@ module Alchemy
context 'an author' do
let(:unpublic) { create(:page, parent: default_language_root) }

before { allow(controller).to receive(:current_alchemy_user).and_return(author_user) }
before { authorize_user(:as_author) }

it "should not be able to visit a unpublic page" do
expect {
Expand Down Expand Up @@ -210,10 +210,8 @@ module Alchemy
end

context 'with user logged in' do
let(:author_user) { mock_model(Alchemy.user_class, cache_key: 'bbb') }

before do
sign_in(author_user)
authorize_user(mock_model(Alchemy.user_class, cache_key: 'bbb'))
end

it "returns another etag for response headers" do
Expand Down
6 changes: 3 additions & 3 deletions spec/controllers/pictures_controller_spec.rb
Expand Up @@ -41,7 +41,7 @@ module Alchemy

context "as member user" do
before do
sign_in(member_user)
authorize_user(build(:alchemy_dummy_user))
end

it "should render the picture" do
Expand Down Expand Up @@ -226,7 +226,7 @@ module Alchemy

context "as member user" do
before do
sign_in(member_user)
authorize_user(build(:alchemy_dummy_user))
end

it "should render the picture" do
Expand Down Expand Up @@ -294,7 +294,7 @@ module Alchemy

context "as member user" do
before do
sign_in(member_user)
authorize_user(build(:alchemy_dummy_user))
end

it "should render the picture" do
Expand Down
20 changes: 11 additions & 9 deletions spec/features/admin/dashboard_spec.rb
@@ -1,11 +1,10 @@
require 'spec_helper'

describe 'Dashboard feature' do
let(:user) { DummyUser.new }
let(:user) { create(:alchemy_dummy_user, :as_admin, name: "Joe User") }

before do
user.update_attributes(alchemy_roles: %w(admin), name: "Joe User", id: 1)
authorize_as_admin(user)
authorize_user(user)
end

describe 'Locked pages summary' do
Expand All @@ -30,11 +29,11 @@
end

context 'When locked by another user' do
it "should show locked by user's name" do
user = DummyUser.new
user.update_attributes(alchemy_roles: %w(admin), name: "Sue Smith", id: 2)
a_page.lock_to!(user)
allow(DummyUser).to receive(:find_by).and_return(user)
let(:other_user) { create(:alchemy_dummy_user, :as_admin, name: "Sue Smith") }

it "shows the name of the user who locked the page" do
a_page.lock_to!(other_user)
allow(user.class).to receive(:find_by).and_return(other_user)
visit admin_dashboard_path
locked_pages_widget = all('div[@class="widget"]').first
expect(locked_pages_widget).to have_content "Currently locked pages:"
Expand All @@ -46,7 +45,10 @@

describe 'Sites widget' do
context 'with multiple sites' do
let!(:site) { Alchemy::Site.create!(name: 'Site', host: 'site.com') }

before do
Alchemy::Site.create!(name: 'Site', host: 'site.com')
end

it "lists all sites" do
visit admin_dashboard_path
Expand Down
11 changes: 5 additions & 6 deletions spec/features/admin/language_tree_feature_spec.rb
@@ -1,18 +1,17 @@
require 'spec_helper'

describe 'Language tree feature', type: :feature, js: true do

let(:klingonian) { FactoryGirl.create(:klingonian) }
let(:german_root) { FactoryGirl.create(:language_root_page) }
let(:klingonian_root) { FactoryGirl.create(:language_root_page, :name => 'Klingonian', :language => klingonian) }

before do
german_root
authorize_as_admin
FactoryGirl.create(:language_root_page)
authorize_user(:as_admin)
end

context "in a multilangual environment" do
before { klingonian_root }
before do
FactoryGirl.create(:language_root_page, :name => 'Klingonian', :language => klingonian)
end

it "one should be able to switch the language tree" do
visit('/admin/pages')
Expand Down
2 changes: 1 addition & 1 deletion spec/features/admin/legacy_page_url_management_spec.rb
Expand Up @@ -3,7 +3,7 @@

describe 'Legacy page url management', type: :feature, js: true do
before do
authorize_as_admin
authorize_user(:as_admin)
end

let!(:a_page) { create(:page) }
Expand Down
2 changes: 1 addition & 1 deletion spec/features/admin/link_overlay_spec.rb
Expand Up @@ -3,7 +3,7 @@
describe "Link overlay" do

before do
authorize_as_admin
authorize_user(:as_admin)
end

context "GUI" do
Expand Down
2 changes: 1 addition & 1 deletion spec/features/admin/locale_select_feature_spec.rb
Expand Up @@ -4,7 +4,7 @@
let(:a_page) { FactoryGirl.create(:public_page) }
before do
allow(Alchemy::I18n).to receive(:translation_files).and_return ['alchemy.kl.yml', 'alchemy.jp.yml', 'alchemy.cz.yml']
authorize_as_admin
authorize_user(:as_admin)
end

it "contains all locales in a selectbox" do
Expand Down
2 changes: 1 addition & 1 deletion spec/features/admin/modules_integration_spec.rb
Expand Up @@ -2,7 +2,7 @@

describe "Modules" do
context "A custom module with a main-apps controller" do
before { authorize_as_admin }
before { authorize_user(:as_admin) }

it "should have a button in main_navigation, pointing to the configured controller" do
Alchemy::Modules.register_module(
Expand Down
2 changes: 1 addition & 1 deletion spec/features/admin/navigation_feature_spec.rb
Expand Up @@ -3,7 +3,7 @@
describe 'Admin navigation feature' do

context 'admin users' do
before { authorize_as_admin }
before { authorize_user(:as_admin) }

it "can leave the admin area" do
visit ('/admin/leave')
Expand Down

0 comments on commit 0f90048

Please sign in to comment.