aboutsummaryrefslogtreecommitdiff
blob: f0cc8afb4fb444fa7510bff121784d4a8677e066 (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
require 'spec_helper.rb'
describe User do
  include Permissions::TestPermissions

  it "should be non-admin recruit" do
    new_user  = User.new(:name => "Some", :email_address => "some@some.com")
    new_user.should_not   be_administrator
    new_user.role.should  == :recruit
  end

  it { should allow_value(:mentor).for(:role) }
  it { should allow_value(:recruiter).for(:role) }

  it "should be valid if recruiter is administrator" do
    Factory(:administrator).should be_valid
  end

  it "should be invalid if non-recruiter is administrator" do
    user = Factory(:recruit)
    user .administrator = true

    for new_role in [:recruit, :mentor]
      user.role        =  new_role
      user.should_not  be_valid
    end
  end

  it "should be prohibited for recruits and mentors to change anyone role to recruiter" do
    user = Factory(:recruit)
    for new_role in [:recruiter, :mentor]
      user.role        =  new_role
      for updater in [Factory(:recruit), Factory(:mentor), Guest.new]
        user.should_not  be_updatable_by(updater)
      end
    end
  end

  it "should be allowed for admin to change anyone else role" do
    for other_user in [:recruit, :administrator]
      for new_role in [:recruit, :mentor, :recruiter]
        user  = Factory(other_user)
        user.role   = new_role
        user.should be_updatable_by(Factory(:administrator))
      end
    end
  end

  it 'should be invalid for recruit to mentor someone' do
    for user in [Factory(:administrator), Factory(:recruiter), Factory(:mentor)] do
      user.mentor = Factory(:recruit)
      user.should_not be_valid
    end
  end

  it 'should be allowed for mentors and recruiters to mentor someone' do
    for user in [Factory(:administrator), Factory(:recruiter), Factory(:mentor)] do
      recruit     = Factory(:recruit, :mentor => user)
      user.should be_valid
    end
  end

  it "should prohibit non-recruiter to change user role" do
    recruit       = Factory(:recruit)
    recruit.role  = :mentor
    for user in [Factory(:recruit), Factory(:mentor)]
      recruit.should_not be_updatable_by(user)
    end
  end

  it "should allow recruiter to change user role" do
    recruit       = Factory(:recruit)
    recruit.role  = :mentor
    for user in [Factory(:recruiter), Factory(:administrator)]
      recruit.should be_updatable_by(user)
    end
  end


  it "should return proper all_questions" do
    r = recruit_with_answered_and_unanswered_questions

    for question in r.answered + r.unanswered
      r.recruit.all_questions.include?(question).should be_true
    end
    r.recruit.all_questions.count.should == r.recruit.all_questions.uniq.count
  end

  it "should return proper answered_questions" do
    r = recruit_with_answered_and_unanswered_questions
    for question in r.answered
      r.recruit.answered_questions.include?(question).should be_true
    end
    for question in r.unanswered
      r.recruit.answered_questions.include?(question).should be_false
    end
    r.recruit.answered_questions.should == r.recruit.answered_questions.uniq
  end

  it "should return proper unanswered_questions" do
    r = recruit_with_answered_and_unanswered_questions
    unanswered = r.recruit.unanswered_questions
    (r.unanswered - unanswered).should be_empty
    (unanswered - r.unanswered).should be_empty
    unanswered.should == unanswered.uniq
  end

  it "should properly check if user answered all questions" do
    r = recruit_with_answered_and_unanswered_questions
    r.recruit.answered_all_questions?.should          be_false
    Factory(:recruit).answered_all_questions?.should  be_true
  end

  it "should return proper recruits with all questions` answered" do
    # recruits that should be returned
    correct_answered_all = [Factory(:recruit)]
    correct_answered_all.push recruit_with_answers_in_categories.recruit

    # and some other users
    recruit_with_answered_and_unanswered_questions
    Factory(:administrator)
    Factory(:mentor)
    Factory(:recruiter)

    answered_all = User.recruits_answered_all

    (answered_all - correct_answered_all).should be_empty
    (correct_answered_all - answered_all).should be_empty
  end

  it "should allow recruiters to change nick of other users" do
    for u in fabricate_all_roles
      u.should be_editable_by Factory(:recruiter), :nick
      u.should be_viewable_by Factory(:recruiter), :nick
      u.nick = 'changed'
      u.should be_updatable_by Factory(:recruiter)
    end
  end

  it "should allow user to change their nicks" do
    for u in fabricate_all_roles
      u.should be_editable_by u, :nick
      u.should be_viewable_by u, :nick
      u.nick = 'changed'
      u.should be_updatable_by u
    end
  end

  it "that is mentorless recruit should allow mentor to pick up" do
    recruit = Factory(:recruit, :mentor => nil)
    mentor  = Factory(:mentor)
    recruit.should be_editable_by(mentor)
    recruit.should be_editable_by(mentor, :mentor)
    recruit.mentor = mentor
    recruit.should be_updatable_by(mentor)
  end

  it "should allow mentor to resign" do
    recruit = Factory(:recruit)
    mentor  = recruit.mentor
    recruit.should be_editable_by(mentor)
    recruit.should be_editable_by(mentor, :mentor)
    recruit.mentor = nil
    recruit.should be_updatable_by(mentor)
  end

  it "should check if mentors were Gentoo devs long enough if configured to" do
    user            = Factory(:recruit)

    # Configure to check using test data
    old_config                              =  APP_CONFIG.to_json # to restore it after his test
                                                                  # it behaves like reference if
                                                                  # I copy a hash
    APP_CONFIG['developer_data']['check']   = true
    APP_CONFIG['developer_data']['min_months_mentor_is_dev']  = '6'
    APP_CONFIG['developer_data']['data']    = %{{"developers": [
      {"joined": "#{4.years.ago.strftime('%Y/%m/%d')}", "nick": "long"},
      {"joined": "#{4.months.ago.strftime('%Y/%m/%d')}", "nick": "short"}
    ]}}

    user.role       = :mentor

    user.nick       = "long"
    user.should     be_valid

    user.nick       = "short"
    user.should_not be_valid

    user.nick       = "short"
    user.should_not be_valid

    silence_warnings { APP_CONFIG = JSON.load(old_config) } # restore config
  end

  it "should allow only recruiters to edit project acceptances" do
    user = Factory(:recruit)
    user.should be_editable_by(Factory(:recruiter), :project_acceptances)

    user.should_not be_editable_by(Factory(:mentor, :project_lead => true), :project_acceptances)
    user.should_not be_editable_by(Factory(:mentor), :project_acceptances)
    user.should_not be_editable_by(Factory(:recruit), :project_acceptances)
    user.should_not be_editable_by(Guest.new, :project_acceptances)
  end
end