Description:
[web] add test result page git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@159 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r79:bfeeb31156cf - - 5 files changed: 41 inserted, 4 deleted

@@ -1,53 +1,71
1 class TestController < ApplicationController
1 class TestController < ApplicationController
2
2
3 before_filter :authenticate
3 before_filter :authenticate
4
4
5 - verify :method => :post, :only => [:test_submit],
5 + verify :method => :post, :only => [:submit],
6 :redirect_to => { :action => :index }
6 :redirect_to => { :action => :index }
7
7
8 def index
8 def index
9 @user = User.find(session[:user_id])
9 @user = User.find(session[:user_id])
10 prepare_index_information
10 prepare_index_information
11 @test_requests = @user.test_requests
11 @test_requests = @user.test_requests
12 end
12 end
13
13
14 def submit
14 def submit
15 @user = User.find(session[:user_id])
15 @user = User.find(session[:user_id])
16 test_request = TestRequest.new_from_form_params(@user,params[:test_request])
16 test_request = TestRequest.new_from_form_params(@user,params[:test_request])
17 if test_request.save
17 if test_request.save
18 redirect_to :action => 'index'
18 redirect_to :action => 'index'
19 else
19 else
20 flash[:notice] = 'Error saving your test submission'
20 flash[:notice] = 'Error saving your test submission'
21 redirect_to :action => 'index'
21 redirect_to :action => 'index'
22 end
22 end
23 end
23 end
24
24
25 def read
25 def read
26 user = User.find(session[:user_id])
26 user = User.find(session[:user_id])
27 - test_request = TestRequest.find(params[:id])
27 + begin
28 - if test_request.user_id != user.id
28 + test_request = TestRequest.find(params[:id])
29 + rescue
30 + test_request = nil
31 + end
32 + if test_request==nil or test_request.user_id != user.id
29 flash[:notice] = 'Invalid output'
33 flash[:notice] = 'Invalid output'
30 redirect_to :action => 'index'
34 redirect_to :action => 'index'
31 return
35 return
32 end
36 end
33 if test_request.output_file_name!=nil
37 if test_request.output_file_name!=nil
34 data = File.open(test_request.output_file_name).read(2048)
38 data = File.open(test_request.output_file_name).read(2048)
35 if data==nil
39 if data==nil
36 data=""
40 data=""
37 end
41 end
38 send_data(data,
42 send_data(data,
39 {:filename => 'output.txt',
43 {:filename => 'output.txt',
40 :type => 'text/plain'})
44 :type => 'text/plain'})
41 return
45 return
42 end
46 end
43 redirect_to :action => 'index'
47 redirect_to :action => 'index'
44 end
48 end
49 +
50 + def result
51 + @user = User.find(session[:user_id])
52 + begin
53 + @test_request = TestRequest.find(params[:id])
54 + rescue
55 + @test_request = nil
56 + end
57 + if @test_request==nil or @test_request.user_id != @user.id
58 + flash[:notice] = 'Invalid request'
59 + redirect_to :action => 'index'
60 + return
61 + end
62 + end
45
63
46 protected
64 protected
47
65
48 def prepare_index_information
66 def prepare_index_information
49 @submissions = Submission.find_last_for_all_available_problems(@user.id)
67 @submissions = Submission.find_last_for_all_available_problems(@user.id)
50 @problems = @submissions.collect { |submission| submission.problem }
68 @problems = @submissions.collect { |submission| submission.problem }
51 end
69 end
52
70
53 end
71 end
@@ -1,2 +1,11
1 module TestHelper
1 module TestHelper
2 +
3 + def read_textfile(fname,max_size=2048)
4 + begin
5 + File.open(fname).read(max_size)
6 + rescue
7 + nil
8 + end
9 + end
10 +
2 end
11 end
@@ -1,14 +1,15
1 %tr{:class => (test_request_counter%2==0) ? "info-even" : "info-odd"}
1 %tr{:class => (test_request_counter%2==0) ? "info-even" : "info-odd"}
2 %td{:align => "center"}
2 %td{:align => "center"}
3 = format_short_time(test_request.submitted_at)
3 = format_short_time(test_request.submitted_at)
4 %td= test_request.problem.full_name
4 %td= test_request.problem.full_name
5 %td{:align => "center"}
5 %td{:align => "center"}
6 = test_request.submission.number
6 = test_request.submission.number
7 %td{:align => "center"}
7 %td{:align => "center"}
8 = test_request.status_str
8 = test_request.status_str
9 %td= simple_format((test_request.running_stat or ''))
9 %td= simple_format((test_request.running_stat or ''))
10 %td{:align => "center"}
10 %td{:align => "center"}
11 - if test_request.output_file_name!=nil
11 - if test_request.output_file_name!=nil
12 = link_to '[output]', :action => 'read', :id => test_request.id
12 = link_to '[output]', :action => 'read', :id => test_request.id
13 %td= test_request.grader_comment or ''
13 %td= test_request.grader_comment or ''
14 %td= simple_format((test_request.compiler_message or ''))
14 %td= simple_format((test_request.compiler_message or ''))
15 + %td= link_to '[view]', :action => 'result', :id => test_request.id
@@ -53,30 +53,31
53 </tr>
53 </tr>
54 <tr>
54 <tr>
55 <td>Input data:</td>
55 <td>Input data:</td>
56 <td><%= f.file_field :input_file %></td>
56 <td><%= f.file_field :input_file %></td>
57 <tr>
57 <tr>
58 <td colspan="2">
58 <td colspan="2">
59 <%= submit_tag 'submit' %>
59 <%= submit_tag 'submit' %>
60 </td>
60 </td>
61 </tr>
61 </tr>
62 </table>
62 </table>
63 <% end %>
63 <% end %>
64
64
65 <h3>Previous requests</h3>
65 <h3>Previous requests</h3>
66
66
67 <table class="info">
67 <table class="info">
68 <tr class="info-head">
68 <tr class="info-head">
69 <th>at</th>
69 <th>at</th>
70 <th>problem</th>
70 <th>problem</th>
71 <th>sub #</th>
71 <th>sub #</th>
72 <th>status</th>
72 <th>status</th>
73 <th>running stat</th>
73 <th>running stat</th>
74 <th>output (first 2kb)</th>
74 <th>output (first 2kb)</th>
75 <th>grading comment</th>
75 <th>grading comment</th>
76 <th>compiler message</th>
76 <th>compiler message</th>
77 + <th>Detail</th>
77 </tr>
78 </tr>
78 <%= render :partial => 'test_request', :collection => @test_requests %>
79 <%= render :partial => 'test_request', :collection => @test_requests %>
79 </table>
80 </table>
80
81
81 <% end %>
82 <% end %>
82
83
@@ -1,42 +1,50
1
1
2 require File.dirname(__FILE__) + '/../spec_helper'
2 require File.dirname(__FILE__) + '/../spec_helper'
3
3
4 describe MainController do
4 describe MainController do
5
5
6 before(:each) do
6 before(:each) do
7 @problem = mock(Problem, :name => 'test')
7 @problem = mock(Problem, :name => 'test')
8 @language = mock(Language, :name => 'cpp', :ext => 'cpp')
8 @language = mock(Language, :name => 'cpp', :ext => 'cpp')
9 @submission = mock(Submission,
9 @submission = mock(Submission,
10 :id => 1,
10 :id => 1,
11 :user_id => 1,
11 :user_id => 1,
12 :problem => @problem,
12 :problem => @problem,
13 :language => @language,
13 :language => @language,
14 :source => 'sample source',
14 :source => 'sample source',
15 :compiler_message => 'none')
15 :compiler_message => 'none')
16 @user = mock(User, :id => 1, :login => 'john')
16 @user = mock(User, :id => 1, :login => 'john')
17 - Submission.should_receive(:find).with(@submission.id.to_s).and_return(@submission)
17 + end
18 +
19 + it "should redirect user to login page when unlogged-in user try to access main/list" do
20 + get 'list'
21 + response.should redirect_to(:action => 'login')
18 end
22 end
19
23
20 it "should let user sees her own source" do
24 it "should let user sees her own source" do
25 + Submission.should_receive(:find).with(@submission.id.to_s).and_return(@submission)
21 get 'source', {:id => @submission.id}, {:user_id => 1}
26 get 'source', {:id => @submission.id}, {:user_id => 1}
22 response.should be_success
27 response.should be_success
23 end
28 end
24
29
25 it "should let user sees her own compiler message" do
30 it "should let user sees her own compiler message" do
31 + Submission.should_receive(:find).with(@submission.id.to_s).and_return(@submission)
26 get 'compiler_msg', {:id => @submission.id}, {:user_id => 1}
32 get 'compiler_msg', {:id => @submission.id}, {:user_id => 1}
27 response.should be_success
33 response.should be_success
28 end
34 end
29
35
30 it "should not let user sees other user's source" do
36 it "should not let user sees other user's source" do
37 + Submission.should_receive(:find).with(@submission.id.to_s).and_return(@submission)
31 get 'source', {:id => @submission.id}, {:user_id => 2}
38 get 'source', {:id => @submission.id}, {:user_id => 2}
32 flash[:notice].should =~ /[Ee]rror/
39 flash[:notice].should =~ /[Ee]rror/
33 response.should redirect_to(:action => 'list')
40 response.should redirect_to(:action => 'list')
34 end
41 end
35
42
36 it "should not let user sees other user's compiler message" do
43 it "should not let user sees other user's compiler message" do
44 + Submission.should_receive(:find).with(@submission.id.to_s).and_return(@submission)
37 get 'compiler_msg', {:id => @submission.id}, {:user_id => 2}
45 get 'compiler_msg', {:id => @submission.id}, {:user_id => 2}
38 flash[:notice].should =~ /[Ee]rror/
46 flash[:notice].should =~ /[Ee]rror/
39 response.should redirect_to(:action => 'list')
47 response.should redirect_to(:action => 'list')
40 end
48 end
41
49
42 end
50 end
You need to be logged in to leave comments. Login now