123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- # frozen_string_literal: true
- require 'rails_helper'
- describe ApplicationController, type: :controller do
- controller do
- include SignatureVerification
- def success
- head 200
- end
- def alternative_success
- head 200
- end
- end
- before do
- routes.draw { match via: [:get, :post], 'success' => 'anonymous#success' }
- end
- context 'without signature header' do
- before do
- get :success
- end
- describe '#signed_request?' do
- it 'returns false' do
- expect(controller.signed_request?).to be false
- end
- end
- describe '#signed_request_account' do
- it 'returns nil' do
- expect(controller.signed_request_account).to be_nil
- end
- end
- end
- context 'with signature header' do
- let!(:author) { Fabricate(:account) }
- context 'without body' do
- before do
- get :success
- fake_request = Request.new(:get, request.url)
- fake_request.on_behalf_of(author)
- request.headers.merge!(fake_request.headers)
- end
- describe '#signed_request?' do
- it 'returns true' do
- expect(controller.signed_request?).to be true
- end
- end
- describe '#signed_request_account' do
- it 'returns an account' do
- expect(controller.signed_request_account).to eq author
- end
- it 'returns nil when path does not match' do
- request.path = '/alternative-path'
- expect(controller.signed_request_account).to be_nil
- end
- it 'returns nil when method does not match' do
- post :success
- expect(controller.signed_request_account).to be_nil
- end
- end
- end
- context 'with body' do
- before do
- post :success, body: 'Hello world'
- fake_request = Request.new(:post, request.url, body: 'Hello world')
- fake_request.on_behalf_of(author)
- request.headers.merge!(fake_request.headers)
- end
- describe '#signed_request?' do
- it 'returns true' do
- expect(controller.signed_request?).to be true
- end
- end
- describe '#signed_request_account' do
- it 'returns an account' do
- expect(controller.signed_request_account).to eq author
- end
- it 'returns nil when path does not match' do
- request.path = '/alternative-path'
- expect(controller.signed_request_account).to be_nil
- end
- it 'returns nil when method does not match' do
- get :success
- expect(controller.signed_request_account).to be_nil
- end
- it 'returns nil when body has been tampered' do
- request.headers['RAW_POST_DATA'] = 'doo doo doo'
- expect(controller.signed_request_account).to be_nil
- end
- end
- end
- end
- end
|