<commit_before>require 'test_helper'

class ModelsControllerTest < ActionController::TestCase
  
  fixtures :all
  
  include AuthenticatedTestHelper
  include RestTestCases
  include SharingFormTestHelper
  
  def setup
    login_as(:model_owner)
    @object=models(:teusink)
  end
  
  test "should get index" do
    get :index
    assert_response :success
    assert_not_nil assigns(:models)
  end    
  
  test "should not create model with file url" do
    file_path=File.expand_path(__FILE__) #use the current file
    file_url="file://"+file_path
    uri=URI.parse(file_url)    
   
    assert_no_difference('Model.count') do
      assert_no_difference('ContentBlob.count') do
        post :create, :model => { :title=>"Test",:data_url=>uri.to_s}, :sharing=>valid_sharing
      end
    end
    assert_not_nil flash[:error]    
  end

  test 'creators show in list item' do
    p1=Factory :person
    p2=Factory :person
    model=Factory(:model,:title=>"ZZZZZ",:creators=>[p2],:contributor=>p1.user,:policy=>Factory(:public_policy, :access_type=>Policy::VISIBLE))

    get :index,:page=>"Z"

    #check the test is behaving as expected:
    assert_equal p1.user,model.contributor
    assert model.creators.include?(p2)
    assert_select ".list_item_title a[href=?]",model_path(model),"ZZZZZ","the data file for this test should appear as a list item"

    #check for avatars
    assert_select ".list_item_avatar" do
      assert_select "a[href=?]",person_path(p2) do
        assert_select "img"
      end
      assert_select "a[href=?]",person_path(p1) do
        assert_select "img"
      end
    end
  end
  
  test "shouldn't show hidden items in index" do
    login_as(:aaron)
    get :index, :page => "all"
    assert_response :success
    assert_equal assigns(:models).sort_by(&:id), Authorization.authorize_collection("view", assigns(:models), users(:aaron)).sort_by(&:id), "models haven't been authorized properly"
  end

  test "should contain only model assays " do
    login_as(:aaron)
    assay = assays(:metabolomics_assay)
    assert_equal false, assay.is_modelling?
    assay = assays(:modelling_assay_with_data_and_relationship)
    assert_equal true, assay.is_modelling?

  end

  test "should show only modelling assays in associate assay form" do
    login_as(:model_owner)
    get :new
    assert_response :success
    assert_select 'select#possible_assays' do
      assert_select "option", :text=>/Select Assay .../,:count=>1
      assert_select "option", :text=>/Modelling Assay/,:count=>1
      assert_select "option", :text=>/Metabolomics Assay/,:count=>0
    end
  end

  test "fail gracefullly when trying to access a missing model" do
    get :show,:id=>99999
    assert_redirected_to models_path
    assert_not_nil flash[:error]
  end
  
  test "should get new" do
    get :new    
    assert_response :success
    assert_select "h1",:text=>"New Model"
  end    
  
  test "should correctly handle bad data url" do
    model={:title=>"Test",:data_url=>"http://sdfsdfkh.com/sdfsd.png",:projects=>[projects(:sysmo_project)]}
    assert_no_difference('Model.count') do
      assert_no_difference('ContentBlob.count') do
        post :create, :model => model, :sharing=>valid_sharing
      end
    end
    assert_not_nil flash.now[:error]
  end
  
  test "should not create invalid model" do
    model={:title=>"Test"}
    assert_no_difference('Model.count') do
      assert_no_difference('ContentBlob.count') do
        post :create, :model => model, :sharing=>valid_sharing
      end
    end
    assert_not_nil flash.now[:error]
  end
    test "associates assay" do
    login_as(:model_owner) #can edit assay_can_edit_by_my_first_sop_owner
    m = models(:teusink)
    original_assay = assays(:assay_with_a_model)
    asset_ids = original_assay.related_asset_ids 'Model'
    assert asset_ids.include? m.id
    new_assay=assays(:modelling_assay)
    new_asset_ids = new_assay.related_asset_ids 'Model'
    assert !new_asset_ids.include?(m.id)

    put :update, :id => m, :model =>{}, :assay_ids=>[new_assay.id.to_s]

    assert_redirected_to model_path(m)
    m.reload
    original_assay.reload
    new_assay.reload
    assert !original_assay.related_asset_ids('Model').include?(m.id)
    assert new_assay.related_asset_ids('Model').include?(m.id)
    end

  test "associate sample" do
     # assign to a new model
     model_with_samples = valid_model
     model_with_samples[:sample_ids] = [Factory(:sample,:title=>"newTestSample",:contributor=> User.current_user).id]
     assert_difference("Model.count") do
       post :create,:model => model_with_samples,:content_blob=>{:file_0=>fixture_file_upload('files/little_file.txt',Mime::TEXT)}
     end

    m = assigns(:model)
    assert_equal "newTestSample", m.samples.first.title

    #edit associations of samples to an existing model
    put :update,:id=> m.id, :model => {:sample_ids=> [Factory(:sample,:title=>"editTestSample",:contributor=> User.current_user).id]}
    m = assigns(:model)
    assert_equal "editTestSample", m.samples.first.title
  end

  test "should create model" do
    login_as(:model_owner)
    assay = assays(:modelling_assay)
    assert_difference('Model.count') do
      post :create, :model => valid_model,:content_blob=>{:file_0=>fixture_file_upload('files/little_file.txt',Mime::TEXT)}, :sharing=>valid_sharing, :assay_ids => [assay.id.to_s]
    end
    
    assert_redirected_to model_path(assigns(:model))
    assay.reload
    assert assay.related_asset_ids('Model').include? assigns(:model).id
  end
  
  def test_missing_sharing_should_default_to_private<commit_msg>tests for creating/new version of  model with image<commit_after>require 'test_helper'

class ModelsControllerTest < ActionController::TestCase
  
  fixtures :all
  
  include AuthenticatedTestHelper
  include RestTestCases
  include SharingFormTestHelper
  
  def setup
    login_as(:model_owner)
    @object=models(:teusink)
  end
  
  test "should get index" do
    get :index
    assert_response :success
    assert_not_nil assigns(:models)
  end    
  
  test "should not create model with file url" do
    file_path=File.expand_path(__FILE__) #use the current file
    file_url="file://"+file_path
    uri=URI.parse(file_url)    
   
    assert_no_difference('Model.count') do
      assert_no_difference('ContentBlob.count') do
        post :create, :model => { :title=>"Test",:data_url=>uri.to_s}, :sharing=>valid_sharing
      end
    end
    assert_not_nil flash[:error]    
  end

  test 'creators show in list item' do
    p1=Factory :person
    p2=Factory :person
    model=Factory(:model,:title=>"ZZZZZ",:creators=>[p2],:contributor=>p1.user,:policy=>Factory(:public_policy, :access_type=>Policy::VISIBLE))

    get :index,:page=>"Z"

    #check the test is behaving as expected:
    assert_equal p1.user,model.contributor
    assert model.creators.include?(p2)
    assert_select ".list_item_title a[href=?]",model_path(model),"ZZZZZ","the data file for this test should appear as a list item"

    #check for avatars
    assert_select ".list_item_avatar" do
      assert_select "a[href=?]",person_path(p2) do
        assert_select "img"
      end
      assert_select "a[href=?]",person_path(p1) do
        assert_select "img"
      end
    end
  end
  
  test "shouldn't show hidden items in index" do
    login_as(:aaron)
    get :index, :page => "all"
    assert_response :success
    assert_equal assigns(:models).sort_by(&:id), Authorization.authorize_collection("view", assigns(:models), users(:aaron)).sort_by(&:id), "models haven't been authorized properly"
  end

  test "should contain only model assays " do
    login_as(:aaron)
    assay = assays(:metabolomics_assay)
    assert_equal false, assay.is_modelling?
    assay = assays(:modelling_assay_with_data_and_relationship)
    assert_equal true, assay.is_modelling?

  end

  test "should show only modelling assays in associate assay form" do
    login_as(:model_owner)
    get :new
    assert_response :success
    assert_select 'select#possible_assays' do
      assert_select "option", :text=>/Select Assay .../,:count=>1
      assert_select "option", :text=>/Modelling Assay/,:count=>1
      assert_select "option", :text=>/Metabolomics Assay/,:count=>0
    end
  end

  test "fail gracefullly when trying to access a missing model" do
    get :show,:id=>99999
    assert_redirected_to models_path
    assert_not_nil flash[:error]
  end
  
  test "should get new" do
    get :new    
    assert_response :success
    assert_select "h1",:text=>"New Model"
  end    
  
  test "should correctly handle bad data url" do
    model={:title=>"Test",:data_url=>"http://sdfsdfkh.com/sdfsd.png",:projects=>[projects(:sysmo_project)]}
    assert_no_difference('Model.count') do
      assert_no_difference('ContentBlob.count') do
        post :create, :model => model, :sharing=>valid_sharing
      end
    end
    assert_not_nil flash.now[:error]
  end
  
  test "should not create invalid model" do
    model={:title=>"Test"}
    assert_no_difference('Model.count') do
      assert_no_difference('ContentBlob.count') do
        post :create, :model => model, :sharing=>valid_sharing
      end
    end
    assert_not_nil flash.now[:error]
  end
    test "associates assay" do
    login_as(:model_owner) #can edit assay_can_edit_by_my_first_sop_owner
    m = models(:teusink)
    original_assay = assays(:assay_with_a_model)
    asset_ids = original_assay.related_asset_ids 'Model'
    assert asset_ids.include? m.id
    new_assay=assays(:modelling_assay)
    new_asset_ids = new_assay.related_asset_ids 'Model'
    assert !new_asset_ids.include?(m.id)

    put :update, :id => m, :model =>{}, :assay_ids=>[new_assay.id.to_s]

    assert_redirected_to model_path(m)
    m.reload
    original_assay.reload
    new_assay.reload
    assert !original_assay.related_asset_ids('Model').include?(m.id)
    assert new_assay.related_asset_ids('Model').include?(m.id)
    end

  test "associate sample" do
     # assign to a new model
     model_with_samples = valid_model
     model_with_samples[:sample_ids] = [Factory(:sample,:title=>"newTestSample",:contributor=> User.current_user).id]
     assert_difference("Model.count") do
       post :create,:model => model_with_samples,:content_blob=>{:file_0=>fixture_file_upload('files/little_file.txt',Mime::TEXT)}
     end

    m = assigns(:model)
    assert_equal "newTestSample", m.samples.first.title

    #edit associations of samples to an existing model
    put :update,:id=> m.id, :model => {:sample_ids=> [Factory(:sample,:title=>"editTestSample",:contributor=> User.current_user).id]}
    m = assigns(:model)
    assert_equal "editTestSample", m.samples.first.title
  end

  test "should create model" do
    login_as(:model_owner)
    assay = assays(:modelling_assay)
    assert_difference('Model.count') do
      post :create, :model => valid_model,:content_blob=>{:file_0=>fixture_file_upload('files/little_file.txt',Mime::TEXT)}, :sharing=>valid_sharing, :assay_ids => [assay.id.to_s]
    end
    
    assert_redirected_to model_path(assigns(:model))
    assay.reload
    assert assay.related_asset_ids('Model').include? assigns(:model).id
  end

  test "should create model with image" do
      login_as(:model_owner)
      assert_difference('Model.count') do
          post :create, :model => valid_model,:content_blob=>{:file_0=>fixture_file_upload('files/little_file.txt',Mime::TEXT)}, :sharing=>valid_sharing, :model_image => {:image_file => fixture_file_upload('files/file_picture.png', 'image/png')}
      end

      model = assigns(:model)
      assert_redirected_to model_path(model)
      assert_equal "file_picture.png", model.model_image.original_filename
  end

  test "should create model with image and without content_blob" do
      login_as(:model_owner)
      assert_difference('Model.count') do
          post :create, :model => valid_model, :sharing=>valid_sharing, :model_image => {:image_file => fixture_file_upload('files/file_picture.png', 'image/png')}
      end

      model = assigns(:model)
      assert_redirected_to model_path(model)
      assert_equal 'Test', model.title
      assert_equal "file_picture.png", model.model_image.original_filename
  end

  test "should not create model without image and without content_blob" do
      login_as(:model_owner)
      assert_no_difference('Model.count') do
          post :create, :model => valid_model, :sharing=>valid_sharing
      end
      assert_not_nil flash[:error]
  end

  test "should create model version with image" do
       m=models(:model_with_format_and_type)
       assert_difference("Model::Version.count", 1) do
         post :new_version, :id=>m, :model=>{},:content_blob=>{:file_0=>fixture_file_upload('files/little_file.txt',Mime::TEXT)}, :revision_comment=>"This is a new revision", :model_image => {:image_file => fixture_file_upload('files/file_picture.png', 'image/png')}
       end

       assert_redirected_to model_path(m)
       assert assigns(:model)

       m=Model.find(m.id)
       assert_equal 2,m.versions.size
       assert_equal 2,m.version
       assert_equal "little_file.txt",m.original_filename
       assert_equal "little_file.txt",m.versions[1].original_filename
       assert_equal "This is a new revision",m.versions[1].revision_comments
       assert_equal 'file_picture.png',assigns(:model).versions[1].model_image.original_filename
       assert_equal 'file_picture.png',assigns(:model).model_image.original_filename

       assert_equal "Teusink.xml",m.versions[0].original_filename
  end
  
  def test_missing_sharing_should_default_to_private
{'fraction_non_alphanumeric': 0.11597938144329897, 'fraction_numerical': 0.0034609720176730484, 'mean_word_length': 4.078908002991772, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 112, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5000043', 'n_tokens_mistral': 4538, 'n_tokens_neox': 4329, 'n_words': 919}