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
Léo-Paul Géneau
gitlab-ce
Commits
9a830f1e
Commit
9a830f1e
authored
Nov 14, 2018
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add unit tests for encrypted token authenticatable
parent
c04f56d3
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
2 deletions
+53
-2
app/models/concerns/token_authenticatable_strategies/base.rb
app/models/concerns/token_authenticatable_strategies/base.rb
+2
-2
spec/models/concerns/token_authenticatable_strategies/encrypted_spec.rb
...ncerns/token_authenticatable_strategies/encrypted_spec.rb
+51
-0
No files found.
app/models/concerns/token_authenticatable_strategies/base.rb
View file @
9a830f1e
# frozen_string_literal: true
module
TokenAuthenticatableStrategies
attr_reader
:klass
,
:token_field
,
:options
class
Base
attr_reader
:klass
,
:token_field
,
:options
def
initialize
(
klass
,
token_field
,
options
)
@klass
=
klass
@token_field
=
token_field
...
...
spec/models/concerns/token_authenticatable_strategies/encrypted_spec.rb
View file @
9a830f1e
...
...
@@ -2,18 +2,69 @@ require 'spec_helper'
describe
TokenAuthenticatableStrategies
::
Encrypted
do
let
(
:model
)
{
double
(
:model
)
}
let
(
:instance
)
{
double
(
:instance
)
}
let
(
:options
)
{
{
fallback:
true
}
}
let
(
:encrypted
)
do
Gitlab
::
CryptoHelper
.
aes256_gcm_encrypt
(
'my-value'
)
end
subject
do
described_class
.
new
(
model
,
'some_field'
,
options
)
end
describe
'#find_token_authenticatable'
do
it
'finds a relevant resource by encrypted value'
do
allow
(
model
).
to
receive
(
:find_by
)
.
with
(
'some_field_encrypted'
=>
encrypted
)
.
and_return
(
'encrypted resource'
)
expect
(
subject
.
find_token_authenticatable
(
'my-value'
))
.
to
eq
'encrypted resource'
end
it
'uses fallback strategy when token can not be found'
do
allow_any_instance_of
(
TokenAuthenticatableStrategies
::
Insecure
)
.
to
receive
(
:find_token_authenticatable
)
.
and_return
(
'plaintext resource'
)
allow
(
model
).
to
receive
(
:find_by
)
.
with
(
'some_field_encrypted'
=>
encrypted
)
.
and_return
(
nil
)
expect
(
subject
.
find_token_authenticatable
(
'my-value'
))
.
to
eq
'plaintext resource'
end
end
describe
'#get_token'
do
it
'decrypts a token when encrypted token is present'
do
allow
(
instance
).
to
receive
(
:read_attribute
)
.
with
(
'some_field_encrypted'
)
.
and_return
(
encrypted
)
expect
(
subject
.
get_token
(
instance
)).
to
eq
'my-value'
end
it
'reads a plaintext token when encrypted token is not present'
do
allow
(
instance
).
to
receive
(
:read_attribute
)
.
with
(
'some_field_encrypted'
)
.
and_return
(
nil
)
allow
(
instance
).
to
receive
(
:read_attribute
)
.
with
(
'some_field'
)
.
and_return
(
'cleartext value'
)
expect
(
subject
.
get_token
(
instance
)).
to
eq
'cleartext value'
end
end
describe
'#set_token'
do
it
'writes encrypted token to a model instance'
do
expect
(
instance
).
to
receive
(
:[]=
)
.
with
(
'some_field_encrypted'
,
encrypted
)
subject
.
set_token
(
instance
,
'my-value'
)
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