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

From Expertiza_Wiki
Jump to navigation Jump to search
Line 2: Line 2:
<br>
<br>
== E2134. Write unit tests for admin controller.rb and institution controller.rb ==
== 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
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 ==
== Problem Statement and Background ==

Revision as of 02:29, 19 October 2021



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. We designed unit tests for these controllers based on several scenarios.

Implementation

Admin_controller

Role 1: admin1

User type: admin, id: 3, role_id: 4, parent_id: 1, name: 'Aministrator1'

  • Tested tasks allowed:
  1. Lists all the instructors
    For users like admin or super admin, they are supposed to be able to list all the instructors in the database.
  2. Remove an instructor
    For users like admin or super admin, they are supposed to be able to remove an instructor from the database.
  • Tested tasks that are not allowed:
  1. Remove an administrator
    For users like admin, they are not supposed to be able to delete his/her own user account or any other administrators.

Role 2: admin2

User type: admin, id: 4, role_id: 4, parent_id: 1, name: 'Aministrator2'

The data of admin 2 is used for super admin to test functions such as list administrators, etc.

Role 3: super_admin

User type: superadmin, id: 1, role_id: 5

Super admin should be able to perform all the tasks other types of users can do.

Role 4: instructor1

User type: instructor, id: 10, role_id: 3, parent_id: 3, name: 'Instructor1'

Tested tasks that are not allowed and will return false:

  1. List all instructors
  2. Remove an instructor
  3. Remove administrator

Role 5: instructor2

User type: instructor, id: 11, role_id: 3, parent_id: 4, name: 'Instructor2'

The data of instructor 2 is used to test functions such as list instructors, etc.

Role 6: student1

User type: student, id: 21, role_id: 1

Student should not be able to perform any tasks within the admin controller.

Tested tasks that are not allowed and will return false:

  1. List all instructors
  2. Remove an instructor
  3. Remove an administrator

Code Snippet

#spec/controllers/admin_controller_spec.rb
describe AdminController do
  # create fake users
  let(:admin1) { build(:admin, id: 3, role_id: 4, parent_id: 1, name: 'Administrator1') }
  let(:admin2) { build(:admin, id: 4, role_id: 4, parent_id: 1, name: 'Administrator2') }
  let(:super_admin) { build(:superadmin, id: 1, role_id: 5) }
  let(:instructor1) { build(:instructor, id: 10, role_id: 3, parent_id: 3, name: 'Instructor1') }
  let(:instructor2) { build(:instructor, id: 11, role_id: 3, parent_id: 4, name: 'Instructor2') }
  let(:student1) { build(:student, id: 21, role_id: 1) }

  # create fake lists
  let(:admin_list) { [admin1, admin2] }
  let(:instructor_list) { [instructor1, instructor2] }
  let(:instructor_list_pid3) { [instructor1] }
  let(:instructor_list_pid4) { [instructor2] }

  # define default behaviors for each method call
  before(:each) do
    allow(User).to receive(:find).with('3').and_return(admin1)
    allow(User).to receive(:find).with('1').and_return(super_admin)
    allow(User).to receive(:find).with('10').and_return(instructor1)
    allow(User).to receive(:where).with(["role_id = ?", super_admin.role_id]).and_return([ super_admin ])

    allow(User).to receive(:where).with(role_id: admin1.role).and_return(admin_list)
    allow(admin_list).to receive(:order).with(:name).and_return(admin_list)
    allow(admin_list).to receive(:where).with("parent_id = ?", super_admin.id).and_return(admin_list)
    allow(admin_list).to receive(:paginate).with(page: '1', per_page: 50).and_return(admin_list)

    allow(User).to receive(:where).with(role_id: instructor1.role).and_return(instructor_list)
    allow(instructor_list).to receive(:order).with(:name).and_return(instructor_list)
    allow(instructor_list).to receive(:where).with("parent_id = ?", admin1.id).and_return(instructor_list_pid3)
    allow(instructor_list_pid3).to receive(:paginate).with(page: '1', per_page: 50).and_return(instructor_list_pid3)
  end

  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(admin1, admin1.role.name, admin1.role)
          (controller.send(:action_allowed?)).should 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)
          (controller.send(:action_allowed?)).should be true
        end
      end

      context 'when the role of current user is Instructor' do
        it 'refuses certain action' do
          stub_current_user(instructor1, instructor1.role.name, instructor1.role)
          (controller.send(:action_allowed?)).should 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)
          (controller.send(:action_allowed?)).should 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(admin1, admin1.role.name, admin1.role)
          (controller.send(:action_allowed?)).should 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)
          (controller.send(:action_allowed?)).should be true
        end
      end

      context 'when the role of current user is Instructor' do
        it 'refuses certain action' do
          stub_current_user(instructor1, instructor1.role.name, instructor1.role)
          (controller.send(:action_allowed?)).should 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)
          (controller.send(:action_allowed?)).should 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 'refuses certain action' do
          stub_current_user(admin1, admin1.role.name, admin1.role)
          (controller.send(:action_allowed?)).should 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)
          (controller.send(:action_allowed?)).should be true
        end
      end

      context 'when the role of current user is Instructor' do
        it 'refuses certain action' do
          stub_current_user(instructor1, instructor1.role.name, instructor1.role)
          (controller.send(:action_allowed?)).should 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)
          (controller.send(:action_allowed?)).should be false
        end
      end
    end
  end

   context '#list_super_administrators' do
     it 'lists all the Super-Administrators' do
       stub_current_user(super_admin, super_admin.role.name, super_admin.role)
       get :list_super_administrators
       expect(assigns(:users)).to eq([ super_admin ])
     end
   end

  context '#show_super_administrator' do
    it 'find selected Super-Administrator and render #show' do
      stub_current_user(super_admin, super_admin.role.name, super_admin.role)
      params = {id: '1'}
      get :show_super_administrator, params
      expect(assigns(:user)).to eq(super_admin)
      expect(assigns(:role)).to eq(super_admin.role)
      expect(response).to render_template(:show_super_administrator)
    end
  end

  context '#list_administrators' do
    it 'lists all the admins' do
      stub_current_user(super_admin, super_admin.role.name, super_admin.role)
      params = {page: '1'}
      get :list_administrators, params
      expect(assigns(:users)).to eq(admin_list)
    end
  end

  context '#show_administrator' do
    it 'find selected admin and render #show' do
      stub_current_user(super_admin, super_admin.role.name, super_admin.role)
      params = {id: '3'}
      get :show_administrator, params
      expect(assigns(:user)).to eq(admin1)
      expect(assigns(:role)).to eq(admin1.role)
      expect(response).to render_template(:show_administrator)
    end
  end

  context '#list_instructors' do
    it 'lists all the instructors' do
      stub_current_user(admin1, admin1.role.name, admin1.role)
      params = {page: '1'}
      get :list_instructors, params
      expect(assigns(:users)).to eq(instructor_list_pid3)
    end
  end

  context '#show_instructor' do
    it 'find selected instructor and render #show' do
      stub_current_user(super_admin, super_admin.role.name, super_admin.role)
      params = {id: '10'}
      get :show_instructor, params
      expect(assigns(:user)).to eq(instructor1)
      expect(assigns(:role)).to eq(instructor1.role)
      expect(response).to render_template(:show_instructor)
    end
  end
end

Admin_controller

Role 1: admin1

User type: admin, id: 3, role_id: 4, parent_id: 1, name: 'Aministrator1'

  • Tested tasks allowed:
  1. Lists all the instructors
    For users like admin or super admin, they are supposed to be able to list all the instructors in the database.
  2. Remove an instructor
    For users like admin or super admin, they are supposed to be able to remove an instructor from the database.
  • Tested tasks that are not allowed:
  1. Remove an administrator
    For users like admin, they are not supposed to be able to delete his/her own user account or any other administrators.

Role 2: admin2

The data of admin 2 is used for super admin to test functions such as list administrators, etc.

Role 3: super_admin

Super admin should be able to perform all the tasks other types of users can do.

Role 4: instructor1

Tested tasks that are not allowed and will return false:

  1. List all instructors
  2. Remove an instructor
  3. Remove administrator

Role 5: instructor2

The data of instructor 2 is used to test functions such as list instructors, etc.

Role 6: student1

Student should not be able to perform any tasks within the admin controller.

Tested tasks that are not allowed and will return false:

  1. List all instructors
  2. Remove an instructor
  3. Remove an administrator


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 

Demo

Demo of admin_controller_spec click here

Demo of institution_controller_spec click here

Our Team

References

Expertiza on GitHub click here

To visit our forked repo click here

View our pull request click here