From a2f9612de8f01df1ad464a93d212ef4043c4ed73 Mon Sep 17 00:00:00 2001 From: Daniela Soares Feitosa Date: Thu, 21 Oct 2010 20:31:15 -0300 Subject: [PATCH] Allowing users to delete profile --- app/controllers/my_profile/memberships_controller.rb | 11 ----------- app/controllers/my_profile/profile_editor_controller.rb | 13 ++++++++++++- app/models/person.rb | 2 +- app/models/profile.rb | 2 +- app/views/memberships/destroy_community.rhtml | 9 --------- app/views/memberships/index.rhtml | 2 +- app/views/profile_editor/destroy_profile.rhtml | 10 ++++++++++ app/views/profile_editor/edit.rhtml | 4 ++++ features/delete_profile.feature | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ public/stylesheets/application.css | 4 ++++ test/fixtures/roles.yml | 2 ++ test/functional/memberships_controller_test.rb | 25 ++++--------------------- test/functional/profile_controller_test.rb | 17 +++++++++++++++++ test/functional/profile_editor_controller_test.rb | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/unit/person_test.rb | 8 ++++++++ test/unit/profile_test.rb | 9 +++++++++ 16 files changed, 259 insertions(+), 45 deletions(-) delete mode 100644 app/views/memberships/destroy_community.rhtml create mode 100644 app/views/profile_editor/destroy_profile.rhtml create mode 100644 features/delete_profile.feature diff --git a/app/controllers/my_profile/memberships_controller.rb b/app/controllers/my_profile/memberships_controller.rb index e9f8683..1bfe8c0 100644 --- a/app/controllers/my_profile/memberships_controller.rb +++ b/app/controllers/my_profile/memberships_controller.rb @@ -24,15 +24,4 @@ class MembershipsController < MyProfileController render :layout => 'wizard' end end - - def destroy_community - @community = Community.find(params[:id]) - if request.post? - if @community.destroy - session[:notice] = _('%s was removed.') % @community.short_name - redirect_to :action => 'index' - end - end - end - end diff --git a/app/controllers/my_profile/profile_editor_controller.rb b/app/controllers/my_profile/profile_editor_controller.rb index 7c194ba..fbedfd4 100644 --- a/app/controllers/my_profile/profile_editor_controller.rb +++ b/app/controllers/my_profile/profile_editor_controller.rb @@ -1,6 +1,7 @@ class ProfileEditorController < MyProfileController - protect 'edit_profile', :profile + protect 'edit_profile', :profile, :except => [:destroy_profile] + protect 'destroy_profile', :profile, :only => [:destroy_profile] def index @pending_tasks = profile.all_pending_tasks.select{|i| user.has_permission?(i.permission, profile)} @@ -72,4 +73,14 @@ class ProfileEditorController < MyProfileController end end + def destroy_profile + if request.post? + if @profile.destroy + session[:notice] = _('The profile was deleted.') + redirect_to :controller => 'home' + else + session[:notice] = _('Could not delete profile') + end + end + end end diff --git a/app/models/person.rb b/app/models/person.rb index 68936e8..0510bd3 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -19,7 +19,7 @@ class Person < Profile has_many :mailings - has_many :scraps_sent, :class_name => 'Scrap', :foreign_key => :sender_id + has_many :scraps_sent, :class_name => 'Scrap', :foreign_key => :sender_id, :dependent => :destroy named_scope :more_popular, :select => "#{Profile.qualified_column_names}, count(friend_id) as total", diff --git a/app/models/profile.rb b/app/models/profile.rb index 237e55f..18a894d 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -91,7 +91,7 @@ class Profile < ActiveRecord::Base has_many :action_tracker_notifications, :foreign_key => 'profile_id' has_many :tracked_notifications, :through => :action_tracker_notifications, :source => :action_tracker, :order => 'updated_at DESC' - has_many :scraps_received, :class_name => 'Scrap', :foreign_key => :receiver_id, :order => "updated_at DESC" + has_many :scraps_received, :class_name => 'Scrap', :foreign_key => :receiver_id, :order => "updated_at DESC", :dependent => :destroy # FIXME ugly workaround def self.human_attribute_name(attrib) diff --git a/app/views/memberships/destroy_community.rhtml b/app/views/memberships/destroy_community.rhtml deleted file mode 100644 index f1aa4c8..0000000 --- a/app/views/memberships/destroy_community.rhtml +++ /dev/null @@ -1,9 +0,0 @@ -

<%= _('To remove %s') % @community.short_name %>

- -

<%= _('Are you sure you want to remove "%s"?') % content_tag( 'i', @community.name ) %>

- -<% form_tag do %> - <%= hidden_field_tag(:confirmation, 1) %> - <%= submit_button(:ok, _("Yes, I want to remove.") % @community.short_name) %> - <%= button(:cancel, _("No, I don't want."), :action => 'index') %> -<% end %> diff --git a/app/views/memberships/index.rhtml b/app/views/memberships/index.rhtml index 8247608..a4b323d 100644 --- a/app/views/memberships/index.rhtml +++ b/app/views/memberships/index.rhtml @@ -25,7 +25,7 @@ <%= button 'menu-ctrl-panel', _('Control panel of this group'), membership.admin_url %> <%= lightbox_button 'menu-logout', _('Leave'), membership.leave_url %> <% if (membership.community? && user.has_permission?(:destroy_profile, membership)) %> - <%= button 'delete', _('Remove'), { :action => 'destroy_community', :id => membership } %> + <%= button 'delete', _('Remove'), { :controller => 'profile_editor', :action => 'destroy_profile', :profile => membership.identifier } %> <% end %> <% end %> diff --git a/app/views/profile_editor/destroy_profile.rhtml b/app/views/profile_editor/destroy_profile.rhtml new file mode 100644 index 0000000..a79dee2 --- /dev/null +++ b/app/views/profile_editor/destroy_profile.rhtml @@ -0,0 +1,10 @@ +

<%= _('Deleting profile %s') % profile.short_name(30) %>

+ +

<%= _('Are you sure you want to delete this profile?') %>

+

<%= _('You must be aware that all content of this profile (articles, events, files and pictures) will also be deleted.') %>

+ +<% button_bar do %> + <%= button(:remove, _('Yes, I am sure'), {:action => 'destroy_profile'}, :method => :post) %> + <%= button(:cancel, _('No, I gave up'), profile.url) %> +<% end %> + diff --git a/app/views/profile_editor/edit.rhtml b/app/views/profile_editor/edit.rhtml index fcade60..7daca88 100644 --- a/app/views/profile_editor/edit.rhtml +++ b/app/views/profile_editor/edit.rhtml @@ -76,6 +76,10 @@ <%= select_categories(:profile_data, _('Select the categories of your interest'), 2) %> + <% if user && user.has_permission?('destroy_profile', profile) %> + <%= link_to _('Delete profile'), {:action => :destroy_profile}, :id => 'delete-profile-link' %> + <% end %> + <% button_bar do %> <%= submit_button('save', _('Save'), :cancel => {:action => 'index'}) %> <%= button(:back, _('Back to control panel'), :controller => 'profile_editor') %> diff --git a/features/delete_profile.feature b/features/delete_profile.feature new file mode 100644 index 0000000..92b3bc7 --- /dev/null +++ b/features/delete_profile.feature @@ -0,0 +1,126 @@ +Feature: delete profile + As a noosfero user + I want to delete my profile + In order to leave the network + + Background: + Given the following users + | login | name | + | joaosilva | Joao Silva | + + Scenario: deleting profile + Given I am logged in as "joaosilva" + And I am on Joao Silva's control panel + And I follow "Profile Info and settings" + And I follow "Delete profile" + Then I should see "Are you sure you want to delete this profile?" + When I follow "Yes, I am sure" + Then I should be on the homepage + When I go to /joaosilva + Then I should see "There is no such page" + + Scenario: deleting other profile + Given the following users + | login | name | + | mariasilva | Maria Silva | + And I am logged in as "mariasilva" + And I go to /myprofile/joaosilva/profile_editor/destroy_profile + Then I should see "Access denied" + + Scenario: giving up of deleting profile + Given I am logged in as "joaosilva" + And I am on Joao Silva's control panel + And I follow "Profile Info and settings" + And I follow "Delete profile" + Then I should see "Are you sure you want to delete this profile?" + When I follow "No, I gave up" + Then I should be on Joao Silva's homepage + + Scenario: community admin can see link to delete profile + Given the following community + | identifier | name | + | sample-community | Sample Community | + And "Joao Silva" is admin of "Sample Community" + And I am logged in as "joaosilva" + And I am on Sample Community's control panel + When I follow "Community Info and settings" + Then I should see "Delete profile" + + Scenario: community admin deletes the community + Given the following community + | identifier | name | + | sample-community | Sample Community | + And "Joao Silva" is admin of "Sample Community" + And I am logged in as "joaosilva" + And I am on Sample Community's control panel + And I follow "Community Info and settings" + And I follow "Delete profile" + Then I should see "Are you sure you want to delete this profile?" + When I follow "Yes, I am sure" + Then I should be on the homepage + When I go to /sample-community + Then I should see "There is no such page" + + Scenario: community regular member tries to delete the community + Given the following community + | identifier | name | + | sample-community | Sample Community | + And "Joao Silva" is a member of "Sample Community" + And I am logged in as "joaosilva" + And I go to /myprofile/sample-community/profile_editor/destroy_profile + Then I should see "Access denied" + + Scenario: enterprise admin can see link to delete enterprise + Given the following enterprise + | identifier | name | + | sample-enterprise | Sample Enterprise | + And "Joao Silva" is admin of "Sample Enterprise" + And I am logged in as "joaosilva" + And I am on Sample Enterprise's control panel + When I follow "Enterprise Info and settings" + Then I should see "Delete profile" + + Scenario: enterprise admin deletes the enterprise + Given the following enterprise + | identifier | name | + | sample-enterprise | Sample Enterprise | + And "Joao Silva" is admin of "Sample Enterprise" + And I am logged in as "joaosilva" + And I am on Sample Enterprise's control panel + When I follow "Enterprise Info and settings" + And I follow "Delete profile" + Then I should see "Are you sure you want to delete this profile?" + When I follow "Yes, I am sure" + Then I should be on the homepage + When I go to /sample-enterprise + Then I should see "There is no such page" + + Scenario: enterprise regular member tries to delete the enterprise + Given the following community + | identifier | name | + | sample-enterprise | Sample Enterprise | + And "Joao Silva" is a member of "Sample Enterprise" + And I am logged in as "joaosilva" + And I go to /myprofile/sample-enterprise/profile_editor/destroy_profile + Then I should see "Access denied" + + Scenario: community regular member cannot see link to delete profile + Given the following community + | identifier | name | + | sample-community | Sample Community | + And "Joao Silva" is a member of "Sample Community" + And I am logged in as "joaosilva" + And I am on Sample Community's control panel + When I follow "Community Info and settings" + Then I should not see "Delete profile" + + Scenario: environment admin deletes profile + Given I am logged in as admin + And I am on Joao Silva's control panel + And I follow "Profile Info and settings" + And I follow "Delete profile" + Then I should see "Are you sure you want to delete this profile?" + When I follow "Yes, I am sure" + Then I should be on the homepage + When I go to /joaosilva + Then I should see "There is no such page" diff --git a/public/stylesheets/application.css b/public/stylesheets/application.css index d2a6f41..f6f1335 100644 --- a/public/stylesheets/application.css +++ b/public/stylesheets/application.css @@ -3503,6 +3503,10 @@ h1#agenda-title { -webkit-border-radius: 3px; } +.controller-profile_editor #delete-profile-link { + float: right; +} + .controller-profile_editor a.control-panel-groups { background-image: url(../images/control-panel/system-users.png) } .controller-profile_editor .msie6 a.control-panel-groups { background-image: url(../images/control-panel/system-users.gif) } diff --git a/test/fixtures/roles.yml b/test/fixtures/roles.yml index f8d7cc7..c0486a4 100644 --- a/test/fixtures/roles.yml +++ b/test/fixtures/roles.yml @@ -86,3 +86,5 @@ environment_administrator: - manage_environment_validators - moderate_comments - manage_environment_users + - edit_profile + - destroy_profile diff --git a/test/functional/memberships_controller_test.rb b/test/functional/memberships_controller_test.rb index 4b01512..c705fbd 100644 --- a/test/functional/memberships_controller_test.rb +++ b/test/functional/memberships_controller_test.rb @@ -122,15 +122,9 @@ class MembershipsControllerTest < Test::Unit::TestCase assert_tag :tag => 'a', :attributes => { :href => "/myprofile/testuser/memberships/new_community" } end - should 'render destroy_community template' do - community = Community.create!(:name => 'A community to destroy') - get :destroy_community, :profile => 'testuser', :id => community.id - assert_template 'destroy_community' - end - should 'display destroy link only to communities' do community = Community.create!(:name => 'A community to destroy') - enterprise = fast_create(Enterprise, :name => 'A enterprise test', :identifier => 'enterprise-test') + enterprise = fast_create(Enterprise, :name => 'A enterprise test') person = Person['testuser'] community.add_admin(person) @@ -138,19 +132,8 @@ class MembershipsControllerTest < Test::Unit::TestCase get :index, :profile => 'testuser' - assert_tag :tag => 'a', :attributes => { :href => "/myprofile/testuser/memberships/destroy_community/#{community.id}" } - assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/testuser/memberships/destroy_community/#{enterprise.id}" } - end - - should 'be able to destroy communities' do - community = Community.create!(:name => 'A community to destroy') - - person = Person['testuser'] - community.add_admin(person) - - assert_difference Community, :count, -1 do - post :destroy_community, :profile => 'testuser', :id => community.id - end + assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{community.identifier}/profile_editor/destroy_profile" } + assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/#{enterprise.identifier}/profile_editor/destroy_profile" } end should 'not display destroy link to normal members' do @@ -163,7 +146,7 @@ class MembershipsControllerTest < Test::Unit::TestCase get :index, :profile => 'testuser' assert_template 'index' - assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/testuser/memberships/destroy_community/#{community.id}" } + assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/#{community.identifier}/profile_editor/destroy_profile" } end should 'use the current environment for the template of user' do diff --git a/test/functional/profile_controller_test.rb b/test/functional/profile_controller_test.rb index 0235f72..411b2c1 100644 --- a/test/functional/profile_controller_test.rb +++ b/test/functional/profile_controller_test.rb @@ -647,6 +647,23 @@ class ProfileControllerTest < Test::Unit::TestCase assert_equal "You can't leave an empty message.", assigns(:message) end + should "display a scrap sent" do + another_person = fast_create(Person) + Scrap.create!(defaults_for_scrap(:sender => another_person, :receiver => profile, :content => 'A scrap')) + login_as(profile.identifier) + get :index, :profile => profile.identifier + assert_tag :tag => 'p', :content => 'A scrap' + end + + should "not display a scrap sent by a removed user" do + another_person = fast_create(Person) + Scrap.create!(defaults_for_scrap(:sender => another_person, :receiver => profile, :content => 'A scrap')) + login_as(profile.identifier) + another_person.destroy + get :index, :profile => profile.identifier + assert_no_tag :tag => 'p', :content => 'A scrap' + end + should 'see all activities of the current profile' do p1= Person.first p2= fast_create(Person) diff --git a/test/functional/profile_editor_controller_test.rb b/test/functional/profile_editor_controller_test.rb index cf081c2..f936fef 100644 --- a/test/functional/profile_editor_controller_test.rb +++ b/test/functional/profile_editor_controller_test.rb @@ -798,4 +798,64 @@ class ProfileEditorControllerTest < Test::Unit::TestCase } end + should 'render destroy_profile template' do + community = fast_create(Community) + get :destroy_profile, :profile => community.identifier + assert_template 'destroy_profile' + end + + should 'be able to destroy a person' do + person = fast_create(Person) + + assert_difference Person, :count, -1 do + post :destroy_profile, :profile => person.identifier + end + end + + should 'be able to destroy communities' do + community = fast_create(Community) + + person = fast_create(Person) + community.add_admin(person) + + assert_difference Community, :count, -1 do + post :destroy_profile, :profile => community.identifier + end + end + + should 'not be able to destroy communities if is a regular member' do + community = fast_create(Community) + + person = fast_create(Person) + community.add_admin(person) + + login_as(person.identifier) + assert_difference Community, :count, 0 do + post :destroy_profile, :profile => community.identifier + end + end + + should 'be able to destroy enterprise' do + enterprise = fast_create(Enterprise) + + person = fast_create(Person) + enterprise.add_admin(person) + + assert_difference Enterprise, :count, -1 do + post :destroy_profile, :profile => enterprise.identifier + end + end + + should 'not be able to destroy enterprise if is a regular member' do + enterprise = fast_create(Enterprise) + + person = fast_create(Person) + enterprise.add_admin(person) + + login_as(person.identifier) + assert_difference Enterprise, :count, 0 do + post :destroy_profile, :profile => enterprise.identifier + end + end + end diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb index 73513bf..1bef6b0 100644 --- a/test/unit/person_test.rb +++ b/test/unit/person_test.rb @@ -786,6 +786,14 @@ class PersonTest < Test::Unit::TestCase assert_equal s2, person.scraps(s2.id.to_s) end + should "destroy scrap if sender was removed" do + person = fast_create(Person) + scrap = fast_create(Scrap, :sender_id => person.id) + assert_not_nil Scrap.find_by_id(scrap.id) + person.destroy + assert_nil Scrap.find_by_id(scrap.id) + end + should "the tracked action be notified to person friends and herself" do p1 = Person.first p2 = fast_create(Person) diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index e3e6200..e9ba7de 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -1805,6 +1805,15 @@ class ProfileTest < Test::Unit::TestCase assert_nil profile.profile_custom_icon end + should "destroy scrap if receiver was removed" do + person = fast_create(Person) + scrap = fast_create(Scrap, :receiver_id => person.id) + assert_not_nil Scrap.find_by_id(scrap.id) + person.destroy + assert_nil Scrap.find_by_id(scrap.id) + end + + private def assert_invalid_identifier(id) -- libgit2 0.21.2