1
+ RSpec . describe Admin ::MemberSearchController , type : :controller do
2
+ let ( :member ) { Fabricate . build ( :member ) }
3
+
4
+ describe 'GET #index' do
5
+ context "when user is not logged in" do
6
+ before do
7
+ get :index
8
+ end
9
+ it "redirects to the home page" do
10
+ expect ( response ) . to redirect_to ( root_path )
11
+ end
12
+ end
13
+
14
+ context "when user is an admin" do
15
+ let ( :fake_relation ) { instance_double ( 'ActiveRecord::Relation' ) }
16
+ let ( :fake_juliet ) { instance_double ( 'Member' , id : 1 , name : 'Juliet' , surname : 'Montague' ) }
17
+
18
+ before do
19
+ login_as_admin ( member )
20
+ get :index
21
+ end
22
+ it "shows user the search page" do
23
+ expect ( response ) . to have_http_status ( :ok )
24
+ end
25
+
26
+ context "and when admin user searches for a single existing user" do
27
+ before do
28
+ allow ( Member ) . to receive ( :find_members ) . with ( 'Juliet' ) . and_return ( fake_relation )
29
+ allow ( fake_relation ) . to receive ( :select ) . with ( any_args ) . and_return ( [ fake_juliet ] )
30
+ get :index , params : { member_search : { name : "Juliet" , callback : root_path } }
31
+ end
32
+ it "redirects to the calling service" do
33
+ expect ( response ) . to have_http_status ( :found )
34
+
35
+ uri = URI . parse ( response . location )
36
+ redirect_params = Rack ::Utils . parse_nested_query ( uri . query )
37
+
38
+ expect ( redirect_params [ "member_pick" ] [ "members" ] ) . to eq ( [ "1" ] )
39
+ end
40
+ end
41
+
42
+ context "and when an admin user searches and there are multiple results" do
43
+ let ( :fake_romeo ) { double ( 'Member' , id : 2 , name : 'Romeo' , surname : 'Capulet' ) }
44
+
45
+ before do
46
+ allow ( Member ) . to receive ( :find_members ) . with ( 'e' ) . and_return ( fake_relation )
47
+ allow ( fake_relation ) . to receive ( :select ) . with ( any_args ) . and_return ( [ fake_juliet , fake_romeo ] )
48
+ get :index , params : { member_search : { name : 'e' , callback : root_path } }
49
+ end
50
+ it "presents the found members on the index page" do
51
+ expect ( response ) . to have_http_status ( :ok )
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
0 commit comments