<commit_before># Copyright © 2012 The Pennsylvania State University
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require 'spec_helper'

describe GenericFilesController do
  before do
    controller.stub(:has_access?).and_return(true)

    @user = FactoryGirl.find_or_create(:user)
    sign_in @user
    User.any_instance.stub(:groups).and_return([])
    controller.stub(:clear_session_user) ## Don't clear out the authenticated session
  end
  describe "#create" do
    before do
      @file_count = GenericFile.count
      @mock = GenericFile.new({:pid => 'test:123'})
      GenericFile.stub(:new).and_return(@mock)
    end
    after do
      begin
        Batch.find("sample:batch_id").delete
      rescue
      end                                         
      @mock.delete unless @mock.inner_object.class == ActiveFedora::UnsavedDigitalObject 
    end

    it "should render error the file wasn't actually a file" do
      file = 'hello'
      xhr :post, :create, :files=>[file], :Filename=>"The World", :batch_id=>'sample:batch_id', :permission=>{"group"=>{"public"=>"read"} }, :terms_of_service => '1'
      response.status.should == 422
      JSON.parse(response.body).first['error'].should match(/no file for upload/i)
    end

    it "should spawn a content deposit event job" do
      file = fixture_file_upload('/world.png','image/png')
      s1 = stub('one')
      ContentDepositEventJob.should_receive(:new).with('test:123', '<EMAIL>').and_return(s1)
      Sufia.queue.should_receive(:push).with(s1).once

      s2 = stub('one')
      CharacterizeJob.should_receive(:new).with('test:123').and_return(s2)
      Sufia.queue.should_receive(:push).with(s2).once
      xhr :post, :create, :files=>[file], :Filename=>"The world", :batch_id => "sample:batch_id", :permission=>{"group"=>{"public"=>"read"} }, :terms_of_service => '1'
    end

    it "should expand zip files" do
      file = fixture_file_upload('/icons.zip','application/zip')
      s1 = stub('one')
      ContentDepositEventJob.should_receive(:new).with('test:123', '<EMAIL>').and_return(s1)
      Sufia.queue.should_receive(:push).with(s1).once

      s2 = stub('one')
      CharacterizeJob.should_receive(:new).with('test:123').and_return(s2)
      Sufia.queue.should_receive(:push).with(s2).once

      s3 = stub('one')
      UnzipJob.should_receive(:new).with('test:123').and_return(s3)
      Sufia.queue.should_receive(:push).with(s3).once

      xhr :post, :create, :files=>[file], :Filename=>"The world", :batch_id => "sample:batch_id", :permission=>{"group"=>{"public"=>"read"} }, :terms_of_service => '1'
    end

    it "should create and save a file asset from the given params" do
      file = fixture_file_upload('/world.png','image/png')
      xhr :post, :create, :files=>[file], :Filename=>"The world", :batch_id => "sample:batch_id", :permission=>{"group"=>{"public"=>"read"} }, :terms_of_service => '1'
      response.should be_success
      GenericFile.count.should == @file_count + 1

      saved_file = GenericFile.find('test:123')

      # This is confirming that the correct file was attached
      saved_file.label.should == 'world.png'
      saved_file.content.checksum.should == 'f794b23c0c6fe1083d0ca8b58261a078cd968967'
      saved_file.content.dsChecksumValid.should be_true

      # Confirming that date_uploaded and date_modified were set
      saved_file.date_uploaded.should have_at_least(1).items
      saved_file.date_modified.should have_at_least(1).items
    end

    it "should record what user created the first version of content" do
      #GenericFile.any_instance.stub(:to_solr).and_return({})
      file = fixture_file_upload('/world.png','image/png')
      xhr :post, :create, :files=>[file], :Filename=>"The world", :terms_of_service=>"1"
<commit_msg>GF controller spec should not assume uploaded and modified dates are arrays, as they are unique values<commit_after># Copyright © 2012 The Pennsylvania State University
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require 'spec_helper'

describe GenericFilesController do
  before do
    controller.stub(:has_access?).and_return(true)

    @user = FactoryGirl.find_or_create(:user)
    sign_in @user
    User.any_instance.stub(:groups).and_return([])
    controller.stub(:clear_session_user) ## Don't clear out the authenticated session
  end
  describe "#create" do
    before do
      @file_count = GenericFile.count
      @mock = GenericFile.new({:pid => 'test:123'})
      GenericFile.stub(:new).and_return(@mock)
    end
    after do
      begin
        Batch.find("sample:batch_id").delete
      rescue
      end                                         
      @mock.delete unless @mock.inner_object.class == ActiveFedora::UnsavedDigitalObject 
    end

    it "should render error the file wasn't actually a file" do
      file = 'hello'
      xhr :post, :create, :files=>[file], :Filename=>"The World", :batch_id=>'sample:batch_id', :permission=>{"group"=>{"public"=>"read"} }, :terms_of_service => '1'
      response.status.should == 422
      JSON.parse(response.body).first['error'].should match(/no file for upload/i)
    end

    it "should spawn a content deposit event job" do
      file = fixture_file_upload('/world.png','image/png')
      s1 = stub('one')
      ContentDepositEventJob.should_receive(:new).with('test:123', '<EMAIL>').and_return(s1)
      Sufia.queue.should_receive(:push).with(s1).once

      s2 = stub('one')
      CharacterizeJob.should_receive(:new).with('test:123').and_return(s2)
      Sufia.queue.should_receive(:push).with(s2).once
      xhr :post, :create, :files=>[file], :Filename=>"The world", :batch_id => "sample:batch_id", :permission=>{"group"=>{"public"=>"read"} }, :terms_of_service => '1'
    end

    it "should expand zip files" do
      file = fixture_file_upload('/icons.zip','application/zip')
      s1 = stub('one')
      ContentDepositEventJob.should_receive(:new).with('test:123', '<EMAIL>').and_return(s1)
      Sufia.queue.should_receive(:push).with(s1).once

      s2 = stub('one')
      CharacterizeJob.should_receive(:new).with('test:123').and_return(s2)
      Sufia.queue.should_receive(:push).with(s2).once

      s3 = stub('one')
      UnzipJob.should_receive(:new).with('test:123').and_return(s3)
      Sufia.queue.should_receive(:push).with(s3).once

      xhr :post, :create, :files=>[file], :Filename=>"The world", :batch_id => "sample:batch_id", :permission=>{"group"=>{"public"=>"read"} }, :terms_of_service => '1'
    end

    it "should create and save a file asset from the given params" do
      date_today = Date.today
      Date.stub(:today).and_return(date_today)
      file = fixture_file_upload('/world.png','image/png')
      xhr :post, :create, :files=>[file], :Filename=>"The world", :batch_id => "sample:batch_id", :permission=>{"group"=>{"public"=>"read"} }, :terms_of_service => '1'
      response.should be_success
      GenericFile.count.should == @file_count + 1

      saved_file = GenericFile.find('test:123')

      # This is confirming that the correct file was attached
      saved_file.label.should == 'world.png'
      saved_file.content.checksum.should == 'f794b23c0c6fe1083d0ca8b58261a078cd968967'
      saved_file.content.dsChecksumValid.should be_true

      # Confirming that date_uploaded and date_modified were set
      saved_file.date_uploaded.should == date_today
      saved_file.date_modified.should == date_today
    end

    it "should record what user created the first version of content" do
      #GenericFile.any_instance.stub(:to_solr).and_return({})
      file = fixture_file_upload('/world.png','image/png')
      xhr :post, :create, :files=>[file], :Filename=>"The world", :terms_of_service=>"1"

{'fraction_non_alphanumeric': 0.13867772402672196, 'fraction_numerical': 0.018659295093296474, 'mean_word_length': 3.872615039281706, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5277415', 'n_tokens_mistral': 2822, 'n_tokens_neox': 2671, 'n_words': 635}