CSC/ECE 517 Fall 2021 - 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
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
Scenario 1:
Scenario 2:
Scenario 3:
Scenario 4:
Code Snippet
#spec/controllers/admin_controller_spec.rb
describe AdminController do
# create fake users
let(:admin1) { build(:admin, id: 3, role_id: 4) }
let(:admin2) { build(:admin, id: 4, role_id: 4) }
let(:super_admin) { build(:superadmin, id: 1, role_id: 5) }
let(:instructor1) { build(:instructor, id: 10, role_id: 2) }
let(:instructor2) { build(:instructor, id: 11, role_id: 2) }
let(:student1) { build(:student, id: 21, role_id: 1) }
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(find).with('21').and_return(student1)
allow(User).to receive(where).with(:role_id => 4).and_return([ admin1, admin2 ])
allow(User).to receive(where).with(:role_id => 5).and_return([ super_admin ])
allow(User).to receive(where).with(:role_id => 2).and_return([ instructor1, instructor2 ])
allow(Role).to recieve(superadministrator).to receive(id).and_return(5)
allow(Role).to recieve(administrator).to receive(id).and_return(4)
allow(Role).to recieve(instructor).to receive(id).and_return(2)
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
user = admin1
stub_current_user(user, user.role.name, user.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
user = super_admin
stub_current_user(user, user.role.name, user.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
user = instructor1
stub_current_user(user, user.role.name, user.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
user = student1
stub_current_user(user, user.role.name, user.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
user = admin1
stub_current_user(user, user.role.name, user.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
user = super_admin
stub_current_user(user, user.role.name, user.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
user = instructor1
stub_current_user(user, user.role.name, user.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
user = student1
stub_current_user(user, user.role.name, user.role)
expect(controller.send(:action_allowed?)).to be false
end
end
@@ -87,48 +103,87 @@
end
context 'when the role of current user is Admin' do
it 'refuses certain action' do
user = admin1
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
user = super_admin
stub_current_user(user, user.role.name, user.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
user = instructor1
stub_current_user(user, user.role.name, user.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
user = student1
stub_current_user(user, user.role.name, user.role)
expect(controller.send(:action_allowed?)).to be false
end
end
end
end
context '#list_super_administrators' do
it 'list all the Super-Administrators and render #list' do
get :list_super_administrators
expect(@user).to eql([ super_admin ])
expect(response).to render_template(list_super_administrators)
end
end
context '#show_super_administrator' do
it 'find selected Super-Administrator and render #show' do
controller.params = {id: '1'}
controller.send(:show_super_administrators)
expect(@user).to eql(super_admin)
expect(@role).to eql(5)
expect(response).to render_template(show_super_administrator)
end
end
context '#list_administrators' do
it 'list all the admins and render #list' do
get :list_administrators
expect(response).to render_template(list_administrators)
end
end
context '#show_administrator' do
it 'find selected admin and render #show' do
controller.params = {id: '3'}
controller.send(:show_administrator)
expect(@user).to eql(admin1)
expect(@role).to eql(4)
expect(response).to render_template(show_administrator)
end
end
context '#list_instructors' do
it 'list all the instructors and render #list' do
get :list_instructors
expect(response).to render_template(list_instructors)
end
end
context '#show_instructors' do
it 'find selected instructor and render #show' do
controller.params = {id: '10'}
controller.send(:show_instructor)
expect(@user).to eql(instructor1)
expect(@role).to eql(2)
expect(response).to render_template(show_instructor)
end
end
end
Institution_controller
Scenario 1:
Scenario 2:
Scenario 3:
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