<commit_before>require 'rails_helper'

describe ProfileController, :type => :controller do
  def create_service(student, educator)
    FactoryBot.create(:service, {
      student: student,
      recorded_by_educator: educator,
      provided_by_educator_name: '<NAME>'
    })
  end

  def create_event_note(student, educator, params = {})
    FactoryBot.create(:event_note, {
      student: student,
      educator: educator,
    }.merge(params))
  end

  def parse_json(response_body)
    JSON.parse(response_body)
  end

  describe '#json' do
    let!(:school) { FactoryBot.create(:school) }
    let(:educator) { FactoryBot.create(:educator_with_homeroom) }
    let(:other_educator) { FactoryBot.create(:educator, full_name: "<NAME>") }
    let(:student) { FactoryBot.create(:student, school: school) }
    let(:course) { FactoryBot.create(:course, school: school) }
    let(:section) { FactoryBot.create(:section, course: course) }
    let!(:ssa) { FactoryBot.create(:student_section_assignment, student: student, section: section) }
    let!(:esa) { FactoryBot.create(:educator_section_assignment, educator: other_educator, section: section)}
    let(:homeroom) { student.homeroom }

    def make_request(educator, student_id)
      request.env['HTTPS'] = 'on'
      sign_in(educator)
      request.env['HTTP_ACCEPT'] = 'application/json'
      get :json, params: {
        id: student_id,
        format: :json
      }
    end

    describe 'integration test for restricted note redactions' do
      let(:educator) { FactoryBot.create(:educator, :admin, school: school, full_name: "<NAME>") }

      before do
        event_note = FactoryBot.create(:event_note, student: student, text: 'RESTRICTED-one', is_restricted: true)
        FactoryBot.create(:event_note_revision, student: student, text: 'RESTRICTED-two', event_note: event_note)
      end

      it 'redacts correctly' do
        sign_in(educator)
        make_request(educator, student.id)
        expect(response.status).to eq 200
        json_string = JSON.parse(response.body).to_json
        expect(json_string).to include('<redacted>'.to_json)
        expect(json_string).not_to include('RESTRICTED-one')
        expect(json_string).not_to include('RESTRICTED-two')
      end
    end

    describe 'integration test for transition note redactions' do
      let(:educator) { FactoryBot.create(:educator, :admin, school: school, full_name: "<NAME>") }

      before do
        FactoryBot.create(:transition_note, student: student, text: 'RESTRICTED-transition-note', is_restricted: true)
      end

      it 'redacts correctly' do
        sign_in(educator)
        make_request(educator, student.id)
        expect(response.status).to eq 200
        json = JSON.parse(response.body)
        expect(json.to_json).to include('<redacted>'.to_json)
        expect(json.to_json).not_to include('RESTRICTED-transition-note')
        expect(json['feed']['transition_notes'][0]['text']).to eq '<redacted>'
      end
    end

    describe 'f_and_p_assessments' do
      let!(:pals) { TestPals.create! }
      let!(:f_and_p) do
        FAndPAssessment.create!({
          student_id: pals.healey_kindergarten_student.id,
          benchmark_date: Date.parse('2018-02-22'),
          instructional_level: 'B',
          f_and_p_code: 'WC',
          uploaded_by_educator_id: pals.uri.id
        })
      end

      it 'works' do
        sign_in(pals.uri)
        make_request(pals.uri, pals.healey_kindergarten_student.id)
        expect(response.status).to eq 200
        json = JSON.parse(response.body)
        expect(json['f_and_p_assessments']).to eq([{
          "id"=>f_and_p.id,
          "benchmark_date"=>"2018-02-22",
          "instructional_level"=>"B",
          "f_and_p_code"=>"WC"
        }])
      end
    end

    describe 'ed_plans' do
      def create_ed_plan!(attrs = {})
        EdPlan.create!({
          student_id: pals.healey_kindergarten_student.id,
          sep_status: 1,
          sep_effective_date: Date.parse('2016-09-15'),
          sep_fieldd_006: 'Health disability',
          sep_fieldd_007: 'Rich Districtwide, Laura Principal, Sarah Teacher, <NAME> (parent)',
          sep_oid: 'test-sep-oid'
        }.merge(attrs))
      end

      let!(:pals) { TestPals.create! }

      it 'guards access' do
        create_ed_plan!
        sign_in(pals.uri)
        make_request(pals.uri, pals.healey_kindergarten_student.id)
        expect(response.status).to eq 200
        json = JSON.parse(response.body)
        expect(json['ed_plans'].size).to eq(0)
      end

      it 'only includes active ed plans' do
        create_ed_plan!(sep_status: 4, sep_oid: 'test-sep-discarded')
        create_ed_plan!(sep_status: 2, sep_oid: 'test-sep-previous')
        create_ed_plan!(sep_status: 0, sep_oid: 'test-sep-draft')
        sign_in(pals.uri)
        make_request(pals.uri, pals.healey_kindergarten_student.id)
        
        expect(response.status).to eq 200
        json = JSON.parse(response.body)
        expect(json['ed_plans'].size).to eq(0)
      end

      it 'works when enabled' do
        create_ed_plan!
        sign_in(pals.uri)
        make_request(pals.uri, pals.healey_kindergarten_student.id)<commit_msg>Update profile spec<commit_after>require 'rails_helper'

describe ProfileController, :type => :controller do
  def create_service(student, educator)
    FactoryBot.create(:service, {
      student: student,
      recorded_by_educator: educator,
      provided_by_educator_name: '<NAME>'
    })
  end

  def create_event_note(student, educator, params = {})
    FactoryBot.create(:event_note, {
      student: student,
      educator: educator,
    }.merge(params))
  end

  def parse_json(response_body)
    JSON.parse(response_body)
  end

  describe '#json' do
    let!(:school) { FactoryBot.create(:school) }
    let(:educator) { FactoryBot.create(:educator_with_homeroom) }
    let(:other_educator) { FactoryBot.create(:educator, full_name: "<NAME>") }
    let(:student) { FactoryBot.create(:student, school: school) }
    let(:course) { FactoryBot.create(:course, school: school) }
    let(:section) { FactoryBot.create(:section, course: course) }
    let!(:ssa) { FactoryBot.create(:student_section_assignment, student: student, section: section) }
    let!(:esa) { FactoryBot.create(:educator_section_assignment, educator: other_educator, section: section)}
    let(:homeroom) { student.homeroom }

    def make_request(educator, student_id)
      request.env['HTTPS'] = 'on'
      sign_in(educator)
      request.env['HTTP_ACCEPT'] = 'application/json'
      get :json, params: {
        id: student_id,
        format: :json
      }
    end

    describe 'integration test for restricted note redactions' do
      let(:educator) { FactoryBot.create(:educator, :admin, school: school, full_name: "<NAME>") }

      before do
        event_note = FactoryBot.create(:event_note, student: student, text: 'RESTRICTED-one', is_restricted: true)
        FactoryBot.create(:event_note_revision, student: student, text: 'RESTRICTED-two', event_note: event_note)
      end

      it 'redacts correctly' do
        sign_in(educator)
        make_request(educator, student.id)
        expect(response.status).to eq 200
        json_string = JSON.parse(response.body).to_json
        expect(json_string).to include('<redacted>'.to_json)
        expect(json_string).not_to include('RESTRICTED-one')
        expect(json_string).not_to include('RESTRICTED-two')
      end
    end

    describe 'integration test for transition note redactions' do
      let(:educator) { FactoryBot.create(:educator, :admin, school: school, full_name: "<NAME>") }

      before do
        FactoryBot.create(:transition_note, student: student, text: 'RESTRICTED-transition-note', is_restricted: true)
      end

      it 'redacts correctly' do
        sign_in(educator)
        make_request(educator, student.id)
        expect(response.status).to eq 200
        json = JSON.parse(response.body)
        expect(json.to_json).to include('<redacted>'.to_json)
        expect(json.to_json).not_to include('RESTRICTED-transition-note')
        expect(json['feed']['transition_notes'][0]['text']).to eq '<redacted>'
      end
    end

    describe 'f_and_p_assessments' do
      let!(:pals) { TestPals.create! }
      let!(:f_and_p) do
        FAndPAssessment.create!({
          student_id: pals.healey_kindergarten_student.id,
          benchmark_date: Date.parse('2018-02-22'),
          instructional_level: 'B',
          f_and_p_code: 'WC',
          uploaded_by_educator_id: pals.uri.id
        })
      end

      it 'works' do
        sign_in(pals.uri)
        make_request(pals.uri, pals.healey_kindergarten_student.id)
        expect(response.status).to eq 200
        json = JSON.parse(response.body)
        expect(json['f_and_p_assessments']).to eq([{
          "id"=>f_and_p.id,
          "benchmark_date"=>"2018-02-22",
          "instructional_level"=>"B",
          "f_and_p_code"=>"WC"
        }])
      end
    end

    describe 'ed_plans' do
      def create_ed_plan!(attrs = {})
        EdPlan.create!({
          student_id: pals.healey_kindergarten_student.id,
          sep_status: 1,
          sep_effective_date: Date.parse('2016-09-15'),
          sep_fieldd_006: 'Health disability',
          sep_fieldd_007: 'Rich Districtwide, Laura Principal, <NAME>acher, <NAME> (parent)',
          sep_oid: 'test-sep-oid'
        }.merge(attrs))
      end

      let!(:pals) { TestPals.create! }

      it 'only includes active ed plans' do
        create_ed_plan!(sep_status: 4, sep_oid: 'test-sep-discarded')
        create_ed_plan!(sep_status: 2, sep_oid: 'test-sep-previous')
        create_ed_plan!(sep_status: 0, sep_oid: 'test-sep-draft')
        sign_in(pals.uri)
        make_request(pals.uri, pals.healey_kindergarten_student.id)

        expect(response.status).to eq 200
        json = JSON.parse(response.body)
        expect(json['ed_plans'].size).to eq(0)
      end

      it 'works when enabled' do
        create_ed_plan!
        sign_in(pals.uri)
        make_request(pals.uri, pals.healey_kindergarten_student.id)
{'fraction_non_alphanumeric': 0.12427203632415358, 'fraction_numerical': 0.009870693909781857, 'mean_word_length': 3.0047430830039525, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 30, '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': '3423276', 'n_tokens_mistral': 3422, 'n_tokens_neox': 3244, 'n_words': 600}