Description:
added spec for user problem access control
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r283:105cc0f11b68 - - 1 file changed: 65 inserted, 0 deleted

@@ -9,96 +9,161
9 @john = stub_model(User, :salt => @salt,
9 @john = stub_model(User, :salt => @salt,
10 :hashed_password => User.encrypt(@password,@salt))
10 :hashed_password => User.encrypt(@password,@salt))
11 end
11 end
12
12
13 it "should be authenticated if activated" do
13 it "should be authenticated if activated" do
14 @john.should_receive(:activated).and_return(true)
14 @john.should_receive(:activated).and_return(true)
15 @john.authenticated?(@password).should == true
15 @john.authenticated?(@password).should == true
16 end
16 end
17
17
18 it "should not be authenticated if inactivated" do
18 it "should not be authenticated if inactivated" do
19 @john.should_receive(:activated).and_return(false)
19 @john.should_receive(:activated).and_return(false)
20 @john.authenticated?(@password).should == false
20 @john.authenticated?(@password).should == false
21 end
21 end
22
22
23 it "should not be authenticated if incorrect password is provided" do
23 it "should not be authenticated if incorrect password is provided" do
24 @john.should_receive(:activated).and_return(true)
24 @john.should_receive(:activated).and_return(true)
25 @john.should_receive(:hashed_password).and_return("byebye")
25 @john.should_receive(:hashed_password).and_return("byebye")
26 @john.authenticated?(@password).should == false
26 @john.authenticated?(@password).should == false
27 end
27 end
28
28
29 end
29 end
30
30
31 describe User, "during registration" do
31 describe User, "during registration" do
32
32
33 class User
33 class User
34 public :encrypt_new_password
34 public :encrypt_new_password
35 end
35 end
36
36
37 before(:each) do
37 before(:each) do
38 @john = User.new(:login => 'john', :password => 'hello')
38 @john = User.new(:login => 'john', :password => 'hello')
39 @john.encrypt_new_password
39 @john.encrypt_new_password
40 end
40 end
41
41
42 it "should produce and accept activation key" do
42 it "should produce and accept activation key" do
43 activation_key = @john.activation_key
43 activation_key = @john.activation_key
44
44
45 @john.verify_activation_key(activation_key).should == true
45 @john.verify_activation_key(activation_key).should == true
46 end
46 end
47
47
48 it "should not accept invalid activation key" do
48 it "should not accept invalid activation key" do
49 @john.verify_activation_key("12345").should == false
49 @john.verify_activation_key("12345").should == false
50 end
50 end
51 end
51 end
52
52
53 describe User, "when re-register with the same e-mail" do
53 describe User, "when re-register with the same e-mail" do
54
54
55 before(:each) do
55 before(:each) do
56 @mary_email = 'mary@in.th'
56 @mary_email = 'mary@in.th'
57
57
58 @time_first_register = Time.local(2008,5,10,9,00).gmtime
58 @time_first_register = Time.local(2008,5,10,9,00).gmtime
59
59
60 @mary_first = mock_model(User,
60 @mary_first = mock_model(User,
61 :login => 'mary1',
61 :login => 'mary1',
62 :password => 'hello',
62 :password => 'hello',
63 :email => @mary_email,
63 :email => @mary_email,
64 :created_at => @time_first_register)
64 :created_at => @time_first_register)
65 @mary_second = User.new(:login => 'mary2',
65 @mary_second = User.new(:login => 'mary2',
66 :password => 'hello',
66 :password => 'hello',
67 :email => @mary_email)
67 :email => @mary_email)
68 User.stub!(:find_by_email).
68 User.stub!(:find_by_email).
69 with(@mary_email, {:order => "created_at DESC"}).
69 with(@mary_email, {:order => "created_at DESC"}).
70 and_return(@mary_first)
70 and_return(@mary_first)
71 end
71 end
72
72
73 class User
73 class User
74 public :enough_time_interval_between_same_email_registrations
74 public :enough_time_interval_between_same_email_registrations
75 end
75 end
76
76
77 it "should not be allowed if the time interval is less than 5 mins" do
77 it "should not be allowed if the time interval is less than 5 mins" do
78 time_now = @time_first_register + 4.minutes
78 time_now = @time_first_register + 4.minutes
79 Time.stub!(:now).and_return(time_now)
79 Time.stub!(:now).and_return(time_now)
80
80
81 @mary_second.enough_time_interval_between_same_email_registrations
81 @mary_second.enough_time_interval_between_same_email_registrations
82 @mary_second.errors.length.should_not be_zero
82 @mary_second.errors.length.should_not be_zero
83 end
83 end
84
84
85 it "should be allowed if the time interval is more than 5 mins" do
85 it "should be allowed if the time interval is more than 5 mins" do
86 time_now = @time_first_register + 6.minutes
86 time_now = @time_first_register + 6.minutes
87 Time.stub!(:now).and_return(time_now)
87 Time.stub!(:now).and_return(time_now)
88
88
89 @mary_second.enough_time_interval_between_same_email_registrations
89 @mary_second.enough_time_interval_between_same_email_registrations
90 @mary_second.errors.length.should be_zero
90 @mary_second.errors.length.should be_zero
91 end
91 end
92
92
93 end
93 end
94
94
95 describe User, "as a class" do
95 describe User, "as a class" do
96
96
97 it "should be able to generate random password" do
97 it "should be able to generate random password" do
98 password1 = User.random_password
98 password1 = User.random_password
99 password2 = User.random_password
99 password2 = User.random_password
100
100
101 password1.should_not == password2
101 password1.should_not == password2
102 end
102 end
103
103
104 end
104 end
105 +
106 + describe User, "when requesting problem description," do
107 +
108 + fixtures :users
109 + fixtures :problems
110 +
111 + before(:each) do
112 + @james = users(:james)
113 + @john = users(:john)
114 + @jack = users(:jack)
115 + @add = problems(:one)
116 + @easy = problems(:easy)
117 + @hard = problems(:hard)
118 + end
119 +
120 + it "should check if a problem is in user's contests" do
121 + @james.problem_in_user_contests?(@easy).should be_true
122 + @james.problem_in_user_contests?(@hard).should be_false
123 +
124 + @john.problem_in_user_contests?(@easy).should be_false
125 + @john.problem_in_user_contests?(@hard).should be_false
126 + end
127 +
128 + it "should be able to view problem in user's contests, when multicontests is on" do
129 + enable_multicontest
130 + @james.can_view_problem?(@easy).should be_true
131 + @jack.can_view_problem?(@easy).should be_true
132 + @jack.can_view_problem?(@hard).should be_true
133 + end
134 +
135 + it "should not be able to view problem not in user's contests, when multicontests is on" do
136 + enable_multicontest
137 + @john.can_view_problem?(@easy).should be_false
138 + @john.can_view_problem?(@hard).should be_false
139 + @james.can_view_problem?(@hard).should be_false
140 + end
141 +
142 + it "should be able to view all available problems, when multicontests is off" do
143 + disable_multicontest
144 + @john.can_view_problem?(@easy).should be_true
145 + @john.can_view_problem?(@hard).should be_true
146 + @james.can_view_problem?(@easy).should be_true
147 + @james.can_view_problem?(@hard).should be_true
148 + end
149 +
150 + it "should be able to view public problems, when multicontests is on" do
151 + enable_multicontest
152 + @john.can_view_problem?(@add).should be_true
153 + @james.can_view_problem?(@add).should be_true
154 + end
155 +
156 + def enable_multicontest
157 + c = Configuration.new(:key => 'system.multicontests',
158 + :value_type => 'boolean',
159 + :value => 'true')
160 + c.save
161 + end
162 +
163 + def disable_multicontest
164 + c = Configuration.new(:key => 'system.multicontests',
165 + :value_type => 'boolean',
166 + :value => 'false')
167 + c.save
168 + end
169 + end
You need to be logged in to leave comments. Login now