From 0c369d6338a362c1e888378b46e8251d8e5de947 Mon Sep 17 00:00:00 2001 From: Maicol Bentancor Date: Sun, 4 Oct 2020 17:34:44 -0300 Subject: [PATCH] Serialize updated at without to s (#1423) * working 4.2 version * cleanup * Lock simplecov to last supported cc version https://github.com/codeclimate/test-reporter/issues/413 Co-authored-by: Brian Dunn --- .../concerns/tokens_serialization.rb | 20 +++++++-- app/models/devise_token_auth/concerns/user.rb | 4 +- gemfiles/rails_4_2.gemfile | 2 +- gemfiles/rails_4_2_mongoid_5.gemfile | 2 +- gemfiles/rails_5_0.gemfile | 2 +- gemfiles/rails_5_1_mongoid_6.gemfile | 2 +- gemfiles/rails_5_2.gemfile | 2 +- gemfiles/rails_5_2_mongoid_6.gemfile | 2 +- gemfiles/rails_5_2_mongoid_7.gemfile | 2 +- gemfiles/rails_6_0.gemfile | 2 +- gemfiles/rails_6_0_mongoid_7.gemfile | 2 +- .../concerns/tokens_serialization_test.rb | 44 ++++++++++++++++--- test/test_helper.rb | 2 +- 13 files changed, 67 insertions(+), 21 deletions(-) diff --git a/app/models/devise_token_auth/concerns/tokens_serialization.rb b/app/models/devise_token_auth/concerns/tokens_serialization.rb index 5e6cc1c6d..d5e376c74 100644 --- a/app/models/devise_token_auth/concerns/tokens_serialization.rb +++ b/app/models/devise_token_auth/concerns/tokens_serialization.rb @@ -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) @@ -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 diff --git a/app/models/devise_token_auth/concerns/user.rb b/app/models/devise_token_auth/concerns/user.rb index d9ec2361e..1a87524b3 100644 --- a/app/models/devise_token_auth/concerns/user.rb +++ b/app/models/devise_token_auth/concerns/user.rb @@ -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) @@ -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 diff --git a/gemfiles/rails_4_2.gemfile b/gemfiles/rails_4_2.gemfile index 1e3131f8a..df4846b8c 100644 --- a/gemfiles/rails_4_2.gemfile +++ b/gemfiles/rails_4_2.gemfile @@ -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 diff --git a/gemfiles/rails_4_2_mongoid_5.gemfile b/gemfiles/rails_4_2_mongoid_5.gemfile index 10acd6238..083bba80b 100644 --- a/gemfiles/rails_4_2_mongoid_5.gemfile +++ b/gemfiles/rails_4_2_mongoid_5.gemfile @@ -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 diff --git a/gemfiles/rails_5_0.gemfile b/gemfiles/rails_5_0.gemfile index 8984b2e08..4f009a808 100644 --- a/gemfiles/rails_5_0.gemfile +++ b/gemfiles/rails_5_0.gemfile @@ -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 diff --git a/gemfiles/rails_5_1_mongoid_6.gemfile b/gemfiles/rails_5_1_mongoid_6.gemfile index a567de647..b875adf55 100644 --- a/gemfiles/rails_5_1_mongoid_6.gemfile +++ b/gemfiles/rails_5_1_mongoid_6.gemfile @@ -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 diff --git a/gemfiles/rails_5_2.gemfile b/gemfiles/rails_5_2.gemfile index 256915322..791f8825b 100644 --- a/gemfiles/rails_5_2.gemfile +++ b/gemfiles/rails_5_2.gemfile @@ -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 diff --git a/gemfiles/rails_5_2_mongoid_6.gemfile b/gemfiles/rails_5_2_mongoid_6.gemfile index 14bad1c02..13cc0599c 100644 --- a/gemfiles/rails_5_2_mongoid_6.gemfile +++ b/gemfiles/rails_5_2_mongoid_6.gemfile @@ -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 diff --git a/gemfiles/rails_5_2_mongoid_7.gemfile b/gemfiles/rails_5_2_mongoid_7.gemfile index 4bf9cb494..261ace014 100644 --- a/gemfiles/rails_5_2_mongoid_7.gemfile +++ b/gemfiles/rails_5_2_mongoid_7.gemfile @@ -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 diff --git a/gemfiles/rails_6_0.gemfile b/gemfiles/rails_6_0.gemfile index e55f37354..2a3ec791c 100644 --- a/gemfiles/rails_6_0.gemfile +++ b/gemfiles/rails_6_0.gemfile @@ -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 diff --git a/gemfiles/rails_6_0_mongoid_7.gemfile b/gemfiles/rails_6_0_mongoid_7.gemfile index 3086df630..8b6bbca87 100644 --- a/gemfiles/rails_6_0_mongoid_7.gemfile +++ b/gemfiles/rails_6_0_mongoid_7.gemfile @@ -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 diff --git a/test/models/concerns/tokens_serialization_test.rb b/test/models/concerns/tokens_serialization_test.rb index 388ec4c86..901e056c3 100644 --- a/test/models/concerns/tokens_serialization_test.rb +++ b/test/models/concerns/tokens_serialization_test.rb @@ -13,7 +13,6 @@ user.tokens end - let(:json) { JSON.generate(tokens) } it 'is defined' do assert_equal(ts.present?, true) @@ -21,6 +20,9 @@ end describe '.load(json)' do + + let(:json) { JSON.generate(tokens) } + let(:default) { {} } it 'is defined' do @@ -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 diff --git a/test/test_helper.rb b/test/test_helper.rb index 37ab874a0..a5bd24834 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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