require 'spec_helper' describe List do let(:employee_password){'secret1'} let(:employee) { create :employee, email: 'supplier@mozo.bar', password: employee_password, confirmation_token: 'abc', confirmed_at: Time.now.utc} let(:supplier) { create :supplier, open: true } let(:user) { create :user } let(:section) { create :section, supplier: supplier} let(:table) { create :table, supplier: supplier} let(:list_options){ {supplier: supplier, table: table, section: section, user_ids: [user.id]} } let(:list){ create :list, list_options} describe 'join requests' do describe '#send_table_join_request_for_user' do it "does not add an existing user to join_request_user_ids" do list.send_table_join_request_for_user! user expect(list.join_request_user_ids).not_to include user.id end it "adds a user to join_request_user_ids" do other_user = create :user list.send_table_join_request_for_user! other_user # test through persistance list.reload expect(list.join_request_user_ids).to eq [other_user.id] end it "does not add a user multiple times" do other_user = create :user 2.times { list.send_table_join_request_for_user! other_user } expect(list.join_request_user_ids).to eq [other_user.id] end it "broadcasts it to the user" do other_user = create :user expect{ list.send_table_join_request_for_user! other_user }.to broadcast_to_user(user.id).message('user_join_request').with( hash_including(:users, :join_request) ) end end describe "#approve_join_request_for_user" do let(:joining_user){ create :user } before do list_options[:join_request_user_ids] = [joining_user.id] end it "does not raise error when no user is associated" do unlinked_user = create :user expect{ list.approve_join_request_for_user!(unlinked_user) }.not_to raise_error end it "removes the user as wanting to join" do list.approve_join_request_for_user! joining_user list.join_request_user_ids.should_not include joining_user.id # persistance check list.reload list.join_request_user_ids.should_not include joining_user.id end it "sets the active list of the user to the specific list" do list.approve_join_request_for_user! joining_user joining_user.active_list_id.should == list.id # persistance check joining_user.reload joining_user.active_list_id.should == list.id end it "broadcasts the event to the user itself" do joining_user expect{ list.approve_join_request_for_user! joining_user } .to broadcast_to_user(joining_user).message('join_request_approved') .with( hash_including(:user) ) end it "broadcasts the event to other associated users" do expect{ list.approve_join_request_for_user! joining_user } .to broadcast_to_user(user).message('join_request_approved') .with( hash_including(:user) ) end end describe "#reject_join_request_for_user" do let(:joining_user){ create :user } before do list_options[:join_request_user_ids] = [joining_user.id] end it "does not raise error when no user is associated" do unlinked_user = create :user expect{ list.reject_join_request_for_user!(unlinked_user) }.not_to raise_error end it "removes the user as wanting to join" do list.reject_join_request_for_user! joining_user list.join_request_user_ids.should_not include joining_user.id # persistance check list.reload list.join_request_user_ids.should_not include joining_user.id end it "does not set the active list of the user to the specific list" do list.reject_join_request_for_user! joining_user joining_user.active_list_id.should_not be_present end it "broadcasts the event to the user itself" do joining_user expect{ list.reject_join_request_for_user! joining_user } .to broadcast_to_user(joining_user).message('join_request_rejected') .with( id: "jr-#{joining_user.id}" ) end it "broadcasts the event to other associated users" do expect{ list.reject_join_request_for_user! joining_user } .to broadcast_to_user(user).message('join_request_rejected') .with( id: "jr-#{joining_user.id}" ) end end end end