<commit_before>require 'test_helper'

class TaskDefinitionsTest < ActiveSupport::TestCase
  include Rack::Test::Methods
  include TestHelpers::AuthHelper
  include TestHelpers::JsonHelper

  def app
    Rails.application
  end

  def test_post_invalid_file_tasksheet
    test_unit = Unit.first
    test_task_definition_id = test_unit.task_definitions.first.id

    data_to_post = {
      file: 'rubbish_path',
      auth_token: <PASSWORD>
    }
    post_json with_auth_token("/api/units/#{test_unit.id}/task_definitions/#{test_task_definition_id}/task_sheet"), data_to_post

    assert last_response_body.key?('error')
  end

  def test_post_tasksheet
    test_unit = Unit.first
    test_task_definition = TaskDefinition.first

    data_to_post = {
      file: Rack::Test::UploadedFile.new('test_files/submissions/00_question.pdf', 'application/pdf')
    }

    post "/api/units/#{test_unit.id}/task_definitions/#{test_task_definition.id}/task_sheet", with_auth_token(data_to_post)

    assert_equal 201, last_response.status
    assert test_task_definition.task_sheet

    assert_equal File.size('test_files/submissions/00_question.pdf'), File.size(TaskDefinition.first.task_sheet)
  end

  def test_post_task_resources
    test_unit_id = Unit.first.id
    test_task_definition_id = TaskDefinition.first.id

    data_to_post = {
      file: Rack::Test::UploadedFile.new('test_files/2015-08-06-COS10001-acain.zip', 'application/zip')
    }
    post "/api/units/#{test_unit_id}/task_definitions/#{test_task_definition_id}/task_resources", with_auth_token(data_to_post)

    puts last_response_body if last_response.status == 403

    assert_equal 201, last_response.status
  end

  def test_change_to_group_after_submissions
    unit = Unit.first
    td = TaskDefinition.new({
        unit_id: unit.id,
        name: 'Task to switch from ind to group after submission',                    
        description: 'test def',
        weighting: 4,
        target_grade: 0,
        start_date: unit.start_date + 1.week,
        target_date: unit.start_date + 2.weeks,
        abbreviation: 'TaskSwitchIndGrp',
        restrict_status_updates: false,
        upload_requirements: [ { "key" => 'file0', "name" => 'Shape Class', "type" => 'code' } ],
        plagiarism_warn_pct: 0.8,
        is_graded: false,
        max_quality_pts: 0
      })
    td.save!

    data_to_post = {
      file0: Rack::Test::UploadedFile.new('test_files/submissions/test.sql', 'text/plain'),
      trigger: 'ready_to_mark'
    }

    project = unit.active_projects.first

    post "/api/projects/#{project.id}/task_def_id/#{td.id}/submission", with_auth_token(data_to_post)

    assert_equal 201, last_response.status

    task = project.task_for_task_definition(td)
    task.convert_submission_to_pdf
    path = task.zip_file_path_for_done_task
    assert path
    assert File.exists? path
    
    # Change it to a group task

    group_set = GroupSet.create!({name: 'test group set', unit: unit})
    group_set.save!

    td.group_set = group_set
    assert !td.save
<commit_msg>FIX: Update path checking in files for submission test<commit_after>require 'test_helper'

class TaskDefinitionsTest < ActiveSupport::TestCase
  include Rack::Test::Methods
  include TestHelpers::AuthHelper
  include TestHelpers::JsonHelper
  include TestHelpers::TestFileHelper

  def app
    Rails.application
  end

  def test_post_invalid_file_tasksheet
    test_unit = Unit.first
    test_task_definition_id = test_unit.task_definitions.first.id

    data_to_post = {
      file: 'rubbish_path',
      auth_token: <PASSWORD>_<PASSWORD>
    }
    post_json with_auth_token("/api/units/#{test_unit.id}/task_definitions/#{test_task_definition_id}/task_sheet"), data_to_post

    assert last_response_body.key?('error')
  end

  def test_post_tasksheet
    test_unit = Unit.first
    test_task_definition = TaskDefinition.first

    data_to_post = {
      file: Rack::Test::UploadedFile.new('test_files/submissions/00_question.pdf', 'application/pdf')
    }

    post "/api/units/#{test_unit.id}/task_definitions/#{test_task_definition.id}/task_sheet", with_auth_token(data_to_post)

    assert_equal 201, last_response.status
    assert test_task_definition.task_sheet

    assert_equal File.size('test_files/submissions/00_question.pdf'), File.size(TaskDefinition.first.task_sheet)
  end

  def test_post_task_resources
    test_unit_id = Unit.first.id
    test_task_definition_id = TaskDefinition.first.id

    data_to_post = {
      file: Rack::Test::UploadedFile.new('test_files/2015-08-06-COS10001-acain.zip', 'application/zip')
    }
    post "/api/units/#{test_unit_id}/task_definitions/#{test_task_definition_id}/task_resources", with_auth_token(data_to_post)

    puts last_response_body if last_response.status == 403

    assert_equal 201, last_response.status
  end

  def test_change_to_group_after_submissions
    unit = Unit.first
    td = TaskDefinition.new({
        unit_id: unit.id,
        name: 'Task to switch from ind to group after submission',                    
        description: 'test def',
        weighting: 4,
        target_grade: 0,
        start_date: unit.start_date + 1.week,
        target_date: unit.start_date + 2.weeks,
        abbreviation: 'TaskSwitchIndGrp',
        restrict_status_updates: false,
        upload_requirements: [ { "key" => 'file0', "name" => 'Shape Class', "type" => 'code' } ],
        plagiarism_warn_pct: 0.8,
        is_graded: false,
        max_quality_pts: 0
      })
    td.save!

    data_to_post = {
      trigger: 'ready_to_mark'
    }

    data_to_post = with_file('test_files/submissions/test.sql', 'text/plain', data_to_post)

    project = unit.active_projects.first

    post "/api/projects/#{project.id}/task_def_id/#{td.id}/submission", with_auth_token(data_to_post)

    assert_equal 201, last_response.status

    task = project.task_for_task_definition(td)
    task.convert_submission_to_pdf
    path = task.zip_file_path_for_done_task
    assert path
    assert File.exists? path
    
    # Change it to a group task

    group_set = GroupSet.create!({name: 'test group set', unit: unit})
    group_set.save!

    td.group_set = group_set
    assert !td.save

{'fraction_non_alphanumeric': 0.10220860873770755, 'fraction_numerical': 0.012090923746574238, 'mean_word_length': 3.5651214128035322, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '1200274', 'n_tokens_mistral': 2137, 'n_tokens_neox': 2028, 'n_words': 347}