aboutsummaryrefslogtreecommitdiff
blob: 5246015718d12c0ceddaa7e3725ed6db8009cd2d (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
require 'spec_helper.rb'

describe User do
  it "should run spec test with shoulda and models from application" do
    Guest.new.should_not be_administrator
  end

  it "should set correct roles for new user" do
    u = User.new :name => 'Example', :email => 'example@example.com',
                  :password => 'Example', :irc_nick => 'example'
    u.save!
    u.should_not be_administrator
    u.should_not be_council_member
    u.should_not be_guest
    u.should     be_signed_up
  end

  describe '.slacking_status_in_period' do
    it 'should give "Was on last meeting" slacking status to user who was on all meetings' do
      u = users_factory(:council)
      agendas = (1..10).collect do |i|
        a = Factory(:agenda, :state => 'old')
        Factory(:participation, :participant => u, :agenda => a)
        a
      end
      u.slacking_status_in_period(agendas.first.meeting_time - 1.minute,
                                    agendas.last.meeting_time + 1.minute).should == 'Was on last meeting'
    end

    it 'should give "Was on last meeting" slacking status to user who was on last meeting and skipped some meetings in past (but not two consecutive)' do
      u = users_factory(:council)
      agendas = (1..10).collect do |i|
        a = Factory(:agenda, :state => 'old')
        Factory(:participation, :participant => u, :agenda => a) if i.even?
        a
      end
      u.slacking_status_in_period(agendas.first.meeting_time - 1.minute,
                                    agendas.last.meeting_time + 1.minute).should == 'Was on last meeting'
    end

    it 'should give "Skipped last meeting" slacking status to user who was absent on last meeting and skipped some meetings in past (but not two consecutive)' do
      u = users_factory(:council)
      agendas = (1..10).collect do |i|
        a = Factory(:agenda, :state => 'old')
        Factory(:participation, :participant => u, :agenda => a) if i.odd?
        a
      end
      u.slacking_status_in_period(agendas.first.meeting_time - 1.minute,
                                    agendas.last.meeting_time + 1.minute).should == 'Skipped last meeting'
    end

    it 'should give "Slacker" slacking status to user who was present on last meeting skipped two consecutive meeting in the past' do
      u = users_factory(:council)
      agendas = (1..10).collect do |i|
        a = Factory(:agenda, :state => 'old')
        Factory(:participation, :participant => u, :agenda => a) unless [1, 3, 5, 6].include?(i)
        a
      end
      u.slacking_status_in_period(agendas.first.meeting_time - 1.minute,
                                    agendas.last.meeting_time + 1.minute).should == 'Slacker'
    end

    it 'should give "No more a council" slacking status to user who skipped two consecutive meeting in the past and the last one' do
      u = users_factory(:council)
      agendas = (1..10).collect do |i|
        a = Factory(:agenda, :state => 'old')
        Factory(:participation, :participant => u, :agenda => a) unless [1, 3, 5, 6, 10].include?(i)
        a
      end
      u.slacking_status_in_period(agendas.first.meeting_time - 1.minute,
                                    agendas.last.meeting_time + 1.minute).should == 'No more a council'
    end

    it 'should give "No more a council" slacking status to user who skipped two consecutive meeting in the past and then one more' do
      u = users_factory(:council)
      agendas = (1..10).collect do |i|
        a = Factory(:agenda, :state => 'old')
        Factory(:participation, :participant => u, :agenda => a) unless [5, 6, 8].include?(i)
        a
      end
      u.slacking_status_in_period(agendas.first.meeting_time - 1.minute,
                                    agendas.last.meeting_time + 1.minute).should == 'No more a council'
    end
  end

  it 'should allow no one to create' do
    for u in users_factory(AllRoles)
      User.new.should_not be_creatable_by(u)
    end
  end

  it 'should allow only administrators to destroy' do
    for u in users_factory(:user, :council, :guest)
      Factory(:user).should_not be_destroyable_by(u)
    end

    for u in users_factory(:admin, :council_admin)
      Factory(:user).should be_destroyable_by(u)
    end
  end

  it 'should allow everybody to view' do
    for u1 in users_factory(AllRoles)
      for u2 in users_factory(AllRoles - [:guest])
        u2.should be_viewable_by(u1)
      end
    end
  end

  describe '#updatable_by?' do
    it 'should return true if user changes own email, irc_nick and password' do
      u = Factory(:user)
      u.password = 'changed'
      u.password_confirmation = 'changed'
      u.current_password = 'changed'
      u.crypted_password = 'changed'
      u.irc_nick = 'changed'
      u.email = 'changed@changed.com'
      u.should be_updatable_by u
    end

    it 'should return false if user changes someone' do
      u = Factory(:user)
      u.should_not be_updatable_by(Factory(:user))
    end

    it 'should return true if administrator changes something' do
      u = Factory(:user)
      u.password = 'changed'
      u.password_confirmation = 'changed'
      u.current_password = 'changed'
      u.crypted_password = 'changed'
      u.email = 'changed@changed.com'
      u.irc_nick = 'changed'
      u.administrator = true
      u.council_member = true
      u.should be_updatable_by(users_factory(:admin))
    end

  end

  describe '#can_appoint_a_proxy?' do
    it 'should return false for users who are not council members' do
      for u in users_factory(:user, :admin, :guest)
        proxy = Factory(:user)
        u.can_appoint_a_proxy?(proxy).should be_false
      end
    end

    it 'should return true for council members who never appointed a proxy' do
      for u in users_factory(:council, :council_admin)
        proxy = Factory(:user)
        u.can_appoint_a_proxy?(proxy).should be_true
      end
    end

    it 'should return true for council members who appointed a proxy for past meeting' do
      Factory(:agenda)
      old_a = Factory(:agenda, :state => 'old')
      for u in users_factory(:council, :council_admin)
        proxy = Factory(:user)
        Factory(:proxy, :council_member => u, :agenda => old_a)
        u.can_appoint_a_proxy?(proxy).should be_true
      end
    end

    it 'should return false for council members who appointed a proxy for current meeting' do
      current_a = Factory(:agenda)
      proxy = Factory(:user)
      for u in users_factory(:council, :council_admin)
        Factory(:proxy, :council_member => u, :agenda => current_a)
        u.can_appoint_a_proxy?(proxy).should be_false
      end
    end

    it 'should return false for council members checking if they can appoint another council member as proxy' do
      proxy = users_factory(:council)
      for u in users_factory(:council, :council_admin)
        u.can_appoint_a_proxy?(proxy).should be_false
      end
    end

    it 'should return false for council members checking if they can appoint someone who is a aleady a proxy for current meeting as proxy' do
      a = Factory(:agenda)
      proxy = users_factory(:user)
      Factory(:proxy, :proxy => proxy, :agenda => a)
      for u in users_factory(:council, :council_admin)
        u.can_appoint_a_proxy?(proxy).should be_false
      end
    end
  end
end