# HG changeset patch # User jittat # Date 2008-04-03 19:33:32 # Node ID bfeeb31156cf4bdbabc1aa1e567c3b56c4c8d94f # Parent 822d6c35aada395ee1f8a80b3349a99d2b931bc2 [web] add test result page git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@159 6386c4cd-e34a-4fa8-8920-d93eb39b512e diff --git a/app/controllers/test_controller.rb b/app/controllers/test_controller.rb --- a/app/controllers/test_controller.rb +++ b/app/controllers/test_controller.rb @@ -2,7 +2,7 @@ before_filter :authenticate - verify :method => :post, :only => [:test_submit], + verify :method => :post, :only => [:submit], :redirect_to => { :action => :index } def index @@ -24,8 +24,12 @@ def read user = User.find(session[:user_id]) - test_request = TestRequest.find(params[:id]) - if test_request.user_id != user.id + begin + test_request = TestRequest.find(params[:id]) + rescue + test_request = nil + end + if test_request==nil or test_request.user_id != user.id flash[:notice] = 'Invalid output' redirect_to :action => 'index' return @@ -42,6 +46,20 @@ end redirect_to :action => 'index' end + + def result + @user = User.find(session[:user_id]) + begin + @test_request = TestRequest.find(params[:id]) + rescue + @test_request = nil + end + if @test_request==nil or @test_request.user_id != @user.id + flash[:notice] = 'Invalid request' + redirect_to :action => 'index' + return + end + end protected diff --git a/app/helpers/test_helper.rb b/app/helpers/test_helper.rb --- a/app/helpers/test_helper.rb +++ b/app/helpers/test_helper.rb @@ -1,2 +1,11 @@ module TestHelper + + def read_textfile(fname,max_size=2048) + begin + File.open(fname).read(max_size) + rescue + nil + end + end + end diff --git a/app/views/test/_test_request.html.haml b/app/views/test/_test_request.html.haml --- a/app/views/test/_test_request.html.haml +++ b/app/views/test/_test_request.html.haml @@ -12,3 +12,4 @@ = link_to '[output]', :action => 'read', :id => test_request.id %td= test_request.grader_comment or '' %td= simple_format((test_request.compiler_message or '')) + %td= link_to '[view]', :action => 'result', :id => test_request.id diff --git a/app/views/test/index.html.erb b/app/views/test/index.html.erb --- a/app/views/test/index.html.erb +++ b/app/views/test/index.html.erb @@ -74,6 +74,7 @@ output (first 2kb) grading comment compiler message + Detail <%= render :partial => 'test_request', :collection => @test_requests %> diff --git a/spec/controllers/main_controller_spec.rb b/spec/controllers/main_controller_spec.rb --- a/spec/controllers/main_controller_spec.rb +++ b/spec/controllers/main_controller_spec.rb @@ -14,26 +14,34 @@ :source => 'sample source', :compiler_message => 'none') @user = mock(User, :id => 1, :login => 'john') - Submission.should_receive(:find).with(@submission.id.to_s).and_return(@submission) + end + + it "should redirect user to login page when unlogged-in user try to access main/list" do + get 'list' + response.should redirect_to(:action => 'login') end it "should let user sees her own source" do + Submission.should_receive(:find).with(@submission.id.to_s).and_return(@submission) get 'source', {:id => @submission.id}, {:user_id => 1} response.should be_success end it "should let user sees her own compiler message" do + Submission.should_receive(:find).with(@submission.id.to_s).and_return(@submission) get 'compiler_msg', {:id => @submission.id}, {:user_id => 1} response.should be_success end it "should not let user sees other user's source" do + Submission.should_receive(:find).with(@submission.id.to_s).and_return(@submission) get 'source', {:id => @submission.id}, {:user_id => 2} flash[:notice].should =~ /[Ee]rror/ response.should redirect_to(:action => 'list') end it "should not let user sees other user's compiler message" do + Submission.should_receive(:find).with(@submission.id.to_s).and_return(@submission) get 'compiler_msg', {:id => @submission.id}, {:user_id => 2} flash[:notice].should =~ /[Ee]rror/ response.should redirect_to(:action => 'list')