From b9c58d9486c34810285bd5a30f7aa1b1d760572f Mon Sep 17 00:00:00 2001 From: Moises Machado Date: Wed, 15 Oct 2008 13:23:14 -0300 Subject: [PATCH] ActionItem780: refactoring tests --- app/models/person.rb | 2 +- app/models/user.rb | 4 +++- app/views/account/_signup_form.rhtml | 2 +- app/views/profile_editor/_person.rhtml | 20 ++------------------ app/views/profile_editor/_person_form.rhtml | 16 ++++++++++++++++ public/stylesheets/controller_account.css | 2 ++ public/stylesheets/controller_profile_editor.css | 3 +-- test/functional/account_controller_test.rb | 48 +++++++++++++++++++++++++++--------------------- test/functional/cms_controller_test.rb | 2 +- test/functional/memberships_controller_test.rb | 6 +++--- test/functional/profile_editor_controller_test.rb | 47 +++++++++++++++++++++++------------------------ test/functional/profile_members_controller_test.rb | 2 +- test/functional/tasks_controller_test.rb | 2 +- test/functional/themes_controller_test.rb | 4 ++-- test/integration/editing_person_info_test.rb | 10 +++++----- test/integration/forgot_password_test.rb | 2 +- test/integration/signup_test.rb | 2 +- test/test_helper.rb | 13 ++++++++++--- test/unit/article_test.rb | 1 + test/unit/change_password_test.rb | 12 ++++++------ test/unit/create_enterprise_test.rb | 8 ++++---- test/unit/person_test.rb | 25 ++++++++++++------------- test/unit/profile_test.rb | 14 +++++++++++--- test/unit/task_test.rb | 5 ++++- test/unit/user_test.rb | 44 ++++++++++++++++++++++++-------------------- 25 files changed, 163 insertions(+), 133 deletions(-) create mode 100644 app/views/profile_editor/_person_form.rhtml diff --git a/app/models/person.rb b/app/models/person.rb index c454e65..897ca12 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -32,7 +32,7 @@ class Person < Profile friends.delete(friend) end - N_('Contact information'); N_('Birth date'); N_('City'); N_('State'); N_('Country'); N_('Sex'); + N_('Contact information'); N_('Birth date'); N_('City'); N_('State'); N_('Country'); N_('Sex'); N_('Zip Code') settings_items :photo, :contact_information, :birth_date, :sex, :city, :state, :country, :zip_code def self.conditions_for_profiles(conditions, person) diff --git a/app/models/user.rb b/app/models/user.rb index 88070b3..473cb87 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -24,7 +24,9 @@ class User < ActiveRecord::Base end after_create do |user| - Person.create!(:identifier => user.login, :name => user.login, :user_id => user.id, :environment_id => user.environment_id) + user.person ||= Person.new + user.person.name ||= user.login + user.person.update_attributes(:identifier => user.login, :user_id => user.id, :environment_id => user.environment_id) end has_one :person, :dependent => :destroy diff --git a/app/views/account/_signup_form.rhtml b/app/views/account/_signup_form.rhtml index 98e67e1..f4857cf 100644 --- a/app/views/account/_signup_form.rhtml +++ b/app/views/account/_signup_form.rhtml @@ -1,6 +1,6 @@ <%= error_messages_for :user %> <% labelled_form_for :user, @user, - :html => { :help=>_('Fill all this fields to join in this environment.

If you forgot your password, do not create a new account, click on the "I forgot my password!" link. ;-)') + :html => { :help=>_('Fill all this fields to join in this environment.

If you forgot your password, do not create a new account, click on the "I forgot my password!" link. ;-)'), :id => 'profile-data' } do |f| -%> <%= f.text_field :login, diff --git a/app/views/profile_editor/_person.rhtml b/app/views/profile_editor/_person.rhtml index f0060fa..8d748c9 100644 --- a/app/views/profile_editor/_person.rhtml +++ b/app/views/profile_editor/_person.rhtml @@ -1,21 +1,5 @@

<%= _('Personal information') %>

+ <%= f.text_field(:email)%> - <%= f.text_field(:nickname, :maxlength => 16, :size => 30) %> -
- <%= _('A short name by which you like to be known. Will be used in friends listings, community member listings etc.') %> -
- - - <%= f.text_field(:name) %> - <%= labelled_form_field(_('e-Mail'), text_field(:profile_data, :email)) %> - <%= f.text_field(:contact_information) %> - <%= f.text_field(:contact_phone) %> - <%# use :size => 3 if you want 3 radios by line %> - <%= f.radio_group :profile_data, :sex, [ ['male',_('Male')], ['female',_('Female')] ] %> - <%= f.text_field(:birth_date) %> - <%= labelled_form_field(_('Address (street and number)'), text_field(:profile_data, :address)) %> - <%= labelled_form_field(_('ZIP code'), text_field(:profile_data, :zip_code)) %> - <%= f.text_field(:city) %> - <%= f.text_field(:state) %> - <%= f.text_field(:country) %> + <%= render :partial => 'person_form', :locals => {:f => f} %> diff --git a/app/views/profile_editor/_person_form.rhtml b/app/views/profile_editor/_person_form.rhtml new file mode 100644 index 0000000..df6682a --- /dev/null +++ b/app/views/profile_editor/_person_form.rhtml @@ -0,0 +1,16 @@ +<%= f.text_field(:nickname, :maxlength => 16, :size => 30) %> +
+ <%= _('A short name by which you like to be known. Will be used in friends listings, community member listings etc.') %> +
+ +<%= f.text_field(:name) %> +<%= f.text_field(:contact_information) %> +<%= f.text_field(:contact_phone) %> +<%# use :size => 3 if you want 3 radios by line %> +<%= f.radio_group :profile_data, :sex, [ ['male',_('Male')], ['female',_('Female')] ] %> +<%= f.text_field(:birth_date) %> +<%= labelled_form_field(_('Address (street and number)'), text_field(:profile_data, :address)) %> +<%= labelled_form_field(_('ZIP code'), text_field(:profile_data, :zip_code)) %> +<%= f.text_field(:city) %> +<%= f.text_field(:state) %> +<%= f.text_field(:country) %> diff --git a/public/stylesheets/controller_account.css b/public/stylesheets/controller_account.css index 7691c2a..0bec27d 100644 --- a/public/stylesheets/controller_account.css +++ b/public/stylesheets/controller_account.css @@ -1,3 +1,5 @@ +@import url(person_data.css); + .button.disabled { opacity: 0.5; } diff --git a/public/stylesheets/controller_profile_editor.css b/public/stylesheets/controller_profile_editor.css index 88a7fd2..48afb4e 100644 --- a/public/stylesheets/controller_profile_editor.css +++ b/public/stylesheets/controller_profile_editor.css @@ -1,4 +1,5 @@ @import url(controller_cms.css); +@import url(person_data.css); .profile_info { width: 100%; @@ -24,7 +25,6 @@ background-color: transparent !important; } - /* file manager (big) style */ .file-manager { @@ -60,4 +60,3 @@ .file-manager-button span { display: block; } - diff --git a/test/functional/account_controller_test.rb b/test/functional/account_controller_test.rb index ab1c22a..832b324 100644 --- a/test/functional/account_controller_test.rb +++ b/test/functional/account_controller_test.rb @@ -34,7 +34,7 @@ class AccountControllerTest < Test::Unit::TestCase end should 'redirect to user control panel on login' do - u = create_user + u = new_user post :login, :user => {:login => 'quire', :password => 'quire'} assert_redirected_to :controller => 'profile_editor', :action => 'index', :profile => 'quire' @@ -49,14 +49,14 @@ class AccountControllerTest < Test::Unit::TestCase def test_should_allow_signup assert_difference User, :count do - create_user + new_user assert_response :redirect end end def test_should_require_login_on_signup assert_no_difference User, :count do - create_user(:login => nil) + new_user(:login => nil) assert assigns(:user).errors.on(:login) assert_response :success end @@ -64,7 +64,7 @@ class AccountControllerTest < Test::Unit::TestCase def test_should_require_password_on_signup assert_no_difference User, :count do - create_user(:password => nil) + new_user(:password => nil) assert assigns(:user).errors.on(:password) assert_response :success end @@ -72,7 +72,7 @@ class AccountControllerTest < Test::Unit::TestCase def test_should_require_password_confirmation_on_signup assert_no_difference User, :count do - create_user(:password_confirmation => nil) + new_user(:password_confirmation => nil) assert assigns(:user).errors.on(:password_confirmation) assert_response :success end @@ -80,7 +80,7 @@ class AccountControllerTest < Test::Unit::TestCase def test_should_require_email_on_signup assert_no_difference User, :count do - create_user(:email => nil) + new_user(:email => nil) assert assigns(:user).errors.on(:email) assert_response :success end @@ -89,7 +89,7 @@ class AccountControllerTest < Test::Unit::TestCase def test_shoud_not_save_without_acceptance_of_terms_of_use_on_signup assert_no_difference User, :count do Environment.default.update_attributes(:terms_of_use => 'some terms ...') - create_user + new_user assert_response :success end end @@ -97,7 +97,7 @@ class AccountControllerTest < Test::Unit::TestCase def test_shoud_save_with_acceptance_of_terms_of_use_on_signup assert_difference User, :count do Environment.default.update_attributes(:terms_of_use => 'some terms ...') - create_user(:terms_accepted => '1') + new_user(:terms_accepted => '1') assert_response :redirect end end @@ -243,7 +243,7 @@ class AccountControllerTest < Test::Unit::TestCase end should 'require password confirmation correctly to enter new pasword' do - user = User.create!(:login => 'testuser', :email => 'testuser@example.com', :password => 'test', :password_confirmation => 'test') + user = create_user('testuser', :email => 'testuser@example.com', :password => 'test', :password_confirmation => 'test') change = ChangePassword.create!(:login => 'testuser', :email => 'testuser@example.com') post :new_password, :code => change.code, :change_password => { :password => 'onepass', :password_confirmation => 'another_pass' } @@ -267,9 +267,9 @@ class AccountControllerTest < Test::Unit::TestCase should 'restrict multiple users with the same e-mail' do assert_difference User, :count do - create_user(:login => 'user1', :email => 'user@example.com') + new_user(:login => 'user1', :email => 'user@example.com') assert assigns(:user).valid? - create_user(:login => 'user2', :email => 'user@example.com') + new_user(:login => 'user2', :email => 'user@example.com') assert assigns(:user).errors.on(:email) end end @@ -398,7 +398,7 @@ class AccountControllerTest < Test::Unit::TestCase should 'not activate if user does not accept terms' do ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :foundation_year => 1998, :enabled => false) - p = User.create!(:login => 'test_user', :password => 'blih', :password_confirmation => 'blih', :email => 'test@noosfero.com').person + p = create_user('test_user', :password => 'blih', :password_confirmation => 'blih', :email => 'test@noosfero.com').person login_as(p.identifier) task = EnterpriseActivation.create!(:enterprise => ent) @@ -423,7 +423,7 @@ class AccountControllerTest < Test::Unit::TestCase should 'activate enterprise and make logged user admin' do ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :foundation_year => 1998, :enabled => false) - p = User.create!(:login => 'test_user', :password => 'blih', :password_confirmation => 'blih', :email => 'test@noosfero.com').person + p = create_user('test_user', :password => 'blih', :password_confirmation => 'blih', :email => 'test@noosfero.com').person login_as(p.identifier) task = EnterpriseActivation.create!(:enterprise => ent) @@ -449,7 +449,7 @@ class AccountControllerTest < Test::Unit::TestCase should 'activate enterprise and make unlogged user admin' do ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :foundation_year => 1998, :enabled => false) - p = User.create!(:login => 'test_user', :password => 'blih', :password_confirmation => 'blih', :email => 'test@noosfero.com').person + p = create_user('test_user', :password => 'blih', :password_confirmation => 'blih', :email => 'test@noosfero.com').person task = EnterpriseActivation.create!(:enterprise => ent) EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once @@ -467,7 +467,7 @@ class AccountControllerTest < Test::Unit::TestCase task = EnterpriseActivation.create!(:enterprise => ent) EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once - post :activate_enterprise, :enterprise_code => '0123456789', :answer => '1998', :terms_accepted => true, :new_user => true, :user => { :login => 'test_user', :password => 'blih', :password_confirmation => 'blih', :email => 'test@noosfero.com' } + post :activate_enterprise, :enterprise_code => '0123456789', :answer => '1998', :terms_accepted => true, :new_user => true, :user => { :login => 'test_user', :password => 'blih', :password_confirmation => 'blih', :email => 'test@noosfero.com' }, :profile_data => person_data ent.reload assert ent.enabled @@ -517,7 +517,7 @@ class AccountControllerTest < Test::Unit::TestCase should 'not be able to signup while inverse captcha field filled' do assert_no_difference User, :count do - create_user({}, @controller.icaptcha_field => 'bli@bla.email.foo') + new_user({}, @controller.icaptcha_field => 'bli@bla.email.foo') end end @@ -527,7 +527,7 @@ class AccountControllerTest < Test::Unit::TestCase end should 'use the current environment for the template of user' do - template = User.create!(:login => 'test_template', :email => 'test@bli.com', :password => 'pass', :password_confirmation => 'pass').person + template = create_user('test_template', :email => 'test@bli.com', :password => 'pass', :password_confirmation => 'pass').person template.boxes.destroy_all template.boxes << Box.new template.boxes[0].blocks << Block.new @@ -538,7 +538,7 @@ class AccountControllerTest < Test::Unit::TestCase @controller.stubs(:environment).returns(env) - create_user + new_user assert_equal 1, assigns(:user).person.boxes.size assert_equal 1, assigns(:user).person.boxes[0].blocks.size @@ -574,13 +574,19 @@ class AccountControllerTest < Test::Unit::TestCase end protected - def create_user(options = {}, extra_options ={}) - post :signup, { :user => { :login => 'quire', + def new_user(options = {}, extra_options ={}) + data = {:profile_data => person_data} + if extra_options[:profile_data] + data[:profile_data].merge! extra_options.delete(:profile_data) + end + data.merge! extra_options + + post :signup, { :user => { :login => 'quire', :email => 'quire@example.com', :password => 'quire', :password_confirmation => 'quire' }.merge(options) - }.merge(extra_options) + }.merge(data) end def auth_token(token) diff --git a/test/functional/cms_controller_test.rb b/test/functional/cms_controller_test.rb index 3dbc2d9..a776b89 100644 --- a/test/functional/cms_controller_test.rb +++ b/test/functional/cms_controller_test.rb @@ -107,7 +107,7 @@ class CmsControllerTest < Test::Unit::TestCase assert_redirected_to :action => 'view', :id => a.id - profile.reload + profile = Profile.find(@profile.id) assert_equal a, profile.home_page end diff --git a/test/functional/memberships_controller_test.rb b/test/functional/memberships_controller_test.rb index 41935c8..902fe80 100644 --- a/test/functional/memberships_controller_test.rb +++ b/test/functional/memberships_controller_test.rb @@ -49,7 +49,7 @@ class MembershipsControllerTest < Test::Unit::TestCase assert_response :redirect assert_redirected_to community.url - profile.reload + profile = Profile.find(@profile.id) assert profile.memberships.include?(community), 'profile should be actually added to the community' end @@ -139,7 +139,7 @@ class MembershipsControllerTest < Test::Unit::TestCase assert_response :redirect assert_redirected_to :action => 'index' - profile.reload + profile = Profile.find(@profile.id) assert_not_includes profile.memberships, community end @@ -239,7 +239,7 @@ class MembershipsControllerTest < Test::Unit::TestCase community.affiliate(profile, Profile::Roles.all_roles) post :leave, :profile => profile.identifier, :id => community.id, :confirmation => true - profile.reload + profile = Profile.find(@profile.id) assert_not_includes profile.memberships, community end diff --git a/test/functional/profile_editor_controller_test.rb b/test/functional/profile_editor_controller_test.rb index 0823eeb..043f645 100644 --- a/test/functional/profile_editor_controller_test.rb +++ b/test/functional/profile_editor_controller_test.rb @@ -12,13 +12,14 @@ class ProfileEditorControllerTest < Test::Unit::TestCase @request = ActionController::TestRequest.new @request.stubs(:ssl?).returns(true) @response = ActionController::TestResponse.new - login_as('ze') - @profile = Person['ze'] + @profile = create_user('default_user').person + Environment.default.affiliate(@profile, [Environment::Roles.admin] + Profile::Roles.all_roles) + login_as('default_user') end attr_reader :profile def test_local_files_reference - assert_local_files_reference :get, :index, :profile => 'ze' + assert_local_files_reference :get, :index, :profile => profile.identifier end def test_valid_xhtml @@ -26,7 +27,7 @@ class ProfileEditorControllerTest < Test::Unit::TestCase end def test_index - person = User.create(:login => 'test_profile', :email => 'test@noosfero.org', :password => 'test', :password_confirmation => 'test').person + person = create_user('test_profile').person person.name = 'a test profile' person.address = 'my address' person.contact_information = 'my contact information' @@ -53,8 +54,7 @@ class ProfileEditorControllerTest < Test::Unit::TestCase end def test_edit_person_info - person = User.create(:login => 'test_profile', :email => 'test@noosfero.org', :password => 'test', :password_confirmation => 'test').person - assert person.valid? + person = create_user('test_profile', :email => 'test@noosfero.org', :password => 'test', :password_confirmation => 'test').person get :edit, :profile => person.identifier assert_response :success assert_template 'edit' @@ -64,7 +64,7 @@ class ProfileEditorControllerTest < Test::Unit::TestCase person = create_user('test_profile').person post :edit, :profile => 'test_profile', :profile_data => { 'name' => 'new person', 'contact_information' => 'new contact information', 'address' => 'new address', 'sex' => 'female' } assert_redirected_to :action => 'index' - person.reload + person = Person.find(person.id) assert_equal 'new person', person.name assert_equal 'new contact information', person.contact_information assert_equal 'new address', person.address @@ -190,28 +190,27 @@ class ProfileEditorControllerTest < Test::Unit::TestCase end should 'display profile publication option in edit profile screen' do - profile = Profile['ze'] - get :edit, :profile => 'ze' + get :edit, :profile => profile.identifier assert_tag :tag => 'input', :attributes => { :type => 'radio', :checked => 'checked', :name => 'profile_data[public_profile]', :value => 'true' } assert_tag :tag => 'input', :attributes => { :type => 'radio', :name => 'profile_data[public_profile]', :value => 'false' } end should 'display properly that the profile is non-public' do - profile = Profile['ze'] profile.update_attributes!(:public_profile => false) - get :edit, :profile => 'ze' + get :edit, :profile => profile.identifier assert_tag :tag => 'input', :attributes => { :type => 'radio', :checked => 'checked', :name => 'profile_data[public_profile]', :value => 'false' } assert_tag :tag => 'input', :attributes => { :type => 'radio', :name => 'profile_data[public_profile]', :value => 'true' } end should 'save profile publication option set to true' do - post :edit, :profile => 'ze', :profile_data => { :public_profile => 'true' } - assert_equal true, Profile['ze'].public_profile + post :edit, :profile => profile.identifier, :profile_data => { :public_profile => 'true' } + assert_equal true, profile.public_profile end should 'save profile publication option set to false' do - post :edit, :profile => 'ze', :profile_data => { :public_profile => 'false' } - assert_equal false, Profile['ze'].public_profile + post :edit, :profile => profile.identifier, :profile_data => { :public_profile => 'false' } + profile = Person.find(@profile.id) + assert_equal false, profile.public_profile end should 'show error messages for invalid foundation_year' do @@ -368,8 +367,8 @@ class ProfileEditorControllerTest < Test::Unit::TestCase should 'link to mailconf' do MailConf.expects(:enabled?).returns(true).at_least_once - get :index, :profile => 'ze' - assert_tag :tag => 'a', :attributes => { :href => '/myprofile/ze/mailconf' } + get :index, :profile => profile.identifier + assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/mailconf" } end should 'not link to mailconf for organizations' do @@ -381,8 +380,8 @@ class ProfileEditorControllerTest < Test::Unit::TestCase should 'not link to mailconf if mail not enabled' do MailConf.expects(:enabled?).returns(false).at_least_once - get :index, :profile => 'ze' - assert_no_tag :tag => 'a', :attributes => { :href => '/myprofile/ze/mailconf' } + get :index, :profile => profile.identifier + assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/mailconf" } end should 'link to enable enterprise' do @@ -465,7 +464,7 @@ class ProfileEditorControllerTest < Test::Unit::TestCase should 'save footer and header' do person = create_user('designtestuser').person post :header_footer, :profile => 'designtestuser', :custom_header => 'new header', :custom_footer => 'new footer' - person.reload + person = Person.find(person.id) assert_equal 'new header', person.custom_header assert_equal 'new footer', person.custom_footer end @@ -477,8 +476,8 @@ class ProfileEditorControllerTest < Test::Unit::TestCase end should 'point to header/footer editing in control panel' do - get :index, :profile => 'ze' - assert_tag :tag => 'a', :attributes => { :href => '/myprofile/ze/profile_editor/header_footer' } + get :index, :profile => profile.identifier + assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/profile_editor/header_footer" } end should 'not list the manage products button if the environment disabled it' do @@ -497,14 +496,14 @@ class ProfileEditorControllerTest < Test::Unit::TestCase should 'display categories if environment disable_categories disabled' do Environment.any_instance.stubs(:enabled?).with(anything).returns(false) - person = User.create(:login => 'test_profile', :email => 'test@noosfero.org', :password => 'test', :password_confirmation => 'test').person + person = create_user('test_profile', :email => 'test@noosfero.org', :password => 'test', :password_confirmation => 'test').person get :edit, :profile => person.identifier assert_tag :tag => 'div', :descendant => { :tag => 'h2', :content => 'Select the categories of your interest' } end should 'not display categories if environment disable_categories enabled' do Environment.any_instance.stubs(:enabled?).with(anything).returns(true) - person = User.create(:login => 'test_profile', :email => 'test@noosfero.org', :password => 'test', :password_confirmation => 'test').person + person = create_user('test_profile', :email => 'test@noosfero.org', :password => 'test', :password_confirmation => 'test').person get :edit, :profile => person.identifier assert_no_tag :tag => 'div', :descendant => { :tag => 'h2', :content => 'Select the categories of your interest' } end diff --git a/test/functional/profile_members_controller_test.rb b/test/functional/profile_members_controller_test.rb index 65931ea..98e17c7 100644 --- a/test/functional/profile_members_controller_test.rb +++ b/test/functional/profile_members_controller_test.rb @@ -74,7 +74,7 @@ class ProfileMembersControllerTest < Test::Unit::TestCase post 'update_roles', :profile => 'test_enterprise', :roles => [role2.id], :person => member assert_response :redirect - member.reload + member = Person.find(member.id) roles = member.find_roles(ent).map(&:role) assert_includes roles, role2 assert_not_includes roles, role1 diff --git a/test/functional/tasks_controller_test.rb b/test/functional/tasks_controller_test.rb index 61e0fb9..f07ca68 100644 --- a/test/functional/tasks_controller_test.rb +++ b/test/functional/tasks_controller_test.rb @@ -67,7 +67,7 @@ class TasksControllerTest < Test::Unit::TestCase t = AddMember.create!(:person => profile, :community => profile) count = profile.members.size post :close, :decision => 'finish', :id => t.id - profile.reload + profile = Profile.find(@profile.id) assert_equal count + 1, profile.members.size end diff --git a/test/functional/themes_controller_test.rb b/test/functional/themes_controller_test.rb index d9682c1..20d7496 100644 --- a/test/functional/themes_controller_test.rb +++ b/test/functional/themes_controller_test.rb @@ -55,7 +55,7 @@ class ThemesControllerTest < Test::Unit::TestCase should 'save selection of theme' do get :set, :profile => 'testinguser', :id => 'onetheme' - profile.reload + profile = Profile.find(@profile.id) assert_equal 'onetheme', profile.theme end @@ -234,7 +234,7 @@ class ThemesControllerTest < Test::Unit::TestCase should 'set template' do post :set_layout_template, :profile => 'testinguser', :id => 'leftbar' - profile.reload + profile = Profile.find(@profile.id) assert_equal 'leftbar', profile.layout_template assert_redirected_to :action => 'index' end diff --git a/test/integration/editing_person_info_test.rb b/test/integration/editing_person_info_test.rb index 62a13fa..adc54d3 100644 --- a/test/integration/editing_person_info_test.rb +++ b/test/integration/editing_person_info_test.rb @@ -6,19 +6,19 @@ class EditingPersonInfoTest < ActionController::IntegrationTest should 'allow to edit person info' do - profile = Profile.find_by_identifier('ze') + profile = create_user('user_ze', :password => 'test', :password_confirmation => 'test').person - login('ze', 'test') + login(profile.identifier, 'test') - get '/myprofile/ze' + get "/myprofile/#{profile.identifier}" assert_response :success assert_tag :tag => 'a', :content => 'Profile settings' - get '/myprofile/ze/profile_editor/edit' + get "/myprofile/#{profile.identifier}/profile_editor/edit" assert_response :success - post '/myprofile/ze/profile_editor/edit', :profile_data => { :address => 'a new address', :contact_information => 'a new contact information' } + post "/myprofile/#{profile.identifier}/profile_editor/edit", :profile_data => { :address => 'a new address', :contact_information => 'a new contact information' } assert_response :redirect end diff --git a/test/integration/forgot_password_test.rb b/test/integration/forgot_password_test.rb index 66c5b4a..40504eb 100644 --- a/test/integration/forgot_password_test.rb +++ b/test/integration/forgot_password_test.rb @@ -12,7 +12,7 @@ class ForgotPasswordTest < ActionController::IntegrationTest Profile.destroy_all ChangePassword.destroy_all - User.create!(:login => 'forgotten', :password => 'test', :password_confirmation => 'test', :email => 'forgotten@localhost.localdomain') + create_user('forgotten', :password => 'test', :password_confirmation => 'test', :email => 'forgotten@localhost.localdomain') get '/account/forgot_password' diff --git a/test/integration/signup_test.rb b/test/integration/signup_test.rb index dbbd5f4..e09e56d 100644 --- a/test/integration/signup_test.rb +++ b/test/integration/signup_test.rb @@ -21,7 +21,7 @@ class SignupTest < ActionController::IntegrationTest assert_template 'signup' assert_equal count, User.count - post '/account/signup', :user => { :login => 'shouldaccepterms', :password => 'test', :password_confirmation => 'test', :email => 'shouldaccepterms@example.com', :terms_accepted => '1' } + post '/account/signup', :user => { :login => 'shouldaccepterms', :password => 'test', :password_confirmation => 'test', :email => 'shouldaccepterms@example.com', :terms_accepted => '1' }, :profile_data => person_data assert_response :redirect follow_redirect! diff --git a/test/test_helper.rb b/test/test_helper.rb index 0a48307..c80d8d6 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -66,7 +66,7 @@ class Test::Unit::TestCase end def create_admin_user(env) - admin_user = User.find_by_login('adminuser') || User.create!(:login => 'adminuser', :email => 'adminuser@noosfero.org', :password => 'adminuser', :password_confirmation => 'adminuser') + admin_user = User.find_by_login('adminuser') || create_user('adminuser', :email => 'adminuser@noosfero.org', :password => 'adminuser', :password_confirmation => 'adminuser') admin_role = Role.find_by_name('admin_role') || Role.create!(:name => 'admin_role', :permissions => ['view_environment_admin_panel','edit_environment_features', 'edit_environment_design', 'manage_environment_categories', 'manage_environment_roles', 'manage_environment_validators']) RoleAssignment.create!(:accessor => admin_user.person, :role => admin_role, :resource => env) unless admin_user.person.role_assignments.map{|ra|[ra.role, ra.accessor, ra.resource]}.include?([admin_role, admin_user, env]) admin_user.login @@ -79,14 +79,21 @@ class Test::Unit::TestCase env end - def create_user(name, options = {}) + def create_user(name, options = {}, person_options = {}) data = { :login => name, :email => name + '@noosfero.org', :password => name.underscore, :password_confirmation => name.underscore }.merge(options) - User.create!(data) + user = User.new(data) + user.build_person(person_data.merge(person_options)) + user.save! + user + end + + def person_data + {} end def give_permission(user, permission, target) diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index dd71f4a..f9429b3 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -477,6 +477,7 @@ class ArticleTest < Test::Unit::TestCase a = p1.articles.create!(:name => 'test article', :body => 'some text') b = a.copy(:parent => a, :profile => p2) + p2 = Person.find(p2.id) assert_includes p2.articles, b assert_equal 'some text', b.body end diff --git a/test/unit/change_password_test.rb b/test/unit/change_password_test.rb index 76b2817..86896fc 100644 --- a/test/unit/change_password_test.rb +++ b/test/unit/change_password_test.rb @@ -20,7 +20,7 @@ class ChangePasswordTest < Test::Unit::TestCase should 'require a valid username' do User.destroy_all - User.create!(:login => 'testuser', :password => 'test', :password_confirmation => 'test', :email => 'test@example.com') + create_user('testuser', :password => 'test', :password_confirmation => 'test', :email => 'test@example.com') data = ChangePassword.new data.login = 'testuser' @@ -30,7 +30,7 @@ class ChangePasswordTest < Test::Unit::TestCase should 'refuse incorrect e-mail address' do User.destroy_all - User.create!(:login => 'testuser', :password => 'test', :password_confirmation => 'test', :email => 'test@example.com') + create_user('testuser', :password => 'test', :password_confirmation => 'test', :email => 'test@example.com') data = ChangePassword.new data.login = 'testuser' @@ -43,7 +43,7 @@ class ChangePasswordTest < Test::Unit::TestCase should 'require the correct e-mail address' do User.destroy_all - User.create!(:login => 'testuser', :password => 'test', :password_confirmation => 'test', :email => 'test@example.com') + create_user('testuser', :password => 'test', :password_confirmation => 'test', :email => 'test@example.com') data = ChangePassword.new data.login = 'testuser' @@ -55,7 +55,7 @@ class ChangePasswordTest < Test::Unit::TestCase end should 'require correct passsword confirmation' do - User.create!(:login => 'testuser', :password => 'test', :password_confirmation => 'test', :email => 'test@example.com') + create_user('testuser', :password => 'test', :password_confirmation => 'test', :email => 'test@example.com') change = ChangePassword.new change.login = 'testuser' @@ -75,7 +75,7 @@ class ChangePasswordTest < Test::Unit::TestCase should 'actually change password' do User.destroy_all - person = User.create!(:login => 'testuser', :password => 'test', :password_confirmation => 'test', :email => 'test@example.com').person + person = create_user('testuser', :password => 'test', :password_confirmation => 'test', :email => 'test@example.com').person change = ChangePassword.new change.login = 'testuser' @@ -93,7 +93,7 @@ class ChangePasswordTest < Test::Unit::TestCase should 'not require password and password confirmation when cancelling' do User.destroy_all - person = User.create!(:login => 'testuser', :password => 'test', :password_confirmation => 'test', :email => 'test@example.com').person + person = create_user('testuser', :password => 'test', :password_confirmation => 'test', :email => 'test@example.com').person change = ChangePassword.new change.login = 'testuser' diff --git a/test/unit/create_enterprise_test.rb b/test/unit/create_enterprise_test.rb index 5e8f552..550fb29 100644 --- a/test/unit/create_enterprise_test.rb +++ b/test/unit/create_enterprise_test.rb @@ -28,7 +28,7 @@ class CreateEnterpriseTest < Test::Unit::TestCase task.valid? assert task.errors.invalid?(:requestor_id) - task.requestor = User.create!(:login => 'testuser', :password => 'test', :password_confirmation => 'test', :email => 'testuser@localhost.localdomain').person + task.requestor = create_user('testuser', :password => 'test', :password_confirmation => 'test', :email => 'testuser@localhost.localdomain').person task.valid? assert !task.errors.invalid?(:requestor_id) end @@ -98,7 +98,7 @@ class CreateEnterpriseTest < Test::Unit::TestCase region = Region.create!(:name => 'My region', :environment_id => environment.id) validator = Organization.create!(:name => "My organization", :identifier => 'myorg', :environment_id => environment.id) region.validators << validator - person = User.create!(:login => 'testuser', :password => 'test', :password_confirmation => 'test', :email => 'testuser@localhost.localdomain').person + person = create_user('testuser', :password => 'test', :password_confirmation => 'test', :email => 'testuser@localhost.localdomain').person task = CreateEnterprise.create!({ :name => 'My new enterprise', @@ -132,7 +132,7 @@ class CreateEnterpriseTest < Test::Unit::TestCase region = Region.create!(:name => 'My region', :environment_id => environment.id) validator = Organization.create!(:name => "My organization", :identifier => 'myorg', :environment_id => environment.id) region.validators << validator - person = User.create!(:login => 'testuser', :password => 'test', :password_confirmation => 'test', :email => 'testuser@localhost.localdomain').person + person = create_user('testuser', :password => 'test', :password_confirmation => 'test', :email => 'testuser@localhost.localdomain').person task = CreateEnterprise.create!({ :name => 'My new enterprise', @@ -174,7 +174,7 @@ class CreateEnterpriseTest < Test::Unit::TestCase region = Region.create!(:name => 'My region', :environment_id => environment.id) validator = Organization.create!(:name => "My organization", :identifier => 'myorg', :environment_id => environment.id) region.validators << validator - person = User.create!(:login => 'testuser', :password => 'test', :password_confirmation => 'test', :email => 'testuser@localhost.localdomain').person + person = create_user('testuser', :password => 'test', :password_confirmation => 'test', :email => 'testuser@localhost.localdomain').person task = CreateEnterprise.new({ :name => 'My new enterprise', :identifier => 'mynewenterprise', diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb index efe77b4..3800aa0 100644 --- a/test/unit/person_test.rb +++ b/test/unit/person_test.rb @@ -6,16 +6,16 @@ class PersonTest < Test::Unit::TestCase def test_person_must_come_form_the_cration_of_an_user p = Person.new(:name => 'John', :identifier => 'john') assert !p.valid? - p.user = User.create(:login => 'john', :email => 'john@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe') + p.user = create_user('john', :email => 'john@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe') assert !p.valid? - p = User.create(:login => 'johnz', :email => 'johnz@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe').person + p = create_user('johnz', :email => 'johnz@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe').person assert p.valid? end def test_can_associate_to_a_profile pr = Profile.new(:identifier => 'mytestprofile', :name => 'My test profile') pr.save! - pe = User.create(:login => 'person', :email => 'person@test.net', :password => 'dhoe', :password_confirmation => 'dhoe').person + pe = create_user('person', :email => 'person@test.net', :password => 'dhoe', :password_confirmation => 'dhoe').person pe.save! member_role = Role.create(:name => 'somerandomrole') pr.affiliate(pe, member_role) @@ -26,7 +26,7 @@ class PersonTest < Test::Unit::TestCase def test_can_belong_to_an_enterprise e = Enterprise.new(:identifier => 'enterprise', :name => 'enterprise') e.save! - p = User.create(:login => 'person', :email => 'person@test.net', :password => 'dhoe', :password_confirmation => 'dhoe').person + p = create_user('person', :email => 'person@test.net', :password => 'dhoe', :password_confirmation => 'dhoe').person p.save! member_role = Role.create(:name => 'somerandomrole') e.affiliate(p, member_role) @@ -46,7 +46,7 @@ class PersonTest < Test::Unit::TestCase should 'can have user' do u = User.new(:login => 'john', :email => 'john@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe') - p = Person.new(:name => 'John', :identifier => 'john') + p = Person.new(person_data.merge(:name => 'John', :identifier => 'john')) u.person = p assert u.save assert_kind_of User, p.user @@ -54,8 +54,7 @@ class PersonTest < Test::Unit::TestCase end should 'only one person per user' do - u = User.new(:login => 'john', :email => 'john@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe') - assert u.save + u = create_user('john', :email => 'john@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe') p1 = u.person assert_equal u, p1.user @@ -81,30 +80,30 @@ class PersonTest < Test::Unit::TestCase end should 'change the roles of the user' do - p = User.create(:login => 'jonh', :email => 'john@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe').person + p = create_user('jonh', :email => 'john@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe').person e = Enterprise.create(:identifier => 'enter', :name => 'Enter') r1 = Role.create(:name => 'associate') assert e.affiliate(p, r1) r2 = Role.create(:name => 'partner') assert p.define_roles([r2], e) - p.reload + p = Person.find(p.id) assert p.role_assignments.any? {|ra| ra.role == r2} assert !p.role_assignments.any? {|ra| ra.role == r1} end should 'report that the user has the permission' do - p = User.create(:login => 'jonh', :email => 'john@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe').person + p = create_user('john', :email => 'john@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe').person r = Role.create(:name => 'associate', :permissions => ['edit_profile']) e = Enterprise.create(:identifier => 'enterpri', :name => 'Enterpri') assert e.affiliate(p, r) - assert p.reload + p = Person.find(p.id) assert e.reload assert p.has_permission?('edit_profile', e) assert !p.has_permission?('destroy_profile', e) end should 'get an email address from the associated user instance' do - p = User.create!(:login => 'jonh', :email => 'john@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe').person + p = create_user('jonh', :email => 'john@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe').person assert_equal 'john@doe.org', p.email end @@ -131,7 +130,7 @@ class PersonTest < Test::Unit::TestCase env.affiliate(person, role) assert ! person.is_admin? role.update_attributes(:permissions => ['view_environment_admin_panel']) - person.reload + person = Person.find(person.id) assert person.is_admin? end diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index e8d18df..d1c60d6 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -60,7 +60,16 @@ class ProfileTest < Test::Unit::TestCase end should 'set default environment for users created' do - assert_equal Environment.default, create_user('mytestuser').person.environment + user = create_user 'mytestuser' + assert_equal 'mytestuser', user.login + assert !user.new_record? + + p = user.person + + assert !p.new_record? + assert_equal 'mytestuser', p.identifier + e = p.environment + assert_equal Environment.default, e end def test_cannot_rename @@ -86,7 +95,7 @@ class ProfileTest < Test::Unit::TestCase def test_can_have_affiliated_people pr = Profile.create(:name => 'composite_profile', :identifier => 'composite') - pe = User.create(:login => 'aff', :email => 'aff@pr.coop', :password => 'blih', :password_confirmation => 'blih').person + pe = create_user('aff', :email => 'aff@pr.coop', :password => 'blih', :password_confirmation => 'blih').person member_role = Role.new(:name => 'new_member_role') assert member_role.save @@ -817,7 +826,6 @@ class ProfileTest < Test::Unit::TestCase community.remove_member(person) - person.reload assert_not_includes person.memberships, community end diff --git a/test/unit/task_test.rb b/test/unit/task_test.rb index 817ee0d..8a2ffb2 100644 --- a/test/unit/task_test.rb +++ b/test/unit/task_test.rb @@ -194,7 +194,10 @@ class TaskTest < Test::Unit::TestCase protected def sample_user - User.create(:login => 'testfindinactivetask', :password => 'test', :password_confirmation => 'test', :email => 'testfindinactivetask@localhost.localdomain').person + user = User.new(:login => 'testfindinactivetask', :password => 'test', :password_confirmation => 'test', :email => 'testfindinactivetask@localhost.localdomain') + user.build_person(person_data) + user.save + user.person end end diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index a73faec..41ed654 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -8,35 +8,35 @@ class UserTest < Test::Unit::TestCase def test_should_create_user assert_difference User, :count do - user = create_user + user = new_user assert !user.new_record?, "#{user.errors.full_messages.to_sentence}" end end def test_should_require_login assert_no_difference User, :count do - u = create_user(:login => nil) + u = new_user(:login => nil) assert u.errors.on(:login) end end def test_should_require_password assert_no_difference User, :count do - u = create_user(:password => nil) + u = new_user(:password => nil) assert u.errors.on(:password) end end def test_should_require_password_confirmation assert_no_difference User, :count do - u = create_user(:password_confirmation => nil) + u = new_user(:password_confirmation => nil) assert u.errors.on(:password_confirmation) end end def test_should_require_email assert_no_difference User, :count do - u = create_user(:email => nil) + u = new_user(:email => nil) assert u.errors.on(:email) end end @@ -72,7 +72,7 @@ class UserTest < Test::Unit::TestCase users_count = User.count person_count = Person.count - user = User.create!(:login => 'new_user', :email => 'new_user@example.com', :password => 'test', :password_confirmation => 'test') + user = create_user('new_user', :email => 'new_user@example.com', :password => 'test', :password_confirmation => 'test') assert Person.exists?(['user_id = ?', user.id]) @@ -107,7 +107,7 @@ class UserTest < Test::Unit::TestCase end def test_should_change_password - user = User.create!(:login => 'changetest', :password => 'test', :password_confirmation => 'test', :email => 'changetest@example.com') + user = create_user('changetest', :password => 'test', :password_confirmation => 'test', :email => 'changetest@example.com') assert_nothing_raised do user.change_password!('test', 'newpass', 'newpass') end @@ -116,7 +116,7 @@ class UserTest < Test::Unit::TestCase end def test_should_give_correct_current_password_for_changing_password - user = User.create!(:login => 'changetest', :password => 'test', :password_confirmation => 'test', :email => 'changetest@example.com') + user = create_user('changetest', :password => 'test', :password_confirmation => 'test', :email => 'changetest@example.com') assert_raise User::IncorrectPassword do user.change_password!('wrong', 'newpass', 'newpass') end @@ -125,7 +125,7 @@ class UserTest < Test::Unit::TestCase end should 'require matching confirmation when changing password by force' do - user = User.create!(:login => 'changetest', :password => 'test', :password_confirmation => 'test', :email => 'changetest@example.com') + user = create_user('changetest', :password => 'test', :password_confirmation => 'test', :email => 'changetest@example.com') assert_raise ActiveRecord::RecordInvalid do user.force_change_password!('newpass', 'newpasswrong') end @@ -134,7 +134,7 @@ class UserTest < Test::Unit::TestCase end should 'be able to force password change' do - user = User.create!(:login => 'changetest', :password => 'test', :password_confirmation => 'test', :email => 'changetest@example.com') + user = create_user('changetest', :password => 'test', :password_confirmation => 'test', :email => 'changetest@example.com') assert_nothing_raised do user.force_change_password!('newpass', 'newpass') end @@ -144,19 +144,19 @@ class UserTest < Test::Unit::TestCase def test_should_create_person_when_creating_user count = Person.count assert !Person.find_by_identifier('lalala') - create_user(:login => 'lalala', :email => 'lalala@example.com') + new_user(:login => 'lalala', :email => 'lalala@example.com') assert Person.find_by_identifier('lalala') end should 'set the same environment for user and person objects' do env = Environment.create!(:name => 'my test environment') - user = create_user(:environment_id => env.id) + user = new_user(:environment_id => env.id) assert_equal env, user.environment assert_equal env, user.person.environment end def test_should_destroy_person_when_destroying_user - user = create_user(:login => 'lalala', :email => 'lalala@example.com') + user = new_user(:login => 'lalala', :email => 'lalala@example.com') assert Person.find_by_identifier('lalala') user.destroy assert !Person.find_by_identifier('lalala') @@ -164,6 +164,7 @@ class UserTest < Test::Unit::TestCase def test_should_encrypt_password_with_salted_sha1 user = User.new(:login => 'lalala', :email => 'lalala@example.com', :password => 'test', :password_confirmation => 'test') + user.build_person(person_data) user.expects(:salt).returns('testsalt') user.save! @@ -177,12 +178,12 @@ class UserTest < Test::Unit::TestCase def test_should_support_md5_passwords # ATTENTION this test explicitly exposes the crypted form of 'test'. This # makes 'test' a terrible password. :) - user = create_user(:login => 'lalala', :email => 'lalala@example.com', :password => 'test', :password_confirmation => 'test', :password_type => 'md5') + user = new_user(:login => 'lalala', :email => 'lalala@example.com', :password => 'test', :password_confirmation => 'test', :password_type => 'md5') assert_equal '098f6bcd4621d373cade4e832627b4f6', user.crypted_password end def test_should_support_clear_passwords - assert_equal 'test', create_user(:password => 'test', :password_confirmation => 'test', :password_type => 'clear').crypted_password + assert_equal 'test', new_user(:password => 'test', :password_confirmation => 'test', :password_type => 'clear').crypted_password end def test_should_only_allow_know_encryption_methods @@ -214,14 +215,14 @@ class UserTest < Test::Unit::TestCase def test_new_instances_should_use_system_encryption_method User.expects(:system_encryption_method).returns(:clear) - assert_equal 'clear', create_user.password_type + assert_equal 'clear', new_user.password_type end def test_should_reencrypt_password_when_using_different_encryption_method_from_the_system_default User.stubs(:system_encryption_method).returns(:salted_sha1) # a user was created ... - user = create_user(:login => 'lalala', :email => 'lalala@example.com', :password => 'test', :password_confirmation => 'test', :password_type => 'salted_sha1') + user = new_user(:login => 'lalala', :email => 'lalala@example.com', :password => 'test', :password_confirmation => 'test', :password_type => 'salted_sha1') # then the sysadmin decided to change the encryption method User.expects(:system_encryption_method).returns(:md5).at_least_once @@ -238,7 +239,7 @@ class UserTest < Test::Unit::TestCase def test_should_not_update_encryption_if_password_incorrect # a user was created User.stubs(:system_encryption_method).returns(:salted_sha1) - user = create_user(:login => 'lalala', :email => 'lalala@example.com', :password => 'test', :password_confirmation => 'test', :password_type => 'salted_sha1') + user = new_user(:login => 'lalala', :email => 'lalala@example.com', :password => 'test', :password_confirmation => 'test', :password_type => 'salted_sha1') crypted_password = user.crypted_password # then the sysadmin deciced to change the encryption method @@ -262,7 +263,10 @@ class UserTest < Test::Unit::TestCase end protected - def create_user(options = {}) - User.create({ :login => 'quire', :email => 'quire@example.com', :password => 'quire', :password_confirmation => 'quire' }.merge(options)) + def new_user(options = {}) + user = User.new({ :login => 'quire', :email => 'quire@example.com', :password => 'quire', :password_confirmation => 'quire' }.merge(options)) + user.build_person(person_data) + user.save + user end end -- libgit2 0.21.2