CSC/ECE 517 Spring 2018/E1823 Write integration tests for users controller.rb

From Expertiza_Wiki
Jump to navigation Jump to search

This wiki page is a description of E1813 final project for Spring 2018, CSC/ECE 517.

Project Statement

Introduction

users_controller.rb is a file under app/controllers that manages different kinds of methods related to users. This project is to write integration tests for users_controller.rb by using rspec, so our goal is to create a file named users_controller_spec.rb under spec/controllers folder and write integration tests to make the path coverage of users_controller.rb more than 90% and achieve the highest possible branch coverage.

All methods used in this file that can render or redirect to one or more user-related views will be thoroughly tested.(16 in total) We do not need to test private or protected methods directly because these methods should be tested when testing other public methods in the same file.

Files Involved

Newly-created file:

 spec/users_controller_spec.rb

Tested file:

 app/controllers/users_controller.rb

Related view files:

 app/views/users/edit.html.erb
 app/views/users/keys.html.erb
 app/views/users/list.html.erb
 app/views/users/list_pending_requested.erb
 app/views/users/new.html.erb
 app/views/users/request_new.html.erb
 app/views/users/show.html.erb

Team Members

1. Sandeep Rajendran (srajend@ncsu.edu)

2. Xiao Ma (xma21@ncsu.edu)

3. Zekun Zhang (zzhang56@ncsu.edu)

4. Zhiyu Chen (zchen45@ncsu.edu)

Functionality of the Controller

Table

Methods

1. index

This method is used to list users in the database. A permissions check is done first to verify that the current user has permission to view the list of users. If the user is a student, he is redirected away from the page. All other user roles have access to the page.

2. auto_complete_for_user_name

The method finds all users who have names similar to the one being typed and displays only those that have a lower role than the current user. The page is rendered again showing all these suggestions.

3. set_anonymized_view

This method is used to anonymize the usernames in the views for demo purposes.

4. list

This method calls the get_user_list method in the user model which returns a list of users according to the role of the user that calls the method. This list is stored in the @users instance variable which is used by the view.

5. list_pending_requested

This method is called to show all the requests which need to be reviewed. It first obtains all the request users and the roles. Then It will show those request_users who is under review and their details with a selection window--"status" which is used to determine if this request will be approved or rejected.

6. show_selection

This method is used to show the users. It first detects if this user is valid or not. if this user is nil, it will redirect to the "list" page. If this user is valid, it will call the get_role method to get the role information of this user. and then use the role information to determine if this user is available for editing. For users available for editing, this method will render show "page", otherwise, this method will redirect to "list" page. In summary, this method will direct to "show" page, when the user is able to be edited, otherwise, it will go to "list" page.

7. show

This method is used to show the details of a user. It first detect is the user is valid. There two condition for an invalid user: 1) the id of user is nil; 2) current user role is not student and the session id doesn't equal to the user id. Otherwise this user is valid. For a valid user, it will show his/her detailed information including name, email, password, prefs, institution and self_introduction.

8. new

This method is called when method create is used to create a new user.

(This image will be helpful for the next 4 methods)

9. request_new

This method is called when a TA or instructor requests an Expertiza account. To register to use the Expertiza, one needs to be either a course TA or Instructor and then sends a request to Expertiza administrator to get approved. This function only serves TAs and instructors, and the students account will be created by instructors or TAs.

10. create

This method is called when the user wants to create accounts.

11. create_requested_user_record

This method is called after the guest sending a request for the account to Expertiza administrator. After the guest sending a request for the account using request_new method, this method will store the request in the database and list all the requests on the pending_request page waiting for the administrator to operate.

12. create_approved_user

This method is called when the Expertiza administrator deals with the guests' pending requests. By this method, the administrator can approve or decline the pending requests.

13. edit

"resources :users" in routing rule maps /users/1/edit (assuming user's id is 1) to edit action in UsersController.

Thus, this edit method is called when accessing user's edit page and will pull the relevant user out of the database via user's id that stored in params[:id].

14. update

"resources :users" in routing rule makes update action get called when the form in user's edit page is submitted.

This method will first find the relevant user by user's id that stored in params[:id] and then updates the user's information in the database based on the submitted params hash. If the update is successful it shows a success flash and redirects to user's show page otherwise it renders user's edit page.

15. destroy

"resources :users" in routing rule makes destroy action response to the delete method and thus accomplishing the user deletion.

When this method is called, it will first find the relevant user via user's id that stored in params[:id] and then delete related AssignmentParticipant, TeamsUser and AssignmentQuestionnaire models if any (These models are found through their foregin key: user_id). Finally, the user is deleted from the database and it will show a success flash. If any of the above steps fails, it will throw an error flash.

16. keys

"get :keys" under the collection block of "resources :users" will enable Rails to recognize paths such as /users/keys with GET, and route to the key action of UsersController.

When this method is called, if no current user or current user is a student but is not the user stored in session, it will redirect to home page of the user stored in session. Otherwise generate_keys (a method of user model) will be called on the current user and the return value will be assigned to an instance variable @private_key defined in UsersController.

Test Plan

This project aims to writing an integration test for users_controller.rb. There is no this Rspec file exist for this test, so a new test file shall be created and built from scratch. And before writing this test file, some prerequisites must be done. 1. Download and set the Expertiza environment 2. Determine which methods should be tested 3. Define all the conditions needed to be tested for each method

The methods have already been introduced on above, here are the test plan for each method:

1. index

1.1 "If the user is student, we check if proper redirection is taking place"
1.2 "Else, the appropriate list of users should be rendered according to the role of the current user"

2. auto_complete_for_user_name

2.1 "The page should render suggestions only of those user names that are similar to the one being typed and not all user names"

3. set_anonymized_view

3.1 "The method should add the session ip to the list of anonymized ip's and redirect to the previous page "

4. list

4.1 "The @users instance variable should contain all the users that are permitted to be viewed by the current user. This is tested by the same test for index"

5. list_pending_requested

5.1 "check if it will render users/list_pending_requested page"

6. show_selection

6.1 "check if the user is nil, will the page redirect to users/list page"
6.2 "check if the user is not available for editing, will it redirect to users/list page"
6.3 "check if the user is available for editing, will it render users/show page"

7. show

7.1 "check if the user id is nil or if the role is student and session id equals to id, will it redirect to home"
7.2 "if the user is valid, check the return value"

8. new

8.1 "check if this will render users/new page"

9. request_new

9.1 "Here isn't any conditional sentence. So only need to check whether the return value is expected."

10. create

10.1 "Is the name new? If yes, good. Otherwise, register the user by email address."
10.2 "Is the user's information valid? If yes, good. Otherwise, fail"

11. create_requested_user_record

11.1 "does the user belong to any institution? If yes, good. Otherwise, set a new institution."
11.2 "is the user already existed and valid? If both yes, good. If the user already exist, send notification. If the information is not valid, fail."

12. create_approved_user

12.1 "does the request have one status? if yes and the parameters of the request are completed, good. Otherwise, submit a status, approve or reject."
12.2 "is the status Approved or Rejected?"
12.2.1 "if the status is Approved, is the information valid? if yes, good. Otherwise fail"
12.2.2 "if the status is Rejected, is the processing correctly? if yes, reject successfully. Otherwise, error processing request."

13. edit

13.1 "Here isn't any conditional sentence, so we just need to check if it renders users#edit page."

14. update

14.1 "When user is updated successfully, it shows correct flash and redirects to users/show page.
14.2 "When user is not updated successfully, it redirects to users/edit page."

15. destroy

15.1 "When user is not deleted successfully, it shows an error and redirects to users/list page." (This also covers the condition that user is deleted successfully.)

16. keys

16.1 "When params[:id] is not nil, @private_key gets correct value."
16.2 "When params[:id] is nil, it redirects to /tree_display/drill page."

Test Result

Coverage percentage: