<commit_before>require 'rails_helper'

# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to specify the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator.  If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
# It only uses APIs available in rails and/or rspec-rails.  There are a number
# of tools you can use to make these specs even more expressive, but we're
# sticking to rails and rspec-rails APIs to keep things simple and stable.
#
# Compared to earlier versions of this generator, there is very limited use of
# stubs and message expectations in this spec.  Stubs are only used when there
# is no simpler way to get a handle on the object needed for the example.
# Message expectations are only used when there is no simpler way to specify
# that an instance is receiving a specific message.

RSpec.describe SchoolsController, type: :controller do
  # This should return the minimal set of attributes required to create a valid
  # School. As you add validations to School, be sure to
  # adjust the attributes here as well.
  let(:valid_attributes) {
    { urn: 12345, name: 'test school' }
  }

  let(:invalid_attributes) {
    { name: nil }
  }

  context "As an admin user" do
    before(:each) do
      sign_in_user(:admin)
    end
    describe "GET #index" do
      it "assigns schools that are enrolled as @schools_enrolled" do
        school = FactoryGirl.create :school, enrolled: true
        get :index, params: {}
        expect(assigns(:schools_enrolled)).to eq([school])
      end
      it "assigns schools that haven't enrolled as @schools_not_enrolled" do
        school = FactoryGirl.create :school, enrolled: false
        get :index, params: {}
        expect(assigns(:schools_not_enrolled)).to eq([school])
      end
    end

    describe "GET #show" do
      context "the school is not enrolled" do
        context "user cannot manage school" do
          it "redirects to the enrol page" do
            sign_in_user(:guest)
            school = FactoryGirl.create :school, enrolled: false
            get :show, params: { id: school.to_param }
            expect(response).to redirect_to(enrol_path)
          end
        end
      end
      context "the school is enrolled" do
        it "assigns the requested school as @school" do
          school = FactoryGirl.create :school
          get :show, params: { id: school.to_param }
          expect(assigns(:school)).to eq(school)
        end
        it "assigns the school's meters as @meters" do
          school = FactoryGirl.create :school
          meter = FactoryGirl.create :meter, school_id: school.id
          get :show, params: { id: school.to_param }
          expect(assigns(:meters)).to include(meter)
        end
        it "assigns the latest activities as @activities" do
          school = FactoryGirl.create :school
          activity = FactoryGirl.create :activity, school_id: school.id
          get :show, params: { id: school.to_param }
          expect(assigns(:activities)).to include(activity)
        end
        it "does not include activities from other schools" do
          school = FactoryGirl.create :school
          other_school = FactoryGirl.create :school
          FactoryGirl.create :activity, school_id: school.id
          activity_other_school = FactoryGirl.create :activity, school_id: other_school.id
          get :show, params: { id: school.to_param }
          expect(assigns(:activities)).not_to include activity_other_school
        end
        it "assigns the school's achievements to @badges" do
          school = create :school, :with_badges, badges_sashes: 7

          get :show, params: { id: school.to_param }
          expect(assigns(:badges)).to include(school.badges.first)
        end
        it "doesn't include other schools badges" do
          school_one, school_two = create_pair :school, :with_badges, badges_sashes: 2

          get :show, params: { id: school_one.to_param }
          expect(assigns(:badges)).not_to include(school_two.badges.first)
        end
      end
    end

    describe "GET #achievements" do
      it 'assigns achievements as @achievements' do
        school = create :school, :with_badges

        get :achievements, params: { id: school.to_param }
        expect(assigns(:badges)).to include(school.badges.first)
      end
    end

    describe 'GET #leaderboard' do
      it 'assigns schools as @schools in points order' do
        schools = (1..5).collect { |n| create :school, :with_points, score_points: 6 - n }

        get :leaderboard
        expect(School.leaderboard.map(&:id)).to eq(schools.map(&:id))
      end
    end

    describe "GET #new" do
      it "assigns a new school as @school" do
        get :new, params: {}
        expect(assigns(:school)).to be_a_new(School)
      end
    end

    describe "GET #edit" do
      it "assigns the requested school as @school" do
        school = FactoryGirl.create :school
        get :edit, params: { id: school.to_param }
        expect(assigns(:school)).to eq(school)
      end
    end

    describe "POST #create" do
      context "with valid params" do
        it "creates a new School" do
          expect {
            post :create, params: { school: valid_attributes }
          }.to change(School, :count).by(1)
        end
        it "assigns a newly created school as @school" do
          post :create, params: { school: valid_attributes }
          expect(assigns(:school)).to be_a(School)
          expect(assigns(:school)).to be_persisted
        end
        it "creates a calendar for the new School" do
          post :create, params: { school: valid_attributes }
          expect(assigns(:school).calendar).not_to be_nil
        end

        it "redirects to the created school" do
          post :create, params: { school: valid_attributes }
          expect(response).to redirect_to(School.last)
        end
      end

      context "with invalid params" do
        it "assigns a newly created but unsaved school as @school" do
          post :create, params: { school: invalid_attributes }
          expect(assigns(:school)).to be_a_new(School)
        end

        it "re-renders the 'new' template" do
          post :create, params: { school: invalid_attributes }
          expect(response).to render_template("new")
        end
      end
    end

    describe "PUT #update" do
      context "with valid params" do
        let(:new_attributes) {
          { name: '<NAME>' }
        }

        it "updates the requested school" do
          school = FactoryGirl.create :school
          put :update, params: { id: school.to_param, school: new_attributes }
          school.reload
          expect(school.name).to eq new_attributes[:name]
        end

        it "assigns the requested school as @school" do
          school = FactoryGirl.create :school
          put :update, params: { id: school.to_param, school: valid_attributes }
          expect(assigns(:school)).to eq(school)
        end

        it "awards points when enrolled" do
          school = FactoryGirl.create :school
          put :update, params: { id: school.to_param, school: {enrolled: true} }
          school.reload
          expect(school.enrolled).to be(true)
          expect(school.points).to eql(20)
        end

        it "awards a badge when enrolled" do
          school = FactoryGirl.create :school
          put :update, params: { id: school.to_param, school: {enrolled: true} }
          school.reload
          expect(school.badges.first.name).to eql('enrolled')
        end


        it "redirects to the school" do
          school = FactoryGirl.create :school
          put :update, params: { id: school.to_param, school: valid_attributes }
          expect(response).to redirect_to(school)
        end
      end

      context "with invalid params" do
        it "assigns the school as @school" do
          school = FactoryGirl.create :school
          put :update, params: { id: school.to_param, school: invalid_attributes }
          expect(assigns(:school)).to eq(school)
        end

        it "re-renders the 'edit' template" do
          school = FactoryGirl.create :school
          put :update, params: { id: school.to_param, school: invalid_attributes }
          expect(response).to render_template("edit")
        end
      end
    end

    describe "DELETE #destroy" do
      it "destroys the requested school" do
        school = FactoryGirl.create :school
        expect {
          delete :destroy, params: { id: school.to_param }
        }.to change(School, :count).by(-1)
      end

      it "redirects to the schools list" do
        school = FactoryGirl.create :school
        delete :destroy, params: { id: school.to_param }
        expect(response).to redirect_to(schools_url)
      end
    end

    describe "GET #usage" do
      let!(:school) { FactoryGirl.create :school }
      let(:period) { :daily }
      it "assigns the requested school as @school" do
        get :usage, params: { id: school.to_param, period: period }
        expect(assigns(:school)).to eq(school)
      end
      context "to_date is specified" do
        let(:to_date) { Date.current - 1.days }
        it "assigns to_date to @to_date" do
          get :usage, params: { id: school.to_param, period: period, to_date: to_date }
          expect(assigns(:to_date)).to eq to_date
        end
      end
      context "to_date is not specified" do
        it "assigns yesterday's date to  @to_date" do
          get :usage, params: { id: school.to_param, period: period }
          expect(assigns(:to_date)).to eq Date.current - 1.days
        end
      end
      context "period is 'daily'" do<commit_msg>No longer default to yesterday for to_date as we're only displaying single day by default<commit_after>require 'rails_helper'

# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to specify the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator.  If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
# It only uses APIs available in rails and/or rspec-rails.  There are a number
# of tools you can use to make these specs even more expressive, but we're
# sticking to rails and rspec-rails APIs to keep things simple and stable.
#
# Compared to earlier versions of this generator, there is very limited use of
# stubs and message expectations in this spec.  Stubs are only used when there
# is no simpler way to get a handle on the object needed for the example.
# Message expectations are only used when there is no simpler way to specify
# that an instance is receiving a specific message.

RSpec.describe SchoolsController, type: :controller do
  # This should return the minimal set of attributes required to create a valid
  # School. As you add validations to School, be sure to
  # adjust the attributes here as well.
  let(:valid_attributes) {
    { urn: 12345, name: 'test school' }
  }

  let(:invalid_attributes) {
    { name: nil }
  }

  context "As an admin user" do
    before(:each) do
      sign_in_user(:admin)
    end
    describe "GET #index" do
      it "assigns schools that are enrolled as @schools_enrolled" do
        school = FactoryGirl.create :school, enrolled: true
        get :index, params: {}
        expect(assigns(:schools_enrolled)).to eq([school])
      end
      it "assigns schools that haven't enrolled as @schools_not_enrolled" do
        school = FactoryGirl.create :school, enrolled: false
        get :index, params: {}
        expect(assigns(:schools_not_enrolled)).to eq([school])
      end
    end

    describe "GET #show" do
      context "the school is not enrolled" do
        context "user cannot manage school" do
          it "redirects to the enrol page" do
            sign_in_user(:guest)
            school = FactoryGirl.create :school, enrolled: false
            get :show, params: { id: school.to_param }
            expect(response).to redirect_to(enrol_path)
          end
        end
      end
      context "the school is enrolled" do
        it "assigns the requested school as @school" do
          school = FactoryGirl.create :school
          get :show, params: { id: school.to_param }
          expect(assigns(:school)).to eq(school)
        end
        it "assigns the school's meters as @meters" do
          school = FactoryGirl.create :school
          meter = FactoryGirl.create :meter, school_id: school.id
          get :show, params: { id: school.to_param }
          expect(assigns(:meters)).to include(meter)
        end
        it "assigns the latest activities as @activities" do
          school = FactoryGirl.create :school
          activity = FactoryGirl.create :activity, school_id: school.id
          get :show, params: { id: school.to_param }
          expect(assigns(:activities)).to include(activity)
        end
        it "does not include activities from other schools" do
          school = FactoryGirl.create :school
          other_school = FactoryGirl.create :school
          FactoryGirl.create :activity, school_id: school.id
          activity_other_school = FactoryGirl.create :activity, school_id: other_school.id
          get :show, params: { id: school.to_param }
          expect(assigns(:activities)).not_to include activity_other_school
        end
        it "assigns the school's achievements to @badges" do
          school = create :school, :with_badges, badges_sashes: 7

          get :show, params: { id: school.to_param }
          expect(assigns(:badges)).to include(school.badges.first)
        end
        it "doesn't include other schools badges" do
          school_one, school_two = create_pair :school, :with_badges, badges_sashes: 2

          get :show, params: { id: school_one.to_param }
          expect(assigns(:badges)).not_to include(school_two.badges.first)
        end
      end
    end

    describe "GET #achievements" do
      it 'assigns achievements as @achievements' do
        school = create :school, :with_badges

        get :achievements, params: { id: school.to_param }
        expect(assigns(:badges)).to include(school.badges.first)
      end
    end

    describe 'GET #leaderboard' do
      it 'assigns schools as @schools in points order' do
        schools = (1..5).collect { |n| create :school, :with_points, score_points: 6 - n }

        get :leaderboard
        expect(School.leaderboard.map(&:id)).to eq(schools.map(&:id))
      end
    end

    describe "GET #new" do
      it "assigns a new school as @school" do
        get :new, params: {}
        expect(assigns(:school)).to be_a_new(School)
      end
    end

    describe "GET #edit" do
      it "assigns the requested school as @school" do
        school = FactoryGirl.create :school
        get :edit, params: { id: school.to_param }
        expect(assigns(:school)).to eq(school)
      end
    end

    describe "POST #create" do
      context "with valid params" do
        it "creates a new School" do
          expect {
            post :create, params: { school: valid_attributes }
          }.to change(School, :count).by(1)
        end
        it "assigns a newly created school as @school" do
          post :create, params: { school: valid_attributes }
          expect(assigns(:school)).to be_a(School)
          expect(assigns(:school)).to be_persisted
        end
        it "creates a calendar for the new School" do
          post :create, params: { school: valid_attributes }
          expect(assigns(:school).calendar).not_to be_nil
        end

        it "redirects to the created school" do
          post :create, params: { school: valid_attributes }
          expect(response).to redirect_to(School.last)
        end
      end

      context "with invalid params" do
        it "assigns a newly created but unsaved school as @school" do
          post :create, params: { school: invalid_attributes }
          expect(assigns(:school)).to be_a_new(School)
        end

        it "re-renders the 'new' template" do
          post :create, params: { school: invalid_attributes }
          expect(response).to render_template("new")
        end
      end
    end

    describe "PUT #update" do
      context "with valid params" do
        let(:new_attributes) {
          { name: '<NAME>' }
        }

        it "updates the requested school" do
          school = FactoryGirl.create :school
          put :update, params: { id: school.to_param, school: new_attributes }
          school.reload
          expect(school.name).to eq new_attributes[:name]
        end

        it "assigns the requested school as @school" do
          school = FactoryGirl.create :school
          put :update, params: { id: school.to_param, school: valid_attributes }
          expect(assigns(:school)).to eq(school)
        end

        it "awards points when enrolled" do
          school = FactoryGirl.create :school
          put :update, params: { id: school.to_param, school: {enrolled: true} }
          school.reload
          expect(school.enrolled).to be(true)
          expect(school.points).to eql(20)
        end

        it "awards a badge when enrolled" do
          school = FactoryGirl.create :school
          put :update, params: { id: school.to_param, school: {enrolled: true} }
          school.reload
          expect(school.badges.first.name).to eql('enrolled')
        end


        it "redirects to the school" do
          school = FactoryGirl.create :school
          put :update, params: { id: school.to_param, school: valid_attributes }
          expect(response).to redirect_to(school)
        end
      end

      context "with invalid params" do
        it "assigns the school as @school" do
          school = FactoryGirl.create :school
          put :update, params: { id: school.to_param, school: invalid_attributes }
          expect(assigns(:school)).to eq(school)
        end

        it "re-renders the 'edit' template" do
          school = FactoryGirl.create :school
          put :update, params: { id: school.to_param, school: invalid_attributes }
          expect(response).to render_template("edit")
        end
      end
    end

    describe "DELETE #destroy" do
      it "destroys the requested school" do
        school = FactoryGirl.create :school
        expect {
          delete :destroy, params: { id: school.to_param }
        }.to change(School, :count).by(-1)
      end

      it "redirects to the schools list" do
        school = FactoryGirl.create :school
        delete :destroy, params: { id: school.to_param }
        expect(response).to redirect_to(schools_url)
      end
    end

    describe "GET #usage" do
      let!(:school) { FactoryGirl.create :school }
      let(:period) { :daily }
      it "assigns the requested school as @school" do
        get :usage, params: { id: school.to_param, period: period }
        expect(assigns(:school)).to eq(school)
      end
      context "to_date is specified" do
        let(:to_date) { Date.current - 1.days }
        it "assigns to_date to @to_date" do
          get :usage, params: { id: school.to_param, period: period, to_date: to_date }
          expect(assigns(:to_date)).to eq to_date
        end
      end
      context "period is 'daily'" do
{'fraction_non_alphanumeric': 0.08702064896755163, 'fraction_numerical': 0.0015766453056657512, 'mean_word_length': 2.408980582524272, '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': '3822350', 'n_tokens_mistral': 5614, 'n_tokens_neox': 5383, 'n_words': 1919}