CSC/ECE 517 Fall 2021 - E2134. Write unit tests for admin controller.rb and institution controller.rb

From Expertiza_Wiki
Jump to navigation Jump to search



E2134. Write unit tests for admin controller.rb and institution controller.rb

This page is a description of Expertiza OSS project E2134. Write unit tests for admin controller.rb and institution controller.rb

Problem Statement and Background

Expertiza allows authorized users to create/edit/delete information of institutions and for the admin to create/edit/delete information of instructors. The repository lacks systematic unit tests for both the admin controller and institution controller.

Implementation

Admin_controller

Scenario 1:

Scenario 2:

Scenario 3:

Scenario 4:

Code Snippet

#spec/controllers/admin_controller_spec.rb
describe AdminController do
  subject { User.new }

  let(:admin) { build(:admin, id: 3) }
  let(:super_admin) { build(:superadmin, id: 1) }
  let(:instructor) { build(:instructor, id: 2) }
  let(:student1) { build(:student, id: 1, name: :lily) }
  let(:student2) { build(:student) }
  let(:student3) { build(:student, id: 10, role_id: 1, parent_id: nil) }
  let(:student4) { build(:student, id: 20, role_id: 4) }
  let(:student5) { build(:student, role_id: 4, parent_id: 3) }
  let(:student6) { build(:student, role_id: nil, name: :lilith)}


  describe '#action_allowed?' do
    context 'when params action is list all instructors' do
      before(:each) do
        controller.params = {:action => 'list_instructors'}
      end

      context 'when the role of current user is Admin' do
        it 'allows certain action' do
          stub_current_user(admin, admin.role.name, admin.role)
          expect(controller.send(:action_allowed?)).to be true
        end
      end

      context 'when the role of current user is Super-Admin' do
        it 'allows certain action' do
          stub_current_user(super_admin, super_admin.role.name, super_admin.role)
          expect(controller.send(:action_allowed?)).to be true
        end
      end

      context 'when the role of current user is Instructor' do
        it 'refuses certain action' do
          stub_current_user(instructor, instructor.role.name, instructor.role)
          expect(controller.send(:action_allowed?)).to be false
        end
      end

      context 'when the role of current user is Student' do
        it 'refuses certain action' do
          stub_current_user(student1, student1.role.name, student1.role)
          expect(controller.send(:action_allowed?)).to be false
        end
      end
    end

    context 'when params action is remove an instructor' do
      before(:each) do
        controller.params = {:action => 'remove_instructor'}
      end

      context 'when the role of current user is Admin' do
        it 'allows certain action' do
          stub_current_user(admin, admin.role.name, admin.role)
          expect(controller.send(:action_allowed?)).to be true
        end
      end

      context 'when the role of current user is Super-Admin' do
        it 'allows certain action' do
          stub_current_user(super_admin, super_admin.role.name, super_admin.role)
          expect(controller.send(:action_allowed?)).to be true
        end
      end

      context 'when the role of current user is Instructor' do
        it 'refuses certain action' do
          stub_current_user(instructor, instructor.role.name, instructor.role)
          expect(controller.send(:action_allowed?)).to be false
        end
      end

      context 'when the role of current user is Student' do
        it 'refuses certain action' do
          stub_current_user(student1, student1.role.name, student1.role)
          expect(controller.send(:action_allowed?)).to be false
        end
      end
    end

    context 'when params action is other than list and remove instructors' do
      before(:each) do
        controller.params = {:action => 'remove_administrator'}
      end

      context 'when the role of current user is Admin' do
        it 'allows certain action' do
          stub_current_user(admin, admin.role.name, admin.role)
          expect(controller.send(:action_allowed?)).to be false
        end
      end

      context 'when the role of current user is Super-Admin' do
        it 'allows certain action' do
          stub_current_user(super_admin, super_admin.role.name, super_admin.role)
          expect(controller.send(:action_allowed?)).to be true
        end
      end

      context 'when the role of current user is Instructor' do
        it 'refuses certain action' do
          stub_current_user(instructor, instructor.role.name, instructor.role)
          expect(controller.send(:action_allowed?)).to be false
        end
      end

      context 'when the role of current user is Student' do
        it 'refuses certain action' do
          stub_current_user(student1, student1.role.name, student1.role)
          expect(controller.send(:action_allowed?)).to be false
        end
      end
    end
  end

  context 'list_super_administrators' do
    it 'list all the super admins' do
      allow(User).to receive(where).with(["role_id = 1"]).and_return(ActiveRecordRelationStub.new(User, [super_admin]))
      expect(@user).to eql?(ActiveRecordRelationStub.new(User, [super_admin]))
      expect(controller.send(:list_super_administrators)).to render_template('list_administrators.html.erb')
    end
  end

  context 'show_super_administrator' do
    it 'render show page' do
      allow(User).to receive(where).with(["role_id = 1"]).and_return(ActiveRecordRelationStub.new(User, [super_admin]))
      expect(@user).to eql?(ActiveRecordRelationStub.new(User, [super_admin]))
      expect(controller.send(:show_super_administrator)).to render_template('show_administrators.html.erb')
    end
  end
end

Institution_controller

Scenario 1:

Code Snippet

Scenario 2:

Code Snippet

Scenario 3:

Code Snippet

Scenario 4:

Code Snippet

require 'rails_helper'
describe InstitutionController do
  let(:admin) { build(:admin) }
  let(:instructor) { build(:instructor, id: 6) }
  let(:instructor2) { build(:instructor, id: 66) }
  let(:ta) { build(:teaching_assistant, id: 8) }
  let(:student) { build(:student) }

  # create fake data
  let(:institution) do
    build(:institution, id: 1, name: 'test institution')
  end

  let(:course) do
    build(:course, instructor_id: 6, institutions_id:1, name: 'abc')
  end

  # set default testing user
  before(:each) do
    allow(Institution).to receive(:find).with('1').and_return(institution)
    allow(Course).to receive(:where).with(institution_id: '1').and_return(course)
    stub_current_user(instructor, instructor.role.name, instructor.role)
  end
  describe '#action_allowed?' do

    context 'when params action is edit or update' do
      before(:each) do
        controller.params = {id: '1', action: 'edit'}
      end

      context 'when the role name of current user is super admin or admin' do
        it 'allows certain action' do
          stub_current_user(admin, admin.role.name, admin.role)
          (controller.send(:action_allowed?)).should be true
        end
      end

      context 'when current user is the instructor' do
        it 'allows certain action' do
          stub_current_user(instructor, instructor.role.name, instructor.role)
          (controller.send(:action_allowed?)).should be true
        end
      end

      context 'when current user is the TAs or the students' do
        it 'deny certain action if current user is the TA' do
          stub_current_user(ta, ta.role.name, ta.role)
          (controller.send(:action_allowed?)).should be false
        end

        it 'deny certain action if current user is the student' do
          stub_current_user(student, student.role.name, student.role)
          (controller.send(:action_allowed?)).should be false
        end
      end
    end
  end

  describe '#new' do
    it 'creates a new Institution object and renders institution#new page' do
      get :new
      expect(response).to render_template(:new)
    end
  end

  describe '#create' do
    context 'when institution is saved successfully' do
      it 'redirects to institution#list page' do
        allow(institution).to receive(:name).and_return("test institution")
        @params = {
            institution: {
                name: "test institution"
            }
        }
        post :create, @params
        expect(response).to redirect_to("/institution/list")
      end
    end

    context 'when institution is not saved successfully' do
      it 'renders institution#new page' do
        allow(institution).to receive(:save).and_return(false)
        @params = {
            institution: {
                name: ""
            }
        }
        post :create, @params
        expect(flash.now[:error]).to eq('The creation of the institution failed.')
        expect(response).to render_template(:new)
      end
    end
  end
  describe '#edit' do
    it 'renders institution#edit' do
      @params = {
          id: 1
      }
      get :edit, @params
      expect(response).to render_template(:edit)
    end
  end

  describe '#update' do
    context 'when institution is updated successfully' do
      it 'renders institution#list' do
        @params = {
            id:1,
            institution: {
                name: "test institution"
            }
        }
        put :update, @params
        expect(response).to redirect_to("/institution/list")
      end
    end
  end

  describe '#index' do
    context 'when institution query all institution' do
      it 'renders institution#list' do
        get :index
        expect(response).to render_template(:list)
      end
    end
  end

  describe '#show' do
    context 'when try to show a institution' do
      it 'renders institution#show when find the target institution' do
        @params = {
            id:1
        }
        get :show, @params
        expect(response).to render_template(:show)
      end
    end
  end

  describe '#delete' do
    context 'when try to delete a institution' do
      it 'renders institution#list when delete successfully' do
        @params = {
            id:1
        }
        post :delete, @params, session
        expect(response).to redirect_to("/institution/list")
      end
    end
  end
end 

Our Team

References

Expertiza on GitHub click here

To visit our forked repo, click here

View our pull request click here