CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes

From Expertiza_Wiki
Jump to navigation Jump to search

Expertiza

Expertiza is a web application through which students can submit and peer-review learning objects (articles, code, web sites, etc). The National Science Foundation supports the Expertiza project.

It is used in select courses at NC State and by professors at several other colleges and universities.


Expertiza is a Ruby on Rails based open source project.

Problem Statement

Background: Background: The User model is a key component of the Expertiza application, handling user data and authentication/authorization features. It is linked to other models such as Participant, TeamsUser, and Invitation, allowing for associations and a personalized user experience.


Reimplementation (What needs to be done): To set up the project, follow these instructions.

  • Reimplement the search_users, find_user methods in user.rb
  • Reimplement the role, paginate_list functionality in users_controller.rb
  • Instructor.rb
  • Write thorough RSpec tests for the newly implemented functionalities

Files Modified

We have recently made some improvements to the codebase by reimplementing several methods in the application. These changes were made to enhance code readability, reduce redundancy, and promote best coding practices. In particular, we have streamlined methods related to listing, retrieving, and filtering data, resulting in cleaner and more maintainable code. These modifications aim to make our codebase more efficient while maintaining the same functionality as before.

Changed files:

  • app/controllers/users_controller.rb
  • app/models/user.rb
  • spec/models/ta_spec.rb
  • spec/models/user_spec.rb

New Files:

  • app/helpers/anonymized_helper.rb

Reimplement

The refactoring includes the following changes to improve code quality, readability, and maintainability while preserving the functionality of our software.

  • Recognized the need for refactoring to improve code readability and maintainability.
  • Introduced a new method, get_participants_from_instructed_entities, to handle the retrieval of participants from entities (either Course or Assignment) where the given user is an instructor.
  • Replaced nested loops with more concise and efficient array operations using flat_map, reducing temporary arrays and enhancing code organization.
  • Introduced a new method, filter_participants, to handle participant filtering based on user privileges. This reduced code duplication and improved readability.
  • In the my_tas method, replaced the loop with a flat_map operation, extracting TA IDs directly from the TaMapping records. This simplified the code and improved efficiency.
  • In the list_all, list_mine, and get methods, introduced a new method, filter_by_instructor, to handle filtering records based on the instructor ID and whether private records should be included. This reduced code duplication and improved readability.
  • Throughout the refactoring process, maintained the same functionality while adding comments to explain the purpose of each method and operation.
  • Used string interpolation instead of string concatenation for better readability.
  • Removed redundant scopes in user.rb
  • Refactored validations in user.rb
  • Modified method names to reflect responsibility of the code
  • Followed DRY principle and extracted repeated methods into helpers
  • Added separate module for functionality pertaining to anonymized view.
  • Made changes to refactor check_if_input_is_valid in impersonate_controller.rb using DRY principle to make code concise and readable.
  • In user.rb renamed get_available_users method and it's function calls to get_visible_users_with_lesser_roles to make it more meaningful.
  • Removed salt_first method in user_spec.rb as it does not contribute to application and is only used in test files.
  • Moved functionality associated with anonymized view from user.rb to a new file app/helpers/anonymized_helper.rb.
  • Replaced global variable usage of redis. Used singleton design pattern to enforce single instance of redis variable.
  • Fixed lint errors
users_controller.rb

app/controllers/users_controller.rb

TEXT: TEXT:


ta_spec.rb

Test Case Changes: The test cases have been updated to reflect these method name changes. For example, the test case for get_instructor has been changed to test instructor instead.

Method Call Changes: The way the instructor= method is called has been changed. Instead of ta.set_instructor(assignment), it’s now ta.instructor = assignment.

Expectation Changes: The expectations in the test cases have been updated. For example, in the test case for instructor=, instead of expecting the method to return a value, it now checks that the instructor_id and course_id attributes of the assignment have been set correctly.

Query Changes: In the test case for ‘list_mine’, the SQL query string has been changed from using simple string concatenation to using a parameterized query. This can help prevent SQL injection attacks.


spec/models/ta_spec.rb


Method Name Changes: The method names get_instructor and set_instructor have been changed to instructor and instructor= respectively. This is more in line with Ruby’s convention for getter and setter methods.

user_spec.rb

spec/models/user_spec.rb

Method Name Changes: The method names get_instructor and get_available_users have been changed to instructor and get_visible_users_with_lesser_roles, respectively.

Test Case Changes: The test cases have been updated to reflect these method name changes. For example, the test case for get_instructor has been changed to test instructor instead, and the test case for get_available_users has been changed to test get_visible_users_with_lesser_roles.

Expectation Changes: The expectations in the test cases have been updated. For example, in the test case for instructor, it now checks that the instructor method returns the correct id.

Test Cases for User Controller and child classes

Background: The User Controller is an essential part of our application, managing user-related functionalities. It is responsible for handling user data and managing user roles and permissions. This includes viewing, creating, and managing users and instructors, as well as assigning courses to TAs.

Test Cases:

  • Accessing the 'Manage Users' Tab
   - Objective: Ensure that the 'Manage Users' tab is accessible.
   - Steps: Navigate to the 'Manage Users' tab and verify that the tab opens without any errors.
  • Viewing the User Table as an Instructor
   - Objective: Verify that an instructor can view a table of all users.
   - Steps: Log in as an instructor, navigate to the 'Manage Users' tab,
     and verify that a table listing all users is visible.
  • Checking User Roles
   - Objective: Confirm that user roles are correctly displayed.
   - Steps: In the 'Manage Users' tab, locate the column for 'Roles' and verify that each user has a
role assigned and it's correctly displayed.
  • Creating Users and Instructors
   - Objective: Test the user and instructor creation process.
   - Steps: Navigate to the 'Create User' option, fill out the necessary information
and select the role as 'User' or 'Instructor'. Submit the form and verify that the user/instructor is created successfully.
  • Listing and Assigning Courses to a TA
   - Objective: Ensure that courses can be listed and assigned to a TA.
   - Steps: Log in as an instructor, navigate to the TA's profile,
verify that a list of courses assigned to the TA is visible. Try assigning a new course to the TA
and verify that it's added to their list.
  • Managing User Profile Content
   - Objective: Check the functionality of editing user profile content.
   - Steps: Navigate to a user profile, verify that you can edit the content of the user profile 

and the changes are saved correctly.

  • User Login and Logout
   - Objective: Test the user login and logout process.
   - Steps: On the login page, enter the user's credentials and submit.
Verify that the user is logged in successfully. Click on the 'Logout' button and verify that the user is logged out.

Note: These are basic test cases and might need to be adjusted based on the actual functionalities of your application. It's also a good practice to include negative test cases to ensure the system can handle errors gracefully. b

Next Steps

Team

Mentor
  • Devashish Vachhani
Members
  • Doddaguni, Sachin R
  • Mahesh, Amogh
  • Villar, Sergio Vargas

References and Relevant Links

  1. Expertiza
  2. Final Projects on Expertiza
  3. Github
  4. [ Pull Request]