Skip to content

Commit 29575b2

Browse files
Build interface
1 parent 417c014 commit 29575b2

File tree

4 files changed

+74
-10
lines changed

4 files changed

+74
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,25 @@
11
class ActiveSessionsController < ApplicationController
2+
before_action :authenticate_user!
3+
4+
def destroy
5+
@active_session = current_user.active_sessions.find(params[:id])
6+
7+
@active_session.destroy
8+
9+
if current_user
10+
redirect_to account_path, notice: "Session deleted."
11+
else
12+
reset_session
13+
redirect_to root_path, notice: "Signed out."
14+
end
15+
end
16+
17+
def destroy_all
18+
user = current_user
19+
20+
current_user.active_sessions.destroy_all
21+
reset_session
22+
23+
redirect_to root_path, notice: "Signed out."
24+
end
225
end

app/views/users/edit.html.erb

+3-4
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,18 @@
2525
<% end %>
2626
<h2>Current Logins</h2>
2727
<% if @active_sessions.any? %>
28-
<%= link_to "Log out of all other sessions", destroy_all_active_sessions_path %>
28+
<%= button_to "Log out of all other sessions", destroy_all_active_sessions_path, method: :delete %>
2929
<table>
3030
<thead>
3131
<tr>
3232
<th>User Agent</th>
3333
<th>IP Address</th>
3434
<th>Signed In At</th>
35+
<th>Sign Out</th>
3536
</tr>
3637
</thead>
3738
<tbody>
38-
<tr>
39-
<%= render @active_sessions %>
40-
</tr>
39+
<%= render @active_sessions %>
4140
</tbody>
4241
</table>
4342
<% end %>
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,45 @@
11
require "test_helper"
22

33
class ActiveSessionsControllerTest < ActionDispatch::IntegrationTest
4-
# test "the truth" do
5-
# assert true
6-
# end
4+
setup do
5+
@confirmed_user = User.create!(email: "confirmed_user@example.com", password: "password", password_confirmation: "password", confirmed_at: Time.current)
6+
end
7+
8+
test "should destroy all active sessions" do
9+
login @confirmed_user
10+
@confirmed_user.active_sessions.create!
11+
12+
assert_difference("ActiveSession.count", -2) do
13+
delete destroy_all_active_sessions_path
14+
end
15+
16+
assert_redirected_to root_path
17+
assert_nil current_user
18+
assert_not_nil flash[:notice]
19+
end
20+
21+
test "should destroy another session" do
22+
login @confirmed_user
23+
@confirmed_user.active_sessions.create!
24+
25+
assert_difference("ActiveSession.count", -1) do
26+
delete active_session_path(@confirmed_user.active_sessions.last)
27+
end
28+
29+
assert_redirected_to account_path
30+
assert_not_nil current_user
31+
assert_not_nil flash[:notice]
32+
end
33+
34+
test "should destroy current session" do
35+
login @confirmed_user
36+
37+
assert_difference("ActiveSession.count", -1) do
38+
delete active_session_path(@confirmed_user.active_sessions.last)
39+
end
40+
41+
assert_redirected_to root_path
42+
assert_nil current_user
43+
assert_not_nil flash[:notice]
44+
end
745
end

test/integration/user_interface_test.rb

+7-3
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@ class UserInterfaceTest < ActionDispatch::IntegrationTest
2020

2121
get account_path
2222

23-
assert_select "a", "Log out of all other sessions"
23+
assert_select "input[type='submit']" do
24+
assert_select "[value=?]", "Log out of all other sessions"
25+
end
2426
assert_match destroy_all_active_sessions_path, @response.body
2527

2628
assert_select "table" do
27-
assert_select "a", "Sign Out"
29+
assert_select "input[type='submit']" do
30+
assert_select "[value=?]", "Sign Out"
31+
end
2832
end
29-
assert_match destroy_active_session_path(@confirmed_user.active_sessions.last), @response.body
33+
assert_match active_session_path(@confirmed_user.active_sessions.last), @response.body
3034
end
3135
end

0 commit comments

Comments
 (0)