aboutsummaryrefslogtreecommitdiff
blob: 738db635a7b76122d4be714a2c1ece07841abf13 (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
require 'spec_helper'

describe Agenda do
  it 'shouldn not allow anyone to create' do
    a = Agenda.new
    for u in users_factory(:all_roles)
      a.should_not be_creatable_by(u)
      a.should_not be_destroyable_by(u)
    end
  end

  it 'shouldn not allow anyone to destory' do
    a = Factory(:agenda)
    for u in users_factory(:all_roles)
      a.should_not be_destroyable_by(u)
    end
  end

  it 'should allow everybody to view' do
    a = Factory(:agenda)
    for u in users_factory(:all_roles)
      a.should be_viewable_by(u)
    end
  end

  it 'should allow everybody to view summaries after 4 council members approved it' do
    agenda = Agenda.current

    for u in users_factory(:guest, :user, :admin)
      agenda.should_not be_viewable_by(u, :summary)
    end

    (1..4).each { |i| Factory(:approval, :agenda => agenda) }

    for u in users_factory(:all_roles)
      agenda.should be_viewable_by(u, :summary)
    end
  end

  it 'should allow only administrators and council members to edit and update' do
    a = Factory(:agenda)
    for u in users_factory(:guest, :user)
      a.should_not be_editable_by(u)
      a.should_not be_updatable_by(u)
    end
  end

  it 'should allow no one to edit or change meeting_log' do
    a = Factory(:agenda)
    a.meeting_log = 'changed'

    for u in users_factory(:all_roles)
      a.should_not be_editable_by(u, :meeting_log)
      a.should_not be_updatable_by(u)
    end
  end

  it 'should allow council memebers to change summaries of old meetings' do
    a = Factory(:agenda, :state => 'old')
    a.summary = 'changed'

    for u in users_factory(:council, :council_admin)
      a.should be_editable_by(u)
      a.should be_editable_by(u, :summary)
      a.should be_updatable_by(u)
    end
  end

  def test_migration(object, migration, prohibited, allowed, final_state)
    # object - object to migrate
    # migration - migration name
    # prohibited - array of users who can not perform migration
    # allowed - *one* user who can perform migration
    # final_state - state of object after migration
    for user in prohibited
      lambda { object.lifecycle.send("#{migration}!", user) }.should raise_error
    end

    object.lifecycle.send("#{migration}!", allowed)
    object.state.should == final_state
  end

  it 'should have working transitions, available only to council members and administrators' do
    Factory(:agenda)
    prohibited = users_factory(:guest, :user)
    allowed = users_factory(:council, :admin, :council_admin)

    for user in allowed
      agenda = Agenda.last
      test_migration(agenda, :close, prohibited, user, 'submissions_closed')
      test_migration(agenda, :reopen, prohibited, user, 'open')
      test_migration(agenda, :close, prohibited, user, 'submissions_closed')
      test_migration(agenda, :archive, prohibited, user, 'old')
    end

  end

  it 'that is non-archival should not be valid if there other open agenda' do
    a = Factory(:agenda)
    Agenda.new.should_not be_valid
    Agenda.new(:state => 'submissions_closed').should_not be_valid
  end

  it 'that is non-archival should not be valid if there other closed agenda' do
    a = Factory(:agenda, :state => 'submissions_closed')
    Agenda.new.should_not be_valid
    Agenda.new(:state => 'submissions_closed').should_not be_valid
  end

  it 'that is archival should be valid' do
    a = Factory(:agenda, :state => 'old')
    a.should be_valid
  end

  it 'should create new open agenda, when current agenda is archived' do
    a = Factory(:agenda)
    u = users_factory(:admin)
    a.lifecycle.close! u
    a.lifecycle.archive! u
    Agenda.last.state.should == 'open'
  end

  it 'should set meeting time to now by default' do
    a1 = Factory(:agenda)
    a2 = Factory(:agenda, :meeting_time => 2.days.ago, :state => 'old')
    today = Time.now.strftime('%Y-%m-%d')

    a1.meeting_time.strftime('%Y-%m-%d').should == today
    a2.meeting_time.strftime('%Y-%m-%d').should_not == today
  end

  it 'should not create  add all council members and only them as participants when archived' do
    users_factory(:user, :admin)
    users_factory(:council, :council)
    agenda = Factory(:agenda, :state => 'submissions_closed')
    agenda.lifecycle.archive!(User.council_member_is(true).first)

    agenda.participations.should be_empty
  end
  it 'should properly create votes' do
    Factory(:agenda)
    a1 = Factory(:agenda_item, :agenda => Agenda.current)
    a2 = Factory(:agenda_item, :agenda => Agenda.current)
    a3 = Factory(:agenda_item, :agenda => Agenda.current)
    Agenda.current.agenda_items.each do |item|
      Factory(:voting_option, :agenda_item => item, :description => 'Yes')
      Factory(:voting_option, :agenda_item => item, :description => 'No')
      Factory(:voting_option, :agenda_item => item, :description => 'Dunno')
    end

    u = users_factory(:council, :council, :council)
    Factory(:vote, :user => u.first, :voting_option => a1.voting_options.first)

    Vote.count.should be_equal(1)

    results_hash = {
        a1.title => { u[0].irc_nick => 'Yes', u[1].irc_nick => 'Yes', u[2].irc_nick => 'Yes'},
        a2.title => { u[0].irc_nick => 'Yes', u[1].irc_nick => 'No', u[2].irc_nick => 'Dunno'},
        a3.title => { u[0].irc_nick => 'Dunno', u[1].irc_nick => 'Dunno', u[2].irc_nick => 'No'}
    }

    Agenda.process_results results_hash

    Vote.count.should be_equal(9)
    Vote.council_vote_is(true).count.should be_equal(9)

    u[0].votes.*.voting_option.*.description.sort.should == ['Dunno', 'Yes', 'Yes']
    u[1].votes.*.voting_option.*.description.sort.should == ['Dunno', 'No', 'Yes']
    u[2].votes.*.voting_option.*.description.sort.should == ['Dunno', 'No', 'Yes']
    a1.voting_options.*.votes.flatten.*.voting_option.*.description.should == ['Yes', 'Yes', 'Yes']
    a2.voting_options.*.votes.flatten.*.voting_option.*.description.should == ['Yes', 'No', 'Dunno']
    a3.voting_options.*.votes.flatten.*.voting_option.*.description.should == ['No', 'Dunno', 'Dunno']
  end

  it 'should return proper voters' do
    users = users_factory([:council]*3 + [:user])
    proxy = Factory(:proxy, :council_member => users.first, :proxy => users.last)
    voters = Agenda.voters

    voters.length.should be_equal(3)
    nicks = users.*.irc_nick - [users.first.irc_nick]
    voters.length.should be_equal(nicks.length)
    (voters - nicks).should be_empty
    (nicks - voters).should be_empty
  end

  it 'should add Agenda.send_current_agenda_reminders to delayed jobs when created' do
    Agenda.should_receive_delayed(:send_current_agenda_reminders)
    Factory(:agenda)
  end

  it 'should add Agenda.send_current_agenda_reminders to delayed jobs when meeting time changes' do
    a = Factory(:agenda)
    Agenda.should_receive_delayed(:send_current_agenda_reminders)
    a.meeting_time = Time.now + 24.hours
    a.save!
  end

  it 'should set email_reminder_sent to false when time changes' do
    a = Factory(:agenda, :email_reminder_sent => true)
    lambda {
    a.meeting_time = Time.now + 24.hours
    a.save!
    }.should change(a, :email_reminder_sent?).from(true).to(false)
  end

  it 'should send reminders properly with send_current_agenda_reminders using delayed jobs' do
    agenda = Factory(:agenda)
    users = users_factory([:user] * 2)
    council = users_factory([:council] * 2)
    Factory(:proxy, :proxy => users.first, :council_member => council.first, :agenda => agenda)
    UserMailer.should_receive_delayed(:deliver_meeting_reminder, council.last, agenda)
    UserMailer.should_receive_delayed(:deliver_meeting_reminder, users.first, agenda)

    Agenda.send_current_agenda_reminders

    agenda.reload
    agenda.email_reminder_sent.should be_true
  end

  it 'should not send reminders with send_current_agenda_reminders if Agenda.current.email_reminder_sent is true' do
    a = Factory(:agenda, :email_reminder_sent => true)
    users = users_factory([:user] * 2)
    UserMailer.should_not_receive(:delay)
    Agenda.send_current_agenda_reminders
  end

  it 'should return proper irc_reminders hash' do
    CustomConfig['Reminders']["hours_befeore_meeting_to_send_irc_reminders"] = 2

    a1 = Factory(:agenda)
    users = users_factory([:council]*2 + [:user]*2)
    Agenda.irc_reminders.keys.should include('remind_time')
    Agenda.irc_reminders.keys.should include('message')
    Agenda.irc_reminders.keys.should include('users')

    Agenda.irc_reminders['remind_time'].should == Agenda.current.meeting_time.strftime('%a %b %d %H:%M:%S %Y')
    Agenda.irc_reminders['users'].should == Agenda.voters

    a1.meeting_time = 10.years.from_now
    a1.save!

    Agenda.irc_reminders.should be_empty
  end

  it 'should return proper possible_transitions for each state' do
    a = Factory(:agenda)
    u = users_factory(:council)
    a.possible_transitions.should == [["Close this agenda.", "agenda_close_path"]]
    a.lifecycle.close!(u)
    a.possible_transitions.should == [["Reopen this agenda.", "agenda_reopen_path"], ["Archive this agenda.", "agenda_archive_path"]]
    a.lifecycle.archive!(u)
    a.possible_transitions.should == []
  end

  describe '#current?' do
    it 'should return true if agenda is in open state' do
      Factory(:agenda).should be_current
    end

    it 'should return true if agenda is in submissions_closed state' do
      Factory(:agenda, :state => 'submissions_closed').should be_current
    end

    it 'should return true if agenda is in meeting_ongoing state' do
      Factory(:agenda, :state => 'meeting_ongoing').should be_current
    end

    it 'should return true if agenda is in old state' do
      Factory(:agenda, :state => 'old').should_not be_current
    end
  end

  describe '#current?' do
    it 'should create new agenda if needed' do
      Agenda.count.should be_zero
      agenda = Agenda.current
      agenda2 = Agenda.current
      agenda.should be_a(Agenda)
      agenda2.should be_a(Agenda)
      Agenda.count.should be_equal(1)
      agenda.id.should be_equal(agenda2.id)
    end
  end

  it 'should return proper voting_array' do
    old_agenda = Factory(:agenda, :state => 'old')
    current_agenda = Factory(:agenda)
    i1 = Factory(:agenda_item, :agenda => old_agenda, :timelimits => '0:0')
    i2 = Factory(:agenda_item, :agenda => current_agenda, :timelimits => "10:0 Ten minutes passed")
    i3 = Factory(:agenda_item, :agenda => current_agenda, :timelimits => "0:10 Ten seconds passed")

    v11 = Factory(:voting_option, :agenda_item => i1)
    v21 = Factory(:voting_option, :agenda_item => i2)
    v22 = Factory(:voting_option, :agenda_item => i2, :description => 'other')

    old_agenda.voting_array.should == [[i1.title, [v11.description], i1.timelimits]]
    current_agenda.voting_array.should == [[i2.title, [v21.description, v22.description],
                                            i2.timelimits], [i3.title, [], i3.timelimits]]
  end

  describe '.update_voting_options' do
    it 'should remove unneeded voting options and keep existing needed options' do
      current_agenda = Factory(:agenda)
      item = Factory(:agenda_item, :agenda => current_agenda)
      unneeded_option = Factory(:voting_option, :agenda_item => item, :description => 'unneeded')
      needed_option = Factory(:voting_option, :agenda_item => item, :description => 'needed')

      VotingOption.count.should be_equal(2)

      Agenda.update_voting_options [[item.title, [needed_option.description]]]

      VotingOption.count.should be_equal(1)
      VotingOption.first.description.should == needed_option.description
      VotingOption.first.id.should == needed_option.id
    end
    it 'should create requested new voting options' do
      current_agenda = Factory(:agenda)
      item = Factory(:agenda_item, :agenda => current_agenda)
      needed_option = Factory(:voting_option, :agenda_item => item, :description => 'needed')

      Agenda.update_voting_options [[item.title, [needed_option.description, 'new option']]]
      VotingOption.count.should be_equal(2)
      VotingOption.last.description.should == 'new option'
    end
  end

  it 'should remove approvals for summary, when summary changes' do
    agenda = Agenda.current
    Factory(:approval, :agenda => agenda)
    agenda.summary = 'changed'
    agenda.save!
    Approval.count.should be_zero
  end
end