|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +describe "Deleting own user", type: :request, integration: true do |
| 4 | + include IntegrationTestActions |
| 5 | + |
| 6 | + it 'sending email should generate token' do |
| 7 | + user = User.create!(login: 'user', password: 'password', email: 'theuser@example.com') |
| 8 | + |
| 9 | + visit '/' |
| 10 | + log_in_as('user', 'password') |
| 11 | + |
| 12 | + visit '/user' |
| 13 | + |
| 14 | + click_button 'Request deleting account' |
| 15 | + |
| 16 | + # Yay, you got mail! |
| 17 | + |
| 18 | + token = user.verification_tokens.delete_user[0] |
| 19 | + |
| 20 | + expect(token.nil? == false) |
| 21 | + expect(User.find_by(login: user.login) == user) |
| 22 | + end |
| 23 | + |
| 24 | + it 'verify destroy page should have a verification button and password field' do |
| 25 | + user = User.create!(login: 'user', password: 'password', email: 'theuser@example.com') |
| 26 | + |
| 27 | + visit '/' |
| 28 | + log_in_as('user', 'password') |
| 29 | + |
| 30 | + visit '/user' |
| 31 | + |
| 32 | + click_button 'Request deleting account' |
| 33 | + |
| 34 | + # Yay, you got mail! |
| 35 | + |
| 36 | + token = user.verification_tokens.delete_user[0] |
| 37 | + |
| 38 | + visit"/users/#{user.id}/destroy/#{token.token}" |
| 39 | + |
| 40 | + expect(page).to have_content("Deleting the account #{user.login}") |
| 41 | + |
| 42 | + expect(page).to have_button('Destroy my account permanently', disabled: true) |
| 43 | + |
| 44 | + check "I've read the above and i'm sure i understand the consequences" |
| 45 | + |
| 46 | + expect(page).to have_button('Destroy my account permanently', disabled: false) |
| 47 | + expect(User.find_by(login: user.login) == user) |
| 48 | + expect(page).to have_content('Your password') |
| 49 | + end |
| 50 | + |
| 51 | + it 'pressing verification button destroys user' do |
| 52 | + user = User.create!(login: 'user', password: 'password', email: 'theuser@example.com') |
| 53 | + username = user.login |
| 54 | + |
| 55 | + visit '/' |
| 56 | + log_in_as('user', 'password') |
| 57 | + |
| 58 | + visit '/user' |
| 59 | + |
| 60 | + click_button 'Request deleting account' |
| 61 | + |
| 62 | + # Yay, you got mail! |
| 63 | + |
| 64 | + token = user.verification_tokens.delete_user[0] |
| 65 | + |
| 66 | + visit"/users/#{user.id}/destroy/#{token.token}" |
| 67 | + |
| 68 | + check "I've read the above and i'm sure i understand the consequences" |
| 69 | + |
| 70 | + fill_in 'user[password]', with: user.password |
| 71 | + |
| 72 | + click_button 'Destroy my account permanently' |
| 73 | + |
| 74 | + page.accept_alert |
| 75 | + |
| 76 | + expect { User.find_by!(login: username) }.to raise_error ActiveRecord::RecordNotFound |
| 77 | + expect(page).to have_content("The account #{username} has been permanently destroyed") |
| 78 | + end |
| 79 | + |
| 80 | + it 'will not destroy user if password incorrect' do |
| 81 | + user = User.create!(login: 'user', password: 'password', email: 'theuser@example.com') |
| 82 | + |
| 83 | + visit '/' |
| 84 | + log_in_as('user', 'password') |
| 85 | + |
| 86 | + visit '/user' |
| 87 | + |
| 88 | + click_button 'Request deleting account' |
| 89 | + |
| 90 | + # Yay, you got mail! |
| 91 | + |
| 92 | + token = user.verification_tokens.delete_user[0] |
| 93 | + |
| 94 | + visit"/users/#{user.id}/destroy/#{token.token}" |
| 95 | + |
| 96 | + check "I've read the above and i'm sure i understand the consequences" |
| 97 | + |
| 98 | + fill_in 'user[password]', with: 'thisiswrongpassword' |
| 99 | + |
| 100 | + click_button 'Destroy my account permanently' |
| 101 | + |
| 102 | + page.accept_alert |
| 103 | + |
| 104 | + expect(User.find_by(login: user.login) == user) |
| 105 | + expect(page).to have_content('The password was incorrect') |
| 106 | + end |
| 107 | + |
| 108 | + it 'cannot be verified by another user' do |
| 109 | + user1 = User.create!(login: 'user1', password: 'password1', email: 'user1@example.com') |
| 110 | + user2 = User.create!(login: 'user2', password: 'password2', email: 'user2@example.com') |
| 111 | + |
| 112 | + visit '/' |
| 113 | + log_in_as('user1', 'password1') |
| 114 | + |
| 115 | + visit '/user' |
| 116 | + |
| 117 | + click_button 'Request deleting account' |
| 118 | + |
| 119 | + log_out |
| 120 | + |
| 121 | + log_in_as('user2', 'password2') |
| 122 | + |
| 123 | + token = user1.verification_tokens.delete_user[0] |
| 124 | + |
| 125 | + visit "/users/#{user1.id}/destroy/#{token.token}" |
| 126 | + |
| 127 | + expect(page).to have_content("Access denied") |
| 128 | + end |
| 129 | + |
| 130 | +end |
0 commit comments