Description:
added spec for user problem access control
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r283:105cc0f11b68 - - 1 file changed: 65 inserted, 0 deleted
@@ -9,96 +9,161 | |||
|
9 | 9 | @john = stub_model(User, :salt => @salt, |
|
10 | 10 | :hashed_password => User.encrypt(@password,@salt)) |
|
11 | 11 | end |
|
12 | 12 | |
|
13 | 13 | it "should be authenticated if activated" do |
|
14 | 14 | @john.should_receive(:activated).and_return(true) |
|
15 | 15 | @john.authenticated?(@password).should == true |
|
16 | 16 | end |
|
17 | 17 | |
|
18 | 18 | it "should not be authenticated if inactivated" do |
|
19 | 19 | @john.should_receive(:activated).and_return(false) |
|
20 | 20 | @john.authenticated?(@password).should == false |
|
21 | 21 | end |
|
22 | 22 | |
|
23 | 23 | it "should not be authenticated if incorrect password is provided" do |
|
24 | 24 | @john.should_receive(:activated).and_return(true) |
|
25 | 25 | @john.should_receive(:hashed_password).and_return("byebye") |
|
26 | 26 | @john.authenticated?(@password).should == false |
|
27 | 27 | end |
|
28 | 28 | |
|
29 | 29 | end |
|
30 | 30 | |
|
31 | 31 | describe User, "during registration" do |
|
32 | 32 | |
|
33 | 33 | class User |
|
34 | 34 | public :encrypt_new_password |
|
35 | 35 | end |
|
36 | 36 | |
|
37 | 37 | before(:each) do |
|
38 | 38 | @john = User.new(:login => 'john', :password => 'hello') |
|
39 | 39 | @john.encrypt_new_password |
|
40 | 40 | end |
|
41 | 41 | |
|
42 | 42 | it "should produce and accept activation key" do |
|
43 | 43 | activation_key = @john.activation_key |
|
44 | 44 | |
|
45 | 45 | @john.verify_activation_key(activation_key).should == true |
|
46 | 46 | end |
|
47 | 47 | |
|
48 | 48 | it "should not accept invalid activation key" do |
|
49 | 49 | @john.verify_activation_key("12345").should == false |
|
50 | 50 | end |
|
51 | 51 | end |
|
52 | 52 | |
|
53 | 53 | describe User, "when re-register with the same e-mail" do |
|
54 | 54 | |
|
55 | 55 | before(:each) do |
|
56 | 56 | @mary_email = 'mary@in.th' |
|
57 | 57 | |
|
58 | 58 | @time_first_register = Time.local(2008,5,10,9,00).gmtime |
|
59 | 59 | |
|
60 | 60 | @mary_first = mock_model(User, |
|
61 | 61 | :login => 'mary1', |
|
62 | 62 | :password => 'hello', |
|
63 | 63 | :email => @mary_email, |
|
64 | 64 | :created_at => @time_first_register) |
|
65 | 65 | @mary_second = User.new(:login => 'mary2', |
|
66 | 66 | :password => 'hello', |
|
67 | 67 | :email => @mary_email) |
|
68 | 68 | User.stub!(:find_by_email). |
|
69 | 69 | with(@mary_email, {:order => "created_at DESC"}). |
|
70 | 70 | and_return(@mary_first) |
|
71 | 71 | end |
|
72 | 72 | |
|
73 | 73 | class User |
|
74 | 74 | public :enough_time_interval_between_same_email_registrations |
|
75 | 75 | end |
|
76 | 76 | |
|
77 | 77 | it "should not be allowed if the time interval is less than 5 mins" do |
|
78 | 78 | time_now = @time_first_register + 4.minutes |
|
79 | 79 | Time.stub!(:now).and_return(time_now) |
|
80 | 80 | |
|
81 | 81 | @mary_second.enough_time_interval_between_same_email_registrations |
|
82 | 82 | @mary_second.errors.length.should_not be_zero |
|
83 | 83 | end |
|
84 | 84 | |
|
85 | 85 | it "should be allowed if the time interval is more than 5 mins" do |
|
86 | 86 | time_now = @time_first_register + 6.minutes |
|
87 | 87 | Time.stub!(:now).and_return(time_now) |
|
88 | 88 | |
|
89 | 89 | @mary_second.enough_time_interval_between_same_email_registrations |
|
90 | 90 | @mary_second.errors.length.should be_zero |
|
91 | 91 | end |
|
92 | 92 | |
|
93 | 93 | end |
|
94 | 94 | |
|
95 | 95 | describe User, "as a class" do |
|
96 | 96 | |
|
97 | 97 | it "should be able to generate random password" do |
|
98 | 98 | password1 = User.random_password |
|
99 | 99 | password2 = User.random_password |
|
100 | 100 | |
|
101 | 101 | password1.should_not == password2 |
|
102 | 102 | end |
|
103 | 103 | |
|
104 | 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