aboutsummaryrefslogtreecommitdiffstats
path: root/spec/features/referentials_spec.rb
blob: d4890fda447fb5e363b702a0822b31119f8b12a2 (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
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
describe "Referentials", :type => :feature do
  login_user

  let(:referential) { Referential.first }

  describe "index" do

    # FIXME #823
    # it "should support no referential" do
    #   visit referentials_path
    #   expect(page).to have_content("Jeux de Données")
    # end

    context "when several referentials exist" do

      def retrieve_referential_by_slug( slug)
        @user.organisation.referentials.find_by_slug(slug) ||
          create(:referential, :slug => slug, :name => slug, :organisation => @user.organisation)
      end

      let!(:referentials) { [ retrieve_referential_by_slug("aa"),
                              retrieve_referential_by_slug("bb")] }

      # FIXME #823
      # it "should show n referentials" do
      #   visit referentials_path
      #   expect(page).to have_content(referentials.first.name)
      #   expect(page).to have_content(referentials.last.name)
      # end

    end

  end

  describe "show" do
    before(:each) { visit referential_path(referential) }

    it "displays referential" do
      expect(page).to have_content(referential.name)
    end

    context 'archived referential' do
      it 'link to edit referetnial is not displayed' do
        referential.archive!
        visit referential_path(referential)
        expect(page).not_to have_link(I18n.t('actions.edit'), href: edit_referential_path(referential))
      end
    end

    context 'unarchived referential' do
      it 'link to edit referetnial is displayed' do
        expect(page).to have_link(I18n.t('actions.edit'), href: edit_referential_path(referential))
      end
    end

    context 'user has the permission to create referentials' do
      it 'shows the clone link for referetnial' do
        expect(page).to have_link(I18n.t('actions.clone'), href: new_workbench_referential_path(referential.workbench, from: referential.id))
      end
    end

    context 'user does not have the permission to create referentials' do
      it 'does not show the clone link for referetnial' do
        @user.update_attribute(:permissions, [])
        visit referential_path(referential)
        expect(page).not_to have_link(I18n.t('actions.clone'), href: new_workbench_referential_path(referential.workbench, from: referential.id))
      end
    end

    context 'user has the permission to edit referentials' do
      it 'shows the link to edit the referential' do
        expect(page).to have_link(I18n.t('actions.edit'), href: edit_referential_path(referential))
      end

      it 'shows the link to archive the referential' do
        expect(page).to have_link(I18n.t('actions.archive'), href: archive_referential_path(referential))
      end
    end

    context 'user does not have the permission to edit referentials' do
      before(:each) do
        @user.update_attribute(:permissions, [])
        visit referential_path(referential)
      end

      it 'does not show the link to edit the referential' do
        expect(page).not_to have_link(I18n.t('actions.edit'), href: edit_referential_path(referential))
      end

      it 'does not show the link to archive the referential' do
        expect(page).not_to have_link(I18n.t('actions.archive'), href: archive_referential_path(referential))
      end
    end

    context 'user has the permission to destroy referentials' do
      it 'shows the link to destroy the referential' do
        expect(page).to have_link(I18n.t('actions.destroy'), href: referential_path(referential))
      end
    end

    context 'user does not have the permission to destroy referentials' do
      it 'does not show the destroy link for referetnial' do
        @user.update_attribute(:permissions, [])
        visit referential_path(referential)
        expect(page).not_to have_link(I18n.t('actions.destroy'), href: referential_path(referential))
      end
    end
  end

  describe "create" do
    let(:workbench){ @user.organisation.workbenches.last }
    it "should" do
      visit new_workbench_referential_path(workbench)
      fill_in "Nom", :with => "Test"
      click_button "Valider"

      expect(Referential.where(:name => "Test")).not_to be_nil
      # CREATE SCHEMA
    end

  end

  describe "new_from" do
    # let(:cloning)
    let(:worker) { ReferentialCloningWorker.new }

    let(:line) { create(:line_with_stop_areas) }
    let(:jp) { create(:journey_pattern, route: line.routes.first) }
    let(:tt) { create(:time_table) }
    let(:vj) { create(:vehicle_journey, journey_pattern: jp, time_table: tt) }
    let(:ref_metadata) { create(:referential_metadata, lines: [line], referential: referential) }

    context "when user is from the same organisation" do

      xit "should" do
        visit new_workbench_referential_path(referential.workbench, from: referential.id, current_workbench_id: @user.organisation.workbenches.first.id)

        select "2018", :from => "referential_metadatas_attributes_0_periods_attributes_0_begin_1i"

        select "2018", :from => "referential_metadatas_attributes_0_periods_attributes_0_end_1i"

        click_button "Valider"

        clone = Referential.where(name: "Copie de first")

        expect(clone.lines).to include(line)
        expect(clone.lines.first.routes).to match_array(referential.lines.first.routes)

        clone_jp = clone.lines.first.routes.first.journey_patterns
        expect(clone_jp).to include(jp)

        clone_vj = clone.lines.first.routes.first.journey_patterns.first.vehicle_journeys
        expect(clone_vj).to include(vj)

        clone_tt = clone.lines.first.routes.first.journey_patterns.first.vehicle_journeys.first.time_tables
        expect(clone_tt).to include(tt)
      end

      # it "should have the lines from source" do
      #   expect(clone.lines).to include(line)
      # end
      #
      # it "should have the routes from source" do
      #   expect(clone.lines.first.routes).to match_array(referential.lines.first.routes)
      # end
      #
      # it "should have the journey patterns from source" do
      #   clone_jp = clone.lines.first.routes.first.journey_patterns
      #   expect(clone_jp).to include(jp)
      # end
      #
      # it "should have the vehicle journeys from source" do
      #   clone_vj = clone.lines.first.routes.first.journey_patterns.first.vehicle_journeys
      #   expect(clone_vj).to include(vj)
      # end
      #
      # it "should have the timetables from source" do
      #   clone_tt = clone.lines.first.routes.first.journey_patterns.first.vehicle_journeys.first.time_tables
      #   expect(clone_tt).to include(tt)
      # end
    end

    # context "when user is from another organisation" do
    #   before :each do
    #
    #   end
    # end
  end

  describe "destroy" do
    let(:workbench){ @user.organisation.workbenches.last }
    let(:referential) {  create(:referential, :organisation => @user.organisation, workbench: workbench) }

    it "should remove referential" do
      visit referential_path(referential)
      #click_link "Supprimer"
      #expect(Referential.where(:slug => referential.slug)).to be_blank
    end

  end

end