From: Brett Smith Date: Wed, 30 Apr 2014 14:38:08 +0000 (-0400) Subject: workbench: Add Collections controller tests. X-Git-Tag: 1.1.0~2596^2~16^2~2 X-Git-Url: https://git.arvados.org/arvados.git/commitdiff_plain/256142bddd532e2834b4e7f79c3146009e23059e workbench: Add Collections controller tests. Working to establish a baseline of behavior before I go mucking with this. --- diff --git a/apps/workbench/app/controllers/collections_controller.rb b/apps/workbench/app/controllers/collections_controller.rb index 3089a1ed3f..eae7a77071 100644 --- a/apps/workbench/app/controllers/collections_controller.rb +++ b/apps/workbench/app/controllers/collections_controller.rb @@ -95,7 +95,7 @@ class CollectionsController < ApplicationController Rack::Mime::MIME_TYPES[ext] || 'application/octet-stream' self.response.headers['Content-Length'] = params[:size] if params[:size] self.response.headers['Content-Disposition'] = params[:disposition] if params[:disposition] - self.response_body = FileStreamer.new opts + self.response_body = file_enumerator opts end def show @@ -161,6 +161,11 @@ class CollectionsController < ApplicationController end protected + + def file_enumerator(opts) + FileStreamer.new opts + end + class FileStreamer def initialize(opts={}) @opts = opts diff --git a/apps/workbench/test/functional/collections_controller_test.rb b/apps/workbench/test/functional/collections_controller_test.rb index f4a02c1f4c..d82ba6bb01 100644 --- a/apps/workbench/test/functional/collections_controller_test.rb +++ b/apps/workbench/test/functional/collections_controller_test.rb @@ -1,4 +1,49 @@ require 'test_helper' class CollectionsControllerTest < ActionController::TestCase + def collection_params(collection_name, file_name=nil) + uuid = api_fixture('collections')[collection_name.to_s]['uuid'] + params = {uuid: uuid, id: uuid} + params[:file] = file_name if file_name + params + end + + def expected_contents(params, token) + unless token.is_a? String + token = params[:api_token] || token[:arvados_api_token] + end + [token, params[:uuid], params[:file]].join('/') + end + + # Mock the collection file reader to avoid external calls and return + # a predictable string. + CollectionsController.class_eval do + def file_enumerator(opts) + [[opts[:arvados_api_token], opts[:uuid], opts[:file]].join('/')] + end + end + + test "viewing a collection" do + params = collection_params(:foo_file) + sess = session_for(:active) + get(:show, params, sess) + assert_response :success + assert_equal([['.', 'foo', 3]], assigns(:object).files) + end + + test "getting a file from Keep" do + params = collection_params(:foo_file, 'foo') + sess = session_for(:active) + get(:show_file, params, sess) + assert_response :success + assert_equal(expected_contents(params, sess), @response.body, + "failed to get a correct file from Keep") + end + + test "can't get a file from Keep without permission" do + params = collection_params(:foo_file, 'foo') + sess = session_for(:spectator) + get(:show_file, params, sess) + assert_includes([403, 422], @response.code.to_i) + end end