File: strategy_spec.rb

package info (click to toggle)
ruby-omniauth 2.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 400 kB
  • sloc: ruby: 2,483; makefile: 7
file content (1145 lines) | stat: -rw-r--r-- 41,033 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
require 'helper'

def make_env(path = '/auth/test', props = {})
  {
    'REQUEST_METHOD' => 'POST',
    'PATH_INFO' => path,
    'rack.session' => {},
    'rack.input' => StringIO.new('test=true')
  }.merge(props)
end

describe OmniAuth::Strategy do
  let(:app) do
    lambda { |_env| [404, {}, ['Awesome']] }
  end

  let(:fresh_strategy) do
    c = Class.new
    c.send(:include, OmniAuth::Strategy)
  end

  describe '.default_options' do
    it 'is inherited from a parent class' do
      superklass = Class.new
      superklass.send :include, OmniAuth::Strategy
      superklass.configure do |c|
        c.foo = 'bar'
      end

      klass = Class.new(superklass)
      expect(klass.default_options.foo).to eq('bar')
    end
  end

  describe 'user_info' do
    it 'should default to an empty hash' do
      expect(fresh_strategy.new(app, :skip_info => true).user_info).to eq({})
    end
  end

  describe '.configure' do
    subject do
      c = Class.new
      c.send(:include, OmniAuth::Strategy)
    end

    context 'when block is passed' do
      it 'allows for default options setting' do
        subject.configure do |c|
          c.wakka = 'doo'
        end
        expect(subject.default_options['wakka']).to eq('doo')
      end

      it "works when block doesn't evaluate to true" do
        environment_variable = nil
        subject.configure do |c|
          c.abc = '123'
          c.hgi = environment_variable
        end
        expect(subject.default_options['abc']).to eq('123')
      end
    end

    it 'takes a hash and deep merge it' do
      subject.configure :abc => {:def => 123}
      subject.configure :abc => {:hgi => 456}
      expect(subject.default_options['abc']).to eq('def' => 123, 'hgi' => 456)
    end
  end

  describe '#fail!' do
    it 'provides exception information when one is provided' do
      env = make_env
      exception = RuntimeError.new('No session!')

      expect(OmniAuth.logger).to receive(:error).with(
        "(test) Authentication failure! failed: #{exception.class}, #{exception.message}"
      )

      ExampleStrategy.new(app, :failure => :failed, :failure_exception => exception).call(env)
    end

    it 'provides a generic message when not provided an exception' do
      env = make_env

      expect(OmniAuth.logger).to receive(:error).with(
        '(test) Authentication failure! Some Issue encountered.'
      )

      ExampleStrategy.new(app, :failure => 'Some Issue').call(env)
    end
  end

  describe '#skip_info?' do
    it 'is true if options.skip_info is true' do
      expect(ExampleStrategy.new(app, :skip_info => true)).to be_skip_info
    end

    it 'is false if options.skip_info is false' do
      expect(ExampleStrategy.new(app, :skip_info => false)).not_to be_skip_info
    end

    it 'is false by default' do
      expect(ExampleStrategy.new(app)).not_to be_skip_info
    end

    it 'is true if options.skip_info is a callable that evaluates to truthy' do
      instance = ExampleStrategy.new(app, :skip_info => lambda { |uid| uid })
      expect(instance).to receive(:uid).and_return(true)
      expect(instance).to be_skip_info
    end
  end

  describe '.option' do
    subject do
      c = Class.new
      c.send(:include, OmniAuth::Strategy)
    end
    it 'sets a default value' do
      subject.option :abc, 123
      expect(subject.default_options.abc).to eq(123)
    end

    it 'sets the default value to nil if none is provided' do
      subject.option :abc
      expect(subject.default_options.abc).to be_nil
    end
  end

  describe '.args' do
    subject do
      c = Class.new
      c.send(:include, OmniAuth::Strategy)
    end

    it 'sets args to the specified argument if there is one' do
      subject.args %i[abc def]
      expect(subject.args).to eq(%i[abc def])
    end

    it 'is inheritable' do
      subject.args %i[abc def]
      c = Class.new(subject)
      expect(c.args).to eq(%i[abc def])
    end

    it 'accepts corresponding options as default arg values' do
      subject.args %i[a b]
      subject.option :a, '1'
      subject.option :b, '2'

      expect(subject.new(nil).options.a).to eq '1'
      expect(subject.new(nil).options.b).to eq '2'
      expect(subject.new(nil, '3', '4').options.b).to eq '4'
      expect(subject.new(nil, nil, '4').options.a).to eq nil
    end
  end

  context 'fetcher procs' do
    subject { fresh_strategy }
    %w[uid info credentials extra].each do |fetcher|
      describe ".#{fetcher}" do
        it 'sets and retrieve a proc' do
          proc = lambda { 'Hello' }
          subject.send(fetcher, &proc)
          expect(subject.send(fetcher)).to eq(proc)
        end
      end
    end
  end

  context 'fetcher stacks' do
    subject { fresh_strategy }
    %w[uid info credentials extra].each do |fetcher|
      describe ".#{fetcher}_stack" do
        it 'is an array of called ancestral procs' do
          fetchy = proc { 'Hello' }
          subject.send(fetcher, &fetchy)
          expect(subject.send("#{fetcher}_stack", subject.new(app))).to eq(['Hello'])
        end
      end
    end
  end

  %w[request_phase].each do |abstract_method|
    context abstract_method.to_s do
      it 'raises a NotImplementedError' do
        strat = Class.new
        strat.send :include, OmniAuth::Strategy
        expect { strat.new(app).send(abstract_method) }.to raise_error(NotImplementedError)
      end
    end
  end

  describe '#auth_hash' do
    subject do
      klass = Class.new
      klass.send :include, OmniAuth::Strategy
      klass.option :name, 'auth_hasher'
      klass
    end
    let(:instance) { subject.new(app) }

    it 'calls through to uid, info, credentials, and extra' do
      expect(instance).to receive(:uid)
      expect(instance).to receive(:info)
      expect(instance).to receive(:credentials).and_return(expires: true).once
      expect(instance).to receive(:extra).and_return(something: 'else').once
      instance.auth_hash
    end

    it 'returns an AuthHash' do
      allow(instance).to receive(:uid).and_return('123')
      allow(instance).to receive(:info).and_return(:name => 'Hal Awesome')
      allow(instance).to receive(:credentials).and_return(expires: true)
      allow(instance).to receive(:extra).and_return(something: 'else')
      hash = instance.auth_hash
      expect(hash).to be_kind_of(OmniAuth::AuthHash)
      expect(hash.uid).to eq('123')
      expect(hash.info.name).to eq('Hal Awesome')
      expect(hash.credentials.expires).to eq(true)
      expect(hash.extra.something).to eq('else')
    end
  end

  describe '#initialize' do
    context 'options extraction' do
      it 'is the last argument if the last argument is a Hash' do
        expect(ExampleStrategy.new(app, :abc => 123).options[:abc]).to eq(123)
      end

      it 'is the default options if any are provided' do
        allow(ExampleStrategy).to receive(:default_options).and_return(OmniAuth::Strategy::Options.new(:abc => 123))
        expect(ExampleStrategy.new(app).options.abc).to eq(123)
      end
    end

    context 'custom args' do
      subject do
        c = Class.new
        c.send(:include, OmniAuth::Strategy)
      end

      it 'sets options based on the arguments if they are supplied' do
        subject.args %i[abc def]
        s = subject.new app, 123, 456
        expect(s.options[:abc]).to eq(123)
        expect(s.options[:def]).to eq(456)
      end
    end
  end

  describe '#call' do
    it 'duplicates and calls' do
      klass = Class.new
      klass.send :include, OmniAuth::Strategy
      instance = klass.new(app)
      expect(instance).to receive(:dup).and_return(instance)
      instance.call('rack.session' => {})
    end

    it 'raises NoSessionError if rack.session isn\'t set' do
      klass = Class.new
      klass.send :include, OmniAuth::Strategy
      instance = klass.new(app)
      expect { instance.call({}) }.to raise_error(OmniAuth::NoSessionError)
    end
  end

  describe '#inspect' do
    it 'returns the class name' do
      expect(ExampleStrategy.new(app).inspect).to eq('#<ExampleStrategy>')
    end
  end

  describe '#redirect' do
    xit 'uses javascript if :iframe is true' do
      response = ExampleStrategy.new(app, :iframe => true).redirect('http://abc.com')
      expected_body = "<script type='text/javascript' charset='utf-8'>top.location.href = 'http://abc.com';</script>"

      expect(response.last).to include(expected_body)
    end
  end

  describe '#callback_phase' do
    subject do
      c = Class.new
      c.send(:include, OmniAuth::Strategy)
      c.new(app)
    end

    it 'sets the auth hash' do
      env = make_env
      allow(subject).to receive(:env).and_return(env)
      allow(subject).to receive(:auth_hash).and_return('AUTH HASH')
      subject.callback_phase
      expect(env['omniauth.auth']).to eq('AUTH HASH')
    end
  end

  describe '#full_host' do
    let(:strategy) { ExampleStrategy.new(app, {}) }
    it 'remains calm when there is a pipe in the URL' do
      strategy.call!(make_env('/whatever', 'rack.url_scheme' => 'http', 'SERVER_NAME' => 'facebook.lame', 'QUERY_STRING' => 'code=asofibasf|asoidnasd', 'SCRIPT_NAME' => '', 'SERVER_PORT' => 80))
      expect { strategy.full_host }.not_to raise_error
    end
  end

  describe '#uid' do
    subject { fresh_strategy }
    it "is the current class's uid if one exists" do
      subject.uid { 'Hi' }
      expect(subject.new(app).uid).to eq('Hi')
    end

    it 'inherits if it can' do
      subject.uid { 'Hi' }
      c = Class.new(subject)
      expect(c.new(app).uid).to eq('Hi')
    end
  end

  %w[info credentials extra].each do |fetcher|
    subject { fresh_strategy }
    it "is the current class's proc call if one exists" do
      subject.send(fetcher) { {:abc => 123} }
      expect(subject.new(app).send(fetcher)).to eq(:abc => 123)
    end

    it 'inherits by merging with preference for the latest class' do
      subject.send(fetcher) { {:abc => 123, :def => 456} }
      c = Class.new(subject)
      c.send(fetcher) { {:abc => 789} }
      expect(c.new(app).send(fetcher)).to eq(:abc => 789, :def => 456)
    end
  end

  describe '#call' do
    before(:all) do
      @options = nil
    end

    let(:strategy) { ExampleStrategy.new(app, @options || {}) }

    context 'omniauth.origin' do
      context 'disabled' do
        it 'does not set omniauth.origin' do
          @options = {:origin_param => false}
          expect(strategy).to receive(:fail!).with('Request Phase', kind_of(StandardError))

          strategy.call(make_env('/auth/test', 'QUERY_STRING' => 'return=/foo'))
          expect(strategy.last_env['rack.session']['omniauth.origin']).to eq(nil)
        end
      end

      context 'custom' do
        it 'sets from a custom param' do
          @options = {:origin_param => 'return'}
          expect(strategy).to receive(:fail!).with('Request Phase', kind_of(StandardError))

          strategy.call(make_env('/auth/test', 'QUERY_STRING' => 'return=/foo'))
          expect(strategy.last_env['rack.session']['omniauth.origin']).to eq('/foo')
        end
      end

      context 'default flow' do
        it 'is set on the request phase' do
          expect(strategy).to receive(:fail!).with("Request Phase", kind_of(StandardError))
          strategy.call(make_env('/auth/test', 'HTTP_REFERER' => 'http://example.com/origin'))

          expect(strategy.last_env['rack.session']['omniauth.origin']).to eq('http://example.com/origin')
        end

        it 'is turned into an env variable on the callback phase' do
          expect(strategy).to receive(:fail!).with("Callback Phase", kind_of(StandardError))
          strategy.call(make_env('/auth/test/callback', 'rack.session' => {'omniauth.origin' => 'http://example.com/origin'}))

          expect(strategy.last_env['omniauth.origin']).to eq('http://example.com/origin')
        end

        it 'sets from the params if provided' do
          expect(strategy).to receive(:fail!).with('Request Phase', kind_of(StandardError))
          strategy.call(make_env('/auth/test', 'QUERY_STRING' => 'origin=/foo'))
          expect(strategy.last_env['rack.session']['omniauth.origin']).to eq('/foo')
        end

        it 'is set on the failure env' do
          expect(OmniAuth.config).to receive(:on_failure).and_return(lambda { |env| env })
          @options = {:failure => :forced_fail}
          strategy.call(make_env('/auth/test/callback', 'rack.session' => {'omniauth.origin' => '/awesome'}))
        end

        context 'with script_name' do
          it 'is set on the request phase, containing full path' do
            env = {'HTTP_REFERER' => 'http://example.com/sub_uri/origin', 'SCRIPT_NAME' => '/sub_uri'}
            expect(strategy).to receive(:fail!).with('Request Phase', kind_of(StandardError))

            strategy.call(make_env('/auth/test', env))
            expect(strategy.last_env['rack.session']['omniauth.origin']).to eq('http://example.com/sub_uri/origin')
          end

          it 'is turned into an env variable on the callback phase, containing full path' do
            env = {
              'rack.session' => {'omniauth.origin' => 'http://example.com/sub_uri/origin'},
              'SCRIPT_NAME' => '/sub_uri'
            }
            expect(strategy).to receive(:fail!).with('Callback Phase', kind_of(StandardError))

            strategy.call(make_env('/auth/test/callback', env))
            expect(strategy.last_env['omniauth.origin']).to eq('http://example.com/sub_uri/origin')
          end
        end
      end
    end

    context 'default paths' do
      it 'uses the default request path' do
        expect(strategy).to receive(:fail!).with('Request Phase', kind_of(StandardError))
        strategy.call(make_env)
      end

      it 'is case insensitive on request path' do
        expect(strategy).to receive(:fail!).with('Request Phase', kind_of(StandardError))
        strategy.call(make_env('/AUTH/Test'))
      end

      it 'is case insensitive on callback path' do
        expect(strategy).to receive(:fail!).with('Callback Phase', kind_of(StandardError))
        strategy.call(make_env('/AUTH/TeSt/CaLlBAck'))
      end

      it 'uses the default callback path' do
        expect(strategy).to receive(:fail!).with('Callback Phase', kind_of(StandardError))
        strategy.call(make_env('/auth/test/callback'))
      end

      it 'strips trailing spaces on request' do
        expect(strategy).to receive(:fail!).with('Request Phase', kind_of(StandardError))
        strategy.call(make_env('/auth/test/'))
      end

      it 'strips trailing spaces on callback' do
        expect(strategy).to receive(:fail!).with('Callback Phase', kind_of(StandardError))
        strategy.call(make_env('/auth/test/callback/'))
      end

      context 'callback_url' do
        it 'uses the default callback_path' do
          expect(strategy).to receive(:full_host).and_return('http://example.com')
          expect(strategy).to receive(:fail!).with('Request Phase', kind_of(StandardError))

          strategy.call(make_env)

          expect(strategy.callback_url).to eq('http://example.com/auth/test/callback')
        end

        it 'preserves the query parameters' do
          allow(strategy).to receive(:full_host).and_return('http://example.com')
          begin
            strategy.call(make_env('/auth/test', 'QUERY_STRING' => 'id=5'))
          rescue RuntimeError
          end
          expect(strategy.callback_url).to eq('http://example.com/auth/test/callback?id=5')
        end

        it 'consider script name' do
          allow(strategy).to receive(:full_host).and_return('http://example.com')
          begin
            strategy.call(make_env('/auth/test', 'SCRIPT_NAME' => '/sub_uri'))
          rescue RuntimeError
          end
          expect(strategy.callback_url).to eq('http://example.com/sub_uri/auth/test/callback')
        end
      end
    end

    context ':form option' do
      it 'calls through to the supplied form option if one exists' do
        strategy.options.form = lambda { |_env| 'Called me!' }
        expect(strategy.call(make_env('/auth/test'))).to eq('Called me!')
      end

      it 'calls through to the app if :form => true is set as an option' do
        strategy.options.form = true
        expect(strategy.call(make_env('/auth/test'))).to eq(app.call(make_env('/auth/test')))
      end
    end

    context 'dynamic paths' do
      it 'runs the request phase if the custom request path evaluator is truthy' do
        @options = {:request_path => lambda { |_env| true }}
        expect(strategy).to receive(:fail!).with('Request Phase', kind_of(StandardError))
        strategy.call(make_env('/asoufibasfi'))
      end

      it 'runs the callback phase if the custom callback path evaluator is truthy' do
        @options = {:callback_path => lambda { |_env| true }}
        expect(strategy).to receive(:fail!).with('Callback Phase', kind_of(StandardError))

        strategy.call(make_env('/asoufiasod'))
      end

      it 'provides a custom callback path if request_path evals to a string' do
        strategy_instance = fresh_strategy.new(nil, :request_path => lambda { |_env| '/auth/boo/callback/22' })
        expect(strategy_instance.callback_path).to eq('/auth/boo/callback/22')
      end

      it 'correctly reports the callback path when the custom callback path evaluator is truthy' do
        strategy_instance = ExampleStrategy.new(app, :callback_path => lambda { |env| env['PATH_INFO'] == '/auth/bish/bosh/callback' })
        expect(strategy_instance).to receive(:fail!).with('Callback Phase', kind_of(StandardError))

        strategy_instance.call(make_env('/auth/bish/bosh/callback'))
        expect(strategy_instance.callback_path).to eq('/auth/bish/bosh/callback')
      end
    end

    context 'custom paths' do
      it 'uses a custom request_path if one is provided' do
        @options = {:request_path => '/awesome'}
        expect(strategy).to receive(:fail!).with('Request Phase', kind_of(StandardError))

        strategy.call(make_env('/awesome'))
      end

      it 'uses a custom callback_path if one is provided' do
        @options = {:callback_path => '/radical'}
        expect(strategy).to receive(:fail!).with('Callback Phase', kind_of(StandardError))

        strategy.call(make_env('/radical'))
      end

      context 'callback_url' do
        it 'uses a custom callback_path if one is provided' do
          @options = {:callback_path => '/radical'}
          expect(strategy).to receive(:full_host).and_return('http://example.com')
          expect(strategy).to receive(:fail!).with('Callback Phase', kind_of(StandardError))

          strategy.call(make_env('/radical'))

          expect(strategy.callback_url).to eq('http://example.com/radical')
        end

        it 'preserves the query parameters' do
          @options = {:callback_path => '/radical'}
          allow(strategy).to receive(:full_host).and_return('http://example.com')
          begin
            strategy.call(make_env('/auth/test', 'QUERY_STRING' => 'id=5'))
          rescue RuntimeError
          end
          expect(strategy.callback_url).to eq('http://example.com/radical?id=5')
        end
      end
    end

    context 'custom prefix' do
      before do
        @options = {:path_prefix => '/wowzers'}
      end

      it 'uses a custom prefix for request' do
        expect(strategy).to receive(:fail!).with('Request Phase', kind_of(StandardError))
        strategy.call(make_env('/wowzers/test'))
      end

      it 'uses a custom prefix for callback' do
        expect(strategy).to receive(:fail!).with('Callback Phase', kind_of(StandardError))
        strategy.call(make_env('/wowzers/test/callback'))
      end

      context 'callback_url' do
        it 'uses a custom prefix' do
          expect(strategy).to receive(:full_host).and_return('http://example.com')
          expect(strategy).to receive(:fail!).with('Request Phase', kind_of(StandardError))
          strategy.call(make_env('/wowzers/test'))

          expect(strategy.callback_url).to eq('http://example.com/wowzers/test/callback')
        end

        it 'preserves the query parameters' do
          allow(strategy).to receive(:full_host).and_return('http://example.com')
          begin
            strategy.call(make_env('/auth/test', 'QUERY_STRING' => 'id=5'))
          rescue RuntimeError
          end
          expect(strategy.callback_url).to eq('http://example.com/wowzers/test/callback?id=5')
        end
      end
    end

    context 'with relative url root' do
      let(:props) { {'SCRIPT_NAME' => '/myapp'} }
      it 'accepts the request' do
        expect(strategy).to receive(:fail!).with('Request Phase', kind_of(StandardError))

        strategy.call(make_env('/auth/test', props))
        expect(strategy.request_path).to eq('/myapp/auth/test')
      end

      it 'accepts the callback' do
        expect(strategy).to receive(:fail!).with('Callback Phase', kind_of(StandardError))

        strategy.call(make_env('/auth/test/callback', props))
      end

      context 'callback_url' do
        it 'redirects to the correctly prefixed callback' do
          expect(strategy).to receive(:full_host).and_return('http://example.com')
          expect(strategy).to receive(:fail!).with('Request Phase', kind_of(StandardError))

          strategy.call(make_env('/auth/test', props))

          expect(strategy.callback_url).to eq('http://example.com/myapp/auth/test/callback')
        end
      end

      context 'custom request' do
        before do
          @options = {:request_path => '/myapp/override', :callback_path => '/myapp/override/callback'}
        end
        it 'does not prefix a custom request path' do
          expect(strategy).to receive(:full_host).and_return('http://example.com')
          expect(strategy).to receive(:fail!).with('Request Phase', kind_of(StandardError))

          expect(strategy.request_path).to eq('/myapp/override')

          strategy.call(make_env('/override', props))

          expect(strategy.callback_url).to eq('http://example.com/myapp/override/callback')
        end
      end

      context 'error during call_app!' do
        class OtherPhaseStrategy < ExampleStrategy
          def other_phase
            call_app!
          end
        end
        class AppError < StandardError; end
        let(:app) { ->(_env) { raise(AppError.new('Some error in the app!')) } }
        let(:strategy) { OtherPhaseStrategy.new(app) }

        it 'raises the application error' do
          expect { strategy.call(make_env('/some/path')) }.to raise_error(AppError, 'Some error in the app!')
        end
      end

      context 'error during auth phase' do
        class SomeStrategy < ExampleStrategy
          def request_phase
            raise AuthError.new('Some error in authentication')
          end
        end
        class AuthError < StandardError; end
        let(:strategy) { SomeStrategy.new(app) }

        it 'passes the error to fail!()' do
          expect(strategy).to receive(:fail!).with('Some error in authentication', kind_of(AuthError))
          strategy.call(make_env('/auth/test', props))
        end
      end
    end


    context 'request method restriction' do
      before(:context) do
        OmniAuth.config.allowed_request_methods = %i[put post]
      end

      it 'does not allow a request method of the wrong type' do
        expect { strategy.call(make_env('/auth/test', 'REQUEST_METHOD' => 'GET')) }.not_to raise_error
      end

      it 'allows a request method of the correct type' do
        expect(strategy).to receive(:fail!).with('Request Phase', kind_of(StandardError))
        strategy.call(make_env('/auth/test'))
      end

      after(:context) do
        OmniAuth.config.allowed_request_methods = %i[post]
      end
    end

    context 'receiving an OPTIONS request' do
      shared_examples_for 'an OPTIONS request' do
        it 'responds with 200' do
          expect(response[0]).to eq(200)
        end

        it 'sets the Allow header properly' do
          expect(response[1]['Allow']).to eq('POST')
        end
      end

      context 'to the request path' do
        let(:response) { strategy.call(make_env('/auth/test', 'REQUEST_METHOD' => 'OPTIONS')) }
        it_behaves_like 'an OPTIONS request'
      end

      context 'to the request path' do
        let(:response) { strategy.call(make_env('/auth/test/callback', 'REQUEST_METHOD' => 'OPTIONS')) }
        it_behaves_like 'an OPTIONS request'
      end

      context 'to some other path' do
        it 'does not short-circuit the request' do
          env = make_env('/other', 'REQUEST_METHOD' => 'OPTIONS')
          expect(strategy.call(env)).to eq(app.call(env))
        end
      end
    end

    context 'options mutation' do
      before do
        @options = {:dup => true}
      end

      context 'in request phase' do
        it 'does not affect original options' do
          @options[:test_option] = true
          @options[:mutate_on_request] = proc { |options| options.delete(:test_option) }

          strategy.call(make_env)
          expect(strategy.options).to have_key(:test_option)
        end

        it 'does not affect deep options' do
          @options[:deep_option] = {:test_option => true}
          @options[:mutate_on_request] = proc { |options| options[:deep_option].delete(:test_option) }

          strategy.call(make_env)
          expect(strategy.options[:deep_option]).to have_key(:test_option)
        end
      end

      context 'in callback phase' do
        it 'does not affect original options' do
          @options[:test_option] = true
          @options[:mutate_on_callback] = proc { |options| options.delete(:test_option) }

          strategy.call(make_env('/auth/test/callback', 'REQUEST_METHOD' => 'POST'))
          expect(strategy.options).to have_key(:test_option)
        end

        it 'does not affect deep options' do
          @options[:deep_option] = {:test_option => true}
          @options[:mutate_on_callback] = proc { |options| options[:deep_option].delete(:test_option) }

          strategy.call(make_env('/auth/test/callback', 'REQUEST_METHOD' => 'POST'))
          expect(strategy.options[:deep_option]).to have_key(:test_option)
        end
      end
    end

    context 'test mode' do
      let(:app) do
        # In test mode, the underlying app shouldn't be called on request phase.
        lambda { |_env| [404, {'Content-Type' => 'text/html'}, []] }
      end

      before do
        OmniAuth.config.test_mode = true
      end

      it 'short circuits the request phase entirely' do
        response = strategy.call(make_env)
        expect(response[0]).to eq(302)
        expect(response[1]['Location']).to eq('/auth/test/callback')
      end

      it "doesn't short circuit the request if request method is not allowed" do
        response = strategy.call(make_env('/auth/test', 'REQUEST_METHOD' => 'DELETE'))
        expect(response[0]).to eq(404)
      end

      it 'is case insensitive on request path' do
        expect(strategy.call(make_env('/AUTH/Test'))[0]).to eq(302)
      end

      it 'respects SCRIPT_NAME (a.k.a. BaseURI)' do
        response = strategy.call(make_env('/auth/test', 'SCRIPT_NAME' => '/sub_uri'))
        expect(response[1]['Location']).to eq('/sub_uri/auth/test/callback')
      end

      it 'redirects on failure' do
        response = OmniAuth.config.on_failure.call(make_env('/auth/test', 'omniauth.error.type' => 'error'))
        expect(response[0]).to eq(302)
        expect(response[1]['Location']).to eq('/auth/failure?message=error')
      end

      it 'respects SCRIPT_NAME (a.k.a. BaseURI) on failure' do
        response = OmniAuth.config.on_failure.call(make_env('/auth/test', 'SCRIPT_NAME' => '/sub_uri', 'omniauth.error.type' => 'error'))
        expect(response[0]).to eq(302)
        expect(response[1]['Location']).to eq('/sub_uri/auth/failure?message=error')
      end

      it 'is case insensitive on callback path' do
        expect(strategy.call(make_env('/AUTH/TeSt/CaLlBAck')).first).to eq(strategy.call(make_env('/auth/test/callback')).first)
      end

      it 'maintains host and port' do
        response = strategy.call(make_env('/auth/test', 'rack.url_scheme' => 'http', 'SERVER_NAME' => 'example.org', 'SERVER_PORT' => 9292))
        expect(response[1]['Location']).to eq('http://example.org:9292/auth/test/callback')
      end

      it 'maintains query string parameters' do
        response = strategy.call(make_env('/auth/test', 'QUERY_STRING' => 'cheese=stilton'))
        expect(response[1]['Location']).to eq('/auth/test/callback?cheese=stilton')
      end

      it 'does not short circuit requests outside of authentication' do
        expect(strategy.call(make_env('/'))).to eq(app.call(make_env('/')))
      end

      it 'responds with the default hash if none is set' do
        OmniAuth.config.mock_auth[:test] = nil

        strategy.call make_env('/auth/test/callback')
        expect(strategy.env['omniauth.auth']['uid']).to eq('1234')
      end

      it 'responds with a provider-specific hash if one is set' do
        OmniAuth.config.mock_auth[:test] = {
          'uid' => 'abc'
        }

        strategy.call make_env('/auth/test/callback')
        expect(strategy.env['omniauth.auth']['uid']).to eq('abc')
      end

      it 'simulates login failure if mocked data is set as a symbol' do
        OmniAuth.config.mock_auth[:test] = :invalid_credentials

        strategy.call make_env('/auth/test/callback')
        expect(strategy.env['omniauth.error.type']).to eq(:invalid_credentials)
      end

      context 'omniauth.origin' do
        context 'disabled' do
          it 'does not set omniauth.origin' do
            @options = {:origin_param => false}
            strategy.call(make_env('/auth/test', 'HTTP_REFERER' => 'http://example.com/origin'))
            expect(strategy.env['rack.session']['omniauth.origin']).to be_nil
          end
        end

        context 'default flow' do
          it 'sets omniauth.origin to the HTTP_REFERER on the request phase by default' do
            strategy.call(make_env('/auth/test', 'HTTP_REFERER' => 'http://example.com/origin'))
            expect(strategy.env['rack.session']['omniauth.origin']).to eq('http://example.com/origin')
          end

          it 'sets omniauth.origin from the params if provided' do
            strategy.call(make_env('/auth/test', 'QUERY_STRING' => 'origin=/foo'))
            expect(strategy.env['rack.session']['omniauth.origin']).to eq('/foo')
          end
        end

        context 'custom' do
          it 'sets omniauth.origin from a custom param' do
            @options = {:origin_param => 'return'}
            strategy.call(make_env('/auth/test', 'QUERY_STRING' => 'return=/foo'))
            expect(strategy.env['rack.session']['omniauth.origin']).to eq('/foo')
          end
        end
      end

      it 'turns omniauth.origin into an env variable on the callback phase' do
        OmniAuth.config.mock_auth[:test] = {}

        strategy.call(make_env('/auth/test/callback', 'rack.session' => {'omniauth.origin' => 'http://example.com/origin'}))
        expect(strategy.env['omniauth.origin']).to eq('http://example.com/origin')
      end

      it 'does not override omniauth.origin if already set on the callback phase' do
        strategy.call(make_env('/auth/test/callback',
          'rack.session' => {'omniauth.origin' => 'http://example.com/origin'},
          'omniauth.origin' => '/foo'))
        expect(strategy.env['omniauth.origin']).to eq('/foo')
        expect(strategy.env['rack.session']['omniauth.origin']).to be_nil
      end

      it 'executes callback hook on the callback phase' do
        OmniAuth.config.mock_auth[:test] = {}
        OmniAuth.config.before_callback_phase do |env|
          env['foobar'] = 'baz'
        end
        strategy.call(make_env('/auth/test/callback', 'rack.session' => {'omniauth.origin' => 'http://example.com/origin'}))
        expect(strategy.env['foobar']).to eq('baz')
      end

      it 'sets omniauth.params with query params on the request phase' do
        OmniAuth.config.mock_auth[:test] = {}

        strategy.call(make_env('/auth/test', 'QUERY_STRING' => 'foo=bar'))
        expect(strategy.env['rack.session']['omniauth.params']).to eq('foo' => 'bar')
      end

      it 'does not set body parameters of POST request on the request phase' do
        OmniAuth.config.mock_auth[:test] = {}

        props = {
          'REQUEST_METHOD' => 'POST',
          'rack.input' => StringIO.new('foo=bar')
        }
        strategy.call(make_env('/auth/test', props))
        expect(strategy.env['rack.session']['omniauth.params']).to eq({})
      end

      it 'executes request hook on the request phase' do
        OmniAuth.config.mock_auth[:test] = {}
        OmniAuth.config.before_request_phase do |env|
          env['foobar'] = 'baz'
        end
        strategy.call(make_env('/auth/test', 'QUERY_STRING' => 'foo=bar'))
        expect(strategy.env['foobar']).to eq('baz')
      end

      it 'executes after request hook on the request phase' do
        OmniAuth.config.mock_auth[:test] = {}
        OmniAuth.config.after_request_phase do |env|
          env['after_request'] = 'executed'
        end
        strategy.call(make_env('/auth/test', 'QUERY_STRING' => 'foo=bar'))
        expect(strategy.env['after_request']).to eq('executed')
      end

      it 'turns omniauth.params into an env variable on the callback phase' do
        OmniAuth.config.mock_auth[:test] = {}

        strategy.call(make_env('/auth/test/callback', 'rack.session' => {'omniauth.params' => {'foo' => 'bar'}}))
        expect(strategy.env['omniauth.params']).to eq('foo' => 'bar')
      end

      it 'rescues errors in request_call' do
        allow(strategy).to receive(:mock_request_call).and_raise(StandardError.new('Oh no'))
        expect(strategy).to receive(:fail!).with('Oh no', kind_of(StandardError))
        strategy.call(make_env)
      end

      after do
        OmniAuth.config.test_mode = false
      end
    end

    context 'custom full_host' do
      before do
        OmniAuth.config.test_mode = true
      end

      it 'is the string when a string is there' do
        OmniAuth.config.full_host = 'my.host.com'
        expect(strategy.full_host).to eq('my.host.com')
      end

      it 'runs the proc with the env when it is a proc' do
        OmniAuth.config.full_host = proc { |env| env['HOST'] }
        strategy.call(make_env('/auth/test', 'HOST' => 'my.host.net'))
        expect(strategy.full_host).to eq('my.host.net')
      end

      it "is based on the request if it's not a string nor a proc" do
        OmniAuth.config.full_host = nil
        strategy.call(make_env('/whatever', 'rack.url_scheme' => 'http', 'SERVER_NAME' => 'my.host.net', 'SERVER_PORT' => 80))
        expect(strategy.full_host).to eq('http://my.host.net')
      end

      it 'honors HTTP_X_FORWARDED_PROTO if present' do
        OmniAuth.config.full_host = nil
        strategy.call(make_env('/whatever', 'HTTP_X_FORWARDED_PROTO' => 'https', 'rack.url_scheme' => 'http', 'SERVER_NAME' => 'my.host.net', 'SERVER_PORT' => 443))
        expect(strategy.full_host).to eq('https://my.host.net')
      end

      after do
        OmniAuth.config.full_host = nil
        OmniAuth.config.test_mode = false
      end
    end

    context 'authenticity validation' do
      let(:app) { lambda { |_env| [200, {}, ['reached our target']] } }
      let(:strategy) { ExampleStrategy.new(app, :request_path => '/auth/test') }
      before do
        OmniAuth.config.request_validation_phase = OmniAuth::AuthenticityTokenProtection
      end

      context 'with default POST only request methods' do
        let!(:csrf_token) { SecureRandom.base64(32) }
        let(:escaped_token) { URI.encode_www_form_component(csrf_token, Encoding::UTF_8) }

        it 'allows a request with matching authenticity_token' do
          expect(strategy).to receive(:fail!).with('Request Phase', kind_of(StandardError))

          post_env = make_env('/auth/test', 'rack.session' => {:csrf => csrf_token}, 'rack.input' => StringIO.new("authenticity_token=#{escaped_token}"))
          strategy.call(post_env)
        end

        it 'does not allow a request without a matching authenticity token' do
          post_env = make_env('/auth/test', 'rack.input' => StringIO.new("authenticity_token=#{escaped_token}"))
          expect(strategy.call(post_env)[0]).to eq(302)
          expect(strategy.call(post_env)[2]).to eq(['302 Moved'])
        end
      end

      context 'with allowed GET' do
        let(:path) { '/auth/test' }
        let(:get_env) { make_env(path, 'REQUEST_METHOD' => 'GET') }

        before(:context) do
          @old_allowed_request_methods = OmniAuth.config.allowed_request_methods
          OmniAuth.config.allowed_request_methods = %i[post get]
        end

        it 'allows a request without authenticity token' do
          expect(strategy).to receive(:fail!).with('Request Phase', kind_of(StandardError))

          strategy.call(get_env)
        end

        describe 'warning message logging' do
          before { allow(strategy).to receive(:log) }

          it 'logs warning message' do
            strategy.call(get_env)
            expect(strategy).to have_received(:log).with(:warn, a_string_matching('You are using GET as an allowed request method')).once
          end

          context 'when not login path is requested' do
            let(:path) { '/example/path' }

            it 'does not log warning message' do
              strategy.call(get_env)
              expect(strategy).not_to have_received(:log).with(:warn, a_string_matching('You are using GET as an allowed request method'))
            end
          end
        end

        after(:context) do
          OmniAuth.config.allowed_request_methods = @old_allowed_request_methods
        end
      end

      context 'with custom allow_if proc' do
        before do
          OmniAuth.config.request_validation_phase = OmniAuth::AuthenticityTokenProtection.new(allow_if: ->(env) { true })
        end

        it 'allows a valid request' do
          expect(strategy).to receive(:fail!).with('Request Phase', kind_of(StandardError))

          post_env = make_env('/auth/test')
          strategy.call(post_env)
        end
      end

      after do
        OmniAuth.config.request_validation_phase = nil
      end
    end

    it 'calls fail! when encountering an unhandled exception' do
      allow(strategy).to receive(:request_phase).and_raise(Errno::ECONNREFUSED)
      expect(strategy).to receive(:fail!).with('Connection refused', kind_of(Errno::ECONNREFUSED))
      strategy.call(make_env)
    end

    it 'redirects to the fail! result when encountering an unhandled exception' do
      OmniAuth.config.test_mode = false
      expect(strategy.call(make_env).first).to eq 302
    end

    context 'when in test mode and path not on request path' do
      let(:path) { '/foo/bar' }

      before do
        OmniAuth.config.test_mode = true
        OmniAuth.config.request_validation_phase = OmniAuth::AuthenticityTokenProtection
        allow(OmniAuth::AuthenticityTokenProtection).to receive(:call).and_raise(OmniAuth::AuthenticityError)
      end

      it 'does not verify token' do
        expect(strategy).not_to receive(:fail!)
        strategy.call(make_env(path))
      end

      after do
        OmniAuth.config.test_mode = false
        OmniAuth.config.request_validation_phase = false
      end
    end
  end

  context 'setup phase' do
    before do
      OmniAuth.config.test_mode = true
    end

    context 'when options[:setup] = true' do
      let(:strategy) do
        ExampleStrategy.new(app, :setup => true)
      end

      let(:app) do
        lambda do |env|
          env['omniauth.strategy'].options[:awesome] = 'sauce' if env['PATH_INFO'] == '/auth/test/setup'
          [404, {}, 'Awesome']
        end
      end

      it 'calls through to /auth/:provider/setup' do
        strategy.call(make_env('/auth/test'))
        expect(strategy.options[:awesome]).to eq('sauce')
      end

      it 'does not call through on a non-omniauth endpoint' do
        strategy.call(make_env('/somewhere/else'))
        expect(strategy.options[:awesome]).not_to eq('sauce')
      end
    end

    context 'when options[:setup] is an app' do
      let(:setup_proc) do
        proc do |env|
          env['omniauth.strategy'].options[:awesome] = 'sauce'
        end
      end

      let(:strategy) { ExampleStrategy.new(app, :setup => setup_proc) }

      it 'does not call the app on a non-omniauth endpoint' do
        strategy.call(make_env('/somehwere/else'))
        expect(strategy.options[:awesome]).not_to eq('sauce')
      end

      it 'calls the rack app' do
        strategy.call(make_env('/auth/test'))
        expect(strategy.options[:awesome]).to eq('sauce')
      end
    end

    after do
      OmniAuth.config.test_mode = false
    end
  end
end