Skip to content

Commit

Permalink
Adds testing for the admin page and admin role.
Browse files Browse the repository at this point in the history
Initial user gets admin role.
Admin users can set admin properties.

The only admin property is user account creation toggle.
  • Loading branch information
wilkie committed Aug 6, 2014
1 parent 764b9ee commit 4974ded
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 19 deletions.
38 changes: 22 additions & 16 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ GEM
bson_ext (1.9.1)
bson (~> 1.9.1)
builder (3.0.4)
capybara (1.1.4)
capybara (2.0.3)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
rack (>= 1.0.0)
rack-test (>= 0.5.4)
selenium-webdriver (~> 2.0)
xpath (~> 0.1.4)
childprocess (0.3.9)
xpath (~> 1.0.0)
childprocess (0.5.3)
ffi (~> 1.0, >= 1.0.11)
chunky_png (1.2.8)
coffee-rails (3.2.2)
Expand Down Expand Up @@ -111,7 +111,7 @@ GEM
fabrication (1.2.0)
faraday (0.8.7)
multipart-post (~> 1.1)
ffi (1.9.0)
ffi (1.9.3)
fssm (0.2.10)
haml (3.1.8)
haml-rails (0.3.5)
Expand All @@ -133,6 +133,7 @@ GEM
json (1.8.0)
launchy (2.0.5)
addressable (~> 2.2.6)
libv8 (3.11.8.17)
libxml-ruby (2.6.0)
mail (2.5.4)
mime-types (~> 1.16)
Expand Down Expand Up @@ -175,8 +176,8 @@ GEM
polyglot (0.3.3)
pony (1.5)
mail (> 2.0)
puma (1.6.3)
rack (~> 1.2)
puma (2.9.0)
rack (>= 1.1, < 2.0)
pygmentize (0.0.3)
quiet_assets (1.0.2)
railties (>= 3.1, < 5.0)
Expand Down Expand Up @@ -209,24 +210,25 @@ GEM
rdoc (3.12.2)
json (~> 1.4)
redcarpet (1.17.2)
ref (1.0.5)
rest-client (1.6.7)
mime-types (>= 1.16)
rsa (0.1.4)
ruby-hmac (0.4.0)
rubyzip (0.9.9)
rubyzip (1.1.6)
safe_yaml (0.9.4)
sass (3.1.21)
sass-rails (3.2.6)
railties (~> 3.2.0)
sass (>= 3.1.10)
tilt (~> 1.3)
selenium-webdriver (2.33.0)
childprocess (>= 0.2.5)
selenium-webdriver (2.42.0)
childprocess (>= 0.5.0)
multi_json (~> 1.0)
rubyzip
rubyzip (~> 1.0)
websocket (~> 1.0.4)
show_me_the_cookies (1.1.4)
capybara (~> 1.0)
show_me_the_cookies (2.0.2)
capybara (~> 2.0)
simple_oauth (0.2.0)
simplecov (0.4.2)
simplecov-html (~> 0.4.4)
Expand All @@ -236,6 +238,9 @@ GEM
multi_json (~> 1.0)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
therubyracer (0.11.4)
libv8 (~> 3.11.8.12)
ref
thor (0.18.1)
tilt (1.4.1)
tire (0.4.3)
Expand Down Expand Up @@ -263,7 +268,7 @@ GEM
whenever (0.6.8)
aaronh-chronic (>= 0.3.9)
activesupport (>= 2.3.4)
xpath (0.1.4)
xpath (1.0.0)
nokogiri (~> 1.3)

PLATFORMS
Expand All @@ -274,7 +279,7 @@ DEPENDENCIES
bcrypt-ruby (~> 3.0.0)
bson (~> 1.9.0)
bson_ext (~> 1.9.0)
capybara (~> 1.1.2)
capybara (~> 2.0.3)
coffee-rails (~> 3.2.2)
compass-rails (~> 1.0.3)
database_cleaner (~> 0.6.7)
Expand Down Expand Up @@ -302,7 +307,7 @@ DEPENDENCIES
ostatus (~> 1.0.0)
osub (~> 0.0.6)
pony (~> 1.3)
puma (~> 1.6.3)
puma (~> 2.9.0)
pygmentize (~> 0.0.3)
quiet_assets (~> 1.0.0)
rails (= 3.2.14)
Expand All @@ -313,8 +318,9 @@ DEPENDENCIES
rsa (~> 0.1.4)
sass (~> 3.1.10)
sass-rails (~> 3.2.5)
show_me_the_cookies (~> 1.1.0)
show_me_the_cookies (~> 2.0.2)
simplecov (~> 0.4.0)
therubyracer (~> 0.11.3)
tire (~> 0.4.1)
twitter (~> 4.8.0)
tzinfo (~> 0.3.29)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/admin_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def index
def update
return if admin_only!

admin_info.multiuser = (params["multiuser"] == "on")
admin_info.multiuser = params.has_key?("multiuser")
admin_info.save

redirect_to root_path
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/index.haml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@

%br

%input{:type => :submit, :value => :save}
%input{:type => :submit, :id => :submit, :value => :save}
15 changes: 14 additions & 1 deletion test/acceptance/acceptance_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,19 @@ def log_in_username(user)
click_button "Log in"
end

def log_in_admin(user)
User.stubs(:authenticate).returns(user)

visit "/login"

within("form") do
fill_in "username", :with => user.username
fill_in "password", :with => "anything"
end

click_button "Log in"
end

def profile(section = nil)
case section
when "name"
Expand Down Expand Up @@ -177,4 +190,4 @@ def heisenbug_log
end
end

class Heisenbug < StandardError; end
class Heisenbug < StandardError; end
87 changes: 87 additions & 0 deletions test/acceptance/admin_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
require 'require_relative' if RUBY_VERSION[0,3] == '1.8'
require_relative 'acceptance_helper'

describe "admin" do
include AcceptanceHelper

it "creates the first user account as an admin" do
u = User.first(:username => "new_user")
assert u.nil?

visit '/login'

fill_in 'username', :with => 'new_user'
fill_in 'password', :with => 'mypassword'
click_button 'Log in'

u = User.first(:username => "new_user")
refute u.nil?
assert u.admin
end

it "redirects first user to admin page after signup" do
u = User.first(:username => "new_user")
assert u.nil?

visit '/login'

fill_in 'username', :with => 'new_user'
fill_in 'password', :with => 'mypassword'
click_button 'Log in'

assert_match /\/admin$/, page.current_url
end

it "admin page is accessible by admin users" do
existing_user = Fabricate(:user, :admin => true, :username => "taken")
log_in_username(existing_user)

visit '/admin'

assert_match /\/admin$/, page.current_url
end

it "admin page is not accessible by non-admin users" do
existing_user = Fabricate(:user, :admin => false, :username => "taken")
log_in("taken")

visit '/admin'

refute_match /\/admin$/, page.current_url
end

it "admin page allows you to turn on multiuser setting" do
existing_user = Fabricate(:user, :admin => true, :username => "taken")
log_in_username(existing_user)

visit '/admin'

check 'multiuser'
click_button 'submit'

assert Admin.first.multiuser == true
end

it "admin page allows you to turn off multiuser setting" do
existing_user = Fabricate(:user, :admin => true, :username => "taken")
log_in_username(existing_user)

visit '/admin'

uncheck :multiuser
click_button :submit

assert Admin.first.multiuser == false
end

it "admin page allows you to see the current multiuser setting" do
Admin.create(:multiuser => true)

existing_user = Fabricate(:user, :admin => true, :username => "taken")
log_in_username(existing_user)

visit '/admin'

assert find('#multiuser').checked? == "checked"
end
end
4 changes: 4 additions & 0 deletions test/acceptance/signup_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
describe "signup" do
include AcceptanceHelper

before do
Admin.create(:multiuser => true)
end

describe "username" do
it "signs up successfully" do
u = User.first(:username => "new_user")
Expand Down
28 changes: 28 additions & 0 deletions test/models/admin_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# encoding: UTF-8
require_relative '../test_helper'
require "webmock"
include WebMock::API

describe Admin do
include TestHelper

describe "can_create_user?" do
it "must not allow user creation when there are Users and multiuser is false" do
u = Fabricate(:user)
refute Admin.new(:multiuser => false).can_create_user?
end

it "must allow user creation when there are Users and multiuser is true" do
u = Fabricate(:user)
assert Admin.new(:multiuser => true).can_create_user?
end

it "must allow user creation when there aren't Users and multiuser is false" do
assert Admin.new(:multiuser => false).can_create_user?
end

it "must allow user creation when there aren't Users and multiuser is true" do
assert Admin.new(:multiuser => true).can_create_user?
end
end
end
18 changes: 18 additions & 0 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,24 @@ def stub_superfeedr_request_for_user(user)
end
end

describe "admin" do
it "must not be true as default" do
refute User.new.admin == true
end
end

describe "admin?" do
it "must return true when admin is true" do
u = Fabricate.build(:user, :admin => true)
assert u.admin? == true
end

it "must return false when admin is false" do
u = Fabricate.build(:user, :admin => false)
assert u.admin? == false
end
end

describe "twitter auth" do
it "has twitter" do
u = Fabricate(:user)
Expand Down

0 comments on commit 4974ded

Please sign in to comment.