Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
update factories and provide fixture-like accessors
  • Loading branch information
saturnflyer committed Apr 4, 2016
1 parent ca2f1ff commit 3406c7d
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 30 deletions.
50 changes: 21 additions & 29 deletions spec/controllers/radiant/pages_controller_spec.rb
Expand Up @@ -131,14 +131,14 @@
}.each do |method, action|
it "should require login to access the #{action} action" do
logout
send method, action, id: Page.first.id
send method, action, id: page_id(:home)
expect(response).to redirect_to('/admin/login')
end

it "should allow access to #{user.to_s.humanize}s for the #{action} action" do
login_as user
expect(controller).to receive(:paginated?).and_return(false)
send method, action, id: Page.first.id
send method, action, id: page_id(:home)
expect(response).to redirect_to('http://test.host/admin/pages')
end
end
Expand All @@ -153,7 +153,7 @@
when :new
{page_id: page_id(:home)}
else
{id: Page.first.id}
{id: page_id(:home)}
end
end
end
Expand All @@ -163,40 +163,32 @@
expect { send(:get, action, @parameters.call) }.to require_login
end

if action == :show
it "should request authentication for API access on show" do
logout
get action, id: page_id(:home), format: "xml"
expect(response.response_code).to eq(401)
end
else
it "should allow access to admins for the #{action} action" do
expect {
send(:get, action, @parameters.call)
}.to restrict_access(allow: [users(:admin)],
url: '/admin/pages')
end
it "should allow access to admins for the #{action} action" do
expect {
send(:get, action, @parameters.call)
}.to restrict_access(allow: [users(:admin)],
url: '/admin/pages')
end

it "should allow access to designers for the #{action} action" do
expect {
send(:get, action, @parameters.call)
}.to restrict_access(allow: [users(:designer)],
url: '/admin/pages')
end
it "should allow access to designers for the #{action} action" do
expect {
send(:get, action, @parameters.call)
}.to restrict_access(allow: [users(:designer)],
url: '/admin/pages')
end

it "should allow non-designers and non-admins for the #{action} action" do
expect {
send(:get, action, @parameters.call)
}.to restrict_access(allow: [users(:non_admin), users(:existing)],
url: '/admin/pages')
end
it "should allow non-designers and non-admins for the #{action} action" do
expect {
send(:get, action, @parameters.call)
}.to restrict_access(allow: [users(:non_admin), users(:existing)],
url: '/admin/pages')
end
end
end

describe '#preview' do

let(:preview_page){ pages(:home) }
let(:preview_page){ pages(:home, :with_parts) }
let(:body_id){ preview_page.part('body').id }
let(:preview_params){
{'page' => {
Expand Down
12 changes: 12 additions & 0 deletions spec/factories/page_factory.rb
Expand Up @@ -4,6 +4,14 @@
breadcrumb { title }
slug { title.slugify }

trait :with_parts do
page_parts { [FactoryGirl.create(:page_part, name: 'body')] }
end

trait :with_children do
children { [FactoryGirl.create(:page, :with_parts)] }
end

factory :page_with_layout do
layout
end
Expand All @@ -15,6 +23,10 @@
factory :file_not_found_page, class: FileNotFoundPage do
end

factory :parent do

end

factory :published_page do
status_id Status[:published].id

Expand Down
8 changes: 8 additions & 0 deletions spec/matchers/login_system_matcher.rb
Expand Up @@ -25,6 +25,10 @@ def failure_message
def negative_failure_message
"expected not to require login"
end

def supports_block_expectations?
true
end
end

class ActionRestriction
Expand Down Expand Up @@ -57,6 +61,10 @@ def failure_message
message.to_sentence
end

def supports_block_expectations?
true
end

private
def denied?(user)
@example.request.session['user_id'] = user.id
Expand Down
16 changes: 16 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -34,4 +34,20 @@

config.before(:each, type: :controller) { @routes = Radiant::Engine.routes }
config.before(:each, type: :routing) { @routes = Radiant::Engine.routes }
end

def pages(which)
Page.find_or_create_by(**FactoryGirl.attributes_for(which))
end

def users(which)
if User.where(login: which).exists?
User.where(login: which).first
else
FactoryGirl.create(which)
end
end

def page_id(which)
pages(which).id
end
2 changes: 1 addition & 1 deletion spec/support/authentication_helper.rb
@@ -1,6 +1,6 @@
module AuthenticationHelper
def login_as(user)
login_user = user.is_a?(User) ? user : FactoryGirl.create(user)
login_user = user.is_a?(User) ? user : users(user)
flunk "Can't login as non-existing user #{user.to_s}." unless login_user
request.session['user_id'] = login_user.id
login_user
Expand Down

0 comments on commit 3406c7d

Please sign in to comment.