You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.5 KiB

  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe ApplicationController, type: :controller do
  4. controller do
  5. include SignatureVerification
  6. def success
  7. head 200
  8. end
  9. def alternative_success
  10. head 200
  11. end
  12. end
  13. before do
  14. routes.draw { get 'success' => 'anonymous#success' }
  15. end
  16. context 'without signature header' do
  17. before do
  18. get :success
  19. end
  20. describe '#signed_request?' do
  21. it 'returns false' do
  22. expect(controller.signed_request?).to be false
  23. end
  24. end
  25. describe '#signed_request_account' do
  26. it 'returns nil' do
  27. expect(controller.signed_request_account).to be_nil
  28. end
  29. end
  30. end
  31. context 'with signature header' do
  32. let!(:author) { Fabricate(:account) }
  33. before do
  34. get :success
  35. fake_request = Request.new(:get, request.url)
  36. fake_request.on_behalf_of(author)
  37. request.headers.merge!(fake_request.headers)
  38. end
  39. describe '#signed_request?' do
  40. it 'returns true' do
  41. expect(controller.signed_request?).to be true
  42. end
  43. end
  44. describe '#signed_request_account' do
  45. it 'returns an account' do
  46. expect(controller.signed_request_account).to eq author
  47. end
  48. it 'returns nil when path does not match' do
  49. request.path = '/alternative-path'
  50. expect(controller.signed_request_account).to be_nil
  51. end
  52. it 'returns nil when method does not match' do
  53. post :success
  54. expect(controller.signed_request_account).to be_nil
  55. end
  56. end
  57. end
  58. end