workbench: Add Collections controller tests.
authorBrett Smith <brett@curoverse.com>
Wed, 30 Apr 2014 14:38:08 +0000 (10:38 -0400)
committerBrett Smith <brett@curoverse.com>
Mon, 5 May 2014 19:18:56 +0000 (15:18 -0400)
Working to establish a baseline of behavior before I go mucking with
this.

apps/workbench/app/controllers/collections_controller.rb
apps/workbench/test/functional/collections_controller_test.rb

index 3089a1ed3fecea72797904b604df0b69e55b1577..eae7a77071dec213dfc28d01111bb4d68e724da5 100644 (file)
@@ -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
index f4a02c1f4c13c251ad0bd88e90322b669042a46f..d82ba6bb015586dd6c1239f066a4e8a062043282 100644 (file)
@@ -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