CSC/ECE 517 Fall 2021 - E2134. Write unit tests for admin controller.rb and institution controller.rb: Difference between revisions
No edit summary |
|||
(21 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 == | ||
Line 26: | Line 26: | ||
==== <font size = '3'>Role 2: admin2</font> ==== | ==== <font size = '3'>Role 2: admin2</font> ==== | ||
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. | 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> ==== | ==== <font size = '3'>Role 3: super_admin</font> ==== | ||
User type: superadmin, id: 1, role_id: 5 | |||
Super admin should be able to perform all the tasks other types of users can do. | Super admin should be able to perform all the tasks other types of users can do. | ||
==== <font size = '3'>Role 4: instructor1</font> ==== | ==== <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> | <p>Tested tasks that are not allowed and will return false:</p> | ||
#List all instructors | #List all instructors | ||
Line 39: | Line 43: | ||
==== <font size = '3'>Role 5: instructor2</font> ==== | ==== <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. | The data of instructor 2 is used to test functions such as list instructors, etc. | ||
==== <font size = '3'>Role 6: student1</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. | Student should not be able to perform any tasks within the admin controller. | ||
Line 48: | Line 56: | ||
#Remove an instructor | #Remove an instructor | ||
#Remove an administrator | #Remove an administrator | ||
==== <font size = '2'> Code Snippet ==== | ==== <font size = '2'> Code Snippet ==== | ||
Line 253: | 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'> | ==== <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'>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'> | ==== <font size = '3'>Role 4: student</font> ==== | ||
:student | |||
<p>Tested tasks that are not allowed and will return false:</p> | |||
#Edit an institution | |||
==== <font size = '2'> Code Snippet ==== | ==== <font size = '2'> Code Snippet ==== | ||
Line 280: | Line 302: | ||
let(:institution) do | let(:institution) do | ||
build(:institution, id: 1, name: 'test institution') | build(:institution, id: 1, name: 'test institution') | ||
end | end | ||
Line 289: | Line 307: | ||
before(:each) do | before(:each) do | ||
allow(Institution).to receive(:find).with('1').and_return(institution) | allow(Institution).to receive(:find).with('1').and_return(institution) | ||
stub_current_user(instructor, instructor.role.name, instructor.role) | stub_current_user(instructor, instructor.role.name, instructor.role) | ||
end | end | ||
Line 299: | Line 316: | ||
end | end | ||
context 'when the role name of current user is | context 'when the role name 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(admin, admin.role.name, admin.role) | ||
Line 314: | Line 331: | ||
context 'when current user is the TAs or the students' do | context 'when current user is the TAs or the students' do | ||
it 'deny certain action | it 'deny certain action' do | ||
stub_current_user(ta, ta.role.name, ta.role) | stub_current_user(ta, ta.role.name, ta.role) | ||
(controller.send(:action_allowed?)).should be false | (controller.send(:action_allowed?)).should be false | ||
end | end | ||
end | |||
it 'deny certain action | context 'when current user is the students' do | ||
it 'deny certain action' do | |||
stub_current_user(student, student.role.name, student.role) | stub_current_user(student, student.role.name, student.role) | ||
(controller.send(:action_allowed?)).should be false | (controller.send(:action_allowed?)).should be false | ||
Line 329: | Line 347: | ||
describe '#new' do | describe '#new' do | ||
it 'creates a new Institution object and renders institution#new page' do | it 'creates a new Institution object and renders institution#new page' do | ||
stub_current_user(instructor, instructor.role.name, instructor.role) | |||
get :new | get :new | ||
expect(response).to render_template(:new) | expect(response).to render_template(:new) | ||
Line 336: | Line 355: | ||
describe '#create' do | describe '#create' do | ||
context 'when institution is saved successfully' do | context 'when institution is saved successfully' do | ||
it 'redirects to institution#list page' 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") | allow(institution).to receive(:name).and_return("test institution") | ||
@params = { | @params = { | ||
Line 344: | Line 364: | ||
} | } | ||
post :create, @params | post :create, @params | ||
expect(flash.now[:success]).to eq('The institution was successfully created.') | |||
expect(response).to redirect_to("/institution/list") | expect(response).to redirect_to("/institution/list") | ||
end | end | ||
Line 349: | Line 370: | ||
context 'when institution is not saved successfully' do | context 'when institution is not saved successfully' do | ||
it 'renders institution#new page' 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) | allow(institution).to receive(:save).and_return(false) | ||
@params = { | @params = { | ||
Line 374: | Line 397: | ||
describe '#update' do | describe '#update' do | ||
context 'when institution is updated successfully' do | context 'when institution is updated successfully' do | ||
it 'renders institution#list' do | it 'renders institution#list and returns a successful message' do | ||
stub_current_user(instructor, instructor.role.name, instructor.role) | |||
@params = { | @params = { | ||
id:1, | id:1, | ||
Line 382: | Line 406: | ||
} | } | ||
put :update, @params | put :update, @params | ||
expect(flash[:success]).to eq('The institution was successfully updated.') | |||
expect(response).to redirect_to("/institution/list") | 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 | end | ||
Line 411: | Line 450: | ||
context 'when try to delete a institution' do | context 'when try to delete a institution' do | ||
it 'renders institution#list when delete successfully' do | it 'renders institution#list when delete successfully' do | ||
stub_current_user(instructor, instructor.role.name, instructor.role) | |||
@params = { | @params = { | ||
id:1 | id:1 | ||
} | } | ||
post :delete, @params | # 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") | expect(response).to redirect_to("/institution/list") | ||
end | end | ||
end | end | ||
end | end | ||
end | end | ||
</pre> | </pre> | ||
== Demo == | |||
== | Demo of admin_controller_spec [https://www.youtube.com/watch?v=OZaaVLBul6Y&list=PLaoBe-cpzKA6Eq9xSogYiatOcOeSoRLQN&index=1 click here] | ||
Demo of | 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 435: | 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 | To visit our forked repo [https://github.com/yhslo/expertiza click here] | ||
View our pull request [https://github.com/expertiza/expertiza | 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:
- 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.
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:
- List all instructors
- Remove an instructor
- 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:
- List all instructors
- Remove an instructor
- 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:
- 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:
- 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
Role 3: TA
- teaching_assistant, id: 8
Tested tasks that are not allowed and will return false:
- Edit an institution
Role 4: student
- student
Tested tasks that are not allowed and will return false:
- 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