Skip to content

Commit

Permalink
improve tests for preferences and fix a pending cucumber step
Browse files Browse the repository at this point in the history
  • Loading branch information
lrbalt committed Aug 19, 2011
1 parent 07b05d0 commit 84ad01a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 35 deletions.
1 change: 1 addition & 0 deletions app/helpers/todos_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ def date_field_tag(name, id, value = nil, options = {})
def update_needs_to_hide_context
return (@remaining_in_context == 0 && (@todo_hidden_state_changed && @todo.hidden?)) ||
(@remaining_in_context == 0 && @todo_was_deferred_from_active_state) ||
(@remaining_in_context == 0 && @tag_was_removed) ||
(@remaining_in_context == 0 && @todo.completed? && !(@original_item_was_deferred || @original_item_was_hidden)) if source_view_is(:tag)

return false if source_view_is_one_of(:project, :calendar)
Expand Down
2 changes: 1 addition & 1 deletion features/tagging_todos.feature
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Feature: Tagging todos
@selenium
Scenario: I can add a new todo from tag view with a different tag and it will not be added to the page
When I go to the tag page for "tracks"
And I submit a new action with description "prepare release" with tags "release, next" in the context "@pc"
And I submit a new action with description "prepare release" and the tags "release, next" in the context "@pc"
Then I should not see "prepare release"

@selenium
Expand Down
77 changes: 56 additions & 21 deletions test/functional/preferences_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,84 @@
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
require File.dirname(__FILE__) + '/../test_helper'
require 'preferences_controller'
require 'preference'

# Re-raise errors caught by the controller.
class PreferencesController; def rescue_action(e) raise e end; end

class PreferencesControllerTest < ActionController::TestCase
fixtures :users, :preferences

def setup
assert_equal "test", ENV['RAILS_ENV']
assert_equal "change-me", Tracks::Config.salt
@controller = PreferencesController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end

def test_preferences
get :index # should fail because no login
test "render_date_format requires login" do
get :render_date_format
assert_redirected_to :controller => 'login', :action => 'login'
end

test "calling render_date_format returns date" do
login_as :admin_user
get :index

get :render_date_format
assert_response :success
assert_equal assigns['page_title'], "TRACKS::Preferences"
assert_not_nil assigns['prefs']
assert_equal I18n.l(Time.zone.now, :format => "%Y-%m-%d"), @response.body

get(:render_date_format, {:date_format => "%A %Y"})
assert_response :success
assert_equal I18n.l(Time.zone.now, :format => "%A %Y"), @response.body
end
def test_edit_preferences
get :edit # should fail because no login

test "index page requires login" do
get :index # should fail because no login
assert_redirected_to :controller => 'login', :action => 'login'
end

test "index sets prefs and user" do
login_as :admin_user
get :edit
get :index
assert_response :success
assert_equal assigns['page_title'], "TRACKS::Edit Preferences"
assert_template 'preferences/edit'
assert_equal assigns['page_title'], "TRACKS::Preferences"
assert_not_nil assigns['prefs']
assert_not_nil assigns['user']
end
def test_update_preferences

test "should update preferences" do
login_as :admin_user
post :update, {:user => { :first_name => 'Jane', :last_name => 'Doe'}, :prefs => { :date_format => "%m-%d-%Y", :week_starts => "0", :show_number_completed => "10", :show_completed_projects_in_sidebar => "false", :show_hidden_contexts_in_sidebar => "false", :staleness_starts => "14", :due_style => "1", :admin_email => "[email protected]" }}
post :update, {
:user => { :first_name => 'Jane', :last_name => 'Doe'},
:prefs => { :date_format => "%m-%d-%Y", :week_starts => "0", :show_number_completed => "10", :show_completed_projects_in_sidebar => "false", :show_hidden_contexts_in_sidebar => "false", :staleness_starts => "14", :due_style => "1", :admin_email => "[email protected]" }}
updated_admin_user = users(:admin_user).reload
assert_not_nil updated_admin_user.preference
assert_equal 'Jane', updated_admin_user.first_name
assert_equal 'Doe', updated_admin_user.last_name
assert_redirected_to :action => 'index'
end


test "should not update password if left empty" do
login_as :admin_user

old_password_hash = users(:admin_user).password

post :update, {
:user => { :first_name => 'Jane', :last_name => 'Doe', :password => "", :password_confirmation => ""},
:prefs => { :date_format => "%m-%d-%Y", :week_starts => "0", :show_number_completed => "10", :show_completed_projects_in_sidebar => "false", :show_hidden_contexts_in_sidebar => "false", :staleness_starts => "14", :due_style => "1", :admin_email => "[email protected]" }}

updated_admin_user = users(:admin_user).reload
assert_equal old_password_hash, updated_admin_user.password
end

test "should be able to change authentication type" do
assert Tracks::Config.auth_schemes.include?("open_id"), "open_id should be a valid authentication scheme"

login_as :admin_user

post :update, {
:user => { :first_name => 'Jane', :last_name => 'Doe', :auth_type => "open_id", :open_id_url => "http://test"},
:prefs => { :date_format => "%m-%d-%Y", :week_starts => "0", :show_number_completed => "10", :show_completed_projects_in_sidebar => "false", :show_hidden_contexts_in_sidebar => "false", :staleness_starts => "14", :due_style => "1", :admin_email => "[email protected]" }}

updated_admin_user = users(:admin_user).reload
assert_equal "open_id", updated_admin_user.auth_type
end

end
29 changes: 16 additions & 13 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
require 'test_help'
require 'flexmock/test_unit' #and the flexmock gem, too!
require 'action_web_service/test_invoke'

module Tracks
class Config
def self.salt
"change-me"
end
def self.auth_schemes
return ["database","open_id"]
end
end
end

Expand All @@ -30,9 +33,9 @@ def assert_error_on(model_instance, attribute, expected_error)
actual_error = model_instance.errors.on attribute.to_sym
assert_equal expected_error, actual_error
end

alias_method :assert_errors_on, :assert_error_on

def assert_value_changed(object, method = nil)
initial_value = object.send(method)
yield
Expand All @@ -42,7 +45,7 @@ def assert_value_changed(object, method = nil)
end

class ActiveSupport::TestCase

# Generates a random string of ascii characters (a-z, "1 0")
# of a given length for testing assignment to fields
# for validation purposes
Expand All @@ -56,11 +59,11 @@ def generate_random_string(length)
end
return string
end

def next_week
1.week.from_now.utc
end

# Courtesy of http://habtm.com/articles/2006/02/20/assert-yourself-man-redirecting-with-rjs
def assert_js_redirected_to(options={}, message=nil)
clean_backtrace do
Expand Down Expand Up @@ -104,12 +107,12 @@ def set_user_to_current_time_zone(user)

class ActionController::IntegrationTest
Tag #avoid errors in integration tests

def assert_test_environment_ok
assert_equal "test", ENV['RAILS_ENV']
assert_equal "change-me", Tracks::Config.salt
end

def authenticated_post_xml(url, username, password, parameters, headers = {})
post url,
parameters,
Expand All @@ -118,7 +121,7 @@ def authenticated_post_xml(url, username, password, parameters, headers = {})
'CONTENT_TYPE' => 'application/xml'
}.merge(headers)
end

def authenticated_get_xml(url, username, password, parameters, headers = {})
get url,
parameters,
Expand All @@ -127,7 +130,7 @@ def authenticated_get_xml(url, username, password, parameters, headers = {})
'CONTENT_TYPE' => 'application/xml'
}.merge(headers)
end

def assert_response_and_body(type, body, message = nil)
assert_equal body, @response.body, message
assert_response type, message
Expand All @@ -137,13 +140,13 @@ def assert_response_and_body_matches(type, body_regex, message = nil)
assert_response type, message
assert_match body_regex, @response.body, message
end

def assert_401_unauthorized
assert_response_and_body 401, "401 Unauthorized: You are not authorized to interact with Tracks."
end

def assert_401_unauthorized_admin
assert_response_and_body 401, "401 Unauthorized: Only admin users are allowed access to this function."
end

end

0 comments on commit 84ad01a

Please sign in to comment.