Shopify_app: When shopify_app used in automated tests - user_agent_can_partition_cookies fails with exception NoMethodError: undefined method `match' for nil:NilClass

Created on 20 Nov 2018  路  5Comments  路  Source: Shopify/shopify_app

Steps to repeat:

  1. Create the rspec test which calls the fullpage_redirect_to charge.confirmation_url to simulate the redirect to Charge Confirmation URL.
  2. See that the test fails with the following error:
NoMethodError: undefined method `match' for nil:NilClass

NoMethodError:
       undefined method `match' for nil:NilClass
     # /Users/maris/.rvm/gems/ruby-2.4.4/gems/shopify_app-8.4.0/lib/shopify_app/controller_concerns/itp.rb:38:in `user_agent_can_partition_cookies'
     # /Users/maris/.rvm/gems/ruby-2.4.4/gems/shopify_app-8.4.0/lib/shopify_app/controller_concerns/itp.rb:10:in `set_test_cookie'
     # ./spec/controllers/charge_controller_spec.rb:22:in `block (3 levels) in <top (required)>'

Code which fails

File: shopify_app-8.4.0/lib/shopify_app/controller_concerns/itp.rb:38:in 'user_agent_can_partition_cookies'

    def user_agent_can_partition_cookies
      user_agent = BrowserSniffer.new(request.user_agent).browser_info

      is_safari = user_agent[:name].match(/Safari/)

      return false unless is_safari

      user_agent[:version].match(/12.0/)
    end

Explanation:

During the tests, the user_agent is an empty Hash.
So user_agent[:name] produces nil result.

Suggested change:

    def user_agent_can_partition_cookies
      user_agent = BrowserSniffer.new(request.user_agent).browser_info

      is_safari = user_agent[:name]&.match(/Safari/)

      return false unless is_safari

      user_agent[:version]&.match(/12.0/)
    end

Explanation:

Guard against user_agent[:name] being nil.

Most helpful comment

@tylerball - PR created: https://github.com/Shopify/shopify_app/pull/675
Made it a bit different than initially thought, this should be safer.

All 5 comments

Hi @marisveide, thanks for reporting. We would happily merge a PR for this, if not I can make the change later this week.

@tylerball - PR created: https://github.com/Shopify/shopify_app/pull/675
Made it a bit different than initially thought, this should be safer.

Looks like this bug must have been recently introduced. I pinned my version to 8.3.2 and my tests are passing now.

Hi
I'm getting this error too.
Any idea when we'll have a release with this fix?

This should be resolved as soon as we release version 8.4.1.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MisinformedDNA picture MisinformedDNA  路  6Comments

robbyklein picture robbyklein  路  4Comments

matias-eduardo picture matias-eduardo  路  6Comments

wlbrough picture wlbrough  路  6Comments

charlesemarsh picture charlesemarsh  路  6Comments