<commit_before># frozen_string_literal: true

require 'rails_helper'

describe AssignmentsController do
  before :each do
    @roster = create :roster
  end

  describe 'POST #create' do
    before :each do
      @user = roster_user @roster
      @attributes = {
        start_date: Date.today,
        end_date: Date.tomorrow,
        user_id: @user.id,
        roster_id: @roster.id
      }
    end
    let :submit do
      post :create, roster_id: @roster, assignment: @attributes
    end
    context 'admin in roster' do
      before(:each) { when_current_user_is roster_admin(@roster) }
      context 'without errors' do
        it 'creates an assignment' do
          submit
          expect(Assignment.count).to be 1
        end
        it 'sends an email to the new owner of the assignment' do
          expect_any_instance_of(Assignment).to receive(:notify)
          submit
        end
        it 'redirects to the index with a date of the assignment start date' do
          submit
          expect(response).to redirect_to(
            roster_assignments_url(date: @attributes[:start_date])
          )
        end
      end
      context 'with errors' do
        before :each do
          # Guaranteed to not be a user with this ID,
          # but will pass param validation in the controller.
          @attributes[:user_id] = User.pluck(:id).sort.last + 1
        end
        it 'does not create assignment, gives errors, and redirects back' do
          expect { submit }.to redirect_back
          expect(Assignment.all).to be_empty
          expect(flash[:errors]).not_to be_empty
        end
      end
    end
    context 'not admin' do
      before(:each) { when_current_user_is @user }
      context 'creating assignment belonging to self' do
        it 'creates the assignment' do
          expect { submit }.to change(Assignment, :count).by 1
        end
      end
      context 'creating assignment not belonging to self' do
        before(:each) { @attributes[:user_id] = @user.id + 1 }
        it 'does not create the assignment and explains why' do
          expect do
            expect { submit }.to redirect_back
          end.not_to change(Assignment, :count)
          expect(flash[:errors]).not_to be_empty
        end
      end
    end
  end

  describe 'DELETE #destroy' do
    before :each do
      @assignment = create :assignment
      when_current_user_is :whoever
    end
    let :submit do
      delete :destroy, roster_id: @assignment.roster.id, id: @assignment.id
    end
    it 'finds the correct assignment' do
      submit
      expect(assigns.fetch :assignment).to eql @assignment
    end
    it 'destroys the assignment' do
      expect_any_instance_of(Assignment).to receive :destroy
      submit
    end
    it 'sends a notification to the owner of the assignment' do
      expect_any_instance_of(Assignment).to receive :notify
      submit
    end
    it 'redirects to the index' do
      submit
      expect(response).to redirect_to roster_assignments_url
    end
  end

  describe 'GET #edit' do
    before :each do
      @assignment = create :assignment
      @roster = create :roster
      when_current_user_is :whoever
    end
    let :submit do
      get :edit, roster_id: @roster.id, id: @assignment.id
    end
    it 'finds the correct assignment' do
      submit
      expect(assigns.fetch :assignment).to eql @assignment
    end
    it 'populates a users variable of all users' do
      user1 = roster_user @roster
      user2 = roster_user @roster
      user3 = roster_user @roster
      submit
      expect(assigns.fetch :users).to include user1, user2, user3
    end
    it 'renders the edit template' do
      submit
      expect(response).to render_template :edit
    end
  end

  describe 'POST #generate_rotation' do
    before :each do
      @start_date = Date.today.strftime '%Y-%m-%d'
      @end_date = Date.tomorrow.strftime '%Y-%m-%d'
      user1 = roster_user @roster
      user2 = roster_user @roster
      user3 = roster_user @roster
      @user_ids = [user1.id.to_s, user2.id.to_s, user3.id.to_s]
      @starting_user_id = @user_ids[1]
      when_current_user_is :whoever
      # To test the mailer method called on the returned assignments
      @assignments = [create(:assignment)]
    end
    let :submit do
      post :generate_rotation,
           roster_id: @roster.id,
           start_date: @start_date,
           end_date: @end_date,
           user_ids: @user_ids,
           starting_user_id: @starting_user_id
    end
    context 'admin in roster' do
      before(:each) { when_current_user_is roster_admin(@roster) }
      it 'calls Assignment#generate rotation with the given arguments' do
        # remove all other instances of roster so 'any instance' definitely
        # refers to our @roster instance
        Roster.where.not(id: @roster.id).delete_all
        expect_any_instance_of(Roster).to receive(:generate_assignments)
          .with(@user_ids, Date.today, Date.tomorrow, @starting_user_id)
          .and_return @assignments
        expect_any_instance_of(Assignment).to receive :notify
        submit
      end
      it 'has a flash message' do
        submit
        expect(flash[:message]).not_to be_empty
      end
      it 'redirects to the calendar with the start date given' do
        submit
        expect(response)
          .to redirect_to roster_assignments_path(date: Date.today)
      end<commit_msg>Confirmed #69<commit_after># frozen_string_literal: true

require 'rails_helper'

describe AssignmentsController do
  before :each do
    @roster = create :roster
  end

  describe 'POST #create' do
    before :each do
      @user = roster_user @roster
      @attributes = {
        start_date: Date.today,
        end_date: Date.tomorrow,
        user_id: @user.id,
        roster_id: @roster.id
      }
    end
    let :submit do
      post :create, roster_id: @roster, assignment: @attributes
    end
    context 'admin in roster' do
      before(:each) { when_current_user_is roster_admin(@roster) }
      context 'without errors' do
        it 'creates an assignment' do
          submit
          expect(Assignment.count).to be 1
        end
        it 'sends an email to the new owner of the assignment' do
          expect_any_instance_of(Assignment).to receive(:notify)
          submit
        end
        it 'redirects to the index with a date of the assignment start date' do
          submit
          expect(response).to redirect_to(
            roster_assignments_url(date: @attributes[:start_date])
          )
        end
      end
      context 'with errors' do
        before :each do
          # Guaranteed to not be a user with this ID,
          # but will pass param validation in the controller.
          @attributes[:user_id] = User.pluck(:id).sort.last + 1
        end
        it 'does not create assignment, gives errors, and redirects back' do
          expect { submit }.to redirect_back
          expect(Assignment.all).to be_empty
          expect(flash[:errors]).not_to be_empty
        end
      end
    end
    context 'not admin' do
      before(:each) { when_current_user_is @user }
      context 'creating assignment belonging to self' do
        it 'creates the assignment' do
          expect { submit }.to change(Assignment, :count).by 1
        end
      end
      context 'creating assignment not belonging to self' do
        before(:each) { @attributes[:user_id] = @user.id + 1 }
        it 'does not create the assignment and explains why' do
          expect do
            expect { submit }.to redirect_back
          end.not_to change(Assignment, :count)
          expect(flash[:errors]).not_to be_empty
        end
      end
    end
  end

  describe 'DELETE #destroy' do
    before :each do
      @assignment = create :assignment
      when_current_user_is :whoever
    end
    let :submit do
      delete :destroy, roster_id: @assignment.roster.id, id: @assignment.id
    end
    it 'finds the correct assignment' do
      submit
      expect(assigns.fetch :assignment).to eql @assignment
    end
    it 'destroys the assignment' do
      expect_any_instance_of(Assignment).to receive :destroy
      submit
    end
    it 'sends a notification to the owner of the assignment' do
      expect_any_instance_of(Assignment).to receive :notify
      submit
    end
    it 'redirects to the index' do
      submit
      expect(response).to redirect_to roster_assignments_url
    end
  end

  describe 'GET #edit' do
    before :each do
      @assignment = create :assignment
      @roster = create :roster
      when_current_user_is :whoever
    end
    let :submit do
      get :edit, roster_id: @roster.id, id: @assignment.id
    end
    it 'finds the correct assignment' do
      submit
      expect(assigns.fetch :assignment).to eql @assignment
    end
    it 'populates a users variable of all users' do
      user1 = roster_user @roster
      user2 = roster_user @roster
      user3 = roster_user @roster
      submit
      expect(assigns.fetch :users).to include user1, user2, user3
    end
    it 'renders the edit template' do
      submit
      expect(response).to render_template :edit
    end
  end

  describe 'POST #generate_rotation' do
    before :each do
      @start_date = Date.today.strftime '%Y-%m-%d'
      @end_date = Date.tomorrow.strftime '%Y-%m-%d'
      user1 = roster_user @roster
      user2 = roster_user @roster
      user3 = roster_user @roster
      @user_ids = [user1.id.to_s, user2.id.to_s, user3.id.to_s]
      @starting_user_id = @user_ids[1]
      when_current_user_is :whoever
      # To test the mailer method called on the returned assignments
      @assignments = [create(:assignment)]
    end
    let :submit do
      post :generate_rotation,
           roster_id: @roster.id,
           start_date: @start_date,
           end_date: @end_date,
           user_ids: @user_ids,
           starting_user_id: @starting_user_id
    end
    context 'admin in roster' do
      before(:each) { when_current_user_is roster_admin(@roster) }
      it 'calls Assignment#generate rotation with the given arguments' do
        # remove all other instances of roster so 'any instance' definitely
        # refers to our @roster instance
        Roster.where.not(id: @roster.id).delete_all
        expect_any_instance_of(Roster).to receive(:generate_assignments)
          .with(@user_ids, Date.today, Date.tomorrow, @starting_user_id)
          .and_return @assignments
        expect_any_instance_of(Assignment).to receive :notify
        submit
      end
      it 'has a flash message' do
        submit
        expect(flash[:message]).not_to be_empty
      end
      it 'redirects to the calendar with the start date given' do
        submit
        expect(response)
          .to redirect_to roster_assignments_path(date: Date.today)
      end
      context 'starting user not in selected users' do
        before(:each) { @starting_user_id = roster_user(@roster).id }
        it 'warns that the starting user is not in the selected users' do
          submit
          expect(flash[:errors]).not_to be_empty
        end
      end
{'fraction_non_alphanumeric': 0.07474620429431318, 'fraction_numerical': 0.0032342107627347048, 'mean_word_length': 2.3409363745498197, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6073500', 'n_tokens_mistral': 3312, 'n_tokens_neox': 3251, 'n_words': 1031}