@@ -470,4 +470,70 @@ def get(action, params)
470470 )
471471 end
472472 end
473+
474+ describe 'back_or_index' do
475+ before do
476+ allow ( controller ) . to receive ( :index_path ) . and_return ( index_path )
477+ end
478+
479+ let ( :index_path ) { '/' }
480+
481+ it 'returns back to index when return_to is not defined' do
482+ controller . params = { }
483+ expect ( controller . send ( :back_or_index ) ) . to eq ( index_path )
484+ end
485+
486+ it 'returns back to return_to url when it starts with same protocol and host' do
487+ return_to_url = "http://#{ request . host } /teams"
488+ controller . params = { return_to : return_to_url }
489+ expect ( controller . send ( :back_or_index ) ) . to eq ( return_to_url )
490+ end
491+
492+ it 'returns back to return_to url when it contains a path' do
493+ return_to_url = '/teams'
494+ controller . params = { return_to : return_to_url }
495+ expect ( controller . send ( :back_or_index ) ) . to eq ( return_to_url )
496+ end
497+
498+ it 'returns back to index path when return_to path does not start with slash' do
499+ return_to_url = 'teams'
500+ controller . params = { return_to : return_to_url }
501+ expect ( controller . send ( :back_or_index ) ) . to eq ( index_path )
502+ end
503+
504+ it 'returns back to index path when return_to url does not start with full protocol' do
505+ return_to_url = "#{ request . host } /teams"
506+ controller . params = { return_to : return_to_url }
507+ expect ( controller . send ( :back_or_index ) ) . to eq ( index_path )
508+ end
509+
510+ it 'returns back to index path when return_to url starts with double slash' do
511+ return_to_url = "//#{ request . host } /teams"
512+ controller . params = { return_to : return_to_url }
513+ expect ( controller . send ( :back_or_index ) ) . to eq ( index_path )
514+ end
515+
516+ it 'returns back to index path when return_to url starts with tripple slash' do
517+ return_to_url = "///#{ request . host } /teams"
518+ controller . params = { return_to : return_to_url }
519+ expect ( controller . send ( :back_or_index ) ) . to eq ( index_path )
520+ end
521+
522+ it 'returns back to index path when return_to url does not have host' do
523+ return_to_url = 'http:///teams'
524+ controller . params = { return_to : return_to_url }
525+ expect ( controller . send ( :back_or_index ) ) . to eq ( index_path )
526+ end
527+
528+ it 'returns back to index path when return_to url starts with different protocol' do
529+ return_to_url = "other://#{ request . host } /teams"
530+ controller . params = { return_to : return_to_url }
531+ expect ( controller . send ( :back_or_index ) ) . to eq ( index_path )
532+ end
533+
534+ it 'returns back to index path when return_to does not start with the same protocol and host' do
535+ controller . params = { return_to : "http://google.com?#{ request . host } " }
536+ expect ( controller . send ( :back_or_index ) ) . to eq ( index_path )
537+ end
538+ end
473539end
0 commit comments