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

@@ -57,48 +57,113
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