Skip to content

Commit

Permalink
Serialize updated at without to s (lynndylanhurley#1423)
Browse files Browse the repository at this point in the history
* working 4.2 version

* cleanup

* Lock simplecov to last supported cc version

codeclimate/test-reporter#413

Co-authored-by: Brian Dunn <[email protected]>
  • Loading branch information
MaicolBen and briandunn authored Oct 4, 2020
1 parent bb3e3f0 commit 0c369d6
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 21 deletions.
20 changes: 16 additions & 4 deletions app/models/devise_token_auth/concerns/tokens_serialization.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
module DeviseTokenAuth::Concerns::TokensSerialization
extend self
# Serialization hash to json
def self.dump(object)
object.each_value(&:compact!) unless object.nil?
JSON.generate(object)
def dump(object)
JSON.generate(object && object.transform_values do |token|
serialize_updated_at(token).compact
end.compact)
end

# Deserialization json to hash
def self.load(json)
def load(json)
case json
when String
JSON.parse(json)
Expand All @@ -16,4 +18,14 @@ def self.load(json)
json
end
end

private

def serialize_updated_at(token)
updated_at_key = ['updated_at', :updated_at].find(&token.method(:[]))

return token unless token[updated_at_key].respond_to?(:iso8601)

token.merge updated_at_key => token[updated_at_key].iso8601
end
end
4 changes: 2 additions & 2 deletions app/models/devise_token_auth/concerns/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def create_new_auth_token(client = nil)
token = create_token(
client: client,
last_token: tokens.fetch(client, {})['token'],
updated_at: now.to_s(:rfc822)
updated_at: now
)

update_auth_header(token.token, token.client)
Expand Down Expand Up @@ -194,7 +194,7 @@ def build_auth_url(base_url, args)
end

def extend_batch_buffer(token, client)
tokens[client]['updated_at'] = Time.zone.now.to_s(:rfc822)
tokens[client]['updated_at'] = Time.zone.now
update_auth_header(token, client)
end

Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_4_2.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ end

group :test do
gem "rails-controller-testing"
gem "simplecov", require: false
gem "simplecov", "< 0.18", require: false
end

group :development do
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_4_2_mongoid_5.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ end

group :test do
gem "rails-controller-testing"
gem "simplecov", require: false
gem "simplecov", "< 0.18", require: false
end

group :development do
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_5_0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ end

group :test do
gem "rails-controller-testing"
gem "simplecov", require: false
gem "simplecov", "< 0.18", require: false
end

group :development do
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_5_1_mongoid_6.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ end

group :test do
gem "rails-controller-testing"
gem "simplecov", require: false
gem "simplecov", "< 0.18", require: false
end

group :development do
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_5_2.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ end

group :test do
gem "rails-controller-testing"
gem "simplecov", require: false
gem "simplecov", "< 0.18", require: false
end

group :development do
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_5_2_mongoid_6.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ end

group :test do
gem "rails-controller-testing"
gem "simplecov", require: false
gem "simplecov", "< 0.18", require: false
end

group :development do
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_5_2_mongoid_7.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ end

group :test do
gem "rails-controller-testing"
gem "simplecov", require: false
gem "simplecov", "< 0.18", require: false
end

group :development do
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_6_0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ end

group :test do
gem "rails-controller-testing"
gem "simplecov", require: false
gem "simplecov", "< 0.18", require: false
end

group :development do
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_6_0_mongoid_7.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ end

group :test do
gem "rails-controller-testing"
gem "simplecov", require: false
gem "simplecov", "< 0.18", require: false
end

group :development do
Expand Down
44 changes: 39 additions & 5 deletions test/models/concerns/tokens_serialization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@

user.tokens
end
let(:json) { JSON.generate(tokens) }

it 'is defined' do
assert_equal(ts.present?, true)
assert_kind_of(Module, ts)
end

describe '.load(json)' do

let(:json) { JSON.generate(tokens) }

let(:default) { {} }

it 'is defined' do
Expand Down Expand Up @@ -55,16 +57,48 @@
assert_equal(ts.dump({}), '{}')
end

it 'deserialize tokens' do
assert_equal(ts.dump(tokens), json)
end

it 'removes nil values' do
new_tokens = tokens.dup
new_tokens[new_tokens.first[0]][:kos] = nil

assert_equal(ts.dump(tokens), ts.dump(new_tokens))
end

describe 'updated_at' do
before do
@default_format = ::Time::DATE_FORMATS[:default]
::Time::DATE_FORMATS[:default] = 'imprecise format'
end

after do
::Time::DATE_FORMATS[:default] = @default_format
end

def updated_ats(tokens)
tokens.
values.
flat_map do |token|
[:updated_at, 'updated_at'].map do |key|
token[key]
end
end.
compact
end

it 'is defined' do
refute_empty updated_ats(tokens)
end

it 'uses iso8601' do
updated_ats(JSON.parse(ts.dump(tokens))).each do |updated_at|
Time.strptime(updated_at, '%Y-%m-%dT%H:%M:%SZ')
end
end

it 'does not rely on Time#to_s' do
refute_includes(updated_ats(tokens), 'imprecise format')
end
end
end
end
end
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ActiveSupport::TestCase

def age_token(user, client_id)
if user.tokens[client_id]
user.tokens[client_id]['updated_at'] = (Time.zone.now - (DeviseTokenAuth.batch_request_buffer_throttle + 10.seconds)).to_s(:rfc822)
user.tokens[client_id]['updated_at'] = (Time.zone.now - (DeviseTokenAuth.batch_request_buffer_throttle + 10.seconds))
user.save!
end
end
Expand Down

0 comments on commit 0c369d6

Please sign in to comment.