Skip to content

Commit 23b29a0

Browse files
committed
Merge pull request coderwall#236 from YaroSpace/bug_delete_user_#398
Bug delete user #398
2 parents 8f45d3a + d52a703 commit 23b29a0

File tree

5 files changed

+92
-3
lines changed

5 files changed

+92
-3
lines changed

app/models/user.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,12 @@ class User < ActiveRecord::Base
8282
has_one :github_profile , class_name: 'Users::Github::Profile', dependent: :destroy
8383
has_many :github_repositories, through: :github_profile , source: :repositories
8484

85-
8685
geocoded_by :location, latitude: :lat, longitude: :lng, country: :country, state_code: :state_name
86+
# FIXME: Move to background job
8787
after_validation :geocode_location, if: :location_changed? unless Rails.env.test?
8888

89+
before_destroy ->{ protips.destroy_all }, prepend: true
90+
8991
def near
9092
User.near([lat, lng])
9193
end
@@ -130,7 +132,6 @@ def self.with_username(username, provider = :username)
130132
where(["UPPER(#{sql_injection_safe_where_clause}) = UPPER(?)", username]).first
131133
end
132134

133-
134135
# Todo State machine
135136
def banned?
136137
banned_at.present?
@@ -923,7 +924,6 @@ def make_referral_token
923924
end
924925

925926
after_save :refresh_dependencies
926-
after_destroy :refresh_protips
927927

928928
def refresh_dependencies
929929
if username_changed? or avatar_changed? or team_document_id_changed?

spec/features/helpers/general_helpers.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ def login_as(settings = {})
2424
fill_in 'user_location', with: settings[:location]
2525
click_button 'Finish'
2626
end
27+
28+
user
2729
end
2830

2931
def create_team(name = 'TEST_TEAM')
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
require "rails_helper"
2+
3+
feature "User management", js: true do
4+
describe 'deleting a user' do
5+
before do
6+
stub_request(:post, /api.mixpanel.com/)
7+
end
8+
9+
let!(:user) { login_as(username: 'alice', bypass_ui_login: true) }
10+
11+
scenario 'user is presented with confirmation dialog when deletes his account' do
12+
visit '/settings'
13+
find('.delete').click_link 'click here.'
14+
15+
expect(page).to have_content 'Warning: clicking this link below will permenatly delete your Coderwall account and its data.'
16+
expect(page).to have_button 'Delete your account & sign out'
17+
end
18+
19+
scenario 'user is redirected to /welcome after deleting hios account' do
20+
visit '/settings'
21+
find('.delete').click_link 'click here.'
22+
find('.save').click_button 'Delete your account & sign out'
23+
24+
expect(current_path).to eq('/welcome')
25+
end
26+
27+
scenario 'user cannot login after deleting his account' do
28+
visit '/settings'
29+
find('.delete').click_link 'click here.'
30+
find('.save').click_button 'Delete your account & sign out'
31+
32+
visit "/auth/developer"
33+
fill_in 'name', with: user.username
34+
fill_in 'email', with: user.email
35+
click_button 'Sign In'
36+
37+
expect(current_path).to eq(new_user_path)
38+
end
39+
40+
scenario 'users protips are not displayed after he deletes his account' do
41+
Protip.rebuild_index
42+
protip_1, protip_2 = Fabricate.times(2, :protip, user: user)
43+
protip_3 = Fabricate(:protip)
44+
45+
visit '/settings'
46+
find('.delete').click_link 'click here.'
47+
find('.save').click_button 'Delete your account & sign out'
48+
49+
login_as(username: 'bob', bypass_ui_login: true)
50+
visit '/p/fresh'
51+
52+
expect(page).not_to have_content(protip_1.title)
53+
expect(page).not_to have_content(protip_2.title)
54+
expect(page).to have_content(protip_3.title)
55+
end
56+
end
57+
58+
end

spec/models/user_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,16 @@ class AlsoNotaBadge < BadgeBase
329329
it "should not default to banned" do
330330
expect(user.banned?).to eq(false)
331331
end
332+
end
332333

334+
describe 'deleting a user', focus: true do
335+
it 'deletes asosciated prtotips' do
336+
user = Fabricate(:user)
337+
protip = Fabricate(:protip, user: user)
338+
339+
expect(user.reload.protips).to receive(:destroy_all).and_return(false)
340+
user.destroy
341+
end
333342
end
334343

335344
end

spec/requests/users_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
RSpec.describe "User management", :type => :request do
2+
3+
describe 'deleting a user' do
4+
it 'deletes associated protips and reindex search index' do
5+
user = Fabricate(:user)
6+
7+
Protip.rebuild_index
8+
protip_1, protip_2 = Fabricate.times(2, :protip, user: user)
9+
protip_3 = Fabricate(:protip)
10+
11+
user.reload.destroy
12+
search = Protip.search('*').map(&:title)
13+
14+
expect(search).not_to include(protip_1.title)
15+
expect(search).not_to include(protip_2.title)
16+
expect(search).to include(protip_3.title)
17+
end
18+
end
19+
20+
end

0 commit comments

Comments
 (0)