CSC/ECE 517 Spring 2022 - E2205: Testing for participants controller, versions controller: Difference between revisions
(→Links) |
|||
(12 intermediate revisions by 2 users not shown) | |||
Line 25: | Line 25: | ||
1. controller_locale - This is to test the function 'controller_locale' which is to search the courses' locale of current user. | 1. controller_locale - This is to test the function 'controller_locale' which is to search the courses' locale of current user. | ||
<pre> | <pre> | ||
describe '#controller_locale' do | describe '#controller_locale' do | ||
it 'should return I18n.default_locale' do | it 'should return I18n.default_locale' do | ||
user = student | user = student | ||
stub_current_user(user, user.role.name, user.role) | stub_current_user(user, user.role.name, user.role) | ||
expect(controller.send(:controller_locale)).to eq(I18n.default_locale) | expect(controller.send(:controller_locale)).to eq(I18n.default_locale) | ||
end | |||
end | |||
</pre> | |||
2. update_authorizations | |||
<pre> | |||
describe '#update_authorizations' do | |||
it 'updates the authorizations for the participant' do | |||
allow(Participant).to receive(:find).with('1').and_return(participant) | |||
params = { authorization: 'participant', id: 1 } | |||
session = { user: instructor } | |||
get :update_authorizations, params, session | |||
expect(response).to redirect_to('/participants/list?id=1&model=Assignment') | |||
end | |||
it 'updates the authorizations fails' do | |||
allow(Participant).to receive(:find).with('1').and_return(participant) | |||
params = { authorization: 'participant', id: 1 } | |||
session = { user: student } | |||
get :update_authorizations, params, session | |||
expect(flash[:error]).to eq 'A student is not allowed to update_authorizations this/these participants' | |||
expect(response).to redirect_to('/') | |||
end | |||
end | |||
</pre> | |||
3. change_handle | |||
<pre> | |||
describe '#change_handle' do | |||
it 'changes the handle of the participant' do | |||
allow(AssignmentParticipant).to receive(:find).with('1').and_return(participant) | |||
params = { id: 1, participant: { handle: 'new_handle' } } | |||
session = { user: student1 } | |||
xhr :get, :change_handle, params, session | |||
expect(response).to have_http_status(200) | |||
end | |||
end | |||
</pre> | |||
4. view_copyright_grants | |||
<pre> | |||
describe '#view_copyright_grants' do | |||
it 'renders the copyright_grants template' do | |||
allow(Assignment).to receive(:find).with('1').and_return(assignment) | |||
params = { id: 1 } | |||
session = { user: instructor } | |||
get :view_copyright_grants, params, session | |||
expect(response).to render_template('view_copyright_grants') | |||
end | |||
end | |||
</pre> | |||
5. validate_authorizations | |||
<pre> | |||
describe '#validate_authorizations' do | |||
# Test case for successful update of participant to reviewer, expects the success flash message after role is updated. | |||
it 'updates the authorizations for the participant to make them reviewer' do | |||
allow(Participant).to receive(:find).with('1').and_return(participant) | |||
params = { authorization: 'reviewer', id: 1 } | |||
session = { user: instructor } | |||
get :update_authorizations, params, session | |||
expect(flash[:success]).to eq 'The role of the selected participants has been successfully updated.' | |||
expect(participant.can_review).to eq(true) | |||
expect(participant.can_submit).to eq(false) | |||
expect(participant.can_take_quiz).to eq(false) | |||
end | |||
# Test for case where we expect to encounter an error in update_attributes method | |||
it ' throws an exception while validating authorizations' do | |||
allow(Participant).to receive(:find).with('1').and_return(participant) | |||
allow(participant).to receive(:update_attributes).and_raise(StandardError) | |||
params = { authorization: 'reviewer', id: 1 } | |||
session = { user: instructor } | |||
get :update_authorizations, params, session | |||
expect(flash[:error]).to eq 'The update action failed.' | |||
end | |||
end | |||
</pre> | |||
6. get_user_info | |||
<pre> | |||
describe '#get_user_info' do | |||
it 'gives the user information from the the team user' do | |||
allow(assignment).to receive(:participants).and_return([participant]) | |||
allow(participant).to receive(:permission_granted?).and_return(true) | |||
allow(participant).to receive(:user).and_return(student) | |||
allow(student).to receive(:name).and_return('name') | |||
allow(student).to receive(:fullname).and_return('fullname') | |||
pc = ParticipantsController.new | |||
expect(pc.send(:get_user_info, student, assignment)).to eq(name: 'name', fullname: 'fullname', pub_rights: 'Granted', verified: false) | |||
end | |||
end | |||
</pre> | |||
7. get_signup_topics_for_assignment | |||
<pre> | |||
describe '#get_signup_topics_for_assignment' do | |||
it 'gives the signup topics for assignment' do | |||
pc = ParticipantsController.new | |||
expect(pc.send(:get_signup_topics_for_assignment, topic.assignment.id, topic, signed_up_team.team_id)).to eq(true) | |||
end | end | ||
end | end | ||
Line 105: | Line 204: | ||
=== Conclusion === | === Conclusion === | ||
Here is the link for the screenshots of the coverage: | |||
[https://i.imgur.com/IYDSPU3.png Versions Controller Test Coverage] | |||
[https://i.imgur.com/IwDWIcy.png Participants Controller Test Coverage] | |||
=== Links === | === Links === | ||
[https://github.com/expertiza/expertiza/pull/2289 The pull request] | |||
[https://github.com/jiangyy12/expertiza/tree/beta The forked git repository ] | |||
[https://drive.google.com/file/d/1X5AS8-cETX-60j6qzIovXsKADGMZiBtu/view?usp=sharing Test video ] |
Latest revision as of 01:45, 22 March 2022
About Expertiza
Expertiza is the software benefits for both instructors and students by supporting various types of submissions and providing reusable objects for peer review. It is an open-source project based on Ruby on Rails framework. It allows the instructors not only to create and customize new or existing assignments but also to create a list of topics the students can sign up for. Students can form teams to work on various projects and assignments. Expertiza also lets students peer-review other students' submissions, enabling them to work together to improve others' learning experiences.
Description about project
This page is a description of Expertiza OSS project E2205 which is testing for participants_controller and versions_controller
Files Involved
- participants_controller_spec.rb
- versions_controller_spec.rb
Running Tests
rspec ./spec/controllers/participants_controller_spec.rb rspec ./spec/controllers/versions_controller_spec.rb
Requirement
In participants controller had some methods not tested, also some edge cases not included in the previous test work. And There were no tests for the versions controller. Our aim is to test all the methods in these two controllers and try to cover more edge cases when testing.
Analysis
Test for Participants Controller
1. controller_locale - This is to test the function 'controller_locale' which is to search the courses' locale of current user.
describe '#controller_locale' do it 'should return I18n.default_locale' do user = student stub_current_user(user, user.role.name, user.role) expect(controller.send(:controller_locale)).to eq(I18n.default_locale) end end
2. update_authorizations
describe '#update_authorizations' do it 'updates the authorizations for the participant' do allow(Participant).to receive(:find).with('1').and_return(participant) params = { authorization: 'participant', id: 1 } session = { user: instructor } get :update_authorizations, params, session expect(response).to redirect_to('/participants/list?id=1&model=Assignment') end it 'updates the authorizations fails' do allow(Participant).to receive(:find).with('1').and_return(participant) params = { authorization: 'participant', id: 1 } session = { user: student } get :update_authorizations, params, session expect(flash[:error]).to eq 'A student is not allowed to update_authorizations this/these participants' expect(response).to redirect_to('/') end end
3. change_handle
describe '#change_handle' do it 'changes the handle of the participant' do allow(AssignmentParticipant).to receive(:find).with('1').and_return(participant) params = { id: 1, participant: { handle: 'new_handle' } } session = { user: student1 } xhr :get, :change_handle, params, session expect(response).to have_http_status(200) end end
4. view_copyright_grants
describe '#view_copyright_grants' do it 'renders the copyright_grants template' do allow(Assignment).to receive(:find).with('1').and_return(assignment) params = { id: 1 } session = { user: instructor } get :view_copyright_grants, params, session expect(response).to render_template('view_copyright_grants') end end
5. validate_authorizations
describe '#validate_authorizations' do # Test case for successful update of participant to reviewer, expects the success flash message after role is updated. it 'updates the authorizations for the participant to make them reviewer' do allow(Participant).to receive(:find).with('1').and_return(participant) params = { authorization: 'reviewer', id: 1 } session = { user: instructor } get :update_authorizations, params, session expect(flash[:success]).to eq 'The role of the selected participants has been successfully updated.' expect(participant.can_review).to eq(true) expect(participant.can_submit).to eq(false) expect(participant.can_take_quiz).to eq(false) end # Test for case where we expect to encounter an error in update_attributes method it ' throws an exception while validating authorizations' do allow(Participant).to receive(:find).with('1').and_return(participant) allow(participant).to receive(:update_attributes).and_raise(StandardError) params = { authorization: 'reviewer', id: 1 } session = { user: instructor } get :update_authorizations, params, session expect(flash[:error]).to eq 'The update action failed.' end end
6. get_user_info
describe '#get_user_info' do it 'gives the user information from the the team user' do allow(assignment).to receive(:participants).and_return([participant]) allow(participant).to receive(:permission_granted?).and_return(true) allow(participant).to receive(:user).and_return(student) allow(student).to receive(:name).and_return('name') allow(student).to receive(:fullname).and_return('fullname') pc = ParticipantsController.new expect(pc.send(:get_user_info, student, assignment)).to eq(name: 'name', fullname: 'fullname', pub_rights: 'Granted', verified: false) end end
7. get_signup_topics_for_assignment
describe '#get_signup_topics_for_assignment' do it 'gives the signup topics for assignment' do pc = ParticipantsController.new expect(pc.send(:get_signup_topics_for_assignment, topic.assignment.id, topic, signed_up_team.team_id)).to eq(true) end end
Test for Versions Controller
1. action_allowed? - This is the first method that would be called when a user is accessing the version controller. This method ensures that only the users with admin privileges can assess the features.
describe '#action_allowed?' do context 'when user does not have right privilege, it denies action' do it 'for no user' do expect(controller.send(:action_allowed?)).to be false end it 'for student' do allow(controller).to receive(:current_user).and_return(build(:student)) expect(controller.send(:action_allowed?)).to be false end it 'for instructor' do stub_current_user(instructor, instructor.role.name, instructor.role) expect(controller.send(:action_allowed?)).to be false end end context 'when user has right privilege, it allows action' do it 'for admin' do stub_current_user(admin, admin.role.name, admin.role) expect(controller.send(:action_allowed?)).to be true end it 'for super_admin' do stub_current_user(super_admin, super_admin.role.name, super_admin.role) expect(controller.send(:action_allowed?)).to be true end end end
2. index - This method is to display the versions with given id with 25 records each page
describe 'GET /index' do it 'redirect to search' do stub_current_user(admin, admin.role.name, admin.role) get 'index' expect(response).to redirect_to('/versions/search') end end
3. show
describe 'GET /show' do it 'render show' do stub_current_user(admin, admin.role.name, admin.role) allow(Version).to receive(:find).with('1').and_return(version) get 'show', id: 1 expect(response).to render_template('show') end end
4. search - This method is called to search the versions records of given user id
describe 'GET /search' do it 'returns http success' do stub_current_user(admin, admin.role.name, admin.role) params = { id: 3 } get 'search', params expect(response).to be_success end it 'should render search template' do stub_current_user(admin, admin.role.name, admin.role) get 'search' expect(response).to render_template('search') end end
Conclusion
Here is the link for the screenshots of the coverage:
Versions Controller Test Coverage
Participants Controller Test Coverage