Sha256: f15bc2c9cfdae59277ba4d2d0aeded646547eb690cb78f7796544cb4f84b63ac

Contents?: true

Size: 1.18 KB

Versions: 4

Compression:

Stored size: 1.18 KB

Contents

require 'test_helper'
require 'action_controller'
require 'action_controller/base'

class WebhooksController < ActionController::Base
  include ShopifyApp::WebhooksController
  def carts_update
    head :ok
  end
end

class WebhooksControllerTest < ActionController::TestCase
  tests WebhooksController

  setup do
    ShopifyApp::SessionRepository.storage = InMemorySessionStore
    ShopifyApp.configure do |config|
      config.secret = 'secret'
    end
  end

  test "#carts_update should verify request" do
    with_application_test_routes do
      data = {foo: :bar}.to_json
      @controller.expects(:validate_hmac).with('secret', data.to_s).returns(true)
      post :carts_update, data
      assert_response :ok
    end
  end

  test "un-verified request returns unauthorized" do
    with_application_test_routes do
      data = {foo: :bar}.to_json
      @controller.expects(:validate_hmac).with('secret', data.to_s).returns(false)
      post :carts_update, data
      assert_response :unauthorized
    end
  end

  private

  def with_application_test_routes
    with_routing do |set|
      set.draw do
        get '/carts_update' => 'webhooks#carts_update'
      end
      yield
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
shopify_app-6.4.2 test/shopify_app/webhooks_controller_test.rb
shopify_app-6.4.1 test/shopify_app/webhooks_controller_test.rb
shopify_app-6.4.0 test/shopify_app/webhooks_controller_test.rb
shopify_app-6.3.0 test/shopify_app/webhooks_controller_test.rb