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
 
(33 intermediate revisions by the same user not shown)
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 ==


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.
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 ==
== Implementation ==
Line 14: Line 14:




==== <font size = '3'>Scenario 1: </font> ====
==== <font size = '3'>Role 1: admin1</font> ====
User type: admin, id: 3, role_id: 4, parent_id: 1, name: 'Aministrator1'
*Tested tasks allowed:
#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.
#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:
#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.


==== <font size = '3'>Role 2: admin2</font> ====
User type: admin, id: 4, role_id: 4, parent_id: 1, name: 'Aministrator2'


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


==== <font size = '3'>Role 3: super_admin</font> ====
User type: superadmin, id: 1, role_id: 5


==== <font size = '3'>Scenario 2: </font> ====
Super admin should be able to perform all the tasks other types of users can do.


==== <font size = '3'>Role 4: instructor1</font> ====
User type: instructor, id: 10, role_id: 3, parent_id: 3, name: 'Instructor1'
<p>Tested tasks that are not allowed and will return false:</p>
#List all instructors
#Remove an instructor
#Remove administrator


==== Code Snippet ====
==== <font size = '3'>Role 5: instructor2</font> ====
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.


==== <font size = '3'>Scenario 3: </font> ====
==== <font size = '3'>Role 6: student1</font> ====
User type: student, id: 21, role_id: 1


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


==== Code Snippet ====
<p>Tested tasks that are not allowed and will return false:</p>
 
#List all instructors
 
#Remove an instructor
==== <font size = '3'> Scenario 4:</font> ====
#Remove an administrator
 


==== <font size = '2'> Code Snippet ====
==== <font size = '2'> Code Snippet ====
Line 39: Line 61:
#spec/controllers/admin_controller_spec.rb
#spec/controllers/admin_controller_spec.rb
describe AdminController do
describe AdminController do
   subject { User.new }
   # 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 ])


  let(:admin) { build(:admin, id: 3) }
    allow(User).to receive(:where).with(role_id: admin1.role).and_return(admin_list)
  let(:super_admin) { build(:superadmin, id: 1) }
    allow(admin_list).to receive(:order).with(:name).and_return(admin_list)
  let(:instructor) { build(:instructor, id: 2) }
    allow(admin_list).to receive(:where).with("parent_id = ?", super_admin.id).and_return(admin_list)
  let(:student1) { build(:student, id: 1, name: :lily) }
    allow(admin_list).to receive(:paginate).with(page: '1', per_page: 50).and_return(admin_list)
  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)}


    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
   describe '#action_allowed?' do
     context 'when params action is list all instructors' do
     context 'when params action is list all instructors' do
       before(:each) do
       before(:each) do
         controller.params = {:action => 'list_instructors'}
         controller.params = {action: 'list_instructors'}
       end
       end


       context 'when the role of current user is Admin' do
       context 'when the role of current user is Admin' do
         it 'allows certain action' do
         it 'allows certain action' do
           stub_current_user(admin, admin.role.name, admin.role)
           stub_current_user(admin1, admin1.role.name, admin1.role)
           expect(controller.send(:action_allowed?)).to be true
           (controller.send(:action_allowed?)).should be true
         end
         end
       end
       end
Line 68: Line 109:
         it 'allows certain action' do
         it 'allows certain action' do
           stub_current_user(super_admin, super_admin.role.name, super_admin.role)
           stub_current_user(super_admin, super_admin.role.name, super_admin.role)
           expect(controller.send(:action_allowed?)).to be true
           (controller.send(:action_allowed?)).should be true
         end
         end
       end
       end
Line 74: Line 115:
       context 'when the role of current user is Instructor' do
       context 'when the role of current user is Instructor' do
         it 'refuses certain action' do
         it 'refuses certain action' do
           stub_current_user(instructor, instructor.role.name, instructor.role)
           stub_current_user(instructor1, instructor1.role.name, instructor1.role)
           expect(controller.send(:action_allowed?)).to be false
           (controller.send(:action_allowed?)).should be false
         end
         end
       end
       end
Line 82: Line 123:
         it 'refuses certain action' do
         it 'refuses certain action' do
           stub_current_user(student1, student1.role.name, student1.role)
           stub_current_user(student1, student1.role.name, student1.role)
           expect(controller.send(:action_allowed?)).to be false
           (controller.send(:action_allowed?)).should be false
         end
         end
       end
       end
Line 89: Line 130:
     context 'when params action is remove an instructor' do
     context 'when params action is remove an instructor' do
       before(:each) do
       before(:each) do
         controller.params = {:action => 'remove_instructor'}
         controller.params = {action: 'remove_instructor'}
       end
       end


       context 'when the role of current user is Admin' do
       context 'when the role of current user is Admin' do
         it 'allows certain action' do
         it 'allows certain action' do
           stub_current_user(admin, admin.role.name, admin.role)
           stub_current_user(admin1, admin1.role.name, admin1.role)
           expect(controller.send(:action_allowed?)).to be true
           (controller.send(:action_allowed?)).should be true
         end
         end
       end
       end
Line 102: Line 143:
         it 'allows certain action' do
         it 'allows certain action' do
           stub_current_user(super_admin, super_admin.role.name, super_admin.role)
           stub_current_user(super_admin, super_admin.role.name, super_admin.role)
           expect(controller.send(:action_allowed?)).to be true
           (controller.send(:action_allowed?)).should be true
         end
         end
       end
       end
Line 108: Line 149:
       context 'when the role of current user is Instructor' do
       context 'when the role of current user is Instructor' do
         it 'refuses certain action' do
         it 'refuses certain action' do
           stub_current_user(instructor, instructor.role.name, instructor.role)
           stub_current_user(instructor1, instructor1.role.name, instructor1.role)
           expect(controller.send(:action_allowed?)).to be false
           (controller.send(:action_allowed?)).should be false
         end
         end
       end
       end
Line 116: Line 157:
         it 'refuses certain action' do
         it 'refuses certain action' do
           stub_current_user(student1, student1.role.name, student1.role)
           stub_current_user(student1, student1.role.name, student1.role)
           expect(controller.send(:action_allowed?)).to be false
           (controller.send(:action_allowed?)).should be false
         end
         end
       end
       end
Line 127: Line 168:


       context 'when the role of current user is Admin' do
       context 'when the role of current user is Admin' do
         it 'allows certain action' do
         it 'refuses certain action' do
           stub_current_user(admin, admin.role.name, admin.role)
           stub_current_user(admin1, admin1.role.name, admin1.role)
           expect(controller.send(:action_allowed?)).to be false
           (controller.send(:action_allowed?)).should be false
         end
         end
       end
       end
Line 136: Line 177:
         it 'allows certain action' do
         it 'allows certain action' do
           stub_current_user(super_admin, super_admin.role.name, super_admin.role)
           stub_current_user(super_admin, super_admin.role.name, super_admin.role)
           expect(controller.send(:action_allowed?)).to be true
           (controller.send(:action_allowed?)).should be true
         end
         end
       end
       end
Line 142: Line 183:
       context 'when the role of current user is Instructor' do
       context 'when the role of current user is Instructor' do
         it 'refuses certain action' do
         it 'refuses certain action' do
           stub_current_user(instructor, instructor.role.name, instructor.role)
           stub_current_user(instructor1, instructor1.role.name, instructor1.role)
           expect(controller.send(:action_allowed?)).to be false
           (controller.send(:action_allowed?)).should be false
         end
         end
       end
       end
Line 150: Line 191:
         it 'refuses certain action' do
         it 'refuses certain action' do
           stub_current_user(student1, student1.role.name, student1.role)
           stub_current_user(student1, student1.role.name, student1.role)
           expect(controller.send(:action_allowed?)).to be false
           (controller.send(:action_allowed?)).should be false
         end
         end
       end
       end
Line 156: Line 197:
   end
   end


  context 'list_super_administrators' do
  context '#list_super_administrators' do
    it 'list all the super admins' do
    it 'lists all the Super-Administrators' do
      allow(User).to receive(where).with(["role_id = 1"]).and_return(ActiveRecordRelationStub.new(User, [super_admin]))
      stub_current_user(super_admin, super_admin.role.name, super_admin.role)
       expect(@user).to eql?(ActiveRecordRelationStub.new(User, [super_admin]))
      get :list_super_administrators
       expect(controller.send(:list_super_administrators)).to render_template('list_administrators.html.erb')
      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
   end
   end


   context 'show_super_administrator' do
   context '#show_instructor' do
     it 'render show page' do
     it 'find selected instructor and render #show' do
       allow(User).to receive(where).with(["role_id = 1"]).and_return(ActiveRecordRelationStub.new(User, [super_admin]))
       stub_current_user(super_admin, super_admin.role.name, super_admin.role)
       expect(@user).to eql?(ActiveRecordRelationStub.new(User, [super_admin]))
      params = {id: '10'}
       expect(controller.send(:show_super_administrator)).to render_template('show_administrators.html.erb')
      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
   end
Line 175: Line 259:


=== <font size = '4'>Institution_controller ===
=== <font size = '4'>Institution_controller ===
:institution, id: 1, name: 'test institution'
==== <font size = '3'>Role 1: Admin</font> ====
:admin
*Tested tasks allowed:
#Edit an institution
#:For users like admin, they are supposed to be able to edit all the institutions in the database.


==== <font size = '3'>Role 2: Instructor</font> ====
:instructor, id: 6
*Tested tasks allowed:
#Edit an institution
#Creates a new institution object and renders institution new page
#Save a new institution to the database and being redirected to the institution list page with a successful message
#Update the information of an institution
#Delete an institution
*Tested tasks that will receive false:
#Fail to save a new institution to the database
#Fail to update the information of an institution


==== <font size = '3'>Scenario 1: </font>====
==== <font size = '3'>Role 3: TA</font> ====
:teaching_assistant, id: 8
<p>Tested tasks that are not allowed and will return false:</p>
#Edit an institution


==== <font size = '3'>Role 4: student</font> ====
:student
<p>Tested tasks that are not allowed and will return false:</p>
#Edit an institution


==== Code Snippet ====
==== <font size = '2'> Code Snippet ====
<pre>
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
 
  # set default testing user
  before(:each) do
    allow(Institution).to receive(:find).with('1').and_return(institution)
    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 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


==== <font size = '3'>Scenario 2: </font> ====
      context 'when current user is the TAs or the students' do
        it 'deny certain action' do
          stub_current_user(ta, ta.role.name, ta.role)
          (controller.send(:action_allowed?)).should be false
        end
      end
      context 'when current user is the students' do
        it 'deny certain action' 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
      stub_current_user(instructor, instructor.role.name, instructor.role)
      get :new
      expect(response).to render_template(:new)
    end
  end


==== Code Snippet ====
  describe '#create' do
    context 'when institution is saved successfully' do
      it 'redirects to institution#list page and returns a successful message' do
        stub_current_user(instructor, instructor.role.name, instructor.role)
        allow(institution).to receive(:name).and_return("test institution")
        @params = {
            institution: {
                name: "test institution"
            }
        }
        post :create, @params
        expect(flash.now[:success]).to eq('The institution was successfully created.')
        expect(response).to redirect_to("/institution/list")
      end
    end


    context 'when institution is not saved successfully' do
      it 'renders institution#new page and returns an error message' do
        stub_current_user(instructor, instructor.role.name, instructor.role)
        # make save function always return false
        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


==== <font size = '3'>Scenario 3: </font> ====
  describe '#update' do
    context 'when institution is updated successfully' do
      it 'renders institution#list and returns a successful message' do
        stub_current_user(instructor, instructor.role.name, instructor.role)
        @params = {
            id:1,
            institution: {
                name: "test institution"
            }
        }
        put :update, @params
        expect(flash[:success]).to eq('The institution was successfully updated.')
        expect(response).to redirect_to("/institution/list")
      end
    end
    context 'when institution is not updated successfully' do
      it 'renders institution#edit' do
        stub_current_user(instructor, instructor.role.name, instructor.role)
        @params = {
            id:1,
            institution: {
                name: "test institution"
            }
        }
        allow(institution).to receive(:update_attribute).with(any_args).and_return(false)
        put :update, @params
        expect(response).to render_template(:edit)
      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


==== Code Snippet ====
  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
        stub_current_user(instructor, instructor.role.name, instructor.role)
        @params = {
            id:1
        }
        # mock a situation that delete action successfully
        allow(institution).to receive(:destroy).with(any_args).and_return(true)
        post :delete, @params
        expect(response).to redirect_to("/institution/list")
      end
    end
  end
end
</pre>


==== <font size = '3'> Scenario 4:</font> ====
== Demo ==


Demo of admin_controller_spec [https://www.youtube.com/watch?v=OZaaVLBul6Y&list=PLaoBe-cpzKA6Eq9xSogYiatOcOeSoRLQN&index=1 click here]


==== Code Snippet ====
Demo of institution_controller_spec [https://www.youtube.com/watch?v=A1vuITGSRBI&list=PLaoBe-cpzKA6Eq9xSogYiatOcOeSoRLQN&index=2 click here]


== Our Team ==
== Our Team ==
*Yu-Hsuan Lo (ylo) (githubID: [https://github.com/yhslo yhslo])
*Chao-Ting Hung (chung4) (githubID: [https://github.com/gggho0806 gggho0806])


*Lee Shyu (lshyu) (githubID: [https://github.com/ls-hyu ls-hyu])


== References ==
== References ==
Line 207: Line 481:
Expertiza on GitHub [https://github.com/expertiza/expertiza click here]
Expertiza on GitHub [https://github.com/expertiza/expertiza click here]


To visit our forked repo, [https://github.com/ click here]
To visit our forked repo [https://github.com/yhslo/expertiza click here]


View our pull request [https://github.com/expertiza/expertiza/ click here]
View our pull request [https://github.com/expertiza/expertiza click here]

Latest revision as of 19:05, 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

Institution_controller

institution, id: 1, name: 'test institution'

Role 1: Admin

admin
  • Tested tasks allowed:
  1. Edit an institution
    For users like admin, they are supposed to be able to edit all the institutions in the database.

Role 2: Instructor

instructor, id: 6
  • Tested tasks allowed:
  1. Edit an institution
  2. Creates a new institution object and renders institution new page
  3. Save a new institution to the database and being redirected to the institution list page with a successful message
  4. Update the information of an institution
  5. Delete an institution
  • Tested tasks that will receive false:
  1. Fail to save a new institution to the database
  2. Fail to update the information of an institution

Role 3: TA

teaching_assistant, id: 8

Tested tasks that are not allowed and will return false:

  1. Edit an institution

Role 4: student

student

Tested tasks that are not allowed and will return false:

  1. Edit an institution

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

  # set default testing user
  before(:each) do
    allow(Institution).to receive(:find).with('1').and_return(institution)
    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 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' do
          stub_current_user(ta, ta.role.name, ta.role)
          (controller.send(:action_allowed?)).should be false
        end
      end
      context 'when current user is the students' do
        it 'deny certain action' 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
      stub_current_user(instructor, instructor.role.name, instructor.role)
      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 and returns a successful message' do
        stub_current_user(instructor, instructor.role.name, instructor.role)
        allow(institution).to receive(:name).and_return("test institution")
        @params = {
            institution: {
                name: "test institution"
            }
        }
        post :create, @params
        expect(flash.now[:success]).to eq('The institution was successfully created.')
        expect(response).to redirect_to("/institution/list")
      end
    end

    context 'when institution is not saved successfully' do
      it 'renders institution#new page and returns an error message' do
        stub_current_user(instructor, instructor.role.name, instructor.role)
        # make save function always return false
        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 and returns a successful message' do
        stub_current_user(instructor, instructor.role.name, instructor.role)
        @params = {
            id:1,
            institution: {
                name: "test institution"
            }
        }
        put :update, @params
        expect(flash[:success]).to eq('The institution was successfully updated.')
        expect(response).to redirect_to("/institution/list")
      end
    end
    context 'when institution is not updated successfully' do
      it 'renders institution#edit' do
        stub_current_user(instructor, instructor.role.name, instructor.role)
        @params = {
            id:1,
            institution: {
                name: "test institution"
            }
        }
        allow(institution).to receive(:update_attribute).with(any_args).and_return(false)
        put :update, @params
        expect(response).to render_template(:edit)
      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
        stub_current_user(instructor, instructor.role.name, instructor.role)
        @params = {
            id:1
        }
        # mock a situation that delete action successfully
        allow(institution).to receive(:destroy).with(any_args).and_return(true)
        post :delete, @params
        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

  • Yu-Hsuan Lo (ylo) (githubID: yhslo)
  • Chao-Ting Hung (chung4) (githubID: gggho0806)
  • Lee Shyu (lshyu) (githubID: ls-hyu)

References

Expertiza on GitHub click here

To visit our forked repo click here

View our pull request click here