Removed object_daddy dependency.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@9469 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2012-04-22 11:56:47 +00:00
parent 4a3f038595
commit b960470d78
10 changed files with 149 additions and 96 deletions

View File

@ -71,7 +71,6 @@ end
group :test do group :test do
gem "shoulda", "~> 2.10.3" gem "shoulda", "~> 2.10.3"
gem "edavis10-object_daddy", :require => "object_daddy"
gem "mocha" gem "mocha"
end end

View File

@ -15,26 +15,110 @@ module ObjectDaddyHelpers
def User.add_to_project(user, project, roles) def User.add_to_project(user, project, roles)
roles = [roles] unless roles.is_a?(Array) roles = [roles] unless roles.is_a?(Array)
Member.generate!(:principal => user, :project => project, :roles => roles) Member.create!(:principal => user, :project => project, :roles => roles)
end
def User.generate!(attributes={})
@generated_user_login ||= 'user0'
@generated_user_login.succ!
user = User.new(attributes)
user.login = @generated_user_login if user.login.blank?
user.mail = "#{@generated_user_login}@example.com" if user.mail.blank?
user.firstname = "Bob" if user.firstname.blank?
user.lastname = "Doe" if user.lastname.blank?
yield user if block_given?
user.save!
user
end
def Group.generate!(attributes={})
@generated_group_name ||= 'Group 0'
@generated_group_name.succ!
group = Group.new(attributes)
group.lastname = @generated_group_name if group.lastname.blank?
yield group if block_given?
group.save!
group
end
def Project.generate!(attributes={})
@generated_project_identifier ||= 'project-0000'
@generated_project_identifier.succ!
project = Project.new(attributes)
project.name = @generated_project_identifier if project.name.blank?
project.identifier = @generated_project_identifier if project.identifier.blank?
yield project if block_given?
project.save!
project
end
def Tracker.generate!(attributes={})
@generated_tracker_name ||= 'Tracker 0'
@generated_tracker_name.succ!
tracker = Tracker.new(attributes)
tracker.name = @generated_tracker_name if tracker.name.blank?
yield tracker if block_given?
tracker.save!
tracker
end
def Role.generate!(attributes={})
@generated_role_name ||= 'Role 0'
@generated_role_name.succ!
role = Role.new(attributes)
role.name = @generated_role_name if role.name.blank?
yield role if block_given?
role.save!
role
end
def Issue.generate!(attributes={})
issue = Issue.new(attributes)
issue.subject = 'Generated' if issue.subject.blank?
issue.author ||= User.find(2)
yield issue if block_given?
issue.save!
issue
end
def Version.generate!(attributes={})
@generated_version_name ||= 'Version 0'
@generated_version_name.succ!
version = Version.new(attributes)
version.name = @generated_version_name if version.name.blank?
yield version if block_given?
version.save!
version
end
def AuthSource.generate!(attributes={})
@generated_auth_source_name ||= 'Auth 0'
@generated_auth_source_name.succ!
source = AuthSource.new(attributes)
source.name = @generated_auth_source_name if source.name.blank?
yield source if block_given?
source.save!
source
end end
# Generate the default Query # Generate the default Query
def Query.generate_default!(attributes={}) def Query.generate_default!(attributes={})
query = Query.spawn(attributes) query = Query.new(attributes)
query.name ||= '_' query.name = '_' if query.name.blank?
query.save! query.save!
query query
end end
# Generate an issue for a project, using it's trackers # Generate an issue for a project, using it's trackers
def Issue.generate_for_project!(project, attributes={}) def Issue.generate_for_project!(project, attributes={})
issue = Issue.spawn(attributes) do |issue| issue = Issue.new(attributes) do |issue|
issue.project = project issue.project = project
issue.tracker = project.trackers.first unless project.trackers.empty? issue.tracker = project.trackers.first unless project.trackers.empty?
issue.subject = 'Generated' if issue.subject.blank?
issue.author ||= User.find(2)
yield issue if block_given? yield issue if block_given?
end end
issue.save! issue.save!
issue issue
end end
end end

View File

@ -243,7 +243,10 @@ class ActiveSupport::TestCase
context "should allow http basic auth using a username and password for #{http_method} #{url}" do context "should allow http basic auth using a username and password for #{http_method} #{url}" do
context "with a valid HTTP authentication" do context "with a valid HTTP authentication" do
setup do setup do
@user = User.generate_with_protected!(:password => 'my_password', :password_confirmation => 'my_password', :admin => true) # Admin so they can access the project @user = User.generate! do |user|
user.admin = true
user.password = 'my_password'
end
send(http_method, url, parameters, credentials(@user.login, 'my_password')) send(http_method, url, parameters, credentials(@user.login, 'my_password'))
end end
@ -256,7 +259,7 @@ class ActiveSupport::TestCase
context "with an invalid HTTP authentication" do context "with an invalid HTTP authentication" do
setup do setup do
@user = User.generate_with_protected! @user = User.generate!
send(http_method, url, parameters, credentials(@user.login, 'wrong_password')) send(http_method, url, parameters, credentials(@user.login, 'wrong_password'))
end end
@ -297,8 +300,10 @@ class ActiveSupport::TestCase
context "should allow http basic auth with a key for #{http_method} #{url}" do context "should allow http basic auth with a key for #{http_method} #{url}" do
context "with a valid HTTP authentication using the API token" do context "with a valid HTTP authentication using the API token" do
setup do setup do
@user = User.generate_with_protected!(:admin => true) @user = User.generate! do |user|
@token = Token.generate!(:user => @user, :action => 'api') user.admin = true
end
@token = Token.create!(:user => @user, :action => 'api')
send(http_method, url, parameters, credentials(@token.value, 'X')) send(http_method, url, parameters, credentials(@token.value, 'X'))
end end
@ -312,8 +317,8 @@ class ActiveSupport::TestCase
context "with an invalid HTTP authentication" do context "with an invalid HTTP authentication" do
setup do setup do
@user = User.generate_with_protected! @user = User.generate!
@token = Token.generate!(:user => @user, :action => 'feeds') @token = Token.create!(:user => @user, :action => 'feeds')
send(http_method, url, parameters, credentials(@token.value, 'X')) send(http_method, url, parameters, credentials(@token.value, 'X'))
end end
@ -341,8 +346,10 @@ class ActiveSupport::TestCase
context "should allow key based auth using key=X for #{http_method} #{url}" do context "should allow key based auth using key=X for #{http_method} #{url}" do
context "with a valid api token" do context "with a valid api token" do
setup do setup do
@user = User.generate_with_protected!(:admin => true) @user = User.generate! do |user|
@token = Token.generate!(:user => @user, :action => 'api') user.admin = true
end
@token = Token.create!(:user => @user, :action => 'api')
# Simple url parse to add on ?key= or &key= # Simple url parse to add on ?key= or &key=
request_url = if url.match(/\?/) request_url = if url.match(/\?/)
url + "&key=#{@token.value}" url + "&key=#{@token.value}"
@ -362,8 +369,10 @@ class ActiveSupport::TestCase
context "with an invalid api token" do context "with an invalid api token" do
setup do setup do
@user = User.generate_with_protected! @user = User.generate! do |user|
@token = Token.generate!(:user => @user, :action => 'feeds') user.admin = true
end
@token = Token.create!(:user => @user, :action => 'feeds')
# Simple url parse to add on ?key= or &key= # Simple url parse to add on ?key= or &key=
request_url = if url.match(/\?/) request_url = if url.match(/\?/)
url + "&key=#{@token.value}" url + "&key=#{@token.value}"
@ -383,8 +392,10 @@ class ActiveSupport::TestCase
context "should allow key based auth using X-Redmine-API-Key header for #{http_method} #{url}" do context "should allow key based auth using X-Redmine-API-Key header for #{http_method} #{url}" do
setup do setup do
@user = User.generate_with_protected!(:admin => true) @user = User.generate! do |user|
@token = Token.generate!(:user => @user, :action => 'api') user.admin = true
end
@token = Token.create!(:user => @user, :action => 'api')
send(http_method, url, parameters, {'X-Redmine-API-Key' => @token.value.to_s}) send(http_method, url, parameters, {'X-Redmine-API-Key' => @token.value.to_s})
end end

View File

@ -373,13 +373,13 @@ class IssueTest < ActiveSupport::TestCase
tracker = Tracker.find(1) tracker = Tracker.find(1)
user = User.find(2) user = User.find(2)
issue = Issue.generate!(:tracker => tracker, :status => status, :project_id => 1) issue = Issue.generate!(:tracker => tracker, :status => status, :project_id => 1, :author_id => 1)
assert_equal [1, 2], issue.new_statuses_allowed_to(user).map(&:id) assert_equal [1, 2], issue.new_statuses_allowed_to(user).map(&:id)
issue = Issue.generate!(:tracker => tracker, :status => status, :project_id => 1, :author => user) issue = Issue.generate!(:tracker => tracker, :status => status, :project_id => 1, :author => user)
assert_equal [1, 2, 3, 5], issue.new_statuses_allowed_to(user).map(&:id) assert_equal [1, 2, 3, 5], issue.new_statuses_allowed_to(user).map(&:id)
issue = Issue.generate!(:tracker => tracker, :status => status, :project_id => 1, :assigned_to => user) issue = Issue.generate!(:tracker => tracker, :status => status, :project_id => 1, :author_id => 1, :assigned_to => user)
assert_equal [1, 2, 4, 5], issue.new_statuses_allowed_to(user).map(&:id) assert_equal [1, 2, 4, 5], issue.new_statuses_allowed_to(user).map(&:id)
issue = Issue.generate!(:tracker => tracker, :status => status, :project_id => 1, :author => user, :assigned_to => user) issue = Issue.generate!(:tracker => tracker, :status => status, :project_id => 1, :author => user, :assigned_to => user)

View File

@ -21,7 +21,7 @@ class Redmine::CipheringTest < ActiveSupport::TestCase
def test_password_should_be_encrypted def test_password_should_be_encrypted
Redmine::Configuration.with 'database_cipher_key' => 'secret' do Redmine::Configuration.with 'database_cipher_key' => 'secret' do
r = Repository::Subversion.generate!(:password => 'foo') r = Repository::Subversion.create!(:password => 'foo', :url => 'file:///tmp', :identifier => 'svn')
assert_equal 'foo', r.password assert_equal 'foo', r.password
assert r.read_attribute(:password).match(/\Aaes-256-cbc:.+\Z/) assert r.read_attribute(:password).match(/\Aaes-256-cbc:.+\Z/)
end end
@ -29,7 +29,7 @@ class Redmine::CipheringTest < ActiveSupport::TestCase
def test_password_should_be_clear_with_blank_key def test_password_should_be_clear_with_blank_key
Redmine::Configuration.with 'database_cipher_key' => '' do Redmine::Configuration.with 'database_cipher_key' => '' do
r = Repository::Subversion.generate!(:password => 'foo') r = Repository::Subversion.create!(:password => 'foo', :url => 'file:///tmp', :identifier => 'svn')
assert_equal 'foo', r.password assert_equal 'foo', r.password
assert_equal 'foo', r.read_attribute(:password) assert_equal 'foo', r.read_attribute(:password)
end end
@ -37,7 +37,7 @@ class Redmine::CipheringTest < ActiveSupport::TestCase
def test_password_should_be_clear_with_nil_key def test_password_should_be_clear_with_nil_key
Redmine::Configuration.with 'database_cipher_key' => nil do Redmine::Configuration.with 'database_cipher_key' => nil do
r = Repository::Subversion.generate!(:password => 'foo') r = Repository::Subversion.create!(:password => 'foo', :url => 'file:///tmp', :identifier => 'svn')
assert_equal 'foo', r.password assert_equal 'foo', r.password
assert_equal 'foo', r.read_attribute(:password) assert_equal 'foo', r.read_attribute(:password)
end end
@ -45,7 +45,7 @@ class Redmine::CipheringTest < ActiveSupport::TestCase
def test_blank_password_should_be_clear def test_blank_password_should_be_clear
Redmine::Configuration.with 'database_cipher_key' => 'secret' do Redmine::Configuration.with 'database_cipher_key' => 'secret' do
r = Repository::Subversion.generate!(:password => '') r = Repository::Subversion.create!(:password => '', :url => 'file:///tmp', :identifier => 'svn')
assert_equal '', r.password assert_equal '', r.password
assert_equal '', r.read_attribute(:password) assert_equal '', r.read_attribute(:password)
end end
@ -53,7 +53,7 @@ class Redmine::CipheringTest < ActiveSupport::TestCase
def test_unciphered_password_should_be_readable def test_unciphered_password_should_be_readable
Redmine::Configuration.with 'database_cipher_key' => nil do Redmine::Configuration.with 'database_cipher_key' => nil do
r = Repository::Subversion.generate!(:password => 'clear') r = Repository::Subversion.create!(:password => 'clear', :url => 'file:///tmp', :identifier => 'svn')
end end
Redmine::Configuration.with 'database_cipher_key' => 'secret' do Redmine::Configuration.with 'database_cipher_key' => 'secret' do
@ -64,7 +64,7 @@ class Redmine::CipheringTest < ActiveSupport::TestCase
def test_ciphered_password_with_no_cipher_key_configured_should_be_returned_ciphered def test_ciphered_password_with_no_cipher_key_configured_should_be_returned_ciphered
Redmine::Configuration.with 'database_cipher_key' => 'secret' do Redmine::Configuration.with 'database_cipher_key' => 'secret' do
r = Repository::Subversion.generate!(:password => 'clear') r = Repository::Subversion.create!(:password => 'clear', :url => 'file:///tmp', :identifier => 'svn')
end end
Redmine::Configuration.with 'database_cipher_key' => '' do Redmine::Configuration.with 'database_cipher_key' => '' do
@ -79,8 +79,8 @@ class Redmine::CipheringTest < ActiveSupport::TestCase
def test_encrypt_all def test_encrypt_all
Repository.delete_all Repository.delete_all
Redmine::Configuration.with 'database_cipher_key' => nil do Redmine::Configuration.with 'database_cipher_key' => nil do
Repository::Subversion.generate!(:password => 'foo') Repository::Subversion.create!(:password => 'foo', :url => 'file:///tmp', :identifier => 'foo')
Repository::Subversion.generate!(:password => 'bar') Repository::Subversion.create!(:password => 'bar', :url => 'file:///tmp', :identifier => 'bar')
end end
Redmine::Configuration.with 'database_cipher_key' => 'secret' do Redmine::Configuration.with 'database_cipher_key' => 'secret' do
@ -94,8 +94,8 @@ class Redmine::CipheringTest < ActiveSupport::TestCase
def test_decrypt_all def test_decrypt_all
Repository.delete_all Repository.delete_all
Redmine::Configuration.with 'database_cipher_key' => 'secret' do Redmine::Configuration.with 'database_cipher_key' => 'secret' do
Repository::Subversion.generate!(:password => 'foo') Repository::Subversion.create!(:password => 'foo', :url => 'file:///tmp', :identifier => 'foo')
Repository::Subversion.generate!(:password => 'bar') Repository::Subversion.create!(:password => 'bar', :url => 'file:///tmp', :identifier => 'bar')
assert Repository.decrypt_all(:password) assert Repository.decrypt_all(:password)
r = Repository.first(:order => 'id DESC') r = Repository.first(:order => 'id DESC')

View File

@ -160,7 +160,6 @@ class Redmine::Helpers::GanttHelperTest < ActionView::TestCase
context "assigned to a shared version of another project" do context "assigned to a shared version of another project" do
setup do setup do
p = Project.generate! p = Project.generate!
p.trackers << @tracker
p.enabled_module_names = [:issue_tracking] p.enabled_module_names = [:issue_tracking]
@shared_version = Version.generate!(:sharing => 'system') @shared_version = Version.generate!(:sharing => 'system')
p.versions << @shared_version p.versions << @shared_version

View File

@ -742,7 +742,7 @@ class ProjectTest < ActiveSupport::TestCase
project = Project.find(1) project = Project.find(1)
system_activity = TimeEntryActivity.find_by_name('Design') system_activity = TimeEntryActivity.find_by_name('Design')
assert system_activity.active? assert system_activity.active?
overridden_activity = TimeEntryActivity.generate!(:project => project, :parent => system_activity, :active => false) overridden_activity = TimeEntryActivity.create!(:name => "Project", :project => project, :parent => system_activity, :active => false)
assert overridden_activity.save! assert overridden_activity.save!
assert !project.activities.include?(overridden_activity), "Inactive Project specific Activity not found" assert !project.activities.include?(overridden_activity), "Inactive Project specific Activity not found"
@ -822,10 +822,10 @@ class ProjectTest < ActiveSupport::TestCase
:tracker_id => 1, :tracker_id => 1,
:assigned_to_id => 2, :assigned_to_id => 2,
:project_id => @source_project.id) :project_id => @source_project.id)
source_relation = IssueRelation.generate!(:issue_from => Issue.find(4), source_relation = IssueRelation.create!(:issue_from => Issue.find(4),
:issue_to => second_issue, :issue_to => second_issue,
:relation_type => "relates") :relation_type => "relates")
source_relation_cross_project = IssueRelation.generate!(:issue_from => Issue.find(1), source_relation_cross_project = IssueRelation.create!(:issue_from => Issue.find(1),
:issue_to => second_issue, :issue_to => second_issue,
:relation_type => "duplicates") :relation_type => "duplicates")
@ -1104,22 +1104,22 @@ class ProjectTest < ActiveSupport::TestCase
@role = Role.generate! @role = Role.generate!
@user_with_membership_notification = User.generate!(:mail_notification => 'selected') @user_with_membership_notification = User.generate!(:mail_notification => 'selected')
Member.generate!(:project => @project, :roles => [@role], :principal => @user_with_membership_notification, :mail_notification => true) Member.create!(:project => @project, :roles => [@role], :principal => @user_with_membership_notification, :mail_notification => true)
@all_events_user = User.generate!(:mail_notification => 'all') @all_events_user = User.generate!(:mail_notification => 'all')
Member.generate!(:project => @project, :roles => [@role], :principal => @all_events_user) Member.create!(:project => @project, :roles => [@role], :principal => @all_events_user)
@no_events_user = User.generate!(:mail_notification => 'none') @no_events_user = User.generate!(:mail_notification => 'none')
Member.generate!(:project => @project, :roles => [@role], :principal => @no_events_user) Member.create!(:project => @project, :roles => [@role], :principal => @no_events_user)
@only_my_events_user = User.generate!(:mail_notification => 'only_my_events') @only_my_events_user = User.generate!(:mail_notification => 'only_my_events')
Member.generate!(:project => @project, :roles => [@role], :principal => @only_my_events_user) Member.create!(:project => @project, :roles => [@role], :principal => @only_my_events_user)
@only_assigned_user = User.generate!(:mail_notification => 'only_assigned') @only_assigned_user = User.generate!(:mail_notification => 'only_assigned')
Member.generate!(:project => @project, :roles => [@role], :principal => @only_assigned_user) Member.create!(:project => @project, :roles => [@role], :principal => @only_assigned_user)
@only_owned_user = User.generate!(:mail_notification => 'only_owner') @only_owned_user = User.generate!(:mail_notification => 'only_owner')
Member.generate!(:project => @project, :roles => [@role], :principal => @only_owned_user) Member.create!(:project => @project, :roles => [@role], :principal => @only_owned_user)
end end
should "include members with a mail notification" do should "include members with a mail notification" do

View File

@ -670,7 +670,9 @@ class QueryTest < ActiveSupport::TestCase
end end
def test_issue_count_with_archived_issues def test_issue_count_with_archived_issues
p = Project.generate!( :status => Project::STATUS_ARCHIVED ) p = Project.generate! do |project|
project.status = Project::STATUS_ARCHIVED
end
i = Issue.generate!( :project => p, :tracker => p.trackers.first ) i = Issue.generate!( :project => p, :tracker => p.trackers.first )
assert !i.visible? assert !i.visible?

View File

@ -1,46 +0,0 @@
# Redmine - project management software
# Copyright (C) 2006-2011 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require File.expand_path('../../test_helper', __FILE__)
# Test case that checks that the testing infrastructure is setup correctly.
class TestingTest < ActiveSupport::TestCase
def test_working
assert true
end
test "Rails 'test' case syntax" do
assert true
end
test "Generating with object_daddy" do
assert_difference "IssueStatus.count" do
IssueStatus.generate!
end
end
should "work with shoulda" do
assert true
end
context "works with a context" do
should "work" do
assert true
end
end
end

View File

@ -337,8 +337,10 @@ class UserTest < ActiveSupport::TestCase
def test_destroy_should_nullify_changesets def test_destroy_should_nullify_changesets
changeset = Changeset.create!( changeset = Changeset.create!(
:repository => Repository::Subversion.generate!( :repository => Repository::Subversion.create!(
:project_id => 1 :project_id => 1,
:url => 'file:///tmp',
:identifier => 'tmp'
), ),
:revision => '12', :revision => '12',
:committed_on => Time.now, :committed_on => Time.now,
@ -378,9 +380,9 @@ class UserTest < ActiveSupport::TestCase
end end
should "select the exact matching user first" do should "select the exact matching user first" do
case_sensitive_user = User.generate_with_protected!( case_sensitive_user = User.generate! do |user|
:login => 'changed', :password => 'admin', user.password = "admin"
:password_confirmation => 'admin') end
# bypass validations to make it appear like existing data # bypass validations to make it appear like existing data
case_sensitive_user.update_attribute(:login, 'ADMIN') case_sensitive_user.update_attribute(:login, 'ADMIN')
@ -628,7 +630,7 @@ class UserTest < ActiveSupport::TestCase
should "return the existing api token value" do should "return the existing api token value" do
user = User.generate_with_protected! user = User.generate_with_protected!
token = Token.generate!(:action => 'api') token = Token.create!(:action => 'api')
user.api_token = token user.api_token = token
assert user.save assert user.save
@ -643,7 +645,7 @@ class UserTest < ActiveSupport::TestCase
should "return nil if the key is found for an inactive user" do should "return nil if the key is found for an inactive user" do
user = User.generate_with_protected!(:status => User::STATUS_LOCKED) user = User.generate_with_protected!(:status => User::STATUS_LOCKED)
token = Token.generate!(:action => 'api') token = Token.create!(:action => 'api')
user.api_token = token user.api_token = token
user.save user.save
@ -652,7 +654,7 @@ class UserTest < ActiveSupport::TestCase
should "return the user if the key is found for an active user" do should "return the user if the key is found for an active user" do
user = User.generate_with_protected!(:status => User::STATUS_ACTIVE) user = User.generate_with_protected!(:status => User::STATUS_ACTIVE)
token = Token.generate!(:action => 'api') token = Token.create!(:action => 'api')
user.api_token = token user.api_token = token
user.save user.save
@ -823,7 +825,9 @@ class UserTest < ActiveSupport::TestCase
end end
def test_own_account_deletable_should_be_true_for_an_admin_if_other_admin_exists def test_own_account_deletable_should_be_true_for_an_admin_if_other_admin_exists
User.generate_with_protected(:admin => true) User.generate! do |user|
user.admin = true
end
with_settings :unsubscribe => '1' do with_settings :unsubscribe => '1' do
assert_equal true, User.find(1).own_account_deletable? assert_equal true, User.find(1).own_account_deletable?