macmillan-utils 1.0.30 → 1.0.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/macmillan/utils/middleware/uuid.rb +19 -17
- data/spec/lib/macmillan/utils/middleware/uuid_spec.rb +22 -0
- metadata +2 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 7ae6079821a9d8aeea074790ab1ae354fa3b6554
         | 
| 4 | 
            +
              data.tar.gz: e444d8c35eb98e1156f91c04559bfda77fc063d0
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: d3a50baa236a6b1b7feb48bd270cfd676101c1967cda53a2fb6fb2f0857811888393225eb03987e56bd7c4b053b18afaa45225693b1def2a62467763911b1c5a
         | 
| 7 | 
            +
              data.tar.gz: f62ee633295943b1aa944d6da3e87605038977096ad667dc57182dbdeed4db24c68b4f5796303fd8e3db779e698052e3bb9af02f7d2bc871580adb501efd506e
         | 
| @@ -22,7 +22,7 @@ module Macmillan | |
| 22 22 | 
             
                    end
         | 
| 23 23 |  | 
| 24 24 | 
             
                    class CallHandler
         | 
| 25 | 
            -
                      attr_reader :app, :request, :user_env_key, :user_id_method, :cookie_key
         | 
| 25 | 
            +
                      attr_reader :app, :request, :user_env_key, :user_id_method, :cookie_key, :rack_errors, :uuid_is_new_key
         | 
| 26 26 |  | 
| 27 27 | 
             
                      def initialize(env, app, user_env_key, user_id_method, cookie_key)
         | 
| 28 28 | 
             
                        @app            = app
         | 
| @@ -30,10 +30,21 @@ module Macmillan | |
| 30 30 | 
             
                        @user_env_key   = user_env_key
         | 
| 31 31 | 
             
                        @user_id_method = user_id_method
         | 
| 32 32 | 
             
                        @cookie_key     = cookie_key
         | 
| 33 | 
            +
                        @rack_errors    = env['rack.errors']
         | 
| 34 | 
            +
                        @uuid_is_new_key = "#{cookie_key}_is_new"
         | 
| 33 35 |  | 
| 34 | 
            -
                        env[cookie_key] | 
| 36 | 
            +
                        env[cookie_key]      = final_user_uuid
         | 
| 37 | 
            +
                        env[uuid_is_new_key] = true if uuid_is_new?
         | 
| 35 38 | 
             
                      end
         | 
| 36 39 |  | 
| 40 | 
            +
                      def finish
         | 
| 41 | 
            +
                        save_cookie if store_cookie?
         | 
| 42 | 
            +
                        clean_old_cookies
         | 
| 43 | 
            +
                        response.finish
         | 
| 44 | 
            +
                      end
         | 
| 45 | 
            +
             | 
| 46 | 
            +
                      private
         | 
| 47 | 
            +
             | 
| 37 48 | 
             
                      def response
         | 
| 38 49 | 
             
                        @response ||= begin
         | 
| 39 50 | 
             
                                        status, headers, body = app.call(request.env)
         | 
| @@ -41,26 +52,16 @@ module Macmillan | |
| 41 52 | 
             
                                      end
         | 
| 42 53 | 
             
                      end
         | 
| 43 54 |  | 
| 44 | 
            -
                      def finish
         | 
| 45 | 
            -
                        save_cookie if store_cookie?
         | 
| 46 | 
            -
                        clean_old_cookies
         | 
| 47 | 
            -
                        response.finish
         | 
| 48 | 
            -
                      end
         | 
| 49 | 
            -
             | 
| 50 55 | 
             
                      def user
         | 
| 51 56 | 
             
                        request.env[user_env_key]
         | 
| 52 57 | 
             
                      end
         | 
| 53 58 |  | 
| 59 | 
            +
                      def user_hexdigest
         | 
| 60 | 
            +
                        Digest::SHA1.hexdigest(user.public_send(user_id_method).to_s) if user
         | 
| 61 | 
            +
                      end
         | 
| 62 | 
            +
             | 
| 54 63 | 
             
                      def final_user_uuid
         | 
| 55 | 
            -
                        @final_user_uuid ||=  | 
| 56 | 
            -
                                         if user
         | 
| 57 | 
            -
                                           Digest::SHA1.hexdigest(user.public_send(user_id_method).to_s)
         | 
| 58 | 
            -
                                         elsif uuid_from_cookies
         | 
| 59 | 
            -
                                           uuid_from_cookies
         | 
| 60 | 
            -
                                         else
         | 
| 61 | 
            -
                                           SecureRandom.uuid
         | 
| 62 | 
            -
                                         end
         | 
| 63 | 
            -
                                       end
         | 
| 64 | 
            +
                        @final_user_uuid ||= user_hexdigest || uuid_from_cookies || SecureRandom.uuid
         | 
| 64 65 | 
             
                      end
         | 
| 65 66 |  | 
| 66 67 | 
             
                      def uuid_from_cookies
         | 
| @@ -70,6 +71,7 @@ module Macmillan | |
| 70 71 | 
             
                      def store_cookie?
         | 
| 71 72 | 
             
                        final_user_uuid != uuid_from_cookies
         | 
| 72 73 | 
             
                      end
         | 
| 74 | 
            +
                      alias_method :uuid_is_new?, :store_cookie?
         | 
| 73 75 |  | 
| 74 76 | 
             
                      def save_cookie
         | 
| 75 77 | 
             
                        cookie_value = { value: final_user_uuid, path: '/', expires: DateTime.now.next_year.to_time }
         | 
| @@ -24,6 +24,11 @@ RSpec.describe Macmillan::Utils::Middleware::Uuid do | |
| 24 24 | 
             
                    expect(app).to receive(:call).with(hash_including('user.uuid' => user_uuid)).and_return(app_return)
         | 
| 25 25 | 
             
                    _status, _headers, _body = subject.call(request.env)
         | 
| 26 26 | 
             
                  end
         | 
| 27 | 
            +
             | 
| 28 | 
            +
                  it 'tells following rack app the user_uuid is new' do
         | 
| 29 | 
            +
                    expect(app).to receive(:call).with(hash_including('user.uuid_is_new' => true)).and_return(app_return)
         | 
| 30 | 
            +
                    _status, _headers, _body = subject.call(request.env)
         | 
| 31 | 
            +
                  end
         | 
| 27 32 | 
             
                end
         | 
| 28 33 |  | 
| 29 34 | 
             
                context 'who also has a randomly assigned user_uuid cookie (from a previous non-authenticated session)' do
         | 
| @@ -35,6 +40,11 @@ RSpec.describe Macmillan::Utils::Middleware::Uuid do | |
| 35 40 | 
             
                    _status, headers, _body = subject.call(request.env)
         | 
| 36 41 | 
             
                    expect(headers['Set-Cookie']).to include("user.uuid=#{user_uuid}")
         | 
| 37 42 | 
             
                  end
         | 
| 43 | 
            +
             | 
| 44 | 
            +
                  it 'tells following rack app the user_uuid is new' do
         | 
| 45 | 
            +
                    expect(app).to receive(:call).with(hash_including('user.uuid_is_new' => true)).and_return(app_return)
         | 
| 46 | 
            +
                    _status, _headers, _body = subject.call(request.env)
         | 
| 47 | 
            +
                  end
         | 
| 38 48 | 
             
                end
         | 
| 39 49 | 
             
              end
         | 
| 40 50 |  | 
| @@ -54,6 +64,11 @@ RSpec.describe Macmillan::Utils::Middleware::Uuid do | |
| 54 64 | 
             
                    _status, headers, _body = subject.call(request.env)
         | 
| 55 65 | 
             
                    expect(headers['Set-Cookie']).to include('user.uuid=wibble')
         | 
| 56 66 | 
             
                  end
         | 
| 67 | 
            +
             | 
| 68 | 
            +
                  it 'tells following rack app the user_uuid is new' do
         | 
| 69 | 
            +
                    expect(app).to receive(:call).with(hash_including('user.uuid_is_new' => true)).and_return(app_return)
         | 
| 70 | 
            +
                    _status, _headers, _body = subject.call(request.env)
         | 
| 71 | 
            +
                  end
         | 
| 57 72 | 
             
                end
         | 
| 58 73 |  | 
| 59 74 | 
             
                context 'who has visited before and has a user_uuid cookie' do
         | 
| @@ -70,6 +85,13 @@ RSpec.describe Macmillan::Utils::Middleware::Uuid do | |
| 70 85 | 
             
                    _status, headers, _body = subject.call(request.env)
         | 
| 71 86 | 
             
                    expect(headers['Set-Cookie']).to be_nil
         | 
| 72 87 | 
             
                  end
         | 
| 88 | 
            +
             | 
| 89 | 
            +
                  it 'does not tell following rack app the user_uuid is new' do
         | 
| 90 | 
            +
                    expect(app).to receive(:call) do |args|
         | 
| 91 | 
            +
                      expect(args).not_to have_key('user.uuid_is_new')
         | 
| 92 | 
            +
                    end.and_return(app_return)
         | 
| 93 | 
            +
                    _status, _headers, _body = subject.call(request.env)
         | 
| 94 | 
            +
                  end
         | 
| 73 95 | 
             
                end
         | 
| 74 96 | 
             
              end
         | 
| 75 97 | 
             
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: macmillan-utils
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 1.0. | 
| 4 | 
            +
              version: 1.0.31
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Springer Nature
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2016-08- | 
| 11 | 
            +
            date: 2016-08-22 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: bundler
         |