aboutsummaryrefslogtreecommitdiffstats
path: root/spec/controllers/vehicle_journeys_controller_spec.rb
blob: 3006845323076f5823d921b3744c409a3333d0af (plain)
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
RSpec.describe VehicleJourneysController, :type => :controller do

  before do
    @user = build_stubbed(:allmighty_user)
  end

  describe 'user_permissions' do
    let( :referential ){ build_stubbed(:referential) }
    let( :user_context ){ UserContext.new(@user, referential: referential) }

    before do
      allow(controller).to receive(:pundit_user).and_return(user_context)
      allow(controller).to receive(:current_organisation).and_return(@user.organisation)
    end

    it 'computes them correctly if not authorized' do
      expect( controller.send(:user_permissions) ).to eq({'vehicle_journeys.create'  => false,
                                                   'vehicle_journeys.destroy' => false,
                                                   'vehicle_journeys.update'  => false }.to_json)
    end
    it 'computes them correctly if authorized' do
      @user.organisation_id = referential.organisation_id
      expect( controller.send(:user_permissions) ).to eq({'vehicle_journeys.create'  => true,
                                                   'vehicle_journeys.destroy' => true,
                                                   'vehicle_journeys.update'  => true }.to_json)
    end
  end

  describe "GET index" do
    login_user
    render_views

    context "in JSON" do
      let(:vehicle_journey){ create :vehicle_journey }
      let(:route){ vehicle_journey.route }
      let(:line){ route.line }
      let!(:request){ get :index, referential_id: referential.id, line_id: line.id, route_id: route.id, format: :json}
      let(:parsed_response){ JSON.parse response.body }
      it "should have all the attributes" do
        expect(response).to have_http_status 200
        vehicle_journey = parsed_response["vehicle_journeys"].first
        vehicle_journey_at_stops_matrix = vehicle_journey["vehicle_journey_at_stops"]
        vehicle_journey_at_stops_matrix.each do |received_vjas|
          expect(received_vjas).to have_key("id")
          vjas = Chouette::VehicleJourneyAtStop.find received_vjas["id"]
          [:connecting_service_id, :boarding_alighting_possibility].each do |att|
            expect(received_vjas[att]).to eq vjas.send(att)
          end
        end
      end
    end
  end
end