Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
iv
gitlab-ce
Commits
37731ba1
Commit
37731ba1
authored
Dec 23, 2015
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add method that persist ensured token in `TokenAuthenticatable`
parent
76f7e804
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
8 deletions
+26
-8
app/models/concerns/token_authenticatable.rb
app/models/concerns/token_authenticatable.rb
+13
-2
spec/models/concerns/token_authenticatable_spec.rb
spec/models/concerns/token_authenticatable_spec.rb
+13
-6
No files found.
app/models/concerns/token_authenticatable.rb
View file @
37731ba1
...
@@ -17,12 +17,17 @@ module TokenAuthenticatable
...
@@ -17,12 +17,17 @@ module TokenAuthenticatable
end
end
define_method
(
"ensure_
#{
token_field
}
"
)
do
define_method
(
"ensure_
#{
token_field
}
"
)
do
current_token
=
read_attribute
(
token_field
)
current_token
.
blank?
?
write_new_token
(
token_field
)
:
current_token
end
define_method
(
"ensure_
#{
token_field
}
!"
)
do
send
(
"reset_
#{
token_field
}
!"
)
if
read_attribute
(
token_field
).
blank?
send
(
"reset_
#{
token_field
}
!"
)
if
read_attribute
(
token_field
).
blank?
read_attribute
(
token_field
)
read_attribute
(
token_field
)
end
end
define_method
(
"reset_
#{
token_field
}
!"
)
do
define_method
(
"reset_
#{
token_field
}
!"
)
do
write_
attribute
(
token_field
,
generate_token_for
(
token_field
)
)
write_
new_token
(
token_field
)
save!
save!
end
end
end
end
...
@@ -30,10 +35,16 @@ module TokenAuthenticatable
...
@@ -30,10 +35,16 @@ module TokenAuthenticatable
private
private
def
generate_token_for
(
token_field
)
def
write_new_token
(
token_field
)
new_token
=
generate_token
(
token_field
)
write_attribute
(
token_field
,
new_token
)
end
def
generate_token
(
token_field
)
loop
do
loop
do
token
=
Devise
.
friendly_token
token
=
Devise
.
friendly_token
break
token
unless
self
.
class
.
unscoped
.
find_by
(
token_field
=>
token
)
break
token
unless
self
.
class
.
unscoped
.
find_by
(
token_field
=>
token
)
end
end
end
end
end
end
spec/models/concerns/token_authenticatable_spec.rb
View file @
37731ba1
...
@@ -2,7 +2,8 @@ require 'spec_helper'
...
@@ -2,7 +2,8 @@ require 'spec_helper'
shared_examples
'TokenAuthenticatable'
do
shared_examples
'TokenAuthenticatable'
do
describe
'dynamically defined methods'
do
describe
'dynamically defined methods'
do
it
{
expect
(
described_class
).
to
be_private_method_defined
(
:generate_token_for
)
}
it
{
expect
(
described_class
).
to
be_private_method_defined
(
:generate_token
)
}
it
{
expect
(
described_class
).
to
be_private_method_defined
(
:write_new_token
)
}
it
{
expect
(
described_class
).
to
respond_to
(
"find_by_
#{
token_field
}
"
)
}
it
{
expect
(
described_class
).
to
respond_to
(
"find_by_
#{
token_field
}
"
)
}
it
{
is_expected
.
to
respond_to
(
"ensure_
#{
token_field
}
"
)
}
it
{
is_expected
.
to
respond_to
(
"ensure_
#{
token_field
}
"
)
}
it
{
is_expected
.
to
respond_to
(
"reset_
#{
token_field
}
!"
)
}
it
{
is_expected
.
to
respond_to
(
"reset_
#{
token_field
}
!"
)
}
...
@@ -24,17 +25,21 @@ describe ApplicationSetting, 'TokenAuthenticatable' do
...
@@ -24,17 +25,21 @@ describe ApplicationSetting, 'TokenAuthenticatable' do
it_behaves_like
'TokenAuthenticatable'
it_behaves_like
'TokenAuthenticatable'
describe
'generating new token'
do
describe
'generating new token'
do
subject
{
described_class
.
new
}
let
(
:token
)
{
subject
.
send
(
token_field
)
}
context
'token is not generated yet'
do
context
'token is not generated yet'
do
it
{
expect
(
token
).
to
be
nil
}
describe
'token field accessor'
do
subject
{
described_class
.
new
.
send
(
token_field
)
}
it
{
is_expected
.
to_not
be_blank
}
end
describe
'ensured token'
do
describe
'ensured token'
do
subject
{
described_class
.
new
.
send
(
"ensure_
#{
token_field
}
"
)
}
subject
{
described_class
.
new
.
send
(
"ensure_
#{
token_field
}
"
)
}
it
{
is_expected
.
to
be_a
String
}
it
{
is_expected
.
to
be_a
String
}
it
{
is_expected
.
to_not
be_blank
}
it
{
is_expected
.
to_not
be_blank
}
end
describe
'ensured! token'
do
subject
{
described_class
.
new
.
send
(
"ensure_
#{
token_field
}
!"
)
}
it
'should persist new token'
do
it
'should persist new token'
do
expect
(
subject
).
to
eq
described_class
.
current
[
token_field
]
expect
(
subject
).
to
eq
described_class
.
current
[
token_field
]
...
@@ -44,7 +49,9 @@ describe ApplicationSetting, 'TokenAuthenticatable' do
...
@@ -44,7 +49,9 @@ describe ApplicationSetting, 'TokenAuthenticatable' do
context
'token is generated'
do
context
'token is generated'
do
before
{
subject
.
send
(
"reset_
#{
token_field
}
!"
)
}
before
{
subject
.
send
(
"reset_
#{
token_field
}
!"
)
}
it
{
expect
(
token
).
to
be_a
String
}
it
'persists a new token '
do
expect
(
subject
.
send
(
:read_attribute
,
token_field
)).
to
be_a
String
end
end
end
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