Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
sample_app
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Tan Phat Nguyen
sample_app
Commits
36a24f79
Commit
36a24f79
authored
Nov 06, 2014
by
ntphat
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add some logout and login
parent
d29519e3
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
60 additions
and
0 deletions
+60
-0
app/controllers/users_controller.rb
+6
-0
app/helpers/sessions_helper.rb
+5
-0
app/models/user.rb
+6
-0
test/fixtures/users.yml
+5
-0
test/integration/users_login_test.rb
+34
-0
test/integration/users_signup_test.rb
+1
-0
test/test_helper.rb
+3
-0
No files found.
app/controllers/users_controller.rb
View file @
36a24f79
...
...
@@ -8,12 +8,18 @@ class UsersController < ApplicationController
def
create
@user
=
User
.
new
(
user_params
)
if
@user
.
save
log_in
@user
flash
[
:success
]
=
"Welcome to the Sample App!"
redirect_to
@user
else
render
'new'
end
end
def
destroy
log_out
redirect_to
root_url
end
private
def
user_params
params
.
require
(
:user
).
permit
(
:name
,
:email
,
:password
,
:password_confirmation
)
...
...
app/helpers/sessions_helper.rb
View file @
36a24f79
...
...
@@ -10,4 +10,9 @@ module SessionsHelper
def
logged_in?
!
current_user
.
nil?
end
def
log_out
session
.
delete
(
:user_id
)
@current_user
=
nil
end
end
app/models/user.rb
View file @
36a24f79
...
...
@@ -9,4 +9,10 @@ class User < ActiveRecord::Base
validates
:password
,
length:
{
minimum:
6
}
has_secure_password
def
User
.
digest
(
string
)
cost
=
ActiveModel
::
SecurePassword
.
min_cost
?
BCrypt
::
Engine
::
MIN_COST
:
BCrypt
::
Engine
.
cost
BCrypt
::
Password
.
create
(
string
,
cost:
cost
)
end
end
test/fixtures/users.yml
View file @
36a24f79
michael
:
name
:
Michael Example
email
:
michael@example.com
password_digest
:
<%= User.digest('password') %>
\ No newline at end of file
test/integration/users_login_test.rb
View file @
36a24f79
require
'test_helper'
class
UsersLoginTest
<
ActionDispatch
::
IntegrationTest
def
setup
@user
=
users
(
:michael
)
end
test
"login with invalid information"
do
get
login_path
assert_template
'sessions/new'
...
...
@@ -10,4 +14,34 @@ class UsersLoginTest < ActionDispatch::IntegrationTest
get
root_path
assert
flash
.
empty?
end
test
"login with valid information"
do
get
login_path
post
login_path
,
session:
{
email:
@user
.
email
,
password:
'password'
}
assert_redirected_to
@user
follow_redirect!
assert_template
'users/show'
assert_select
"a[href=?]"
,
login_path
,
count:
0
assert_select
"a[href=?]"
,
logout_path
assert_select
"a[href=?]"
,
user_path
(
@user
)
end
test
"login with valid information followed by logout"
do
get
login_path
post
login_path
,
session:
{
email:
@user
.
email
,
password:
'password'
}
assert
is_logged_in?
assert_redirected_to
@user
follow_redirect!
assert_template
'users/show'
assert_select
"a[href=?]"
,
login_path
,
count:
0
assert_select
"a[href=?]"
,
logout_path
assert_select
"a[href=?]"
,
user_path
(
@user
)
delete
logout_path
assert_not
is_logged_in?
assert_redirected_to
root_url
follow_redirect!
assert_select
"a[href=?]"
,
login_path
assert_select
"a[href=?]"
,
logout_path
,
count:
0
assert_select
"a[href=?]"
,
user_path
(
@user
),
count:
0
end
end
test/integration/users_signup_test.rb
View file @
36a24f79
...
...
@@ -18,5 +18,6 @@ class UsersSignupTest < ActionDispatch::IntegrationTest
end
assert_template
'users/show'
assert_not
flash
.
empty?
assert
is_logged_in?
end
end
test/test_helper.rb
View file @
36a24f79
...
...
@@ -8,4 +8,7 @@ class ActiveSupport::TestCase
include
ApplicationHelper
# Add more helper methods to be used by all tests here...
def
is_logged_in?
!
session
[
:user_id
].
nil?
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment