<commit_before>require 'rails_helper'

describe TieringController, :type => :controller do
  before { request.env['HTTPS'] = 'on' }
  let!(:pals) { TestPals.create! }
  let!(:time_now) { pals.time_now }

  describe '#show_json' do
    def get_show_json(educator, params = {})
      sign_in(educator)
      get :show_json, params: {
        format: :json,
        school_id: pals.shs.id,
        time_now: pals.time_now.to_i
      }
      sign_out(educator)
      response
    end

    it 'allows everyone access, but only to authorized students in that school' do
      (Educator.all - [pals.uri]).each do |educator|
        authorized_students = Authorizer.new(educator).authorized { Student.active.to_a }
        response = get_show_json(educator)
        expect(response.status).to eq 200

        json = JSON.parse(response.body)
        expect(json['students_with_tiering'].size).to be <= authorized_students.size
        expect(authorized_students.map(&:id)).to include(*json['students_with_tiering'].map {|s| s['id'] })
      end
    end

    context 'for educator with access' do
      it 'works on happy path' do
        response = get_show_json(pals.uri)
        expect(response.status).to eq 200
        json = JSON.parse(response.body)

        # check shape of data
        expect(json.keys).to eq ['students_with_tiering']
        expect(json['students_with_tiering'].size).to eq 3
        expect(json['students_with_tiering'].map(&:keys).flatten.uniq).to contain_exactly(*[
          "id",
          "grade",
          "first_name",
          "last_name",
          "program_assigned",
          "sped_placement",
          "house",
          "student_section_assignments_right_now",
          "tier",
          "notes"
        ])
        expect(json['students_with_tiering'].map {|s| s['notes'].keys }.flatten.uniq).to eq([
          "last_sst_note",<commit_msg>Update shape of data in tiering controller response<commit_after>require 'rails_helper'

describe TieringController, :type => :controller do
  before { request.env['HTTPS'] = 'on' }
  let!(:pals) { TestPals.create! }
  let!(:time_now) { pals.time_now }

  describe '#show_json' do
    def get_show_json(educator, params = {})
      sign_in(educator)
      get :show_json, params: {
        format: :json,
        school_id: pals.shs.id,
        time_now: pals.time_now.to_i
      }
      sign_out(educator)
      response
    end

    it 'allows everyone access, but only to authorized students in that school' do
      (Educator.all - [pals.uri]).each do |educator|
        authorized_students = Authorizer.new(educator).authorized { Student.active.to_a }
        response = get_show_json(educator)
        expect(response.status).to eq 200

        json = JSON.parse(response.body)
        expect(json['students_with_tiering'].size).to be <= authorized_students.size
        expect(authorized_students.map(&:id)).to include(*json['students_with_tiering'].map {|s| s['id'] })
      end
    end

    context 'for educator with access' do
      it 'works on happy path' do
        response = get_show_json(pals.uri)
        expect(response.status).to eq 200
        json = JSON.parse(response.body)

        # check shape of data
        expect(json.keys).to eq ['students_with_tiering']
        expect(json['students_with_tiering'].size).to eq 3
        expect(json['students_with_tiering'].map(&:keys).flatten.uniq).to contain_exactly(*[
          "id",
          "grade",
          "first_name",
          "last_name",
          "limited_english_proficiency",
          "program_assigned",
          "sped_placement",
          "house",
          "student_section_assignments_right_now",
          "tier",
          "notes"
        ])
        expect(json['students_with_tiering'].map {|s| s['notes'].keys }.flatten.uniq).to eq([
          "last_sst_note",
{'fraction_non_alphanumeric': 0.12490221642764016, 'fraction_numerical': 0.003650586701434159, 'mean_word_length': 2.6360189573459714, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1778441', 'n_tokens_mistral': 1258, 'n_tokens_neox': 1205, 'n_words': 239}