<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Svargas3</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Svargas3"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Svargas3"/>
	<updated>2026-05-17T05:41:56Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152877</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152877"/>
		<updated>2023-12-08T20:06:26Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* Test RSpec */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''list Action:'''&lt;br /&gt;
## The 'list' action will be reimplemented to handle listing of users.&lt;br /&gt;
## It will retrieve a list of users based on certain criteria, such as name, full name or email.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'list' and 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
==== ToDo Status: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done!&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|7&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `role` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;list and paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that list and paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that list and paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
The search_users method accepts three parameters: user_id, word, and search_by. If a valid user_id is provided, the method returns an array containing the user with that specific user_id.&lt;br /&gt;
In cases where user_id is empty or invalid, the method performs a search based on the provided word and search_by parameters.&lt;br /&gt;
To prevent SQL injection, the method validates search_by against a predefined set of acceptable fields, including 'name', 'full_name', 'email', and 'role'. The search is then conducted using a LIKE query on the specified field, ordering the results by the user's name.&lt;br /&gt;
&lt;br /&gt;
This flexible method accommodates various search scenarios, allowing users to query the system based on different criteria, enhancing the overall user experience.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
  def self.search_users(user_id, word, search_by)&lt;br /&gt;
    if user_id.present? &amp;amp;&amp;amp; (user = User.find_by(id: user_id))&lt;br /&gt;
      # If a valid user_id is provided, return the user with that specific user_id&lt;br /&gt;
      return [user]&lt;br /&gt;
    else&lt;br /&gt;
      # If user_id is empty or invalid, perform the search based on word and search_by parameters&lt;br /&gt;
&lt;br /&gt;
      # Validate search_by to avoid SQL injection&lt;br /&gt;
      valid_search_fields = %w[name full_name email role]&lt;br /&gt;
      search_by = valid_search_fields.include?(search_by) ? search_by : nil&lt;br /&gt;
&lt;br /&gt;
      # Perform the LIKE query on the specified field (name, full_name, email, role) and order by name&lt;br /&gt;
      if search_by.present?&lt;br /&gt;
        # Use search_by directly in the where clause&lt;br /&gt;
        users = User.joins(:role).where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name) if search_by == 'role'&lt;br /&gt;
        users ||= User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name)&lt;br /&gt;
        return users&lt;br /&gt;
      else&lt;br /&gt;
        # If search_by is not recognized, return an empty result&lt;br /&gt;
        return []&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The login method, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
==== Attributes ====&lt;br /&gt;
&lt;br /&gt;
* '''name:''' The name of the user.&lt;br /&gt;
* '''full_name:''' The full name of the user, providing a comprehensive representation.&lt;br /&gt;
* '''email:''' The unique email address associated with each user, ensuring distinct identification.&lt;br /&gt;
* '''role:''' A relationship with the [[Role Model|Role model]], defining the user's role and access privileges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    user_id = params[:user_id]&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(user_id, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.present?&lt;br /&gt;
      # If the result is not empty, render the users as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is empty, render a message as JSON with a not found status&lt;br /&gt;
      render json: { error: 'User not found or no matching results' }, status: :not_found&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This method facilitates dynamic user searches, allowing users to query the system based on different parameters. Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
=== role Method ===&lt;br /&gt;
The role method provides the role of a specific user. It is used as a utility method in the controller.&lt;br /&gt;
  def role&lt;br /&gt;
    if @user&amp;amp;.role_id&lt;br /&gt;
      @role = Role.find(@user.role_id)&lt;br /&gt;
    elsif @user&lt;br /&gt;
      @role = Role.new(id: nil, name: '(none)')&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
=== find_by_login Method ===&lt;br /&gt;
The method provides the functionality to search user by email or name&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list Method ===&lt;br /&gt;
Method for displaying the list of users&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list and paginate_list Method ===&lt;br /&gt;
Method for filtering the users list with proper search and pagination.&lt;br /&gt;
  def list&lt;br /&gt;
    # code here&lt;br /&gt;
    letter = params[:letter]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
    # If search parameters present&lt;br /&gt;
    if letter.present? &amp;amp;&amp;amp; search_by.present?&lt;br /&gt;
      case search_by.to_i&lt;br /&gt;
      when 1 # Search by username&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('name LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 2 # Search by fullname&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('fullname LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 3 # Search by email&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('email LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      else&lt;br /&gt;
        @paginated_users = paginate_list&lt;br /&gt;
      end&lt;br /&gt;
    else # Display all users if no search parameters present&lt;br /&gt;
      @paginated_users = paginate_list&lt;br /&gt;
      if @paginated_users&lt;br /&gt;
        puts(&amp;quot;Not empty&amp;quot; + @paginated_users.to_s) else puts(&amp;quot;Empty&amp;quot;)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    render json: @paginated_users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def paginate_list&lt;br /&gt;
    paginate_options = { '1' =&amp;gt; 25, '2' =&amp;gt; 50, '3' =&amp;gt; 100 }&lt;br /&gt;
    # If the above hash does not have a value for the key,&lt;br /&gt;
    # it means that we need to show all the users on the page&lt;br /&gt;
    #&lt;br /&gt;
    # Just a point to remember, when we use pagination, the&lt;br /&gt;
    # 'users' variable should be an object, not an array&lt;br /&gt;
    # The type of condition for the search depends on what the user has selected from the search_by dropdown&lt;br /&gt;
    @search_by = params[:search_by]&lt;br /&gt;
    @per_page = params[:per_page] || 3&lt;br /&gt;
    # search for corresponding users&lt;br /&gt;
    # users = User.search_users(role, user_id, letter, @search_by)&lt;br /&gt;
    # paginate&lt;br /&gt;
    users = if paginate_options[@per_page.to_s].nil? # displaying all - no pagination&lt;br /&gt;
              User.all&lt;br /&gt;
            else # some pagination is active - use the per_page&lt;br /&gt;
              User.paginate(page: params[:page], per_page: paginate_options[@per_page.to_s])&lt;br /&gt;
            end&lt;br /&gt;
    # users = User.all&lt;br /&gt;
    users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Swagger API ===&lt;br /&gt;
&lt;br /&gt;
The image shows that we used Swagger to design and document our API application. Then, we exported the API data in JSON format to use Postman for development and testing. Postman is a tool that lets us send requests, check responses, write tests, and automate workflows for our API.&lt;br /&gt;
&lt;br /&gt;
[[File:Swagger2.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Postman Collection ===&lt;br /&gt;
&lt;br /&gt;
Postman is an all-encompassing tool that enables us to send requests, validate responses, construct tests, and automate workflows for our Application Programming Interface (API). We have imported the Swagger API and created a Postman collection; this helps us to share the progress in real-time, facilitating online modifications because it is a SaaS application. In addition, we have put together a team workspace, as shown in the attached image. This arrangement fosters collaborative work and ensures the efficiency and effectiveness of our API operations.&lt;br /&gt;
&lt;br /&gt;
[[File:postman2.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
&lt;br /&gt;
The endpoint search by Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id (optional)''': The ID of the requesting user. If provided, it is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Swagger Endpoint ====&lt;br /&gt;
&lt;br /&gt;
[[File:Swagger search users.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/list&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''search_by (optional)''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
* '''letter (optional)''': The field to indicate the starting letter of the user's 'name', 'email', or 'full_name'.&lt;br /&gt;
* '''per_page  (optional)''': The field to indicate how many users to be displayed per page.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET api/v1/users/list?letter=n&amp;amp;search_by=1&amp;amp;per_page=3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;newuser&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;new_user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Swagger Endpoint ====&lt;br /&gt;
&lt;br /&gt;
[[File:Swagger-list.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:List_api.png|800px]]&lt;br /&gt;
&lt;br /&gt;
== Swagger YAML fragment ==&lt;br /&gt;
/api/v1/users/search/{user_id}/{word}/{search_by}:&lt;br /&gt;
  parameters:&lt;br /&gt;
    - name: user_id&lt;br /&gt;
      in: path&lt;br /&gt;
      required: true&lt;br /&gt;
      schema:&lt;br /&gt;
        type: integer&lt;br /&gt;
      description: The ID of the user for searching.&lt;br /&gt;
    - name: word&lt;br /&gt;
      in: path&lt;br /&gt;
      required: true&lt;br /&gt;
      schema:&lt;br /&gt;
        type: string&lt;br /&gt;
      description: The search keyword.&lt;br /&gt;
    - name: search_by&lt;br /&gt;
      in: path&lt;br /&gt;
      required: true&lt;br /&gt;
      schema:&lt;br /&gt;
        type: string&lt;br /&gt;
        enum: ['full_name', 'name', 'email', 'role']&lt;br /&gt;
      description: The attribute to search by (full_name, name, email, role).&lt;br /&gt;
  get:&lt;br /&gt;
    summary: Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
    tags:&lt;br /&gt;
      - User Requests&lt;br /&gt;
    responses:&lt;br /&gt;
      '200':&lt;br /&gt;
        description: This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
/api/v1/users/list:&lt;br /&gt;
  get:&lt;br /&gt;
    summary: Retrieve a list of users based on search criteria.&lt;br /&gt;
    tags:&lt;br /&gt;
      - User Requests&lt;br /&gt;
    parameters:&lt;br /&gt;
      - name: search_by&lt;br /&gt;
        in: query&lt;br /&gt;
        required: false&lt;br /&gt;
        schema:&lt;br /&gt;
          type: string&lt;br /&gt;
        description: The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
      - name: letter&lt;br /&gt;
        in: query&lt;br /&gt;
        required: false&lt;br /&gt;
        schema:&lt;br /&gt;
          type: string&lt;br /&gt;
        description: The field to indicate the starting letter of the user's 'name', 'email', or 'full_name'.&lt;br /&gt;
      - name: per_page&lt;br /&gt;
        in: query&lt;br /&gt;
        required: false&lt;br /&gt;
        schema:&lt;br /&gt;
          type: integer&lt;br /&gt;
        description: The field to indicate how many users to be displayed per page.&lt;br /&gt;
    responses:&lt;br /&gt;
      '200':&lt;br /&gt;
        description: OK&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
require 'rails_helper'&lt;br /&gt;
&lt;br /&gt;
RSpec.describe User, type: :model do&lt;br /&gt;
&lt;br /&gt;
  describe '.find_by_login' do&lt;br /&gt;
    context 'when login is an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      it 'returns the user with the matching email' do&lt;br /&gt;
        result = User.find_by_login('test@test.com')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      it 'returns nil when user with email doesnt exist' do&lt;br /&gt;
        result = User.find_by_login('test@unknown.com')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when login is not an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      let!(:user2) { build(:user, name: 'testname', email: 'test@test2.com') }&lt;br /&gt;
      it 'returns the user with the matching name' do&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return first user with the matching name' do&lt;br /&gt;
        user2.save&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return nil when no user with matching name' do&lt;br /&gt;
        result = User.find_by_login('unknown')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  describe '.search_users' do&lt;br /&gt;
    # Creating dummy objects for the test with the help of let statement&lt;br /&gt;
    let(:role) { Role.create(name: 'Instructor', parent_id: nil, id: 2, default_page_id: nil) }&lt;br /&gt;
    let(:instructor) do&lt;br /&gt;
      Instructor.create(id: 1234, name: 'testinstructor', email: 'test@test.com', full_name: 'Test Instructor',&lt;br /&gt;
                        password: '123456', role_id: 2)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by name' do&lt;br /&gt;
      it 'returns users with matching names' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'testins', 'name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by fullname' do&lt;br /&gt;
      it 'returns users with matching fullnames' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'Test', 'full_name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'UnknownName', 'full_name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by email' do&lt;br /&gt;
      it 'returns users with matching emails' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'test@test.com', 'email')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown@test.com', 'email')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by default' do&lt;br /&gt;
      it 'returns users with names starting with the specified id' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(instructor.id, nil, nil)&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(9999, nil, nil) # Use an invalid user_id&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    context 'when searching by role' do&lt;br /&gt;
      it 'returns users with matching roles' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'admin', 'role')&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.role_id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'role')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:Expertiza_tests.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;br /&gt;
* [https://github.com/expertiza/reimplementation-back-end/pull/60/files#diff-a05d611b9fb47bcea66e3aaf6f25dc71d8da9976c5ccc991ea5171e938605644 GITHUB PR]&lt;br /&gt;
* [https://www.youtube.com/watch?v=_aCqMjRwW_U VIDEO DEMO 5min]&lt;br /&gt;
* [https://drive.google.com/file/d/1Cc_ztHnXRlcHJvDXswpHUNe395cGNAcE/view?usp=sharing VIDEO Setup explanation 16min]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152876</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152876"/>
		<updated>2023-12-08T20:03:46Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''list Action:'''&lt;br /&gt;
## The 'list' action will be reimplemented to handle listing of users.&lt;br /&gt;
## It will retrieve a list of users based on certain criteria, such as name, full name or email.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'list' and 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
==== ToDo Status: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done!&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|7&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `role` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;list and paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that list and paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that list and paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
The search_users method accepts three parameters: user_id, word, and search_by. If a valid user_id is provided, the method returns an array containing the user with that specific user_id.&lt;br /&gt;
In cases where user_id is empty or invalid, the method performs a search based on the provided word and search_by parameters.&lt;br /&gt;
To prevent SQL injection, the method validates search_by against a predefined set of acceptable fields, including 'name', 'full_name', 'email', and 'role'. The search is then conducted using a LIKE query on the specified field, ordering the results by the user's name.&lt;br /&gt;
&lt;br /&gt;
This flexible method accommodates various search scenarios, allowing users to query the system based on different criteria, enhancing the overall user experience.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
  def self.search_users(user_id, word, search_by)&lt;br /&gt;
    if user_id.present? &amp;amp;&amp;amp; (user = User.find_by(id: user_id))&lt;br /&gt;
      # If a valid user_id is provided, return the user with that specific user_id&lt;br /&gt;
      return [user]&lt;br /&gt;
    else&lt;br /&gt;
      # If user_id is empty or invalid, perform the search based on word and search_by parameters&lt;br /&gt;
&lt;br /&gt;
      # Validate search_by to avoid SQL injection&lt;br /&gt;
      valid_search_fields = %w[name full_name email role]&lt;br /&gt;
      search_by = valid_search_fields.include?(search_by) ? search_by : nil&lt;br /&gt;
&lt;br /&gt;
      # Perform the LIKE query on the specified field (name, full_name, email, role) and order by name&lt;br /&gt;
      if search_by.present?&lt;br /&gt;
        # Use search_by directly in the where clause&lt;br /&gt;
        users = User.joins(:role).where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name) if search_by == 'role'&lt;br /&gt;
        users ||= User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name)&lt;br /&gt;
        return users&lt;br /&gt;
      else&lt;br /&gt;
        # If search_by is not recognized, return an empty result&lt;br /&gt;
        return []&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The login method, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
==== Attributes ====&lt;br /&gt;
&lt;br /&gt;
* '''name:''' The name of the user.&lt;br /&gt;
* '''full_name:''' The full name of the user, providing a comprehensive representation.&lt;br /&gt;
* '''email:''' The unique email address associated with each user, ensuring distinct identification.&lt;br /&gt;
* '''role:''' A relationship with the [[Role Model|Role model]], defining the user's role and access privileges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    user_id = params[:user_id]&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(user_id, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.present?&lt;br /&gt;
      # If the result is not empty, render the users as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is empty, render a message as JSON with a not found status&lt;br /&gt;
      render json: { error: 'User not found or no matching results' }, status: :not_found&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This method facilitates dynamic user searches, allowing users to query the system based on different parameters. Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
=== role Method ===&lt;br /&gt;
The role method provides the role of a specific user. It is used as a utility method in the controller.&lt;br /&gt;
  def role&lt;br /&gt;
    if @user&amp;amp;.role_id&lt;br /&gt;
      @role = Role.find(@user.role_id)&lt;br /&gt;
    elsif @user&lt;br /&gt;
      @role = Role.new(id: nil, name: '(none)')&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
=== find_by_login Method ===&lt;br /&gt;
The method provides the functionality to search user by email or name&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list Method ===&lt;br /&gt;
Method for displaying the list of users&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list and paginate_list Method ===&lt;br /&gt;
Method for filtering the users list with proper search and pagination.&lt;br /&gt;
  def list&lt;br /&gt;
    # code here&lt;br /&gt;
    letter = params[:letter]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
    # If search parameters present&lt;br /&gt;
    if letter.present? &amp;amp;&amp;amp; search_by.present?&lt;br /&gt;
      case search_by.to_i&lt;br /&gt;
      when 1 # Search by username&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('name LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 2 # Search by fullname&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('fullname LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 3 # Search by email&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('email LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      else&lt;br /&gt;
        @paginated_users = paginate_list&lt;br /&gt;
      end&lt;br /&gt;
    else # Display all users if no search parameters present&lt;br /&gt;
      @paginated_users = paginate_list&lt;br /&gt;
      if @paginated_users&lt;br /&gt;
        puts(&amp;quot;Not empty&amp;quot; + @paginated_users.to_s) else puts(&amp;quot;Empty&amp;quot;)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    render json: @paginated_users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def paginate_list&lt;br /&gt;
    paginate_options = { '1' =&amp;gt; 25, '2' =&amp;gt; 50, '3' =&amp;gt; 100 }&lt;br /&gt;
    # If the above hash does not have a value for the key,&lt;br /&gt;
    # it means that we need to show all the users on the page&lt;br /&gt;
    #&lt;br /&gt;
    # Just a point to remember, when we use pagination, the&lt;br /&gt;
    # 'users' variable should be an object, not an array&lt;br /&gt;
    # The type of condition for the search depends on what the user has selected from the search_by dropdown&lt;br /&gt;
    @search_by = params[:search_by]&lt;br /&gt;
    @per_page = params[:per_page] || 3&lt;br /&gt;
    # search for corresponding users&lt;br /&gt;
    # users = User.search_users(role, user_id, letter, @search_by)&lt;br /&gt;
    # paginate&lt;br /&gt;
    users = if paginate_options[@per_page.to_s].nil? # displaying all - no pagination&lt;br /&gt;
              User.all&lt;br /&gt;
            else # some pagination is active - use the per_page&lt;br /&gt;
              User.paginate(page: params[:page], per_page: paginate_options[@per_page.to_s])&lt;br /&gt;
            end&lt;br /&gt;
    # users = User.all&lt;br /&gt;
    users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Swagger API ===&lt;br /&gt;
&lt;br /&gt;
The image shows that we used Swagger to design and document our API application. Then, we exported the API data in JSON format to use Postman for development and testing. Postman is a tool that lets us send requests, check responses, write tests, and automate workflows for our API.&lt;br /&gt;
&lt;br /&gt;
[[File:Swagger2.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Postman Collection ===&lt;br /&gt;
&lt;br /&gt;
Postman is an all-encompassing tool that enables us to send requests, validate responses, construct tests, and automate workflows for our Application Programming Interface (API). We have imported the Swagger API and created a Postman collection; this helps us to share the progress in real-time, facilitating online modifications because it is a SaaS application. In addition, we have put together a team workspace, as shown in the attached image. This arrangement fosters collaborative work and ensures the efficiency and effectiveness of our API operations.&lt;br /&gt;
&lt;br /&gt;
[[File:postman2.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
&lt;br /&gt;
The endpoint search by Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id (optional)''': The ID of the requesting user. If provided, it is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Swagger Endpoint ====&lt;br /&gt;
&lt;br /&gt;
[[File:Swagger search users.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/list&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''search_by (optional)''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
* '''letter (optional)''': The field to indicate the starting letter of the user's 'name', 'email', or 'full_name'.&lt;br /&gt;
* '''per_page  (optional)''': The field to indicate how many users to be displayed per page.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET api/v1/users/list?letter=n&amp;amp;search_by=1&amp;amp;per_page=3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;newuser&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;new_user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Swagger Endpoint ====&lt;br /&gt;
&lt;br /&gt;
[[File:Swagger-list.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:List_api.png|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
require 'rails_helper'&lt;br /&gt;
&lt;br /&gt;
RSpec.describe User, type: :model do&lt;br /&gt;
&lt;br /&gt;
  describe '.find_by_login' do&lt;br /&gt;
    context 'when login is an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      it 'returns the user with the matching email' do&lt;br /&gt;
        result = User.find_by_login('test@test.com')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      it 'returns nil when user with email doesnt exist' do&lt;br /&gt;
        result = User.find_by_login('test@unknown.com')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when login is not an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      let!(:user2) { build(:user, name: 'testname', email: 'test@test2.com') }&lt;br /&gt;
      it 'returns the user with the matching name' do&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return first user with the matching name' do&lt;br /&gt;
        user2.save&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return nil when no user with matching name' do&lt;br /&gt;
        result = User.find_by_login('unknown')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  describe '.search_users' do&lt;br /&gt;
    # Creating dummy objects for the test with the help of let statement&lt;br /&gt;
    let(:role) { Role.create(name: 'Instructor', parent_id: nil, id: 2, default_page_id: nil) }&lt;br /&gt;
    let(:instructor) do&lt;br /&gt;
      Instructor.create(id: 1234, name: 'testinstructor', email: 'test@test.com', full_name: 'Test Instructor',&lt;br /&gt;
                        password: '123456', role_id: 2)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by name' do&lt;br /&gt;
      it 'returns users with matching names' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'testins', 'name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by fullname' do&lt;br /&gt;
      it 'returns users with matching fullnames' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'Test', 'full_name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'UnknownName', 'full_name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by email' do&lt;br /&gt;
      it 'returns users with matching emails' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'test@test.com', 'email')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown@test.com', 'email')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by default' do&lt;br /&gt;
      it 'returns users with names starting with the specified id' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(instructor.id, nil, nil)&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(9999, nil, nil) # Use an invalid user_id&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    context 'when searching by role' do&lt;br /&gt;
      it 'returns users with matching roles' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'admin', 'role')&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.role_id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'role')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:Expertiza_tests.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;br /&gt;
* [https://github.com/expertiza/reimplementation-back-end/pull/60/files#diff-a05d611b9fb47bcea66e3aaf6f25dc71d8da9976c5ccc991ea5171e938605644 GITHUB PR]&lt;br /&gt;
* [https://www.youtube.com/watch?v=_aCqMjRwW_U VIDEO DEMO 5min]&lt;br /&gt;
* [https://drive.google.com/file/d/1Cc_ztHnXRlcHJvDXswpHUNe395cGNAcE/view?usp=sharing VIDEO Setup explanation 16min]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152875</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152875"/>
		<updated>2023-12-08T20:02:00Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* Swagger Endpoint */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''list Action:'''&lt;br /&gt;
## The 'list' action will be reimplemented to handle listing of users.&lt;br /&gt;
## It will retrieve a list of users based on certain criteria, such as name, full name or email.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'list' and 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
==== ToDo Status: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done!&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|7&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `role` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;list and paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that list and paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that list and paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
The search_users method accepts three parameters: user_id, word, and search_by. If a valid user_id is provided, the method returns an array containing the user with that specific user_id.&lt;br /&gt;
In cases where user_id is empty or invalid, the method performs a search based on the provided word and search_by parameters.&lt;br /&gt;
To prevent SQL injection, the method validates search_by against a predefined set of acceptable fields, including 'name', 'full_name', 'email', and 'role'. The search is then conducted using a LIKE query on the specified field, ordering the results by the user's name.&lt;br /&gt;
&lt;br /&gt;
This flexible method accommodates various search scenarios, allowing users to query the system based on different criteria, enhancing the overall user experience.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
  def self.search_users(user_id, word, search_by)&lt;br /&gt;
    if user_id.present? &amp;amp;&amp;amp; (user = User.find_by(id: user_id))&lt;br /&gt;
      # If a valid user_id is provided, return the user with that specific user_id&lt;br /&gt;
      return [user]&lt;br /&gt;
    else&lt;br /&gt;
      # If user_id is empty or invalid, perform the search based on word and search_by parameters&lt;br /&gt;
&lt;br /&gt;
      # Validate search_by to avoid SQL injection&lt;br /&gt;
      valid_search_fields = %w[name full_name email role]&lt;br /&gt;
      search_by = valid_search_fields.include?(search_by) ? search_by : nil&lt;br /&gt;
&lt;br /&gt;
      # Perform the LIKE query on the specified field (name, full_name, email, role) and order by name&lt;br /&gt;
      if search_by.present?&lt;br /&gt;
        # Use search_by directly in the where clause&lt;br /&gt;
        users = User.joins(:role).where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name) if search_by == 'role'&lt;br /&gt;
        users ||= User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name)&lt;br /&gt;
        return users&lt;br /&gt;
      else&lt;br /&gt;
        # If search_by is not recognized, return an empty result&lt;br /&gt;
        return []&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The login method, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
==== Attributes ====&lt;br /&gt;
&lt;br /&gt;
* '''name:''' The name of the user.&lt;br /&gt;
* '''full_name:''' The full name of the user, providing a comprehensive representation.&lt;br /&gt;
* '''email:''' The unique email address associated with each user, ensuring distinct identification.&lt;br /&gt;
* '''role:''' A relationship with the [[Role Model|Role model]], defining the user's role and access privileges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    user_id = params[:user_id]&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(user_id, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.present?&lt;br /&gt;
      # If the result is not empty, render the users as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is empty, render a message as JSON with a not found status&lt;br /&gt;
      render json: { error: 'User not found or no matching results' }, status: :not_found&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This method facilitates dynamic user searches, allowing users to query the system based on different parameters. Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
=== role Method ===&lt;br /&gt;
The role method provides the role of a specific user. It is used as a utility method in the controller.&lt;br /&gt;
  def role&lt;br /&gt;
    if @user&amp;amp;.role_id&lt;br /&gt;
      @role = Role.find(@user.role_id)&lt;br /&gt;
    elsif @user&lt;br /&gt;
      @role = Role.new(id: nil, name: '(none)')&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
=== find_by_login Method ===&lt;br /&gt;
The method provides the functionality to search user by email or name&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list Method ===&lt;br /&gt;
Method for displaying the list of users&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list and paginate_list Method ===&lt;br /&gt;
Method for filtering the users list with proper search and pagination.&lt;br /&gt;
  def list&lt;br /&gt;
    # code here&lt;br /&gt;
    letter = params[:letter]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
    # If search parameters present&lt;br /&gt;
    if letter.present? &amp;amp;&amp;amp; search_by.present?&lt;br /&gt;
      case search_by.to_i&lt;br /&gt;
      when 1 # Search by username&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('name LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 2 # Search by fullname&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('fullname LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 3 # Search by email&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('email LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      else&lt;br /&gt;
        @paginated_users = paginate_list&lt;br /&gt;
      end&lt;br /&gt;
    else # Display all users if no search parameters present&lt;br /&gt;
      @paginated_users = paginate_list&lt;br /&gt;
      if @paginated_users&lt;br /&gt;
        puts(&amp;quot;Not empty&amp;quot; + @paginated_users.to_s) else puts(&amp;quot;Empty&amp;quot;)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    render json: @paginated_users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def paginate_list&lt;br /&gt;
    paginate_options = { '1' =&amp;gt; 25, '2' =&amp;gt; 50, '3' =&amp;gt; 100 }&lt;br /&gt;
    # If the above hash does not have a value for the key,&lt;br /&gt;
    # it means that we need to show all the users on the page&lt;br /&gt;
    #&lt;br /&gt;
    # Just a point to remember, when we use pagination, the&lt;br /&gt;
    # 'users' variable should be an object, not an array&lt;br /&gt;
    # The type of condition for the search depends on what the user has selected from the search_by dropdown&lt;br /&gt;
    @search_by = params[:search_by]&lt;br /&gt;
    @per_page = params[:per_page] || 3&lt;br /&gt;
    # search for corresponding users&lt;br /&gt;
    # users = User.search_users(role, user_id, letter, @search_by)&lt;br /&gt;
    # paginate&lt;br /&gt;
    users = if paginate_options[@per_page.to_s].nil? # displaying all - no pagination&lt;br /&gt;
              User.all&lt;br /&gt;
            else # some pagination is active - use the per_page&lt;br /&gt;
              User.paginate(page: params[:page], per_page: paginate_options[@per_page.to_s])&lt;br /&gt;
            end&lt;br /&gt;
    # users = User.all&lt;br /&gt;
    users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Swagger API ===&lt;br /&gt;
&lt;br /&gt;
The image shows that we used Swagger to design and document our API application. Then, we exported the API data in JSON format to use Postman for development and testing. Postman is a tool that lets us send requests, check responses, write tests, and automate workflows for our API.&lt;br /&gt;
&lt;br /&gt;
[[File:Swagger2.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Postman Collection ===&lt;br /&gt;
&lt;br /&gt;
Postman is an all-encompassing tool that enables us to send requests, validate responses, construct tests, and automate workflows for our Application Programming Interface (API). We have imported the Swagger API and created a Postman collection; this helps us to share the progress in real-time, facilitating online modifications because it is a SaaS application. In addition, we have put together a team workspace, as shown in the attached image. This arrangement fosters collaborative work and ensures the efficiency and effectiveness of our API operations.&lt;br /&gt;
&lt;br /&gt;
[[File:postman2.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
&lt;br /&gt;
The endpoint search by Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id (optional)''': The ID of the requesting user. If provided, it is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Swagger Endpoint ====&lt;br /&gt;
&lt;br /&gt;
[[File:Swagger search users.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/list&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''search_by (optional)''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
* '''letter (optional)''': The field to indicate the starting letter of the user's 'name', 'email', or 'full_name'.&lt;br /&gt;
* '''per_page  (optional)''': The field to indicate how many users to be displayed per page.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET api/v1/users/list?letter=n&amp;amp;search_by=1&amp;amp;per_page=3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;newuser&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;new_user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Swagger Endpoint ====&lt;br /&gt;
&lt;br /&gt;
[[File:Swagger-list.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:List_api.png|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
require 'rails_helper'&lt;br /&gt;
&lt;br /&gt;
RSpec.describe User, type: :model do&lt;br /&gt;
&lt;br /&gt;
  describe '.find_by_login' do&lt;br /&gt;
    context 'when login is an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      it 'returns the user with the matching email' do&lt;br /&gt;
        result = User.find_by_login('test@test.com')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      it 'returns nil when user with email doesnt exist' do&lt;br /&gt;
        result = User.find_by_login('test@unknown.com')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when login is not an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      let!(:user2) { build(:user, name: 'testname', email: 'test@test2.com') }&lt;br /&gt;
      it 'returns the user with the matching name' do&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return first user with the matching name' do&lt;br /&gt;
        user2.save&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return nil when no user with matching name' do&lt;br /&gt;
        result = User.find_by_login('unknown')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  describe '.search_users' do&lt;br /&gt;
    # Creating dummy objects for the test with the help of let statement&lt;br /&gt;
    let(:role) { Role.create(name: 'Instructor', parent_id: nil, id: 2, default_page_id: nil) }&lt;br /&gt;
    let(:instructor) do&lt;br /&gt;
      Instructor.create(id: 1234, name: 'testinstructor', email: 'test@test.com', full_name: 'Test Instructor',&lt;br /&gt;
                        password: '123456', role_id: 2)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by name' do&lt;br /&gt;
      it 'returns users with matching names' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'testins', 'name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by fullname' do&lt;br /&gt;
      it 'returns users with matching fullnames' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'Test', 'full_name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'UnknownName', 'full_name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by email' do&lt;br /&gt;
      it 'returns users with matching emails' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'test@test.com', 'email')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown@test.com', 'email')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by default' do&lt;br /&gt;
      it 'returns users with names starting with the specified id' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(instructor.id, nil, nil)&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(9999, nil, nil) # Use an invalid user_id&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    context 'when searching by role' do&lt;br /&gt;
      it 'returns users with matching roles' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'admin', 'role')&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.role_id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'role')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:Expertiza_tests.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;br /&gt;
* [https://github.com/expertiza/reimplementation-back-end/pull/60/files#diff-a05d611b9fb47bcea66e3aaf6f25dc71d8da9976c5ccc991ea5171e938605644 GITHUB PR]&lt;br /&gt;
* [https://www.youtube.com/watch?v=_aCqMjRwW_U VIDEO DEMO 5min]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Swagger-list.png&amp;diff=152874</id>
		<title>File:Swagger-list.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Swagger-list.png&amp;diff=152874"/>
		<updated>2023-12-08T20:01:25Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152873</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152873"/>
		<updated>2023-12-08T20:00:54Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* Postman Implementation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''list Action:'''&lt;br /&gt;
## The 'list' action will be reimplemented to handle listing of users.&lt;br /&gt;
## It will retrieve a list of users based on certain criteria, such as name, full name or email.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'list' and 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
==== ToDo Status: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done!&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|7&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `role` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;list and paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that list and paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that list and paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
The search_users method accepts three parameters: user_id, word, and search_by. If a valid user_id is provided, the method returns an array containing the user with that specific user_id.&lt;br /&gt;
In cases where user_id is empty or invalid, the method performs a search based on the provided word and search_by parameters.&lt;br /&gt;
To prevent SQL injection, the method validates search_by against a predefined set of acceptable fields, including 'name', 'full_name', 'email', and 'role'. The search is then conducted using a LIKE query on the specified field, ordering the results by the user's name.&lt;br /&gt;
&lt;br /&gt;
This flexible method accommodates various search scenarios, allowing users to query the system based on different criteria, enhancing the overall user experience.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
  def self.search_users(user_id, word, search_by)&lt;br /&gt;
    if user_id.present? &amp;amp;&amp;amp; (user = User.find_by(id: user_id))&lt;br /&gt;
      # If a valid user_id is provided, return the user with that specific user_id&lt;br /&gt;
      return [user]&lt;br /&gt;
    else&lt;br /&gt;
      # If user_id is empty or invalid, perform the search based on word and search_by parameters&lt;br /&gt;
&lt;br /&gt;
      # Validate search_by to avoid SQL injection&lt;br /&gt;
      valid_search_fields = %w[name full_name email role]&lt;br /&gt;
      search_by = valid_search_fields.include?(search_by) ? search_by : nil&lt;br /&gt;
&lt;br /&gt;
      # Perform the LIKE query on the specified field (name, full_name, email, role) and order by name&lt;br /&gt;
      if search_by.present?&lt;br /&gt;
        # Use search_by directly in the where clause&lt;br /&gt;
        users = User.joins(:role).where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name) if search_by == 'role'&lt;br /&gt;
        users ||= User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name)&lt;br /&gt;
        return users&lt;br /&gt;
      else&lt;br /&gt;
        # If search_by is not recognized, return an empty result&lt;br /&gt;
        return []&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The login method, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
==== Attributes ====&lt;br /&gt;
&lt;br /&gt;
* '''name:''' The name of the user.&lt;br /&gt;
* '''full_name:''' The full name of the user, providing a comprehensive representation.&lt;br /&gt;
* '''email:''' The unique email address associated with each user, ensuring distinct identification.&lt;br /&gt;
* '''role:''' A relationship with the [[Role Model|Role model]], defining the user's role and access privileges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    user_id = params[:user_id]&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(user_id, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.present?&lt;br /&gt;
      # If the result is not empty, render the users as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is empty, render a message as JSON with a not found status&lt;br /&gt;
      render json: { error: 'User not found or no matching results' }, status: :not_found&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This method facilitates dynamic user searches, allowing users to query the system based on different parameters. Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
=== role Method ===&lt;br /&gt;
The role method provides the role of a specific user. It is used as a utility method in the controller.&lt;br /&gt;
  def role&lt;br /&gt;
    if @user&amp;amp;.role_id&lt;br /&gt;
      @role = Role.find(@user.role_id)&lt;br /&gt;
    elsif @user&lt;br /&gt;
      @role = Role.new(id: nil, name: '(none)')&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
=== find_by_login Method ===&lt;br /&gt;
The method provides the functionality to search user by email or name&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list Method ===&lt;br /&gt;
Method for displaying the list of users&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list and paginate_list Method ===&lt;br /&gt;
Method for filtering the users list with proper search and pagination.&lt;br /&gt;
  def list&lt;br /&gt;
    # code here&lt;br /&gt;
    letter = params[:letter]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
    # If search parameters present&lt;br /&gt;
    if letter.present? &amp;amp;&amp;amp; search_by.present?&lt;br /&gt;
      case search_by.to_i&lt;br /&gt;
      when 1 # Search by username&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('name LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 2 # Search by fullname&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('fullname LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 3 # Search by email&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('email LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      else&lt;br /&gt;
        @paginated_users = paginate_list&lt;br /&gt;
      end&lt;br /&gt;
    else # Display all users if no search parameters present&lt;br /&gt;
      @paginated_users = paginate_list&lt;br /&gt;
      if @paginated_users&lt;br /&gt;
        puts(&amp;quot;Not empty&amp;quot; + @paginated_users.to_s) else puts(&amp;quot;Empty&amp;quot;)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    render json: @paginated_users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def paginate_list&lt;br /&gt;
    paginate_options = { '1' =&amp;gt; 25, '2' =&amp;gt; 50, '3' =&amp;gt; 100 }&lt;br /&gt;
    # If the above hash does not have a value for the key,&lt;br /&gt;
    # it means that we need to show all the users on the page&lt;br /&gt;
    #&lt;br /&gt;
    # Just a point to remember, when we use pagination, the&lt;br /&gt;
    # 'users' variable should be an object, not an array&lt;br /&gt;
    # The type of condition for the search depends on what the user has selected from the search_by dropdown&lt;br /&gt;
    @search_by = params[:search_by]&lt;br /&gt;
    @per_page = params[:per_page] || 3&lt;br /&gt;
    # search for corresponding users&lt;br /&gt;
    # users = User.search_users(role, user_id, letter, @search_by)&lt;br /&gt;
    # paginate&lt;br /&gt;
    users = if paginate_options[@per_page.to_s].nil? # displaying all - no pagination&lt;br /&gt;
              User.all&lt;br /&gt;
            else # some pagination is active - use the per_page&lt;br /&gt;
              User.paginate(page: params[:page], per_page: paginate_options[@per_page.to_s])&lt;br /&gt;
            end&lt;br /&gt;
    # users = User.all&lt;br /&gt;
    users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Swagger API ===&lt;br /&gt;
&lt;br /&gt;
The image shows that we used Swagger to design and document our API application. Then, we exported the API data in JSON format to use Postman for development and testing. Postman is a tool that lets us send requests, check responses, write tests, and automate workflows for our API.&lt;br /&gt;
&lt;br /&gt;
[[File:Swagger2.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Postman Collection ===&lt;br /&gt;
&lt;br /&gt;
Postman is an all-encompassing tool that enables us to send requests, validate responses, construct tests, and automate workflows for our Application Programming Interface (API). We have imported the Swagger API and created a Postman collection; this helps us to share the progress in real-time, facilitating online modifications because it is a SaaS application. In addition, we have put together a team workspace, as shown in the attached image. This arrangement fosters collaborative work and ensures the efficiency and effectiveness of our API operations.&lt;br /&gt;
&lt;br /&gt;
[[File:postman2.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
&lt;br /&gt;
The endpoint search by Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id (optional)''': The ID of the requesting user. If provided, it is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Swagger Endpoint ====&lt;br /&gt;
&lt;br /&gt;
[[File:Swagger search users.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/list&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''search_by (optional)''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
* '''letter (optional)''': The field to indicate the starting letter of the user's 'name', 'email', or 'full_name'.&lt;br /&gt;
* '''per_page  (optional)''': The field to indicate how many users to be displayed per page.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET api/v1/users/list?letter=n&amp;amp;search_by=1&amp;amp;per_page=3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;newuser&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;new_user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Swagger Endpoint ====&lt;br /&gt;
&lt;br /&gt;
[[File:swagger-list.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:List_api.png|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
require 'rails_helper'&lt;br /&gt;
&lt;br /&gt;
RSpec.describe User, type: :model do&lt;br /&gt;
&lt;br /&gt;
  describe '.find_by_login' do&lt;br /&gt;
    context 'when login is an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      it 'returns the user with the matching email' do&lt;br /&gt;
        result = User.find_by_login('test@test.com')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      it 'returns nil when user with email doesnt exist' do&lt;br /&gt;
        result = User.find_by_login('test@unknown.com')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when login is not an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      let!(:user2) { build(:user, name: 'testname', email: 'test@test2.com') }&lt;br /&gt;
      it 'returns the user with the matching name' do&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return first user with the matching name' do&lt;br /&gt;
        user2.save&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return nil when no user with matching name' do&lt;br /&gt;
        result = User.find_by_login('unknown')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  describe '.search_users' do&lt;br /&gt;
    # Creating dummy objects for the test with the help of let statement&lt;br /&gt;
    let(:role) { Role.create(name: 'Instructor', parent_id: nil, id: 2, default_page_id: nil) }&lt;br /&gt;
    let(:instructor) do&lt;br /&gt;
      Instructor.create(id: 1234, name: 'testinstructor', email: 'test@test.com', full_name: 'Test Instructor',&lt;br /&gt;
                        password: '123456', role_id: 2)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by name' do&lt;br /&gt;
      it 'returns users with matching names' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'testins', 'name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by fullname' do&lt;br /&gt;
      it 'returns users with matching fullnames' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'Test', 'full_name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'UnknownName', 'full_name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by email' do&lt;br /&gt;
      it 'returns users with matching emails' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'test@test.com', 'email')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown@test.com', 'email')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by default' do&lt;br /&gt;
      it 'returns users with names starting with the specified id' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(instructor.id, nil, nil)&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(9999, nil, nil) # Use an invalid user_id&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    context 'when searching by role' do&lt;br /&gt;
      it 'returns users with matching roles' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'admin', 'role')&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.role_id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'role')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:Expertiza_tests.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;br /&gt;
* [https://github.com/expertiza/reimplementation-back-end/pull/60/files#diff-a05d611b9fb47bcea66e3aaf6f25dc71d8da9976c5ccc991ea5171e938605644 GITHUB PR]&lt;br /&gt;
* [https://www.youtube.com/watch?v=_aCqMjRwW_U VIDEO DEMO 5min]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152872</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152872"/>
		<updated>2023-12-08T19:38:43Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* Endpoint: Search Users */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''list Action:'''&lt;br /&gt;
## The 'list' action will be reimplemented to handle listing of users.&lt;br /&gt;
## It will retrieve a list of users based on certain criteria, such as name, full name or email.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'list' and 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
==== ToDo Status: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done!&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|7&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `role` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;list and paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that list and paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that list and paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
The search_users method accepts three parameters: user_id, word, and search_by. If a valid user_id is provided, the method returns an array containing the user with that specific user_id.&lt;br /&gt;
In cases where user_id is empty or invalid, the method performs a search based on the provided word and search_by parameters.&lt;br /&gt;
To prevent SQL injection, the method validates search_by against a predefined set of acceptable fields, including 'name', 'full_name', 'email', and 'role'. The search is then conducted using a LIKE query on the specified field, ordering the results by the user's name.&lt;br /&gt;
&lt;br /&gt;
This flexible method accommodates various search scenarios, allowing users to query the system based on different criteria, enhancing the overall user experience.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
  def self.search_users(user_id, word, search_by)&lt;br /&gt;
    if user_id.present? &amp;amp;&amp;amp; (user = User.find_by(id: user_id))&lt;br /&gt;
      # If a valid user_id is provided, return the user with that specific user_id&lt;br /&gt;
      return [user]&lt;br /&gt;
    else&lt;br /&gt;
      # If user_id is empty or invalid, perform the search based on word and search_by parameters&lt;br /&gt;
&lt;br /&gt;
      # Validate search_by to avoid SQL injection&lt;br /&gt;
      valid_search_fields = %w[name full_name email role]&lt;br /&gt;
      search_by = valid_search_fields.include?(search_by) ? search_by : nil&lt;br /&gt;
&lt;br /&gt;
      # Perform the LIKE query on the specified field (name, full_name, email, role) and order by name&lt;br /&gt;
      if search_by.present?&lt;br /&gt;
        # Use search_by directly in the where clause&lt;br /&gt;
        users = User.joins(:role).where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name) if search_by == 'role'&lt;br /&gt;
        users ||= User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name)&lt;br /&gt;
        return users&lt;br /&gt;
      else&lt;br /&gt;
        # If search_by is not recognized, return an empty result&lt;br /&gt;
        return []&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The login method, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
==== Attributes ====&lt;br /&gt;
&lt;br /&gt;
* '''name:''' The name of the user.&lt;br /&gt;
* '''full_name:''' The full name of the user, providing a comprehensive representation.&lt;br /&gt;
* '''email:''' The unique email address associated with each user, ensuring distinct identification.&lt;br /&gt;
* '''role:''' A relationship with the [[Role Model|Role model]], defining the user's role and access privileges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    user_id = params[:user_id]&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(user_id, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.present?&lt;br /&gt;
      # If the result is not empty, render the users as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is empty, render a message as JSON with a not found status&lt;br /&gt;
      render json: { error: 'User not found or no matching results' }, status: :not_found&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This method facilitates dynamic user searches, allowing users to query the system based on different parameters. Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
=== role Method ===&lt;br /&gt;
The role method provides the role of a specific user. It is used as a utility method in the controller.&lt;br /&gt;
  def role&lt;br /&gt;
    if @user&amp;amp;.role_id&lt;br /&gt;
      @role = Role.find(@user.role_id)&lt;br /&gt;
    elsif @user&lt;br /&gt;
      @role = Role.new(id: nil, name: '(none)')&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
=== find_by_login Method ===&lt;br /&gt;
The method provides the functionality to search user by email or name&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list Method ===&lt;br /&gt;
Method for displaying the list of users&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list and paginate_list Method ===&lt;br /&gt;
Method for filtering the users list with proper search and pagination.&lt;br /&gt;
  def list&lt;br /&gt;
    # code here&lt;br /&gt;
    letter = params[:letter]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
    # If search parameters present&lt;br /&gt;
    if letter.present? &amp;amp;&amp;amp; search_by.present?&lt;br /&gt;
      case search_by.to_i&lt;br /&gt;
      when 1 # Search by username&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('name LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 2 # Search by fullname&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('fullname LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 3 # Search by email&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('email LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      else&lt;br /&gt;
        @paginated_users = paginate_list&lt;br /&gt;
      end&lt;br /&gt;
    else # Display all users if no search parameters present&lt;br /&gt;
      @paginated_users = paginate_list&lt;br /&gt;
      if @paginated_users&lt;br /&gt;
        puts(&amp;quot;Not empty&amp;quot; + @paginated_users.to_s) else puts(&amp;quot;Empty&amp;quot;)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    render json: @paginated_users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def paginate_list&lt;br /&gt;
    paginate_options = { '1' =&amp;gt; 25, '2' =&amp;gt; 50, '3' =&amp;gt; 100 }&lt;br /&gt;
    # If the above hash does not have a value for the key,&lt;br /&gt;
    # it means that we need to show all the users on the page&lt;br /&gt;
    #&lt;br /&gt;
    # Just a point to remember, when we use pagination, the&lt;br /&gt;
    # 'users' variable should be an object, not an array&lt;br /&gt;
    # The type of condition for the search depends on what the user has selected from the search_by dropdown&lt;br /&gt;
    @search_by = params[:search_by]&lt;br /&gt;
    @per_page = params[:per_page] || 3&lt;br /&gt;
    # search for corresponding users&lt;br /&gt;
    # users = User.search_users(role, user_id, letter, @search_by)&lt;br /&gt;
    # paginate&lt;br /&gt;
    users = if paginate_options[@per_page.to_s].nil? # displaying all - no pagination&lt;br /&gt;
              User.all&lt;br /&gt;
            else # some pagination is active - use the per_page&lt;br /&gt;
              User.paginate(page: params[:page], per_page: paginate_options[@per_page.to_s])&lt;br /&gt;
            end&lt;br /&gt;
    # users = User.all&lt;br /&gt;
    users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Swagger API ===&lt;br /&gt;
&lt;br /&gt;
The image shows that we used Swagger to design and document our API application. Then, we exported the API data in JSON format to use Postman for development and testing. Postman is a tool that lets us send requests, check responses, write tests, and automate workflows for our API.&lt;br /&gt;
&lt;br /&gt;
[[File:Swagger2.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Postman Collection ===&lt;br /&gt;
&lt;br /&gt;
Postman is an all-encompassing tool that enables us to send requests, validate responses, construct tests, and automate workflows for our Application Programming Interface (API). We have imported the Swagger API and created a Postman collection; this helps us to share the progress in real-time, facilitating online modifications because it is a SaaS application. In addition, we have put together a team workspace, as shown in the attached image. This arrangement fosters collaborative work and ensures the efficiency and effectiveness of our API operations.&lt;br /&gt;
&lt;br /&gt;
[[File:postman2.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
&lt;br /&gt;
The endpoint search by Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id (optional)''': The ID of the requesting user. If provided, it is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Swagger Endpoint ====&lt;br /&gt;
&lt;br /&gt;
[[File:Swagger search users.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/list&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''search_by (optional)''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
* '''letter (optional)''': The field to indicate the starting letter of the user's 'name', 'email', or 'full_name'.&lt;br /&gt;
* '''per_page  (optional)''': The field to indicate how many users to be displayed per page.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET api/v1/users/list?letter=n&amp;amp;search_by=1&amp;amp;per_page=3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;newuser&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;new_user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:List_api.png|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
require 'rails_helper'&lt;br /&gt;
&lt;br /&gt;
RSpec.describe User, type: :model do&lt;br /&gt;
&lt;br /&gt;
  describe '.find_by_login' do&lt;br /&gt;
    context 'when login is an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      it 'returns the user with the matching email' do&lt;br /&gt;
        result = User.find_by_login('test@test.com')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      it 'returns nil when user with email doesnt exist' do&lt;br /&gt;
        result = User.find_by_login('test@unknown.com')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when login is not an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      let!(:user2) { build(:user, name: 'testname', email: 'test@test2.com') }&lt;br /&gt;
      it 'returns the user with the matching name' do&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return first user with the matching name' do&lt;br /&gt;
        user2.save&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return nil when no user with matching name' do&lt;br /&gt;
        result = User.find_by_login('unknown')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  describe '.search_users' do&lt;br /&gt;
    # Creating dummy objects for the test with the help of let statement&lt;br /&gt;
    let(:role) { Role.create(name: 'Instructor', parent_id: nil, id: 2, default_page_id: nil) }&lt;br /&gt;
    let(:instructor) do&lt;br /&gt;
      Instructor.create(id: 1234, name: 'testinstructor', email: 'test@test.com', full_name: 'Test Instructor',&lt;br /&gt;
                        password: '123456', role_id: 2)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by name' do&lt;br /&gt;
      it 'returns users with matching names' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'testins', 'name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by fullname' do&lt;br /&gt;
      it 'returns users with matching fullnames' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'Test', 'full_name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'UnknownName', 'full_name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by email' do&lt;br /&gt;
      it 'returns users with matching emails' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'test@test.com', 'email')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown@test.com', 'email')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by default' do&lt;br /&gt;
      it 'returns users with names starting with the specified id' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(instructor.id, nil, nil)&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(9999, nil, nil) # Use an invalid user_id&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    context 'when searching by role' do&lt;br /&gt;
      it 'returns users with matching roles' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'admin', 'role')&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.role_id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'role')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:Expertiza_tests.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;br /&gt;
* [https://github.com/expertiza/reimplementation-back-end/pull/60/files#diff-a05d611b9fb47bcea66e3aaf6f25dc71d8da9976c5ccc991ea5171e938605644 GITHUB PR]&lt;br /&gt;
* [https://www.youtube.com/watch?v=_aCqMjRwW_U VIDEO DEMO 5min]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152871</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152871"/>
		<updated>2023-12-08T19:38:15Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* search_users Method */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''list Action:'''&lt;br /&gt;
## The 'list' action will be reimplemented to handle listing of users.&lt;br /&gt;
## It will retrieve a list of users based on certain criteria, such as name, full name or email.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'list' and 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
==== ToDo Status: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done!&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|7&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `role` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;list and paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that list and paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that list and paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
The search_users method accepts three parameters: user_id, word, and search_by. If a valid user_id is provided, the method returns an array containing the user with that specific user_id.&lt;br /&gt;
In cases where user_id is empty or invalid, the method performs a search based on the provided word and search_by parameters.&lt;br /&gt;
To prevent SQL injection, the method validates search_by against a predefined set of acceptable fields, including 'name', 'full_name', 'email', and 'role'. The search is then conducted using a LIKE query on the specified field, ordering the results by the user's name.&lt;br /&gt;
&lt;br /&gt;
This flexible method accommodates various search scenarios, allowing users to query the system based on different criteria, enhancing the overall user experience.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
  def self.search_users(user_id, word, search_by)&lt;br /&gt;
    if user_id.present? &amp;amp;&amp;amp; (user = User.find_by(id: user_id))&lt;br /&gt;
      # If a valid user_id is provided, return the user with that specific user_id&lt;br /&gt;
      return [user]&lt;br /&gt;
    else&lt;br /&gt;
      # If user_id is empty or invalid, perform the search based on word and search_by parameters&lt;br /&gt;
&lt;br /&gt;
      # Validate search_by to avoid SQL injection&lt;br /&gt;
      valid_search_fields = %w[name full_name email role]&lt;br /&gt;
      search_by = valid_search_fields.include?(search_by) ? search_by : nil&lt;br /&gt;
&lt;br /&gt;
      # Perform the LIKE query on the specified field (name, full_name, email, role) and order by name&lt;br /&gt;
      if search_by.present?&lt;br /&gt;
        # Use search_by directly in the where clause&lt;br /&gt;
        users = User.joins(:role).where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name) if search_by == 'role'&lt;br /&gt;
        users ||= User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name)&lt;br /&gt;
        return users&lt;br /&gt;
      else&lt;br /&gt;
        # If search_by is not recognized, return an empty result&lt;br /&gt;
        return []&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The login method, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
==== Attributes ====&lt;br /&gt;
&lt;br /&gt;
* '''name:''' The name of the user.&lt;br /&gt;
* '''full_name:''' The full name of the user, providing a comprehensive representation.&lt;br /&gt;
* '''email:''' The unique email address associated with each user, ensuring distinct identification.&lt;br /&gt;
* '''role:''' A relationship with the [[Role Model|Role model]], defining the user's role and access privileges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    user_id = params[:user_id]&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(user_id, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.present?&lt;br /&gt;
      # If the result is not empty, render the users as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is empty, render a message as JSON with a not found status&lt;br /&gt;
      render json: { error: 'User not found or no matching results' }, status: :not_found&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This method facilitates dynamic user searches, allowing users to query the system based on different parameters. Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
=== role Method ===&lt;br /&gt;
The role method provides the role of a specific user. It is used as a utility method in the controller.&lt;br /&gt;
  def role&lt;br /&gt;
    if @user&amp;amp;.role_id&lt;br /&gt;
      @role = Role.find(@user.role_id)&lt;br /&gt;
    elsif @user&lt;br /&gt;
      @role = Role.new(id: nil, name: '(none)')&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
=== find_by_login Method ===&lt;br /&gt;
The method provides the functionality to search user by email or name&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list Method ===&lt;br /&gt;
Method for displaying the list of users&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list and paginate_list Method ===&lt;br /&gt;
Method for filtering the users list with proper search and pagination.&lt;br /&gt;
  def list&lt;br /&gt;
    # code here&lt;br /&gt;
    letter = params[:letter]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
    # If search parameters present&lt;br /&gt;
    if letter.present? &amp;amp;&amp;amp; search_by.present?&lt;br /&gt;
      case search_by.to_i&lt;br /&gt;
      when 1 # Search by username&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('name LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 2 # Search by fullname&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('fullname LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 3 # Search by email&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('email LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      else&lt;br /&gt;
        @paginated_users = paginate_list&lt;br /&gt;
      end&lt;br /&gt;
    else # Display all users if no search parameters present&lt;br /&gt;
      @paginated_users = paginate_list&lt;br /&gt;
      if @paginated_users&lt;br /&gt;
        puts(&amp;quot;Not empty&amp;quot; + @paginated_users.to_s) else puts(&amp;quot;Empty&amp;quot;)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    render json: @paginated_users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def paginate_list&lt;br /&gt;
    paginate_options = { '1' =&amp;gt; 25, '2' =&amp;gt; 50, '3' =&amp;gt; 100 }&lt;br /&gt;
    # If the above hash does not have a value for the key,&lt;br /&gt;
    # it means that we need to show all the users on the page&lt;br /&gt;
    #&lt;br /&gt;
    # Just a point to remember, when we use pagination, the&lt;br /&gt;
    # 'users' variable should be an object, not an array&lt;br /&gt;
    # The type of condition for the search depends on what the user has selected from the search_by dropdown&lt;br /&gt;
    @search_by = params[:search_by]&lt;br /&gt;
    @per_page = params[:per_page] || 3&lt;br /&gt;
    # search for corresponding users&lt;br /&gt;
    # users = User.search_users(role, user_id, letter, @search_by)&lt;br /&gt;
    # paginate&lt;br /&gt;
    users = if paginate_options[@per_page.to_s].nil? # displaying all - no pagination&lt;br /&gt;
              User.all&lt;br /&gt;
            else # some pagination is active - use the per_page&lt;br /&gt;
              User.paginate(page: params[:page], per_page: paginate_options[@per_page.to_s])&lt;br /&gt;
            end&lt;br /&gt;
    # users = User.all&lt;br /&gt;
    users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Swagger API ===&lt;br /&gt;
&lt;br /&gt;
The image shows that we used Swagger to design and document our API application. Then, we exported the API data in JSON format to use Postman for development and testing. Postman is a tool that lets us send requests, check responses, write tests, and automate workflows for our API.&lt;br /&gt;
&lt;br /&gt;
[[File:Swagger2.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Postman Collection ===&lt;br /&gt;
&lt;br /&gt;
Postman is an all-encompassing tool that enables us to send requests, validate responses, construct tests, and automate workflows for our Application Programming Interface (API). We have imported the Swagger API and created a Postman collection; this helps us to share the progress in real-time, facilitating online modifications because it is a SaaS application. In addition, we have put together a team workspace, as shown in the attached image. This arrangement fosters collaborative work and ensures the efficiency and effectiveness of our API operations.&lt;br /&gt;
&lt;br /&gt;
[[File:postman2.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
&lt;br /&gt;
The endpoint search by Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id (optional)''': The ID of the requesting user. If provided, it is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/list&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''search_by (optional)''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
* '''letter (optional)''': The field to indicate the starting letter of the user's 'name', 'email', or 'full_name'.&lt;br /&gt;
* '''per_page  (optional)''': The field to indicate how many users to be displayed per page.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET api/v1/users/list?letter=n&amp;amp;search_by=1&amp;amp;per_page=3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;newuser&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;new_user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:List_api.png|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
require 'rails_helper'&lt;br /&gt;
&lt;br /&gt;
RSpec.describe User, type: :model do&lt;br /&gt;
&lt;br /&gt;
  describe '.find_by_login' do&lt;br /&gt;
    context 'when login is an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      it 'returns the user with the matching email' do&lt;br /&gt;
        result = User.find_by_login('test@test.com')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      it 'returns nil when user with email doesnt exist' do&lt;br /&gt;
        result = User.find_by_login('test@unknown.com')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when login is not an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      let!(:user2) { build(:user, name: 'testname', email: 'test@test2.com') }&lt;br /&gt;
      it 'returns the user with the matching name' do&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return first user with the matching name' do&lt;br /&gt;
        user2.save&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return nil when no user with matching name' do&lt;br /&gt;
        result = User.find_by_login('unknown')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  describe '.search_users' do&lt;br /&gt;
    # Creating dummy objects for the test with the help of let statement&lt;br /&gt;
    let(:role) { Role.create(name: 'Instructor', parent_id: nil, id: 2, default_page_id: nil) }&lt;br /&gt;
    let(:instructor) do&lt;br /&gt;
      Instructor.create(id: 1234, name: 'testinstructor', email: 'test@test.com', full_name: 'Test Instructor',&lt;br /&gt;
                        password: '123456', role_id: 2)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by name' do&lt;br /&gt;
      it 'returns users with matching names' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'testins', 'name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by fullname' do&lt;br /&gt;
      it 'returns users with matching fullnames' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'Test', 'full_name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'UnknownName', 'full_name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by email' do&lt;br /&gt;
      it 'returns users with matching emails' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'test@test.com', 'email')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown@test.com', 'email')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by default' do&lt;br /&gt;
      it 'returns users with names starting with the specified id' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(instructor.id, nil, nil)&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(9999, nil, nil) # Use an invalid user_id&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    context 'when searching by role' do&lt;br /&gt;
      it 'returns users with matching roles' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'admin', 'role')&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.role_id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'role')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:Expertiza_tests.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;br /&gt;
* [https://github.com/expertiza/reimplementation-back-end/pull/60/files#diff-a05d611b9fb47bcea66e3aaf6f25dc71d8da9976c5ccc991ea5171e938605644 GITHUB PR]&lt;br /&gt;
* [https://www.youtube.com/watch?v=_aCqMjRwW_U VIDEO DEMO 5min]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152870</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152870"/>
		<updated>2023-12-08T19:37:33Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* Swagger Endpoint */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''list Action:'''&lt;br /&gt;
## The 'list' action will be reimplemented to handle listing of users.&lt;br /&gt;
## It will retrieve a list of users based on certain criteria, such as name, full name or email.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'list' and 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
==== ToDo Status: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done!&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|7&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `role` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;list and paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that list and paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that list and paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
The search_users method accepts three parameters: user_id, word, and search_by. If a valid user_id is provided, the method returns an array containing the user with that specific user_id.&lt;br /&gt;
In cases where user_id is empty or invalid, the method performs a search based on the provided word and search_by parameters.&lt;br /&gt;
To prevent SQL injection, the method validates search_by against a predefined set of acceptable fields, including 'name', 'full_name', 'email', and 'role'. The search is then conducted using a LIKE query on the specified field, ordering the results by the user's name.&lt;br /&gt;
&lt;br /&gt;
This flexible method accommodates various search scenarios, allowing users to query the system based on different criteria, enhancing the overall user experience.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
  def self.search_users(user_id, word, search_by)&lt;br /&gt;
    if user_id.present? &amp;amp;&amp;amp; (user = User.find_by(id: user_id))&lt;br /&gt;
      # If a valid user_id is provided, return the user with that specific user_id&lt;br /&gt;
      return [user]&lt;br /&gt;
    else&lt;br /&gt;
      # If user_id is empty or invalid, perform the search based on word and search_by parameters&lt;br /&gt;
&lt;br /&gt;
      # Validate search_by to avoid SQL injection&lt;br /&gt;
      valid_search_fields = %w[name full_name email role]&lt;br /&gt;
      search_by = valid_search_fields.include?(search_by) ? search_by : nil&lt;br /&gt;
&lt;br /&gt;
      # Perform the LIKE query on the specified field (name, full_name, email, role) and order by name&lt;br /&gt;
      if search_by.present?&lt;br /&gt;
        # Use search_by directly in the where clause&lt;br /&gt;
        users = User.joins(:role).where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name) if search_by == 'role'&lt;br /&gt;
        users ||= User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name)&lt;br /&gt;
        return users&lt;br /&gt;
      else&lt;br /&gt;
        # If search_by is not recognized, return an empty result&lt;br /&gt;
        return []&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The login method, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
==== Swagger Endpoint ====&lt;br /&gt;
[[File:Swagger search users.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
==== Attributes ====&lt;br /&gt;
&lt;br /&gt;
* '''name:''' The name of the user.&lt;br /&gt;
* '''full_name:''' The full name of the user, providing a comprehensive representation.&lt;br /&gt;
* '''email:''' The unique email address associated with each user, ensuring distinct identification.&lt;br /&gt;
* '''role:''' A relationship with the [[Role Model|Role model]], defining the user's role and access privileges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    user_id = params[:user_id]&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(user_id, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.present?&lt;br /&gt;
      # If the result is not empty, render the users as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is empty, render a message as JSON with a not found status&lt;br /&gt;
      render json: { error: 'User not found or no matching results' }, status: :not_found&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This method facilitates dynamic user searches, allowing users to query the system based on different parameters. Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
=== role Method ===&lt;br /&gt;
The role method provides the role of a specific user. It is used as a utility method in the controller.&lt;br /&gt;
  def role&lt;br /&gt;
    if @user&amp;amp;.role_id&lt;br /&gt;
      @role = Role.find(@user.role_id)&lt;br /&gt;
    elsif @user&lt;br /&gt;
      @role = Role.new(id: nil, name: '(none)')&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
=== find_by_login Method ===&lt;br /&gt;
The method provides the functionality to search user by email or name&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list Method ===&lt;br /&gt;
Method for displaying the list of users&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list and paginate_list Method ===&lt;br /&gt;
Method for filtering the users list with proper search and pagination.&lt;br /&gt;
  def list&lt;br /&gt;
    # code here&lt;br /&gt;
    letter = params[:letter]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
    # If search parameters present&lt;br /&gt;
    if letter.present? &amp;amp;&amp;amp; search_by.present?&lt;br /&gt;
      case search_by.to_i&lt;br /&gt;
      when 1 # Search by username&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('name LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 2 # Search by fullname&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('fullname LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 3 # Search by email&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('email LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      else&lt;br /&gt;
        @paginated_users = paginate_list&lt;br /&gt;
      end&lt;br /&gt;
    else # Display all users if no search parameters present&lt;br /&gt;
      @paginated_users = paginate_list&lt;br /&gt;
      if @paginated_users&lt;br /&gt;
        puts(&amp;quot;Not empty&amp;quot; + @paginated_users.to_s) else puts(&amp;quot;Empty&amp;quot;)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    render json: @paginated_users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def paginate_list&lt;br /&gt;
    paginate_options = { '1' =&amp;gt; 25, '2' =&amp;gt; 50, '3' =&amp;gt; 100 }&lt;br /&gt;
    # If the above hash does not have a value for the key,&lt;br /&gt;
    # it means that we need to show all the users on the page&lt;br /&gt;
    #&lt;br /&gt;
    # Just a point to remember, when we use pagination, the&lt;br /&gt;
    # 'users' variable should be an object, not an array&lt;br /&gt;
    # The type of condition for the search depends on what the user has selected from the search_by dropdown&lt;br /&gt;
    @search_by = params[:search_by]&lt;br /&gt;
    @per_page = params[:per_page] || 3&lt;br /&gt;
    # search for corresponding users&lt;br /&gt;
    # users = User.search_users(role, user_id, letter, @search_by)&lt;br /&gt;
    # paginate&lt;br /&gt;
    users = if paginate_options[@per_page.to_s].nil? # displaying all - no pagination&lt;br /&gt;
              User.all&lt;br /&gt;
            else # some pagination is active - use the per_page&lt;br /&gt;
              User.paginate(page: params[:page], per_page: paginate_options[@per_page.to_s])&lt;br /&gt;
            end&lt;br /&gt;
    # users = User.all&lt;br /&gt;
    users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Swagger API ===&lt;br /&gt;
&lt;br /&gt;
The image shows that we used Swagger to design and document our API application. Then, we exported the API data in JSON format to use Postman for development and testing. Postman is a tool that lets us send requests, check responses, write tests, and automate workflows for our API.&lt;br /&gt;
&lt;br /&gt;
[[File:Swagger2.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Postman Collection ===&lt;br /&gt;
&lt;br /&gt;
Postman is an all-encompassing tool that enables us to send requests, validate responses, construct tests, and automate workflows for our Application Programming Interface (API). We have imported the Swagger API and created a Postman collection; this helps us to share the progress in real-time, facilitating online modifications because it is a SaaS application. In addition, we have put together a team workspace, as shown in the attached image. This arrangement fosters collaborative work and ensures the efficiency and effectiveness of our API operations.&lt;br /&gt;
&lt;br /&gt;
[[File:postman2.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
&lt;br /&gt;
The endpoint search by Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id (optional)''': The ID of the requesting user. If provided, it is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/list&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''search_by (optional)''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
* '''letter (optional)''': The field to indicate the starting letter of the user's 'name', 'email', or 'full_name'.&lt;br /&gt;
* '''per_page  (optional)''': The field to indicate how many users to be displayed per page.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET api/v1/users/list?letter=n&amp;amp;search_by=1&amp;amp;per_page=3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;newuser&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;new_user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:List_api.png|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
require 'rails_helper'&lt;br /&gt;
&lt;br /&gt;
RSpec.describe User, type: :model do&lt;br /&gt;
&lt;br /&gt;
  describe '.find_by_login' do&lt;br /&gt;
    context 'when login is an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      it 'returns the user with the matching email' do&lt;br /&gt;
        result = User.find_by_login('test@test.com')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      it 'returns nil when user with email doesnt exist' do&lt;br /&gt;
        result = User.find_by_login('test@unknown.com')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when login is not an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      let!(:user2) { build(:user, name: 'testname', email: 'test@test2.com') }&lt;br /&gt;
      it 'returns the user with the matching name' do&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return first user with the matching name' do&lt;br /&gt;
        user2.save&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return nil when no user with matching name' do&lt;br /&gt;
        result = User.find_by_login('unknown')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  describe '.search_users' do&lt;br /&gt;
    # Creating dummy objects for the test with the help of let statement&lt;br /&gt;
    let(:role) { Role.create(name: 'Instructor', parent_id: nil, id: 2, default_page_id: nil) }&lt;br /&gt;
    let(:instructor) do&lt;br /&gt;
      Instructor.create(id: 1234, name: 'testinstructor', email: 'test@test.com', full_name: 'Test Instructor',&lt;br /&gt;
                        password: '123456', role_id: 2)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by name' do&lt;br /&gt;
      it 'returns users with matching names' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'testins', 'name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by fullname' do&lt;br /&gt;
      it 'returns users with matching fullnames' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'Test', 'full_name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'UnknownName', 'full_name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by email' do&lt;br /&gt;
      it 'returns users with matching emails' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'test@test.com', 'email')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown@test.com', 'email')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by default' do&lt;br /&gt;
      it 'returns users with names starting with the specified id' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(instructor.id, nil, nil)&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(9999, nil, nil) # Use an invalid user_id&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    context 'when searching by role' do&lt;br /&gt;
      it 'returns users with matching roles' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'admin', 'role')&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.role_id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'role')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:Expertiza_tests.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;br /&gt;
* [https://github.com/expertiza/reimplementation-back-end/pull/60/files#diff-a05d611b9fb47bcea66e3aaf6f25dc71d8da9976c5ccc991ea5171e938605644 GITHUB PR]&lt;br /&gt;
* [https://www.youtube.com/watch?v=_aCqMjRwW_U VIDEO DEMO 5min]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152869</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152869"/>
		<updated>2023-12-08T19:37:17Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* Swagger Endpoint */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''list Action:'''&lt;br /&gt;
## The 'list' action will be reimplemented to handle listing of users.&lt;br /&gt;
## It will retrieve a list of users based on certain criteria, such as name, full name or email.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'list' and 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
==== ToDo Status: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done!&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|7&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `role` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;list and paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that list and paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that list and paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
The search_users method accepts three parameters: user_id, word, and search_by. If a valid user_id is provided, the method returns an array containing the user with that specific user_id.&lt;br /&gt;
In cases where user_id is empty or invalid, the method performs a search based on the provided word and search_by parameters.&lt;br /&gt;
To prevent SQL injection, the method validates search_by against a predefined set of acceptable fields, including 'name', 'full_name', 'email', and 'role'. The search is then conducted using a LIKE query on the specified field, ordering the results by the user's name.&lt;br /&gt;
&lt;br /&gt;
This flexible method accommodates various search scenarios, allowing users to query the system based on different criteria, enhancing the overall user experience.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
  def self.search_users(user_id, word, search_by)&lt;br /&gt;
    if user_id.present? &amp;amp;&amp;amp; (user = User.find_by(id: user_id))&lt;br /&gt;
      # If a valid user_id is provided, return the user with that specific user_id&lt;br /&gt;
      return [user]&lt;br /&gt;
    else&lt;br /&gt;
      # If user_id is empty or invalid, perform the search based on word and search_by parameters&lt;br /&gt;
&lt;br /&gt;
      # Validate search_by to avoid SQL injection&lt;br /&gt;
      valid_search_fields = %w[name full_name email role]&lt;br /&gt;
      search_by = valid_search_fields.include?(search_by) ? search_by : nil&lt;br /&gt;
&lt;br /&gt;
      # Perform the LIKE query on the specified field (name, full_name, email, role) and order by name&lt;br /&gt;
      if search_by.present?&lt;br /&gt;
        # Use search_by directly in the where clause&lt;br /&gt;
        users = User.joins(:role).where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name) if search_by == 'role'&lt;br /&gt;
        users ||= User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name)&lt;br /&gt;
        return users&lt;br /&gt;
      else&lt;br /&gt;
        # If search_by is not recognized, return an empty result&lt;br /&gt;
        return []&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The login method, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
==== Swagger Endpoint ====&lt;br /&gt;
[[File:Swagger search users.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==== Attributes ====&lt;br /&gt;
&lt;br /&gt;
* '''name:''' The name of the user.&lt;br /&gt;
* '''full_name:''' The full name of the user, providing a comprehensive representation.&lt;br /&gt;
* '''email:''' The unique email address associated with each user, ensuring distinct identification.&lt;br /&gt;
* '''role:''' A relationship with the [[Role Model|Role model]], defining the user's role and access privileges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    user_id = params[:user_id]&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(user_id, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.present?&lt;br /&gt;
      # If the result is not empty, render the users as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is empty, render a message as JSON with a not found status&lt;br /&gt;
      render json: { error: 'User not found or no matching results' }, status: :not_found&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This method facilitates dynamic user searches, allowing users to query the system based on different parameters. Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
=== role Method ===&lt;br /&gt;
The role method provides the role of a specific user. It is used as a utility method in the controller.&lt;br /&gt;
  def role&lt;br /&gt;
    if @user&amp;amp;.role_id&lt;br /&gt;
      @role = Role.find(@user.role_id)&lt;br /&gt;
    elsif @user&lt;br /&gt;
      @role = Role.new(id: nil, name: '(none)')&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
=== find_by_login Method ===&lt;br /&gt;
The method provides the functionality to search user by email or name&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list Method ===&lt;br /&gt;
Method for displaying the list of users&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list and paginate_list Method ===&lt;br /&gt;
Method for filtering the users list with proper search and pagination.&lt;br /&gt;
  def list&lt;br /&gt;
    # code here&lt;br /&gt;
    letter = params[:letter]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
    # If search parameters present&lt;br /&gt;
    if letter.present? &amp;amp;&amp;amp; search_by.present?&lt;br /&gt;
      case search_by.to_i&lt;br /&gt;
      when 1 # Search by username&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('name LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 2 # Search by fullname&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('fullname LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 3 # Search by email&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('email LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      else&lt;br /&gt;
        @paginated_users = paginate_list&lt;br /&gt;
      end&lt;br /&gt;
    else # Display all users if no search parameters present&lt;br /&gt;
      @paginated_users = paginate_list&lt;br /&gt;
      if @paginated_users&lt;br /&gt;
        puts(&amp;quot;Not empty&amp;quot; + @paginated_users.to_s) else puts(&amp;quot;Empty&amp;quot;)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    render json: @paginated_users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def paginate_list&lt;br /&gt;
    paginate_options = { '1' =&amp;gt; 25, '2' =&amp;gt; 50, '3' =&amp;gt; 100 }&lt;br /&gt;
    # If the above hash does not have a value for the key,&lt;br /&gt;
    # it means that we need to show all the users on the page&lt;br /&gt;
    #&lt;br /&gt;
    # Just a point to remember, when we use pagination, the&lt;br /&gt;
    # 'users' variable should be an object, not an array&lt;br /&gt;
    # The type of condition for the search depends on what the user has selected from the search_by dropdown&lt;br /&gt;
    @search_by = params[:search_by]&lt;br /&gt;
    @per_page = params[:per_page] || 3&lt;br /&gt;
    # search for corresponding users&lt;br /&gt;
    # users = User.search_users(role, user_id, letter, @search_by)&lt;br /&gt;
    # paginate&lt;br /&gt;
    users = if paginate_options[@per_page.to_s].nil? # displaying all - no pagination&lt;br /&gt;
              User.all&lt;br /&gt;
            else # some pagination is active - use the per_page&lt;br /&gt;
              User.paginate(page: params[:page], per_page: paginate_options[@per_page.to_s])&lt;br /&gt;
            end&lt;br /&gt;
    # users = User.all&lt;br /&gt;
    users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Swagger API ===&lt;br /&gt;
&lt;br /&gt;
The image shows that we used Swagger to design and document our API application. Then, we exported the API data in JSON format to use Postman for development and testing. Postman is a tool that lets us send requests, check responses, write tests, and automate workflows for our API.&lt;br /&gt;
&lt;br /&gt;
[[File:Swagger2.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Postman Collection ===&lt;br /&gt;
&lt;br /&gt;
Postman is an all-encompassing tool that enables us to send requests, validate responses, construct tests, and automate workflows for our Application Programming Interface (API). We have imported the Swagger API and created a Postman collection; this helps us to share the progress in real-time, facilitating online modifications because it is a SaaS application. In addition, we have put together a team workspace, as shown in the attached image. This arrangement fosters collaborative work and ensures the efficiency and effectiveness of our API operations.&lt;br /&gt;
&lt;br /&gt;
[[File:postman2.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
&lt;br /&gt;
The endpoint search by Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id (optional)''': The ID of the requesting user. If provided, it is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/list&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''search_by (optional)''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
* '''letter (optional)''': The field to indicate the starting letter of the user's 'name', 'email', or 'full_name'.&lt;br /&gt;
* '''per_page  (optional)''': The field to indicate how many users to be displayed per page.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET api/v1/users/list?letter=n&amp;amp;search_by=1&amp;amp;per_page=3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;newuser&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;new_user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:List_api.png|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
require 'rails_helper'&lt;br /&gt;
&lt;br /&gt;
RSpec.describe User, type: :model do&lt;br /&gt;
&lt;br /&gt;
  describe '.find_by_login' do&lt;br /&gt;
    context 'when login is an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      it 'returns the user with the matching email' do&lt;br /&gt;
        result = User.find_by_login('test@test.com')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      it 'returns nil when user with email doesnt exist' do&lt;br /&gt;
        result = User.find_by_login('test@unknown.com')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when login is not an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      let!(:user2) { build(:user, name: 'testname', email: 'test@test2.com') }&lt;br /&gt;
      it 'returns the user with the matching name' do&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return first user with the matching name' do&lt;br /&gt;
        user2.save&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return nil when no user with matching name' do&lt;br /&gt;
        result = User.find_by_login('unknown')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  describe '.search_users' do&lt;br /&gt;
    # Creating dummy objects for the test with the help of let statement&lt;br /&gt;
    let(:role) { Role.create(name: 'Instructor', parent_id: nil, id: 2, default_page_id: nil) }&lt;br /&gt;
    let(:instructor) do&lt;br /&gt;
      Instructor.create(id: 1234, name: 'testinstructor', email: 'test@test.com', full_name: 'Test Instructor',&lt;br /&gt;
                        password: '123456', role_id: 2)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by name' do&lt;br /&gt;
      it 'returns users with matching names' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'testins', 'name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by fullname' do&lt;br /&gt;
      it 'returns users with matching fullnames' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'Test', 'full_name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'UnknownName', 'full_name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by email' do&lt;br /&gt;
      it 'returns users with matching emails' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'test@test.com', 'email')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown@test.com', 'email')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by default' do&lt;br /&gt;
      it 'returns users with names starting with the specified id' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(instructor.id, nil, nil)&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(9999, nil, nil) # Use an invalid user_id&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    context 'when searching by role' do&lt;br /&gt;
      it 'returns users with matching roles' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'admin', 'role')&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.role_id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'role')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:Expertiza_tests.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;br /&gt;
* [https://github.com/expertiza/reimplementation-back-end/pull/60/files#diff-a05d611b9fb47bcea66e3aaf6f25dc71d8da9976c5ccc991ea5171e938605644 GITHUB PR]&lt;br /&gt;
* [https://www.youtube.com/watch?v=_aCqMjRwW_U VIDEO DEMO 5min]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Swagger_search_users.png&amp;diff=152868</id>
		<title>File:Swagger search users.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Swagger_search_users.png&amp;diff=152868"/>
		<updated>2023-12-08T19:36:29Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152867</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152867"/>
		<updated>2023-12-08T19:34:35Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* Swagger Endpoint */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''list Action:'''&lt;br /&gt;
## The 'list' action will be reimplemented to handle listing of users.&lt;br /&gt;
## It will retrieve a list of users based on certain criteria, such as name, full name or email.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'list' and 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
==== ToDo Status: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done!&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|7&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `role` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;list and paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that list and paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that list and paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
The search_users method accepts three parameters: user_id, word, and search_by. If a valid user_id is provided, the method returns an array containing the user with that specific user_id.&lt;br /&gt;
In cases where user_id is empty or invalid, the method performs a search based on the provided word and search_by parameters.&lt;br /&gt;
To prevent SQL injection, the method validates search_by against a predefined set of acceptable fields, including 'name', 'full_name', 'email', and 'role'. The search is then conducted using a LIKE query on the specified field, ordering the results by the user's name.&lt;br /&gt;
&lt;br /&gt;
This flexible method accommodates various search scenarios, allowing users to query the system based on different criteria, enhancing the overall user experience.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
  def self.search_users(user_id, word, search_by)&lt;br /&gt;
    if user_id.present? &amp;amp;&amp;amp; (user = User.find_by(id: user_id))&lt;br /&gt;
      # If a valid user_id is provided, return the user with that specific user_id&lt;br /&gt;
      return [user]&lt;br /&gt;
    else&lt;br /&gt;
      # If user_id is empty or invalid, perform the search based on word and search_by parameters&lt;br /&gt;
&lt;br /&gt;
      # Validate search_by to avoid SQL injection&lt;br /&gt;
      valid_search_fields = %w[name full_name email role]&lt;br /&gt;
      search_by = valid_search_fields.include?(search_by) ? search_by : nil&lt;br /&gt;
&lt;br /&gt;
      # Perform the LIKE query on the specified field (name, full_name, email, role) and order by name&lt;br /&gt;
      if search_by.present?&lt;br /&gt;
        # Use search_by directly in the where clause&lt;br /&gt;
        users = User.joins(:role).where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name) if search_by == 'role'&lt;br /&gt;
        users ||= User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name)&lt;br /&gt;
        return users&lt;br /&gt;
      else&lt;br /&gt;
        # If search_by is not recognized, return an empty result&lt;br /&gt;
        return []&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The login method, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
==== Swagger Endpoint ====&lt;br /&gt;
[[file:swagger_search_users.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==== Attributes ====&lt;br /&gt;
&lt;br /&gt;
* '''name:''' The name of the user.&lt;br /&gt;
* '''full_name:''' The full name of the user, providing a comprehensive representation.&lt;br /&gt;
* '''email:''' The unique email address associated with each user, ensuring distinct identification.&lt;br /&gt;
* '''role:''' A relationship with the [[Role Model|Role model]], defining the user's role and access privileges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    user_id = params[:user_id]&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(user_id, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.present?&lt;br /&gt;
      # If the result is not empty, render the users as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is empty, render a message as JSON with a not found status&lt;br /&gt;
      render json: { error: 'User not found or no matching results' }, status: :not_found&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This method facilitates dynamic user searches, allowing users to query the system based on different parameters. Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
=== role Method ===&lt;br /&gt;
The role method provides the role of a specific user. It is used as a utility method in the controller.&lt;br /&gt;
  def role&lt;br /&gt;
    if @user&amp;amp;.role_id&lt;br /&gt;
      @role = Role.find(@user.role_id)&lt;br /&gt;
    elsif @user&lt;br /&gt;
      @role = Role.new(id: nil, name: '(none)')&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
=== find_by_login Method ===&lt;br /&gt;
The method provides the functionality to search user by email or name&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list Method ===&lt;br /&gt;
Method for displaying the list of users&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list and paginate_list Method ===&lt;br /&gt;
Method for filtering the users list with proper search and pagination.&lt;br /&gt;
  def list&lt;br /&gt;
    # code here&lt;br /&gt;
    letter = params[:letter]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
    # If search parameters present&lt;br /&gt;
    if letter.present? &amp;amp;&amp;amp; search_by.present?&lt;br /&gt;
      case search_by.to_i&lt;br /&gt;
      when 1 # Search by username&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('name LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 2 # Search by fullname&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('fullname LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 3 # Search by email&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('email LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      else&lt;br /&gt;
        @paginated_users = paginate_list&lt;br /&gt;
      end&lt;br /&gt;
    else # Display all users if no search parameters present&lt;br /&gt;
      @paginated_users = paginate_list&lt;br /&gt;
      if @paginated_users&lt;br /&gt;
        puts(&amp;quot;Not empty&amp;quot; + @paginated_users.to_s) else puts(&amp;quot;Empty&amp;quot;)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    render json: @paginated_users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def paginate_list&lt;br /&gt;
    paginate_options = { '1' =&amp;gt; 25, '2' =&amp;gt; 50, '3' =&amp;gt; 100 }&lt;br /&gt;
    # If the above hash does not have a value for the key,&lt;br /&gt;
    # it means that we need to show all the users on the page&lt;br /&gt;
    #&lt;br /&gt;
    # Just a point to remember, when we use pagination, the&lt;br /&gt;
    # 'users' variable should be an object, not an array&lt;br /&gt;
    # The type of condition for the search depends on what the user has selected from the search_by dropdown&lt;br /&gt;
    @search_by = params[:search_by]&lt;br /&gt;
    @per_page = params[:per_page] || 3&lt;br /&gt;
    # search for corresponding users&lt;br /&gt;
    # users = User.search_users(role, user_id, letter, @search_by)&lt;br /&gt;
    # paginate&lt;br /&gt;
    users = if paginate_options[@per_page.to_s].nil? # displaying all - no pagination&lt;br /&gt;
              User.all&lt;br /&gt;
            else # some pagination is active - use the per_page&lt;br /&gt;
              User.paginate(page: params[:page], per_page: paginate_options[@per_page.to_s])&lt;br /&gt;
            end&lt;br /&gt;
    # users = User.all&lt;br /&gt;
    users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Swagger API ===&lt;br /&gt;
&lt;br /&gt;
The image shows that we used Swagger to design and document our API application. Then, we exported the API data in JSON format to use Postman for development and testing. Postman is a tool that lets us send requests, check responses, write tests, and automate workflows for our API.&lt;br /&gt;
&lt;br /&gt;
[[File:Swagger2.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Postman Collection ===&lt;br /&gt;
&lt;br /&gt;
Postman is an all-encompassing tool that enables us to send requests, validate responses, construct tests, and automate workflows for our Application Programming Interface (API). We have imported the Swagger API and created a Postman collection; this helps us to share the progress in real-time, facilitating online modifications because it is a SaaS application. In addition, we have put together a team workspace, as shown in the attached image. This arrangement fosters collaborative work and ensures the efficiency and effectiveness of our API operations.&lt;br /&gt;
&lt;br /&gt;
[[File:postman2.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
&lt;br /&gt;
The endpoint search by Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id (optional)''': The ID of the requesting user. If provided, it is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/list&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''search_by (optional)''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
* '''letter (optional)''': The field to indicate the starting letter of the user's 'name', 'email', or 'full_name'.&lt;br /&gt;
* '''per_page  (optional)''': The field to indicate how many users to be displayed per page.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET api/v1/users/list?letter=n&amp;amp;search_by=1&amp;amp;per_page=3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;newuser&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;new_user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:List_api.png|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
require 'rails_helper'&lt;br /&gt;
&lt;br /&gt;
RSpec.describe User, type: :model do&lt;br /&gt;
&lt;br /&gt;
  describe '.find_by_login' do&lt;br /&gt;
    context 'when login is an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      it 'returns the user with the matching email' do&lt;br /&gt;
        result = User.find_by_login('test@test.com')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      it 'returns nil when user with email doesnt exist' do&lt;br /&gt;
        result = User.find_by_login('test@unknown.com')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when login is not an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      let!(:user2) { build(:user, name: 'testname', email: 'test@test2.com') }&lt;br /&gt;
      it 'returns the user with the matching name' do&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return first user with the matching name' do&lt;br /&gt;
        user2.save&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return nil when no user with matching name' do&lt;br /&gt;
        result = User.find_by_login('unknown')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  describe '.search_users' do&lt;br /&gt;
    # Creating dummy objects for the test with the help of let statement&lt;br /&gt;
    let(:role) { Role.create(name: 'Instructor', parent_id: nil, id: 2, default_page_id: nil) }&lt;br /&gt;
    let(:instructor) do&lt;br /&gt;
      Instructor.create(id: 1234, name: 'testinstructor', email: 'test@test.com', full_name: 'Test Instructor',&lt;br /&gt;
                        password: '123456', role_id: 2)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by name' do&lt;br /&gt;
      it 'returns users with matching names' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'testins', 'name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by fullname' do&lt;br /&gt;
      it 'returns users with matching fullnames' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'Test', 'full_name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'UnknownName', 'full_name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by email' do&lt;br /&gt;
      it 'returns users with matching emails' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'test@test.com', 'email')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown@test.com', 'email')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by default' do&lt;br /&gt;
      it 'returns users with names starting with the specified id' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(instructor.id, nil, nil)&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(9999, nil, nil) # Use an invalid user_id&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    context 'when searching by role' do&lt;br /&gt;
      it 'returns users with matching roles' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'admin', 'role')&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.role_id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'role')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:Expertiza_tests.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;br /&gt;
* [https://github.com/expertiza/reimplementation-back-end/pull/60/files#diff-a05d611b9fb47bcea66e3aaf6f25dc71d8da9976c5ccc991ea5171e938605644 GITHUB PR]&lt;br /&gt;
* [https://www.youtube.com/watch?v=_aCqMjRwW_U VIDEO DEMO 5min]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152866</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152866"/>
		<updated>2023-12-08T19:33:58Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* search_users Method */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''list Action:'''&lt;br /&gt;
## The 'list' action will be reimplemented to handle listing of users.&lt;br /&gt;
## It will retrieve a list of users based on certain criteria, such as name, full name or email.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'list' and 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
==== ToDo Status: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done!&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|7&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `role` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;list and paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that list and paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that list and paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
The search_users method accepts three parameters: user_id, word, and search_by. If a valid user_id is provided, the method returns an array containing the user with that specific user_id.&lt;br /&gt;
In cases where user_id is empty or invalid, the method performs a search based on the provided word and search_by parameters.&lt;br /&gt;
To prevent SQL injection, the method validates search_by against a predefined set of acceptable fields, including 'name', 'full_name', 'email', and 'role'. The search is then conducted using a LIKE query on the specified field, ordering the results by the user's name.&lt;br /&gt;
&lt;br /&gt;
This flexible method accommodates various search scenarios, allowing users to query the system based on different criteria, enhancing the overall user experience.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
  def self.search_users(user_id, word, search_by)&lt;br /&gt;
    if user_id.present? &amp;amp;&amp;amp; (user = User.find_by(id: user_id))&lt;br /&gt;
      # If a valid user_id is provided, return the user with that specific user_id&lt;br /&gt;
      return [user]&lt;br /&gt;
    else&lt;br /&gt;
      # If user_id is empty or invalid, perform the search based on word and search_by parameters&lt;br /&gt;
&lt;br /&gt;
      # Validate search_by to avoid SQL injection&lt;br /&gt;
      valid_search_fields = %w[name full_name email role]&lt;br /&gt;
      search_by = valid_search_fields.include?(search_by) ? search_by : nil&lt;br /&gt;
&lt;br /&gt;
      # Perform the LIKE query on the specified field (name, full_name, email, role) and order by name&lt;br /&gt;
      if search_by.present?&lt;br /&gt;
        # Use search_by directly in the where clause&lt;br /&gt;
        users = User.joins(:role).where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name) if search_by == 'role'&lt;br /&gt;
        users ||= User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name)&lt;br /&gt;
        return users&lt;br /&gt;
      else&lt;br /&gt;
        # If search_by is not recognized, return an empty result&lt;br /&gt;
        return []&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The login method, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
==== Swagger Endpoint ====&lt;br /&gt;
[file: swagger_search_users.png]&lt;br /&gt;
&lt;br /&gt;
==== Attributes ====&lt;br /&gt;
&lt;br /&gt;
* '''name:''' The name of the user.&lt;br /&gt;
* '''full_name:''' The full name of the user, providing a comprehensive representation.&lt;br /&gt;
* '''email:''' The unique email address associated with each user, ensuring distinct identification.&lt;br /&gt;
* '''role:''' A relationship with the [[Role Model|Role model]], defining the user's role and access privileges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    user_id = params[:user_id]&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(user_id, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.present?&lt;br /&gt;
      # If the result is not empty, render the users as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is empty, render a message as JSON with a not found status&lt;br /&gt;
      render json: { error: 'User not found or no matching results' }, status: :not_found&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This method facilitates dynamic user searches, allowing users to query the system based on different parameters. Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
=== role Method ===&lt;br /&gt;
The role method provides the role of a specific user. It is used as a utility method in the controller.&lt;br /&gt;
  def role&lt;br /&gt;
    if @user&amp;amp;.role_id&lt;br /&gt;
      @role = Role.find(@user.role_id)&lt;br /&gt;
    elsif @user&lt;br /&gt;
      @role = Role.new(id: nil, name: '(none)')&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
=== find_by_login Method ===&lt;br /&gt;
The method provides the functionality to search user by email or name&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list Method ===&lt;br /&gt;
Method for displaying the list of users&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list and paginate_list Method ===&lt;br /&gt;
Method for filtering the users list with proper search and pagination.&lt;br /&gt;
  def list&lt;br /&gt;
    # code here&lt;br /&gt;
    letter = params[:letter]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
    # If search parameters present&lt;br /&gt;
    if letter.present? &amp;amp;&amp;amp; search_by.present?&lt;br /&gt;
      case search_by.to_i&lt;br /&gt;
      when 1 # Search by username&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('name LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 2 # Search by fullname&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('fullname LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 3 # Search by email&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('email LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      else&lt;br /&gt;
        @paginated_users = paginate_list&lt;br /&gt;
      end&lt;br /&gt;
    else # Display all users if no search parameters present&lt;br /&gt;
      @paginated_users = paginate_list&lt;br /&gt;
      if @paginated_users&lt;br /&gt;
        puts(&amp;quot;Not empty&amp;quot; + @paginated_users.to_s) else puts(&amp;quot;Empty&amp;quot;)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    render json: @paginated_users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def paginate_list&lt;br /&gt;
    paginate_options = { '1' =&amp;gt; 25, '2' =&amp;gt; 50, '3' =&amp;gt; 100 }&lt;br /&gt;
    # If the above hash does not have a value for the key,&lt;br /&gt;
    # it means that we need to show all the users on the page&lt;br /&gt;
    #&lt;br /&gt;
    # Just a point to remember, when we use pagination, the&lt;br /&gt;
    # 'users' variable should be an object, not an array&lt;br /&gt;
    # The type of condition for the search depends on what the user has selected from the search_by dropdown&lt;br /&gt;
    @search_by = params[:search_by]&lt;br /&gt;
    @per_page = params[:per_page] || 3&lt;br /&gt;
    # search for corresponding users&lt;br /&gt;
    # users = User.search_users(role, user_id, letter, @search_by)&lt;br /&gt;
    # paginate&lt;br /&gt;
    users = if paginate_options[@per_page.to_s].nil? # displaying all - no pagination&lt;br /&gt;
              User.all&lt;br /&gt;
            else # some pagination is active - use the per_page&lt;br /&gt;
              User.paginate(page: params[:page], per_page: paginate_options[@per_page.to_s])&lt;br /&gt;
            end&lt;br /&gt;
    # users = User.all&lt;br /&gt;
    users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Swagger API ===&lt;br /&gt;
&lt;br /&gt;
The image shows that we used Swagger to design and document our API application. Then, we exported the API data in JSON format to use Postman for development and testing. Postman is a tool that lets us send requests, check responses, write tests, and automate workflows for our API.&lt;br /&gt;
&lt;br /&gt;
[[File:Swagger2.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Postman Collection ===&lt;br /&gt;
&lt;br /&gt;
Postman is an all-encompassing tool that enables us to send requests, validate responses, construct tests, and automate workflows for our Application Programming Interface (API). We have imported the Swagger API and created a Postman collection; this helps us to share the progress in real-time, facilitating online modifications because it is a SaaS application. In addition, we have put together a team workspace, as shown in the attached image. This arrangement fosters collaborative work and ensures the efficiency and effectiveness of our API operations.&lt;br /&gt;
&lt;br /&gt;
[[File:postman2.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
&lt;br /&gt;
The endpoint search by Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id (optional)''': The ID of the requesting user. If provided, it is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/list&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''search_by (optional)''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
* '''letter (optional)''': The field to indicate the starting letter of the user's 'name', 'email', or 'full_name'.&lt;br /&gt;
* '''per_page  (optional)''': The field to indicate how many users to be displayed per page.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET api/v1/users/list?letter=n&amp;amp;search_by=1&amp;amp;per_page=3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;newuser&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;new_user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:List_api.png|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
require 'rails_helper'&lt;br /&gt;
&lt;br /&gt;
RSpec.describe User, type: :model do&lt;br /&gt;
&lt;br /&gt;
  describe '.find_by_login' do&lt;br /&gt;
    context 'when login is an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      it 'returns the user with the matching email' do&lt;br /&gt;
        result = User.find_by_login('test@test.com')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      it 'returns nil when user with email doesnt exist' do&lt;br /&gt;
        result = User.find_by_login('test@unknown.com')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when login is not an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      let!(:user2) { build(:user, name: 'testname', email: 'test@test2.com') }&lt;br /&gt;
      it 'returns the user with the matching name' do&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return first user with the matching name' do&lt;br /&gt;
        user2.save&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return nil when no user with matching name' do&lt;br /&gt;
        result = User.find_by_login('unknown')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  describe '.search_users' do&lt;br /&gt;
    # Creating dummy objects for the test with the help of let statement&lt;br /&gt;
    let(:role) { Role.create(name: 'Instructor', parent_id: nil, id: 2, default_page_id: nil) }&lt;br /&gt;
    let(:instructor) do&lt;br /&gt;
      Instructor.create(id: 1234, name: 'testinstructor', email: 'test@test.com', full_name: 'Test Instructor',&lt;br /&gt;
                        password: '123456', role_id: 2)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by name' do&lt;br /&gt;
      it 'returns users with matching names' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'testins', 'name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by fullname' do&lt;br /&gt;
      it 'returns users with matching fullnames' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'Test', 'full_name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'UnknownName', 'full_name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by email' do&lt;br /&gt;
      it 'returns users with matching emails' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'test@test.com', 'email')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown@test.com', 'email')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by default' do&lt;br /&gt;
      it 'returns users with names starting with the specified id' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(instructor.id, nil, nil)&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(9999, nil, nil) # Use an invalid user_id&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    context 'when searching by role' do&lt;br /&gt;
      it 'returns users with matching roles' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'admin', 'role')&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.role_id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'role')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:Expertiza_tests.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;br /&gt;
* [https://github.com/expertiza/reimplementation-back-end/pull/60/files#diff-a05d611b9fb47bcea66e3aaf6f25dc71d8da9976c5ccc991ea5171e938605644 GITHUB PR]&lt;br /&gt;
* [https://www.youtube.com/watch?v=_aCqMjRwW_U VIDEO DEMO 5min]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152861</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152861"/>
		<updated>2023-12-08T18:56:15Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* Response */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''list Action:'''&lt;br /&gt;
## The 'list' action will be reimplemented to handle listing of users.&lt;br /&gt;
## It will retrieve a list of users based on certain criteria, such as name, full name or email.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'list' and 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
==== ToDo Status: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done!&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|7&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `role` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;list and paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that list and paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that list and paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
The search_users method accepts three parameters: user_id, word, and search_by. If a valid user_id is provided, the method returns an array containing the user with that specific user_id.&lt;br /&gt;
In cases where user_id is empty or invalid, the method performs a search based on the provided word and search_by parameters.&lt;br /&gt;
To prevent SQL injection, the method validates search_by against a predefined set of acceptable fields, including 'name', 'full_name', 'email', and 'role'. The search is then conducted using a LIKE query on the specified field, ordering the results by the user's name.&lt;br /&gt;
&lt;br /&gt;
This flexible method accommodates various search scenarios, allowing users to query the system based on different criteria, enhancing the overall user experience.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
  def self.search_users(user_id, word, search_by)&lt;br /&gt;
    if user_id.present? &amp;amp;&amp;amp; (user = User.find_by(id: user_id))&lt;br /&gt;
      # If a valid user_id is provided, return the user with that specific user_id&lt;br /&gt;
      return [user]&lt;br /&gt;
    else&lt;br /&gt;
      # If user_id is empty or invalid, perform the search based on word and search_by parameters&lt;br /&gt;
&lt;br /&gt;
      # Validate search_by to avoid SQL injection&lt;br /&gt;
      valid_search_fields = %w[name full_name email role]&lt;br /&gt;
      search_by = valid_search_fields.include?(search_by) ? search_by : nil&lt;br /&gt;
&lt;br /&gt;
      # Perform the LIKE query on the specified field (name, full_name, email, role) and order by name&lt;br /&gt;
      if search_by.present?&lt;br /&gt;
        # Use search_by directly in the where clause&lt;br /&gt;
        users = User.joins(:role).where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name) if search_by == 'role'&lt;br /&gt;
        users ||= User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name)&lt;br /&gt;
        return users&lt;br /&gt;
      else&lt;br /&gt;
        # If search_by is not recognized, return an empty result&lt;br /&gt;
        return []&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The login method, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Attributes ====&lt;br /&gt;
&lt;br /&gt;
* '''name:''' The name of the user.&lt;br /&gt;
* '''full_name:''' The full name of the user, providing a comprehensive representation.&lt;br /&gt;
* '''email:''' The unique email address associated with each user, ensuring distinct identification.&lt;br /&gt;
* '''role:''' A relationship with the [[Role Model|Role model]], defining the user's role and access privileges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    user_id = params[:user_id]&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(user_id, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.present?&lt;br /&gt;
      # If the result is not empty, render the users as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is empty, render a message as JSON with a not found status&lt;br /&gt;
      render json: { error: 'User not found or no matching results' }, status: :not_found&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This method facilitates dynamic user searches, allowing users to query the system based on different parameters. Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
=== role Method ===&lt;br /&gt;
The role method provides the role of a specific user. It is used as a utility method in the controller.&lt;br /&gt;
  def role&lt;br /&gt;
    if @user&amp;amp;.role_id&lt;br /&gt;
      @role = Role.find(@user.role_id)&lt;br /&gt;
    elsif @user&lt;br /&gt;
      @role = Role.new(id: nil, name: '(none)')&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
=== find_by_login Method ===&lt;br /&gt;
The method provides the functionality to search user by email or name&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list Method ===&lt;br /&gt;
Method for displaying the list of users&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list and paginate_list Method ===&lt;br /&gt;
Method for filtering the users list with proper search and pagination.&lt;br /&gt;
  def list&lt;br /&gt;
    # code here&lt;br /&gt;
    letter = params[:letter]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
    # If search parameters present&lt;br /&gt;
    if letter.present? &amp;amp;&amp;amp; search_by.present?&lt;br /&gt;
      case search_by.to_i&lt;br /&gt;
      when 1 # Search by username&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('name LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 2 # Search by fullname&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('fullname LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 3 # Search by email&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('email LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      else&lt;br /&gt;
        @paginated_users = paginate_list&lt;br /&gt;
      end&lt;br /&gt;
    else # Display all users if no search parameters present&lt;br /&gt;
      @paginated_users = paginate_list&lt;br /&gt;
      if @paginated_users&lt;br /&gt;
        puts(&amp;quot;Not empty&amp;quot; + @paginated_users.to_s) else puts(&amp;quot;Empty&amp;quot;)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    render json: @paginated_users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def paginate_list&lt;br /&gt;
    paginate_options = { '1' =&amp;gt; 25, '2' =&amp;gt; 50, '3' =&amp;gt; 100 }&lt;br /&gt;
    # If the above hash does not have a value for the key,&lt;br /&gt;
    # it means that we need to show all the users on the page&lt;br /&gt;
    #&lt;br /&gt;
    # Just a point to remember, when we use pagination, the&lt;br /&gt;
    # 'users' variable should be an object, not an array&lt;br /&gt;
    # The type of condition for the search depends on what the user has selected from the search_by dropdown&lt;br /&gt;
    @search_by = params[:search_by]&lt;br /&gt;
    @per_page = params[:per_page] || 3&lt;br /&gt;
    # search for corresponding users&lt;br /&gt;
    # users = User.search_users(role, user_id, letter, @search_by)&lt;br /&gt;
    # paginate&lt;br /&gt;
    users = if paginate_options[@per_page.to_s].nil? # displaying all - no pagination&lt;br /&gt;
              User.all&lt;br /&gt;
            else # some pagination is active - use the per_page&lt;br /&gt;
              User.paginate(page: params[:page], per_page: paginate_options[@per_page.to_s])&lt;br /&gt;
            end&lt;br /&gt;
    # users = User.all&lt;br /&gt;
    users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Swagger API ===&lt;br /&gt;
&lt;br /&gt;
The image shows that we used Swagger to design and document our API application. Then, we exported the API data in JSON format to use Postman for development and testing. Postman is a tool that lets us send requests, check responses, write tests, and automate workflows for our API.&lt;br /&gt;
&lt;br /&gt;
[[File:Swagger2.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Postman Collection ===&lt;br /&gt;
&lt;br /&gt;
Postman is an all-encompassing tool that enables us to send requests, validate responses, construct tests, and automate workflows for our Application Programming Interface (API). We have imported the Swagger API and created a Postman collection; this helps us to share the progress in real-time, facilitating online modifications because it is a SaaS application. In addition, we have put together a team workspace, as shown in the attached image. This arrangement fosters collaborative work and ensures the efficiency and effectiveness of our API operations.&lt;br /&gt;
&lt;br /&gt;
[[File:postman2.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
&lt;br /&gt;
The endpoint search by Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id (optional)''': The ID of the requesting user. If provided, it is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/list&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''search_by (optional)''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
* '''letter (optional)''': The field to indicate the starting letter of the user's 'name', 'email', or 'full_name'.&lt;br /&gt;
* '''per_page  (optional)''': The field to indicate how many users to be displayed per page.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET api/v1/users/list?letter=n&amp;amp;search_by=1&amp;amp;per_page=3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;newuser&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;new_user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:List_api.png|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
require 'rails_helper'&lt;br /&gt;
&lt;br /&gt;
RSpec.describe User, type: :model do&lt;br /&gt;
&lt;br /&gt;
  describe '.find_by_login' do&lt;br /&gt;
    context 'when login is an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      it 'returns the user with the matching email' do&lt;br /&gt;
        result = User.find_by_login('test@test.com')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      it 'returns nil when user with email doesnt exist' do&lt;br /&gt;
        result = User.find_by_login('test@unknown.com')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when login is not an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      let!(:user2) { build(:user, name: 'testname', email: 'test@test2.com') }&lt;br /&gt;
      it 'returns the user with the matching name' do&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return first user with the matching name' do&lt;br /&gt;
        user2.save&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return nil when no user with matching name' do&lt;br /&gt;
        result = User.find_by_login('unknown')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  describe '.search_users' do&lt;br /&gt;
    # Creating dummy objects for the test with the help of let statement&lt;br /&gt;
    let(:role) { Role.create(name: 'Instructor', parent_id: nil, id: 2, default_page_id: nil) }&lt;br /&gt;
    let(:instructor) do&lt;br /&gt;
      Instructor.create(id: 1234, name: 'testinstructor', email: 'test@test.com', full_name: 'Test Instructor',&lt;br /&gt;
                        password: '123456', role_id: 2)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by name' do&lt;br /&gt;
      it 'returns users with matching names' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'testins', 'name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by fullname' do&lt;br /&gt;
      it 'returns users with matching fullnames' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'Test', 'full_name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'UnknownName', 'full_name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by email' do&lt;br /&gt;
      it 'returns users with matching emails' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'test@test.com', 'email')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown@test.com', 'email')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by default' do&lt;br /&gt;
      it 'returns users with names starting with the specified id' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(instructor.id, nil, nil)&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(9999, nil, nil) # Use an invalid user_id&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    context 'when searching by role' do&lt;br /&gt;
      it 'returns users with matching roles' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'admin', 'role')&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.role_id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'role')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:Expertiza_tests.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;br /&gt;
* [https://github.com/expertiza/reimplementation-back-end/pull/60/files#diff-a05d611b9fb47bcea66e3aaf6f25dc71d8da9976c5ccc991ea5171e938605644 GITHUB PR]&lt;br /&gt;
* [https://www.youtube.com/watch?v=_aCqMjRwW_U VIDEO DEMO 5min]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152860</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152860"/>
		<updated>2023-12-08T18:55:00Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* Description */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''list Action:'''&lt;br /&gt;
## The 'list' action will be reimplemented to handle listing of users.&lt;br /&gt;
## It will retrieve a list of users based on certain criteria, such as name, full name or email.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'list' and 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
==== ToDo Status: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done!&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|7&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `role` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;list and paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that list and paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that list and paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
The search_users method accepts three parameters: user_id, word, and search_by. If a valid user_id is provided, the method returns an array containing the user with that specific user_id.&lt;br /&gt;
In cases where user_id is empty or invalid, the method performs a search based on the provided word and search_by parameters.&lt;br /&gt;
To prevent SQL injection, the method validates search_by against a predefined set of acceptable fields, including 'name', 'full_name', 'email', and 'role'. The search is then conducted using a LIKE query on the specified field, ordering the results by the user's name.&lt;br /&gt;
&lt;br /&gt;
This flexible method accommodates various search scenarios, allowing users to query the system based on different criteria, enhancing the overall user experience.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
  def self.search_users(user_id, word, search_by)&lt;br /&gt;
    if user_id.present? &amp;amp;&amp;amp; (user = User.find_by(id: user_id))&lt;br /&gt;
      # If a valid user_id is provided, return the user with that specific user_id&lt;br /&gt;
      return [user]&lt;br /&gt;
    else&lt;br /&gt;
      # If user_id is empty or invalid, perform the search based on word and search_by parameters&lt;br /&gt;
&lt;br /&gt;
      # Validate search_by to avoid SQL injection&lt;br /&gt;
      valid_search_fields = %w[name full_name email role]&lt;br /&gt;
      search_by = valid_search_fields.include?(search_by) ? search_by : nil&lt;br /&gt;
&lt;br /&gt;
      # Perform the LIKE query on the specified field (name, full_name, email, role) and order by name&lt;br /&gt;
      if search_by.present?&lt;br /&gt;
        # Use search_by directly in the where clause&lt;br /&gt;
        users = User.joins(:role).where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name) if search_by == 'role'&lt;br /&gt;
        users ||= User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name)&lt;br /&gt;
        return users&lt;br /&gt;
      else&lt;br /&gt;
        # If search_by is not recognized, return an empty result&lt;br /&gt;
        return []&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The login method, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Attributes ====&lt;br /&gt;
&lt;br /&gt;
* '''name:''' The name of the user.&lt;br /&gt;
* '''full_name:''' The full name of the user, providing a comprehensive representation.&lt;br /&gt;
* '''email:''' The unique email address associated with each user, ensuring distinct identification.&lt;br /&gt;
* '''role:''' A relationship with the [[Role Model|Role model]], defining the user's role and access privileges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    user_id = params[:user_id]&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(user_id, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.present?&lt;br /&gt;
      # If the result is not empty, render the users as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is empty, render a message as JSON with a not found status&lt;br /&gt;
      render json: { error: 'User not found or no matching results' }, status: :not_found&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This method facilitates dynamic user searches, allowing users to query the system based on different parameters. Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
=== role Method ===&lt;br /&gt;
The role method provides the role of a specific user. It is used as a utility method in the controller.&lt;br /&gt;
  def role&lt;br /&gt;
    if @user&amp;amp;.role_id&lt;br /&gt;
      @role = Role.find(@user.role_id)&lt;br /&gt;
    elsif @user&lt;br /&gt;
      @role = Role.new(id: nil, name: '(none)')&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
=== find_by_login Method ===&lt;br /&gt;
The method provides the functionality to search user by email or name&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list Method ===&lt;br /&gt;
Method for displaying the list of users&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list and paginate_list Method ===&lt;br /&gt;
Method for filtering the users list with proper search and pagination.&lt;br /&gt;
  def list&lt;br /&gt;
    # code here&lt;br /&gt;
    letter = params[:letter]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
    # If search parameters present&lt;br /&gt;
    if letter.present? &amp;amp;&amp;amp; search_by.present?&lt;br /&gt;
      case search_by.to_i&lt;br /&gt;
      when 1 # Search by username&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('name LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 2 # Search by fullname&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('fullname LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 3 # Search by email&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('email LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      else&lt;br /&gt;
        @paginated_users = paginate_list&lt;br /&gt;
      end&lt;br /&gt;
    else # Display all users if no search parameters present&lt;br /&gt;
      @paginated_users = paginate_list&lt;br /&gt;
      if @paginated_users&lt;br /&gt;
        puts(&amp;quot;Not empty&amp;quot; + @paginated_users.to_s) else puts(&amp;quot;Empty&amp;quot;)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    render json: @paginated_users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def paginate_list&lt;br /&gt;
    paginate_options = { '1' =&amp;gt; 25, '2' =&amp;gt; 50, '3' =&amp;gt; 100 }&lt;br /&gt;
    # If the above hash does not have a value for the key,&lt;br /&gt;
    # it means that we need to show all the users on the page&lt;br /&gt;
    #&lt;br /&gt;
    # Just a point to remember, when we use pagination, the&lt;br /&gt;
    # 'users' variable should be an object, not an array&lt;br /&gt;
    # The type of condition for the search depends on what the user has selected from the search_by dropdown&lt;br /&gt;
    @search_by = params[:search_by]&lt;br /&gt;
    @per_page = params[:per_page] || 3&lt;br /&gt;
    # search for corresponding users&lt;br /&gt;
    # users = User.search_users(role, user_id, letter, @search_by)&lt;br /&gt;
    # paginate&lt;br /&gt;
    users = if paginate_options[@per_page.to_s].nil? # displaying all - no pagination&lt;br /&gt;
              User.all&lt;br /&gt;
            else # some pagination is active - use the per_page&lt;br /&gt;
              User.paginate(page: params[:page], per_page: paginate_options[@per_page.to_s])&lt;br /&gt;
            end&lt;br /&gt;
    # users = User.all&lt;br /&gt;
    users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Swagger API ===&lt;br /&gt;
&lt;br /&gt;
The image shows that we used Swagger to design and document our API application. Then, we exported the API data in JSON format to use Postman for development and testing. Postman is a tool that lets us send requests, check responses, write tests, and automate workflows for our API.&lt;br /&gt;
&lt;br /&gt;
[[File:Swagger2.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Postman Collection ===&lt;br /&gt;
&lt;br /&gt;
Postman is an all-encompassing tool that enables us to send requests, validate responses, construct tests, and automate workflows for our Application Programming Interface (API). We have imported the Swagger API and created a Postman collection; this helps us to share the progress in real-time, facilitating online modifications because it is a SaaS application. In addition, we have put together a team workspace, as shown in the attached image. This arrangement fosters collaborative work and ensures the efficiency and effectiveness of our API operations.&lt;br /&gt;
&lt;br /&gt;
[[File:postman2.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
&lt;br /&gt;
The endpoint search by Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id (optional)''': The ID of the requesting user. If provided, it is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field. If the requesting user is not a Super Administrator, it returns the user with the given user_id.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/list&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''search_by (optional)''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
* '''letter (optional)''': The field to indicate the starting letter of the user's 'name', 'email', or 'full_name'.&lt;br /&gt;
* '''per_page  (optional)''': The field to indicate how many users to be displayed per page.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET api/v1/users/list?letter=n&amp;amp;search_by=1&amp;amp;per_page=3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;newuser&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;new_user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:List_api.png|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
require 'rails_helper'&lt;br /&gt;
&lt;br /&gt;
RSpec.describe User, type: :model do&lt;br /&gt;
&lt;br /&gt;
  describe '.find_by_login' do&lt;br /&gt;
    context 'when login is an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      it 'returns the user with the matching email' do&lt;br /&gt;
        result = User.find_by_login('test@test.com')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      it 'returns nil when user with email doesnt exist' do&lt;br /&gt;
        result = User.find_by_login('test@unknown.com')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when login is not an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      let!(:user2) { build(:user, name: 'testname', email: 'test@test2.com') }&lt;br /&gt;
      it 'returns the user with the matching name' do&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return first user with the matching name' do&lt;br /&gt;
        user2.save&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return nil when no user with matching name' do&lt;br /&gt;
        result = User.find_by_login('unknown')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  describe '.search_users' do&lt;br /&gt;
    # Creating dummy objects for the test with the help of let statement&lt;br /&gt;
    let(:role) { Role.create(name: 'Instructor', parent_id: nil, id: 2, default_page_id: nil) }&lt;br /&gt;
    let(:instructor) do&lt;br /&gt;
      Instructor.create(id: 1234, name: 'testinstructor', email: 'test@test.com', full_name: 'Test Instructor',&lt;br /&gt;
                        password: '123456', role_id: 2)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by name' do&lt;br /&gt;
      it 'returns users with matching names' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'testins', 'name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by fullname' do&lt;br /&gt;
      it 'returns users with matching fullnames' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'Test', 'full_name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'UnknownName', 'full_name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by email' do&lt;br /&gt;
      it 'returns users with matching emails' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'test@test.com', 'email')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown@test.com', 'email')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by default' do&lt;br /&gt;
      it 'returns users with names starting with the specified id' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(instructor.id, nil, nil)&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(9999, nil, nil) # Use an invalid user_id&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    context 'when searching by role' do&lt;br /&gt;
      it 'returns users with matching roles' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'admin', 'role')&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.role_id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'role')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:Expertiza_tests.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;br /&gt;
* [https://github.com/expertiza/reimplementation-back-end/pull/60/files#diff-a05d611b9fb47bcea66e3aaf6f25dc71d8da9976c5ccc991ea5171e938605644 GITHUB PR]&lt;br /&gt;
* [https://www.youtube.com/watch?v=_aCqMjRwW_U VIDEO DEMO 5min]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152859</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152859"/>
		<updated>2023-12-08T18:21:45Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* Postman Collection */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''list Action:'''&lt;br /&gt;
## The 'list' action will be reimplemented to handle listing of users.&lt;br /&gt;
## It will retrieve a list of users based on certain criteria, such as name, full name or email.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'list' and 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
==== ToDo Status: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done!&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|7&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `role` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;list and paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that list and paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that list and paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
The search_users method accepts three parameters: user_id, word, and search_by. If a valid user_id is provided, the method returns an array containing the user with that specific user_id.&lt;br /&gt;
In cases where user_id is empty or invalid, the method performs a search based on the provided word and search_by parameters.&lt;br /&gt;
To prevent SQL injection, the method validates search_by against a predefined set of acceptable fields, including 'name', 'full_name', 'email', and 'role'. The search is then conducted using a LIKE query on the specified field, ordering the results by the user's name.&lt;br /&gt;
&lt;br /&gt;
This flexible method accommodates various search scenarios, allowing users to query the system based on different criteria, enhancing the overall user experience.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
  def self.search_users(user_id, word, search_by)&lt;br /&gt;
    if user_id.present? &amp;amp;&amp;amp; (user = User.find_by(id: user_id))&lt;br /&gt;
      # If a valid user_id is provided, return the user with that specific user_id&lt;br /&gt;
      return [user]&lt;br /&gt;
    else&lt;br /&gt;
      # If user_id is empty or invalid, perform the search based on word and search_by parameters&lt;br /&gt;
&lt;br /&gt;
      # Validate search_by to avoid SQL injection&lt;br /&gt;
      valid_search_fields = %w[name full_name email role]&lt;br /&gt;
      search_by = valid_search_fields.include?(search_by) ? search_by : nil&lt;br /&gt;
&lt;br /&gt;
      # Perform the LIKE query on the specified field (name, full_name, email, role) and order by name&lt;br /&gt;
      if search_by.present?&lt;br /&gt;
        # Use search_by directly in the where clause&lt;br /&gt;
        users = User.joins(:role).where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name) if search_by == 'role'&lt;br /&gt;
        users ||= User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name)&lt;br /&gt;
        return users&lt;br /&gt;
      else&lt;br /&gt;
        # If search_by is not recognized, return an empty result&lt;br /&gt;
        return []&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The login method, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Attributes ====&lt;br /&gt;
&lt;br /&gt;
* '''name:''' The name of the user.&lt;br /&gt;
* '''full_name:''' The full name of the user, providing a comprehensive representation.&lt;br /&gt;
* '''email:''' The unique email address associated with each user, ensuring distinct identification.&lt;br /&gt;
* '''role:''' A relationship with the [[Role Model|Role model]], defining the user's role and access privileges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    user_id = params[:user_id]&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(user_id, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.present?&lt;br /&gt;
      # If the result is not empty, render the users as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is empty, render a message as JSON with a not found status&lt;br /&gt;
      render json: { error: 'User not found or no matching results' }, status: :not_found&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This method facilitates dynamic user searches, allowing users to query the system based on different parameters. Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
=== role Method ===&lt;br /&gt;
The role method provides the role of a specific user. It is used as a utility method in the controller.&lt;br /&gt;
  def role&lt;br /&gt;
    if @user&amp;amp;.role_id&lt;br /&gt;
      @role = Role.find(@user.role_id)&lt;br /&gt;
    elsif @user&lt;br /&gt;
      @role = Role.new(id: nil, name: '(none)')&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
=== find_by_login Method ===&lt;br /&gt;
The method provides the functionality to search user by email or name&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list Method ===&lt;br /&gt;
Method for displaying the list of users&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list and paginate_list Method ===&lt;br /&gt;
Method for filtering the users list with proper search and pagination.&lt;br /&gt;
  def list&lt;br /&gt;
    # code here&lt;br /&gt;
    letter = params[:letter]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
    # If search parameters present&lt;br /&gt;
    if letter.present? &amp;amp;&amp;amp; search_by.present?&lt;br /&gt;
      case search_by.to_i&lt;br /&gt;
      when 1 # Search by username&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('name LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 2 # Search by fullname&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('fullname LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 3 # Search by email&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('email LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      else&lt;br /&gt;
        @paginated_users = paginate_list&lt;br /&gt;
      end&lt;br /&gt;
    else # Display all users if no search parameters present&lt;br /&gt;
      @paginated_users = paginate_list&lt;br /&gt;
      if @paginated_users&lt;br /&gt;
        puts(&amp;quot;Not empty&amp;quot; + @paginated_users.to_s) else puts(&amp;quot;Empty&amp;quot;)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    render json: @paginated_users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def paginate_list&lt;br /&gt;
    paginate_options = { '1' =&amp;gt; 25, '2' =&amp;gt; 50, '3' =&amp;gt; 100 }&lt;br /&gt;
    # If the above hash does not have a value for the key,&lt;br /&gt;
    # it means that we need to show all the users on the page&lt;br /&gt;
    #&lt;br /&gt;
    # Just a point to remember, when we use pagination, the&lt;br /&gt;
    # 'users' variable should be an object, not an array&lt;br /&gt;
    # The type of condition for the search depends on what the user has selected from the search_by dropdown&lt;br /&gt;
    @search_by = params[:search_by]&lt;br /&gt;
    @per_page = params[:per_page] || 3&lt;br /&gt;
    # search for corresponding users&lt;br /&gt;
    # users = User.search_users(role, user_id, letter, @search_by)&lt;br /&gt;
    # paginate&lt;br /&gt;
    users = if paginate_options[@per_page.to_s].nil? # displaying all - no pagination&lt;br /&gt;
              User.all&lt;br /&gt;
            else # some pagination is active - use the per_page&lt;br /&gt;
              User.paginate(page: params[:page], per_page: paginate_options[@per_page.to_s])&lt;br /&gt;
            end&lt;br /&gt;
    # users = User.all&lt;br /&gt;
    users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Swagger API ===&lt;br /&gt;
&lt;br /&gt;
The image shows that we used Swagger to design and document our API application. Then, we exported the API data in JSON format to use Postman for development and testing. Postman is a tool that lets us send requests, check responses, write tests, and automate workflows for our API.&lt;br /&gt;
&lt;br /&gt;
[[File:Swagger2.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Postman Collection ===&lt;br /&gt;
&lt;br /&gt;
Postman is an all-encompassing tool that enables us to send requests, validate responses, construct tests, and automate workflows for our Application Programming Interface (API). We have imported the Swagger API and created a Postman collection; this helps us to share the progress in real-time, facilitating online modifications because it is a SaaS application. In addition, we have put together a team workspace, as shown in the attached image. This arrangement fosters collaborative work and ensures the efficiency and effectiveness of our API operations.&lt;br /&gt;
&lt;br /&gt;
[[File:postman2.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
&lt;br /&gt;
This endpoint allows you to search for users based on various fields. If the requesting user is a Super Administrator, the search can include 'name', 'email', 'full_name', 'id', or 'role'. If the user is not a Super Administrator, the search is limited to the user with the provided user_id.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id (optional)''': The ID of the requesting user. If provided, it is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field. If the requesting user is not a Super Administrator, it returns the user with the given user_id.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/list&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''search_by (optional)''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
* '''letter (optional)''': The field to indicate the starting letter of the user's 'name', 'email', or 'full_name'.&lt;br /&gt;
* '''per_page  (optional)''': The field to indicate how many users to be displayed per page.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET api/v1/users/list?letter=n&amp;amp;search_by=1&amp;amp;per_page=3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;newuser&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;new_user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:List_api.png|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
require 'rails_helper'&lt;br /&gt;
&lt;br /&gt;
RSpec.describe User, type: :model do&lt;br /&gt;
&lt;br /&gt;
  describe '.find_by_login' do&lt;br /&gt;
    context 'when login is an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      it 'returns the user with the matching email' do&lt;br /&gt;
        result = User.find_by_login('test@test.com')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      it 'returns nil when user with email doesnt exist' do&lt;br /&gt;
        result = User.find_by_login('test@unknown.com')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when login is not an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      let!(:user2) { build(:user, name: 'testname', email: 'test@test2.com') }&lt;br /&gt;
      it 'returns the user with the matching name' do&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return first user with the matching name' do&lt;br /&gt;
        user2.save&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return nil when no user with matching name' do&lt;br /&gt;
        result = User.find_by_login('unknown')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  describe '.search_users' do&lt;br /&gt;
    # Creating dummy objects for the test with the help of let statement&lt;br /&gt;
    let(:role) { Role.create(name: 'Instructor', parent_id: nil, id: 2, default_page_id: nil) }&lt;br /&gt;
    let(:instructor) do&lt;br /&gt;
      Instructor.create(id: 1234, name: 'testinstructor', email: 'test@test.com', full_name: 'Test Instructor',&lt;br /&gt;
                        password: '123456', role_id: 2)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by name' do&lt;br /&gt;
      it 'returns users with matching names' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'testins', 'name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by fullname' do&lt;br /&gt;
      it 'returns users with matching fullnames' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'Test', 'full_name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'UnknownName', 'full_name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by email' do&lt;br /&gt;
      it 'returns users with matching emails' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'test@test.com', 'email')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown@test.com', 'email')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by default' do&lt;br /&gt;
      it 'returns users with names starting with the specified id' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(instructor.id, nil, nil)&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(9999, nil, nil) # Use an invalid user_id&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    context 'when searching by role' do&lt;br /&gt;
      it 'returns users with matching roles' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'admin', 'role')&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.role_id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'role')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:Expertiza_tests.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;br /&gt;
* [https://github.com/expertiza/reimplementation-back-end/pull/60/files#diff-a05d611b9fb47bcea66e3aaf6f25dc71d8da9976c5ccc991ea5171e938605644 GITHUB PR]&lt;br /&gt;
* [https://www.youtube.com/watch?v=_aCqMjRwW_U VIDEO DEMO 5min]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Postman2.png&amp;diff=152858</id>
		<title>File:Postman2.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Postman2.png&amp;diff=152858"/>
		<updated>2023-12-08T18:21:12Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152857</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152857"/>
		<updated>2023-12-08T18:20:55Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* Swagger API */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''list Action:'''&lt;br /&gt;
## The 'list' action will be reimplemented to handle listing of users.&lt;br /&gt;
## It will retrieve a list of users based on certain criteria, such as name, full name or email.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'list' and 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
==== ToDo Status: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done!&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|7&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `role` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;list and paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that list and paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that list and paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
The search_users method accepts three parameters: user_id, word, and search_by. If a valid user_id is provided, the method returns an array containing the user with that specific user_id.&lt;br /&gt;
In cases where user_id is empty or invalid, the method performs a search based on the provided word and search_by parameters.&lt;br /&gt;
To prevent SQL injection, the method validates search_by against a predefined set of acceptable fields, including 'name', 'full_name', 'email', and 'role'. The search is then conducted using a LIKE query on the specified field, ordering the results by the user's name.&lt;br /&gt;
&lt;br /&gt;
This flexible method accommodates various search scenarios, allowing users to query the system based on different criteria, enhancing the overall user experience.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
  def self.search_users(user_id, word, search_by)&lt;br /&gt;
    if user_id.present? &amp;amp;&amp;amp; (user = User.find_by(id: user_id))&lt;br /&gt;
      # If a valid user_id is provided, return the user with that specific user_id&lt;br /&gt;
      return [user]&lt;br /&gt;
    else&lt;br /&gt;
      # If user_id is empty or invalid, perform the search based on word and search_by parameters&lt;br /&gt;
&lt;br /&gt;
      # Validate search_by to avoid SQL injection&lt;br /&gt;
      valid_search_fields = %w[name full_name email role]&lt;br /&gt;
      search_by = valid_search_fields.include?(search_by) ? search_by : nil&lt;br /&gt;
&lt;br /&gt;
      # Perform the LIKE query on the specified field (name, full_name, email, role) and order by name&lt;br /&gt;
      if search_by.present?&lt;br /&gt;
        # Use search_by directly in the where clause&lt;br /&gt;
        users = User.joins(:role).where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name) if search_by == 'role'&lt;br /&gt;
        users ||= User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name)&lt;br /&gt;
        return users&lt;br /&gt;
      else&lt;br /&gt;
        # If search_by is not recognized, return an empty result&lt;br /&gt;
        return []&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The login method, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Attributes ====&lt;br /&gt;
&lt;br /&gt;
* '''name:''' The name of the user.&lt;br /&gt;
* '''full_name:''' The full name of the user, providing a comprehensive representation.&lt;br /&gt;
* '''email:''' The unique email address associated with each user, ensuring distinct identification.&lt;br /&gt;
* '''role:''' A relationship with the [[Role Model|Role model]], defining the user's role and access privileges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    user_id = params[:user_id]&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(user_id, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.present?&lt;br /&gt;
      # If the result is not empty, render the users as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is empty, render a message as JSON with a not found status&lt;br /&gt;
      render json: { error: 'User not found or no matching results' }, status: :not_found&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This method facilitates dynamic user searches, allowing users to query the system based on different parameters. Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
=== role Method ===&lt;br /&gt;
The role method provides the role of a specific user. It is used as a utility method in the controller.&lt;br /&gt;
  def role&lt;br /&gt;
    if @user&amp;amp;.role_id&lt;br /&gt;
      @role = Role.find(@user.role_id)&lt;br /&gt;
    elsif @user&lt;br /&gt;
      @role = Role.new(id: nil, name: '(none)')&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
=== find_by_login Method ===&lt;br /&gt;
The method provides the functionality to search user by email or name&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list Method ===&lt;br /&gt;
Method for displaying the list of users&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list and paginate_list Method ===&lt;br /&gt;
Method for filtering the users list with proper search and pagination.&lt;br /&gt;
  def list&lt;br /&gt;
    # code here&lt;br /&gt;
    letter = params[:letter]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
    # If search parameters present&lt;br /&gt;
    if letter.present? &amp;amp;&amp;amp; search_by.present?&lt;br /&gt;
      case search_by.to_i&lt;br /&gt;
      when 1 # Search by username&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('name LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 2 # Search by fullname&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('fullname LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 3 # Search by email&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('email LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      else&lt;br /&gt;
        @paginated_users = paginate_list&lt;br /&gt;
      end&lt;br /&gt;
    else # Display all users if no search parameters present&lt;br /&gt;
      @paginated_users = paginate_list&lt;br /&gt;
      if @paginated_users&lt;br /&gt;
        puts(&amp;quot;Not empty&amp;quot; + @paginated_users.to_s) else puts(&amp;quot;Empty&amp;quot;)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    render json: @paginated_users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def paginate_list&lt;br /&gt;
    paginate_options = { '1' =&amp;gt; 25, '2' =&amp;gt; 50, '3' =&amp;gt; 100 }&lt;br /&gt;
    # If the above hash does not have a value for the key,&lt;br /&gt;
    # it means that we need to show all the users on the page&lt;br /&gt;
    #&lt;br /&gt;
    # Just a point to remember, when we use pagination, the&lt;br /&gt;
    # 'users' variable should be an object, not an array&lt;br /&gt;
    # The type of condition for the search depends on what the user has selected from the search_by dropdown&lt;br /&gt;
    @search_by = params[:search_by]&lt;br /&gt;
    @per_page = params[:per_page] || 3&lt;br /&gt;
    # search for corresponding users&lt;br /&gt;
    # users = User.search_users(role, user_id, letter, @search_by)&lt;br /&gt;
    # paginate&lt;br /&gt;
    users = if paginate_options[@per_page.to_s].nil? # displaying all - no pagination&lt;br /&gt;
              User.all&lt;br /&gt;
            else # some pagination is active - use the per_page&lt;br /&gt;
              User.paginate(page: params[:page], per_page: paginate_options[@per_page.to_s])&lt;br /&gt;
            end&lt;br /&gt;
    # users = User.all&lt;br /&gt;
    users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Swagger API ===&lt;br /&gt;
&lt;br /&gt;
The image shows that we used Swagger to design and document our API application. Then, we exported the API data in JSON format to use Postman for development and testing. Postman is a tool that lets us send requests, check responses, write tests, and automate workflows for our API.&lt;br /&gt;
&lt;br /&gt;
[[File:Swagger2.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Postman Collection ===&lt;br /&gt;
&lt;br /&gt;
Postman is an all-encompassing tool that enables us to send requests, validate responses, construct tests, and automate workflows for our Application Programming Interface (API). We have imported the Swagger API and created a Postman collection; this helps us to share the progress in real-time, facilitating online modifications because it is a SaaS application. In addition, we have put together a team workspace, as shown in the attached image. This arrangement fosters collaborative work and ensures the efficiency and effectiveness of our API operations.&lt;br /&gt;
&lt;br /&gt;
[[File:Postman2.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
&lt;br /&gt;
This endpoint allows you to search for users based on various fields. If the requesting user is a Super Administrator, the search can include 'name', 'email', 'full_name', 'id', or 'role'. If the user is not a Super Administrator, the search is limited to the user with the provided user_id.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id (optional)''': The ID of the requesting user. If provided, it is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field. If the requesting user is not a Super Administrator, it returns the user with the given user_id.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/list&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''search_by (optional)''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
* '''letter (optional)''': The field to indicate the starting letter of the user's 'name', 'email', or 'full_name'.&lt;br /&gt;
* '''per_page  (optional)''': The field to indicate how many users to be displayed per page.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET api/v1/users/list?letter=n&amp;amp;search_by=1&amp;amp;per_page=3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;newuser&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;new_user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:List_api.png|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
require 'rails_helper'&lt;br /&gt;
&lt;br /&gt;
RSpec.describe User, type: :model do&lt;br /&gt;
&lt;br /&gt;
  describe '.find_by_login' do&lt;br /&gt;
    context 'when login is an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      it 'returns the user with the matching email' do&lt;br /&gt;
        result = User.find_by_login('test@test.com')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      it 'returns nil when user with email doesnt exist' do&lt;br /&gt;
        result = User.find_by_login('test@unknown.com')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when login is not an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      let!(:user2) { build(:user, name: 'testname', email: 'test@test2.com') }&lt;br /&gt;
      it 'returns the user with the matching name' do&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return first user with the matching name' do&lt;br /&gt;
        user2.save&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return nil when no user with matching name' do&lt;br /&gt;
        result = User.find_by_login('unknown')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  describe '.search_users' do&lt;br /&gt;
    # Creating dummy objects for the test with the help of let statement&lt;br /&gt;
    let(:role) { Role.create(name: 'Instructor', parent_id: nil, id: 2, default_page_id: nil) }&lt;br /&gt;
    let(:instructor) do&lt;br /&gt;
      Instructor.create(id: 1234, name: 'testinstructor', email: 'test@test.com', full_name: 'Test Instructor',&lt;br /&gt;
                        password: '123456', role_id: 2)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by name' do&lt;br /&gt;
      it 'returns users with matching names' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'testins', 'name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by fullname' do&lt;br /&gt;
      it 'returns users with matching fullnames' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'Test', 'full_name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'UnknownName', 'full_name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by email' do&lt;br /&gt;
      it 'returns users with matching emails' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'test@test.com', 'email')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown@test.com', 'email')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by default' do&lt;br /&gt;
      it 'returns users with names starting with the specified id' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(instructor.id, nil, nil)&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(9999, nil, nil) # Use an invalid user_id&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    context 'when searching by role' do&lt;br /&gt;
      it 'returns users with matching roles' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'admin', 'role')&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.role_id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'role')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:Expertiza_tests.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;br /&gt;
* [https://github.com/expertiza/reimplementation-back-end/pull/60/files#diff-a05d611b9fb47bcea66e3aaf6f25dc71d8da9976c5ccc991ea5171e938605644 GITHUB PR]&lt;br /&gt;
* [https://www.youtube.com/watch?v=_aCqMjRwW_U VIDEO DEMO 5min]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152856</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152856"/>
		<updated>2023-12-08T18:10:13Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* Swagger API */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''list Action:'''&lt;br /&gt;
## The 'list' action will be reimplemented to handle listing of users.&lt;br /&gt;
## It will retrieve a list of users based on certain criteria, such as name, full name or email.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'list' and 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
==== ToDo Status: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done!&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|7&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `role` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;list and paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that list and paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that list and paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
The search_users method accepts three parameters: user_id, word, and search_by. If a valid user_id is provided, the method returns an array containing the user with that specific user_id.&lt;br /&gt;
In cases where user_id is empty or invalid, the method performs a search based on the provided word and search_by parameters.&lt;br /&gt;
To prevent SQL injection, the method validates search_by against a predefined set of acceptable fields, including 'name', 'full_name', 'email', and 'role'. The search is then conducted using a LIKE query on the specified field, ordering the results by the user's name.&lt;br /&gt;
&lt;br /&gt;
This flexible method accommodates various search scenarios, allowing users to query the system based on different criteria, enhancing the overall user experience.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
  def self.search_users(user_id, word, search_by)&lt;br /&gt;
    if user_id.present? &amp;amp;&amp;amp; (user = User.find_by(id: user_id))&lt;br /&gt;
      # If a valid user_id is provided, return the user with that specific user_id&lt;br /&gt;
      return [user]&lt;br /&gt;
    else&lt;br /&gt;
      # If user_id is empty or invalid, perform the search based on word and search_by parameters&lt;br /&gt;
&lt;br /&gt;
      # Validate search_by to avoid SQL injection&lt;br /&gt;
      valid_search_fields = %w[name full_name email role]&lt;br /&gt;
      search_by = valid_search_fields.include?(search_by) ? search_by : nil&lt;br /&gt;
&lt;br /&gt;
      # Perform the LIKE query on the specified field (name, full_name, email, role) and order by name&lt;br /&gt;
      if search_by.present?&lt;br /&gt;
        # Use search_by directly in the where clause&lt;br /&gt;
        users = User.joins(:role).where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name) if search_by == 'role'&lt;br /&gt;
        users ||= User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name)&lt;br /&gt;
        return users&lt;br /&gt;
      else&lt;br /&gt;
        # If search_by is not recognized, return an empty result&lt;br /&gt;
        return []&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The login method, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Attributes ====&lt;br /&gt;
&lt;br /&gt;
* '''name:''' The name of the user.&lt;br /&gt;
* '''full_name:''' The full name of the user, providing a comprehensive representation.&lt;br /&gt;
* '''email:''' The unique email address associated with each user, ensuring distinct identification.&lt;br /&gt;
* '''role:''' A relationship with the [[Role Model|Role model]], defining the user's role and access privileges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    user_id = params[:user_id]&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(user_id, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.present?&lt;br /&gt;
      # If the result is not empty, render the users as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is empty, render a message as JSON with a not found status&lt;br /&gt;
      render json: { error: 'User not found or no matching results' }, status: :not_found&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This method facilitates dynamic user searches, allowing users to query the system based on different parameters. Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
=== role Method ===&lt;br /&gt;
The role method provides the role of a specific user. It is used as a utility method in the controller.&lt;br /&gt;
  def role&lt;br /&gt;
    if @user&amp;amp;.role_id&lt;br /&gt;
      @role = Role.find(@user.role_id)&lt;br /&gt;
    elsif @user&lt;br /&gt;
      @role = Role.new(id: nil, name: '(none)')&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
=== find_by_login Method ===&lt;br /&gt;
The method provides the functionality to search user by email or name&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list Method ===&lt;br /&gt;
Method for displaying the list of users&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list and paginate_list Method ===&lt;br /&gt;
Method for filtering the users list with proper search and pagination.&lt;br /&gt;
  def list&lt;br /&gt;
    # code here&lt;br /&gt;
    letter = params[:letter]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
    # If search parameters present&lt;br /&gt;
    if letter.present? &amp;amp;&amp;amp; search_by.present?&lt;br /&gt;
      case search_by.to_i&lt;br /&gt;
      when 1 # Search by username&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('name LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 2 # Search by fullname&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('fullname LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 3 # Search by email&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('email LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      else&lt;br /&gt;
        @paginated_users = paginate_list&lt;br /&gt;
      end&lt;br /&gt;
    else # Display all users if no search parameters present&lt;br /&gt;
      @paginated_users = paginate_list&lt;br /&gt;
      if @paginated_users&lt;br /&gt;
        puts(&amp;quot;Not empty&amp;quot; + @paginated_users.to_s) else puts(&amp;quot;Empty&amp;quot;)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    render json: @paginated_users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def paginate_list&lt;br /&gt;
    paginate_options = { '1' =&amp;gt; 25, '2' =&amp;gt; 50, '3' =&amp;gt; 100 }&lt;br /&gt;
    # If the above hash does not have a value for the key,&lt;br /&gt;
    # it means that we need to show all the users on the page&lt;br /&gt;
    #&lt;br /&gt;
    # Just a point to remember, when we use pagination, the&lt;br /&gt;
    # 'users' variable should be an object, not an array&lt;br /&gt;
    # The type of condition for the search depends on what the user has selected from the search_by dropdown&lt;br /&gt;
    @search_by = params[:search_by]&lt;br /&gt;
    @per_page = params[:per_page] || 3&lt;br /&gt;
    # search for corresponding users&lt;br /&gt;
    # users = User.search_users(role, user_id, letter, @search_by)&lt;br /&gt;
    # paginate&lt;br /&gt;
    users = if paginate_options[@per_page.to_s].nil? # displaying all - no pagination&lt;br /&gt;
              User.all&lt;br /&gt;
            else # some pagination is active - use the per_page&lt;br /&gt;
              User.paginate(page: params[:page], per_page: paginate_options[@per_page.to_s])&lt;br /&gt;
            end&lt;br /&gt;
    # users = User.all&lt;br /&gt;
    users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Swagger API ===&lt;br /&gt;
&lt;br /&gt;
As shown in the image, we used Swagger to design and document our API application. Then, we exported the API data in JSON format to use Postman for development and testing. Postman is a tool that lets us send requests, check responses, write tests, and automate workflows for our API.&lt;br /&gt;
&lt;br /&gt;
[[File:Swagger2.png|1024px]]&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
&lt;br /&gt;
This endpoint allows you to search for users based on various fields. If the requesting user is a Super Administrator, the search can include 'name', 'email', 'full_name', 'id', or 'role'. If the user is not a Super Administrator, the search is limited to the user with the provided user_id.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id (optional)''': The ID of the requesting user. If provided, it is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field. If the requesting user is not a Super Administrator, it returns the user with the given user_id.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/list&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''search_by (optional)''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
* '''letter (optional)''': The field to indicate the starting letter of the user's 'name', 'email', or 'full_name'.&lt;br /&gt;
* '''per_page  (optional)''': The field to indicate how many users to be displayed per page.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET api/v1/users/list?letter=n&amp;amp;search_by=1&amp;amp;per_page=3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;newuser&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;new_user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:List_api.png|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
require 'rails_helper'&lt;br /&gt;
&lt;br /&gt;
RSpec.describe User, type: :model do&lt;br /&gt;
&lt;br /&gt;
  describe '.find_by_login' do&lt;br /&gt;
    context 'when login is an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      it 'returns the user with the matching email' do&lt;br /&gt;
        result = User.find_by_login('test@test.com')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      it 'returns nil when user with email doesnt exist' do&lt;br /&gt;
        result = User.find_by_login('test@unknown.com')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when login is not an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      let!(:user2) { build(:user, name: 'testname', email: 'test@test2.com') }&lt;br /&gt;
      it 'returns the user with the matching name' do&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return first user with the matching name' do&lt;br /&gt;
        user2.save&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return nil when no user with matching name' do&lt;br /&gt;
        result = User.find_by_login('unknown')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  describe '.search_users' do&lt;br /&gt;
    # Creating dummy objects for the test with the help of let statement&lt;br /&gt;
    let(:role) { Role.create(name: 'Instructor', parent_id: nil, id: 2, default_page_id: nil) }&lt;br /&gt;
    let(:instructor) do&lt;br /&gt;
      Instructor.create(id: 1234, name: 'testinstructor', email: 'test@test.com', full_name: 'Test Instructor',&lt;br /&gt;
                        password: '123456', role_id: 2)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by name' do&lt;br /&gt;
      it 'returns users with matching names' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'testins', 'name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by fullname' do&lt;br /&gt;
      it 'returns users with matching fullnames' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'Test', 'full_name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'UnknownName', 'full_name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by email' do&lt;br /&gt;
      it 'returns users with matching emails' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'test@test.com', 'email')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown@test.com', 'email')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by default' do&lt;br /&gt;
      it 'returns users with names starting with the specified id' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(instructor.id, nil, nil)&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(9999, nil, nil) # Use an invalid user_id&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    context 'when searching by role' do&lt;br /&gt;
      it 'returns users with matching roles' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'admin', 'role')&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.role_id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'role')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:Expertiza_tests.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;br /&gt;
* [https://github.com/expertiza/reimplementation-back-end/pull/60/files#diff-a05d611b9fb47bcea66e3aaf6f25dc71d8da9976c5ccc991ea5171e938605644 GITHUB PR]&lt;br /&gt;
* [https://www.youtube.com/watch?v=_aCqMjRwW_U VIDEO DEMO 5min]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152855</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152855"/>
		<updated>2023-12-08T18:09:52Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* Swagger API */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''list Action:'''&lt;br /&gt;
## The 'list' action will be reimplemented to handle listing of users.&lt;br /&gt;
## It will retrieve a list of users based on certain criteria, such as name, full name or email.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'list' and 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
==== ToDo Status: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done!&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|7&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `role` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;list and paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that list and paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that list and paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
The search_users method accepts three parameters: user_id, word, and search_by. If a valid user_id is provided, the method returns an array containing the user with that specific user_id.&lt;br /&gt;
In cases where user_id is empty or invalid, the method performs a search based on the provided word and search_by parameters.&lt;br /&gt;
To prevent SQL injection, the method validates search_by against a predefined set of acceptable fields, including 'name', 'full_name', 'email', and 'role'. The search is then conducted using a LIKE query on the specified field, ordering the results by the user's name.&lt;br /&gt;
&lt;br /&gt;
This flexible method accommodates various search scenarios, allowing users to query the system based on different criteria, enhancing the overall user experience.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
  def self.search_users(user_id, word, search_by)&lt;br /&gt;
    if user_id.present? &amp;amp;&amp;amp; (user = User.find_by(id: user_id))&lt;br /&gt;
      # If a valid user_id is provided, return the user with that specific user_id&lt;br /&gt;
      return [user]&lt;br /&gt;
    else&lt;br /&gt;
      # If user_id is empty or invalid, perform the search based on word and search_by parameters&lt;br /&gt;
&lt;br /&gt;
      # Validate search_by to avoid SQL injection&lt;br /&gt;
      valid_search_fields = %w[name full_name email role]&lt;br /&gt;
      search_by = valid_search_fields.include?(search_by) ? search_by : nil&lt;br /&gt;
&lt;br /&gt;
      # Perform the LIKE query on the specified field (name, full_name, email, role) and order by name&lt;br /&gt;
      if search_by.present?&lt;br /&gt;
        # Use search_by directly in the where clause&lt;br /&gt;
        users = User.joins(:role).where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name) if search_by == 'role'&lt;br /&gt;
        users ||= User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name)&lt;br /&gt;
        return users&lt;br /&gt;
      else&lt;br /&gt;
        # If search_by is not recognized, return an empty result&lt;br /&gt;
        return []&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The login method, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Attributes ====&lt;br /&gt;
&lt;br /&gt;
* '''name:''' The name of the user.&lt;br /&gt;
* '''full_name:''' The full name of the user, providing a comprehensive representation.&lt;br /&gt;
* '''email:''' The unique email address associated with each user, ensuring distinct identification.&lt;br /&gt;
* '''role:''' A relationship with the [[Role Model|Role model]], defining the user's role and access privileges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    user_id = params[:user_id]&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(user_id, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.present?&lt;br /&gt;
      # If the result is not empty, render the users as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is empty, render a message as JSON with a not found status&lt;br /&gt;
      render json: { error: 'User not found or no matching results' }, status: :not_found&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This method facilitates dynamic user searches, allowing users to query the system based on different parameters. Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
=== role Method ===&lt;br /&gt;
The role method provides the role of a specific user. It is used as a utility method in the controller.&lt;br /&gt;
  def role&lt;br /&gt;
    if @user&amp;amp;.role_id&lt;br /&gt;
      @role = Role.find(@user.role_id)&lt;br /&gt;
    elsif @user&lt;br /&gt;
      @role = Role.new(id: nil, name: '(none)')&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
=== find_by_login Method ===&lt;br /&gt;
The method provides the functionality to search user by email or name&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list Method ===&lt;br /&gt;
Method for displaying the list of users&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list and paginate_list Method ===&lt;br /&gt;
Method for filtering the users list with proper search and pagination.&lt;br /&gt;
  def list&lt;br /&gt;
    # code here&lt;br /&gt;
    letter = params[:letter]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
    # If search parameters present&lt;br /&gt;
    if letter.present? &amp;amp;&amp;amp; search_by.present?&lt;br /&gt;
      case search_by.to_i&lt;br /&gt;
      when 1 # Search by username&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('name LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 2 # Search by fullname&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('fullname LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 3 # Search by email&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('email LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      else&lt;br /&gt;
        @paginated_users = paginate_list&lt;br /&gt;
      end&lt;br /&gt;
    else # Display all users if no search parameters present&lt;br /&gt;
      @paginated_users = paginate_list&lt;br /&gt;
      if @paginated_users&lt;br /&gt;
        puts(&amp;quot;Not empty&amp;quot; + @paginated_users.to_s) else puts(&amp;quot;Empty&amp;quot;)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    render json: @paginated_users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def paginate_list&lt;br /&gt;
    paginate_options = { '1' =&amp;gt; 25, '2' =&amp;gt; 50, '3' =&amp;gt; 100 }&lt;br /&gt;
    # If the above hash does not have a value for the key,&lt;br /&gt;
    # it means that we need to show all the users on the page&lt;br /&gt;
    #&lt;br /&gt;
    # Just a point to remember, when we use pagination, the&lt;br /&gt;
    # 'users' variable should be an object, not an array&lt;br /&gt;
    # The type of condition for the search depends on what the user has selected from the search_by dropdown&lt;br /&gt;
    @search_by = params[:search_by]&lt;br /&gt;
    @per_page = params[:per_page] || 3&lt;br /&gt;
    # search for corresponding users&lt;br /&gt;
    # users = User.search_users(role, user_id, letter, @search_by)&lt;br /&gt;
    # paginate&lt;br /&gt;
    users = if paginate_options[@per_page.to_s].nil? # displaying all - no pagination&lt;br /&gt;
              User.all&lt;br /&gt;
            else # some pagination is active - use the per_page&lt;br /&gt;
              User.paginate(page: params[:page], per_page: paginate_options[@per_page.to_s])&lt;br /&gt;
            end&lt;br /&gt;
    # users = User.all&lt;br /&gt;
    users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Swagger API ===&lt;br /&gt;
&lt;br /&gt;
As shown in the image, we used Swagger to design and document our API application. Then, we exported the API data in JSON format to use Postman for development and testing. Postman is a tool that lets us send requests, check responses, write tests, and automate workflows for our API.&lt;br /&gt;
&lt;br /&gt;
[[File:Swagger2.png|800px]]&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
&lt;br /&gt;
This endpoint allows you to search for users based on various fields. If the requesting user is a Super Administrator, the search can include 'name', 'email', 'full_name', 'id', or 'role'. If the user is not a Super Administrator, the search is limited to the user with the provided user_id.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id (optional)''': The ID of the requesting user. If provided, it is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field. If the requesting user is not a Super Administrator, it returns the user with the given user_id.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/list&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''search_by (optional)''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
* '''letter (optional)''': The field to indicate the starting letter of the user's 'name', 'email', or 'full_name'.&lt;br /&gt;
* '''per_page  (optional)''': The field to indicate how many users to be displayed per page.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET api/v1/users/list?letter=n&amp;amp;search_by=1&amp;amp;per_page=3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;newuser&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;new_user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:List_api.png|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
require 'rails_helper'&lt;br /&gt;
&lt;br /&gt;
RSpec.describe User, type: :model do&lt;br /&gt;
&lt;br /&gt;
  describe '.find_by_login' do&lt;br /&gt;
    context 'when login is an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      it 'returns the user with the matching email' do&lt;br /&gt;
        result = User.find_by_login('test@test.com')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      it 'returns nil when user with email doesnt exist' do&lt;br /&gt;
        result = User.find_by_login('test@unknown.com')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when login is not an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      let!(:user2) { build(:user, name: 'testname', email: 'test@test2.com') }&lt;br /&gt;
      it 'returns the user with the matching name' do&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return first user with the matching name' do&lt;br /&gt;
        user2.save&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return nil when no user with matching name' do&lt;br /&gt;
        result = User.find_by_login('unknown')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  describe '.search_users' do&lt;br /&gt;
    # Creating dummy objects for the test with the help of let statement&lt;br /&gt;
    let(:role) { Role.create(name: 'Instructor', parent_id: nil, id: 2, default_page_id: nil) }&lt;br /&gt;
    let(:instructor) do&lt;br /&gt;
      Instructor.create(id: 1234, name: 'testinstructor', email: 'test@test.com', full_name: 'Test Instructor',&lt;br /&gt;
                        password: '123456', role_id: 2)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by name' do&lt;br /&gt;
      it 'returns users with matching names' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'testins', 'name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by fullname' do&lt;br /&gt;
      it 'returns users with matching fullnames' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'Test', 'full_name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'UnknownName', 'full_name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by email' do&lt;br /&gt;
      it 'returns users with matching emails' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'test@test.com', 'email')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown@test.com', 'email')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by default' do&lt;br /&gt;
      it 'returns users with names starting with the specified id' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(instructor.id, nil, nil)&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(9999, nil, nil) # Use an invalid user_id&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    context 'when searching by role' do&lt;br /&gt;
      it 'returns users with matching roles' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'admin', 'role')&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.role_id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'role')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:Expertiza_tests.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;br /&gt;
* [https://github.com/expertiza/reimplementation-back-end/pull/60/files#diff-a05d611b9fb47bcea66e3aaf6f25dc71d8da9976c5ccc991ea5171e938605644 GITHUB PR]&lt;br /&gt;
* [https://www.youtube.com/watch?v=_aCqMjRwW_U VIDEO DEMO 5min]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152854</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152854"/>
		<updated>2023-12-08T18:08:45Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* Swagger API */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''list Action:'''&lt;br /&gt;
## The 'list' action will be reimplemented to handle listing of users.&lt;br /&gt;
## It will retrieve a list of users based on certain criteria, such as name, full name or email.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'list' and 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
==== ToDo Status: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done!&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|7&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `role` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;list and paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that list and paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that list and paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
The search_users method accepts three parameters: user_id, word, and search_by. If a valid user_id is provided, the method returns an array containing the user with that specific user_id.&lt;br /&gt;
In cases where user_id is empty or invalid, the method performs a search based on the provided word and search_by parameters.&lt;br /&gt;
To prevent SQL injection, the method validates search_by against a predefined set of acceptable fields, including 'name', 'full_name', 'email', and 'role'. The search is then conducted using a LIKE query on the specified field, ordering the results by the user's name.&lt;br /&gt;
&lt;br /&gt;
This flexible method accommodates various search scenarios, allowing users to query the system based on different criteria, enhancing the overall user experience.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
  def self.search_users(user_id, word, search_by)&lt;br /&gt;
    if user_id.present? &amp;amp;&amp;amp; (user = User.find_by(id: user_id))&lt;br /&gt;
      # If a valid user_id is provided, return the user with that specific user_id&lt;br /&gt;
      return [user]&lt;br /&gt;
    else&lt;br /&gt;
      # If user_id is empty or invalid, perform the search based on word and search_by parameters&lt;br /&gt;
&lt;br /&gt;
      # Validate search_by to avoid SQL injection&lt;br /&gt;
      valid_search_fields = %w[name full_name email role]&lt;br /&gt;
      search_by = valid_search_fields.include?(search_by) ? search_by : nil&lt;br /&gt;
&lt;br /&gt;
      # Perform the LIKE query on the specified field (name, full_name, email, role) and order by name&lt;br /&gt;
      if search_by.present?&lt;br /&gt;
        # Use search_by directly in the where clause&lt;br /&gt;
        users = User.joins(:role).where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name) if search_by == 'role'&lt;br /&gt;
        users ||= User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name)&lt;br /&gt;
        return users&lt;br /&gt;
      else&lt;br /&gt;
        # If search_by is not recognized, return an empty result&lt;br /&gt;
        return []&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The login method, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Attributes ====&lt;br /&gt;
&lt;br /&gt;
* '''name:''' The name of the user.&lt;br /&gt;
* '''full_name:''' The full name of the user, providing a comprehensive representation.&lt;br /&gt;
* '''email:''' The unique email address associated with each user, ensuring distinct identification.&lt;br /&gt;
* '''role:''' A relationship with the [[Role Model|Role model]], defining the user's role and access privileges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    user_id = params[:user_id]&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(user_id, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.present?&lt;br /&gt;
      # If the result is not empty, render the users as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is empty, render a message as JSON with a not found status&lt;br /&gt;
      render json: { error: 'User not found or no matching results' }, status: :not_found&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This method facilitates dynamic user searches, allowing users to query the system based on different parameters. Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
=== role Method ===&lt;br /&gt;
The role method provides the role of a specific user. It is used as a utility method in the controller.&lt;br /&gt;
  def role&lt;br /&gt;
    if @user&amp;amp;.role_id&lt;br /&gt;
      @role = Role.find(@user.role_id)&lt;br /&gt;
    elsif @user&lt;br /&gt;
      @role = Role.new(id: nil, name: '(none)')&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
=== find_by_login Method ===&lt;br /&gt;
The method provides the functionality to search user by email or name&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list Method ===&lt;br /&gt;
Method for displaying the list of users&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list and paginate_list Method ===&lt;br /&gt;
Method for filtering the users list with proper search and pagination.&lt;br /&gt;
  def list&lt;br /&gt;
    # code here&lt;br /&gt;
    letter = params[:letter]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
    # If search parameters present&lt;br /&gt;
    if letter.present? &amp;amp;&amp;amp; search_by.present?&lt;br /&gt;
      case search_by.to_i&lt;br /&gt;
      when 1 # Search by username&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('name LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 2 # Search by fullname&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('fullname LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 3 # Search by email&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('email LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      else&lt;br /&gt;
        @paginated_users = paginate_list&lt;br /&gt;
      end&lt;br /&gt;
    else # Display all users if no search parameters present&lt;br /&gt;
      @paginated_users = paginate_list&lt;br /&gt;
      if @paginated_users&lt;br /&gt;
        puts(&amp;quot;Not empty&amp;quot; + @paginated_users.to_s) else puts(&amp;quot;Empty&amp;quot;)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    render json: @paginated_users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def paginate_list&lt;br /&gt;
    paginate_options = { '1' =&amp;gt; 25, '2' =&amp;gt; 50, '3' =&amp;gt; 100 }&lt;br /&gt;
    # If the above hash does not have a value for the key,&lt;br /&gt;
    # it means that we need to show all the users on the page&lt;br /&gt;
    #&lt;br /&gt;
    # Just a point to remember, when we use pagination, the&lt;br /&gt;
    # 'users' variable should be an object, not an array&lt;br /&gt;
    # The type of condition for the search depends on what the user has selected from the search_by dropdown&lt;br /&gt;
    @search_by = params[:search_by]&lt;br /&gt;
    @per_page = params[:per_page] || 3&lt;br /&gt;
    # search for corresponding users&lt;br /&gt;
    # users = User.search_users(role, user_id, letter, @search_by)&lt;br /&gt;
    # paginate&lt;br /&gt;
    users = if paginate_options[@per_page.to_s].nil? # displaying all - no pagination&lt;br /&gt;
              User.all&lt;br /&gt;
            else # some pagination is active - use the per_page&lt;br /&gt;
              User.paginate(page: params[:page], per_page: paginate_options[@per_page.to_s])&lt;br /&gt;
            end&lt;br /&gt;
    # users = User.all&lt;br /&gt;
    users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Swagger API ===&lt;br /&gt;
&lt;br /&gt;
As shown in the image, we used Swagger to design and document our API application. Then, we exported the API data in JSON format to use Postman for development and testing. Postman is a tool that lets us send requests, check responses, write tests, and automate workflows for our API.&lt;br /&gt;
&lt;br /&gt;
[[File:swagger2.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
&lt;br /&gt;
This endpoint allows you to search for users based on various fields. If the requesting user is a Super Administrator, the search can include 'name', 'email', 'full_name', 'id', or 'role'. If the user is not a Super Administrator, the search is limited to the user with the provided user_id.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id (optional)''': The ID of the requesting user. If provided, it is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field. If the requesting user is not a Super Administrator, it returns the user with the given user_id.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/list&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''search_by (optional)''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
* '''letter (optional)''': The field to indicate the starting letter of the user's 'name', 'email', or 'full_name'.&lt;br /&gt;
* '''per_page  (optional)''': The field to indicate how many users to be displayed per page.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET api/v1/users/list?letter=n&amp;amp;search_by=1&amp;amp;per_page=3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;newuser&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;new_user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:List_api.png|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
require 'rails_helper'&lt;br /&gt;
&lt;br /&gt;
RSpec.describe User, type: :model do&lt;br /&gt;
&lt;br /&gt;
  describe '.find_by_login' do&lt;br /&gt;
    context 'when login is an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      it 'returns the user with the matching email' do&lt;br /&gt;
        result = User.find_by_login('test@test.com')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      it 'returns nil when user with email doesnt exist' do&lt;br /&gt;
        result = User.find_by_login('test@unknown.com')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when login is not an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      let!(:user2) { build(:user, name: 'testname', email: 'test@test2.com') }&lt;br /&gt;
      it 'returns the user with the matching name' do&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return first user with the matching name' do&lt;br /&gt;
        user2.save&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return nil when no user with matching name' do&lt;br /&gt;
        result = User.find_by_login('unknown')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  describe '.search_users' do&lt;br /&gt;
    # Creating dummy objects for the test with the help of let statement&lt;br /&gt;
    let(:role) { Role.create(name: 'Instructor', parent_id: nil, id: 2, default_page_id: nil) }&lt;br /&gt;
    let(:instructor) do&lt;br /&gt;
      Instructor.create(id: 1234, name: 'testinstructor', email: 'test@test.com', full_name: 'Test Instructor',&lt;br /&gt;
                        password: '123456', role_id: 2)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by name' do&lt;br /&gt;
      it 'returns users with matching names' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'testins', 'name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by fullname' do&lt;br /&gt;
      it 'returns users with matching fullnames' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'Test', 'full_name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'UnknownName', 'full_name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by email' do&lt;br /&gt;
      it 'returns users with matching emails' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'test@test.com', 'email')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown@test.com', 'email')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by default' do&lt;br /&gt;
      it 'returns users with names starting with the specified id' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(instructor.id, nil, nil)&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(9999, nil, nil) # Use an invalid user_id&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    context 'when searching by role' do&lt;br /&gt;
      it 'returns users with matching roles' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'admin', 'role')&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.role_id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'role')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:Expertiza_tests.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;br /&gt;
* [https://github.com/expertiza/reimplementation-back-end/pull/60/files#diff-a05d611b9fb47bcea66e3aaf6f25dc71d8da9976c5ccc991ea5171e938605644 GITHUB PR]&lt;br /&gt;
* [https://www.youtube.com/watch?v=_aCqMjRwW_U VIDEO DEMO 5min]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152853</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152853"/>
		<updated>2023-12-08T18:08:14Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* Swagger API */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''list Action:'''&lt;br /&gt;
## The 'list' action will be reimplemented to handle listing of users.&lt;br /&gt;
## It will retrieve a list of users based on certain criteria, such as name, full name or email.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'list' and 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
==== ToDo Status: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done!&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|7&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `role` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;list and paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that list and paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that list and paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
The search_users method accepts three parameters: user_id, word, and search_by. If a valid user_id is provided, the method returns an array containing the user with that specific user_id.&lt;br /&gt;
In cases where user_id is empty or invalid, the method performs a search based on the provided word and search_by parameters.&lt;br /&gt;
To prevent SQL injection, the method validates search_by against a predefined set of acceptable fields, including 'name', 'full_name', 'email', and 'role'. The search is then conducted using a LIKE query on the specified field, ordering the results by the user's name.&lt;br /&gt;
&lt;br /&gt;
This flexible method accommodates various search scenarios, allowing users to query the system based on different criteria, enhancing the overall user experience.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
  def self.search_users(user_id, word, search_by)&lt;br /&gt;
    if user_id.present? &amp;amp;&amp;amp; (user = User.find_by(id: user_id))&lt;br /&gt;
      # If a valid user_id is provided, return the user with that specific user_id&lt;br /&gt;
      return [user]&lt;br /&gt;
    else&lt;br /&gt;
      # If user_id is empty or invalid, perform the search based on word and search_by parameters&lt;br /&gt;
&lt;br /&gt;
      # Validate search_by to avoid SQL injection&lt;br /&gt;
      valid_search_fields = %w[name full_name email role]&lt;br /&gt;
      search_by = valid_search_fields.include?(search_by) ? search_by : nil&lt;br /&gt;
&lt;br /&gt;
      # Perform the LIKE query on the specified field (name, full_name, email, role) and order by name&lt;br /&gt;
      if search_by.present?&lt;br /&gt;
        # Use search_by directly in the where clause&lt;br /&gt;
        users = User.joins(:role).where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name) if search_by == 'role'&lt;br /&gt;
        users ||= User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name)&lt;br /&gt;
        return users&lt;br /&gt;
      else&lt;br /&gt;
        # If search_by is not recognized, return an empty result&lt;br /&gt;
        return []&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The login method, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Attributes ====&lt;br /&gt;
&lt;br /&gt;
* '''name:''' The name of the user.&lt;br /&gt;
* '''full_name:''' The full name of the user, providing a comprehensive representation.&lt;br /&gt;
* '''email:''' The unique email address associated with each user, ensuring distinct identification.&lt;br /&gt;
* '''role:''' A relationship with the [[Role Model|Role model]], defining the user's role and access privileges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    user_id = params[:user_id]&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(user_id, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.present?&lt;br /&gt;
      # If the result is not empty, render the users as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is empty, render a message as JSON with a not found status&lt;br /&gt;
      render json: { error: 'User not found or no matching results' }, status: :not_found&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This method facilitates dynamic user searches, allowing users to query the system based on different parameters. Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
=== role Method ===&lt;br /&gt;
The role method provides the role of a specific user. It is used as a utility method in the controller.&lt;br /&gt;
  def role&lt;br /&gt;
    if @user&amp;amp;.role_id&lt;br /&gt;
      @role = Role.find(@user.role_id)&lt;br /&gt;
    elsif @user&lt;br /&gt;
      @role = Role.new(id: nil, name: '(none)')&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
=== find_by_login Method ===&lt;br /&gt;
The method provides the functionality to search user by email or name&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list Method ===&lt;br /&gt;
Method for displaying the list of users&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list and paginate_list Method ===&lt;br /&gt;
Method for filtering the users list with proper search and pagination.&lt;br /&gt;
  def list&lt;br /&gt;
    # code here&lt;br /&gt;
    letter = params[:letter]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
    # If search parameters present&lt;br /&gt;
    if letter.present? &amp;amp;&amp;amp; search_by.present?&lt;br /&gt;
      case search_by.to_i&lt;br /&gt;
      when 1 # Search by username&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('name LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 2 # Search by fullname&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('fullname LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 3 # Search by email&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('email LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      else&lt;br /&gt;
        @paginated_users = paginate_list&lt;br /&gt;
      end&lt;br /&gt;
    else # Display all users if no search parameters present&lt;br /&gt;
      @paginated_users = paginate_list&lt;br /&gt;
      if @paginated_users&lt;br /&gt;
        puts(&amp;quot;Not empty&amp;quot; + @paginated_users.to_s) else puts(&amp;quot;Empty&amp;quot;)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    render json: @paginated_users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def paginate_list&lt;br /&gt;
    paginate_options = { '1' =&amp;gt; 25, '2' =&amp;gt; 50, '3' =&amp;gt; 100 }&lt;br /&gt;
    # If the above hash does not have a value for the key,&lt;br /&gt;
    # it means that we need to show all the users on the page&lt;br /&gt;
    #&lt;br /&gt;
    # Just a point to remember, when we use pagination, the&lt;br /&gt;
    # 'users' variable should be an object, not an array&lt;br /&gt;
    # The type of condition for the search depends on what the user has selected from the search_by dropdown&lt;br /&gt;
    @search_by = params[:search_by]&lt;br /&gt;
    @per_page = params[:per_page] || 3&lt;br /&gt;
    # search for corresponding users&lt;br /&gt;
    # users = User.search_users(role, user_id, letter, @search_by)&lt;br /&gt;
    # paginate&lt;br /&gt;
    users = if paginate_options[@per_page.to_s].nil? # displaying all - no pagination&lt;br /&gt;
              User.all&lt;br /&gt;
            else # some pagination is active - use the per_page&lt;br /&gt;
              User.paginate(page: params[:page], per_page: paginate_options[@per_page.to_s])&lt;br /&gt;
            end&lt;br /&gt;
    # users = User.all&lt;br /&gt;
    users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Swagger API ===&lt;br /&gt;
&lt;br /&gt;
As shown in the image, we used Swagger to design and document our API application. Then, we exported the API data in JSON format to use Postman for development and testing. Postman is a tool that lets us send requests, check responses, write tests, and automate workflows for our API.&lt;br /&gt;
&lt;br /&gt;
[File: swagger.jpg]&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
&lt;br /&gt;
This endpoint allows you to search for users based on various fields. If the requesting user is a Super Administrator, the search can include 'name', 'email', 'full_name', 'id', or 'role'. If the user is not a Super Administrator, the search is limited to the user with the provided user_id.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id (optional)''': The ID of the requesting user. If provided, it is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field. If the requesting user is not a Super Administrator, it returns the user with the given user_id.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/list&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''search_by (optional)''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
* '''letter (optional)''': The field to indicate the starting letter of the user's 'name', 'email', or 'full_name'.&lt;br /&gt;
* '''per_page  (optional)''': The field to indicate how many users to be displayed per page.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET api/v1/users/list?letter=n&amp;amp;search_by=1&amp;amp;per_page=3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;newuser&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;new_user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:List_api.png|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
require 'rails_helper'&lt;br /&gt;
&lt;br /&gt;
RSpec.describe User, type: :model do&lt;br /&gt;
&lt;br /&gt;
  describe '.find_by_login' do&lt;br /&gt;
    context 'when login is an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      it 'returns the user with the matching email' do&lt;br /&gt;
        result = User.find_by_login('test@test.com')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      it 'returns nil when user with email doesnt exist' do&lt;br /&gt;
        result = User.find_by_login('test@unknown.com')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when login is not an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      let!(:user2) { build(:user, name: 'testname', email: 'test@test2.com') }&lt;br /&gt;
      it 'returns the user with the matching name' do&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return first user with the matching name' do&lt;br /&gt;
        user2.save&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return nil when no user with matching name' do&lt;br /&gt;
        result = User.find_by_login('unknown')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  describe '.search_users' do&lt;br /&gt;
    # Creating dummy objects for the test with the help of let statement&lt;br /&gt;
    let(:role) { Role.create(name: 'Instructor', parent_id: nil, id: 2, default_page_id: nil) }&lt;br /&gt;
    let(:instructor) do&lt;br /&gt;
      Instructor.create(id: 1234, name: 'testinstructor', email: 'test@test.com', full_name: 'Test Instructor',&lt;br /&gt;
                        password: '123456', role_id: 2)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by name' do&lt;br /&gt;
      it 'returns users with matching names' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'testins', 'name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by fullname' do&lt;br /&gt;
      it 'returns users with matching fullnames' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'Test', 'full_name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'UnknownName', 'full_name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by email' do&lt;br /&gt;
      it 'returns users with matching emails' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'test@test.com', 'email')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown@test.com', 'email')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by default' do&lt;br /&gt;
      it 'returns users with names starting with the specified id' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(instructor.id, nil, nil)&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(9999, nil, nil) # Use an invalid user_id&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    context 'when searching by role' do&lt;br /&gt;
      it 'returns users with matching roles' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'admin', 'role')&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.role_id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'role')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:Expertiza_tests.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;br /&gt;
* [https://github.com/expertiza/reimplementation-back-end/pull/60/files#diff-a05d611b9fb47bcea66e3aaf6f25dc71d8da9976c5ccc991ea5171e938605644 GITHUB PR]&lt;br /&gt;
* [https://www.youtube.com/watch?v=_aCqMjRwW_U VIDEO DEMO 5min]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Swagger2.png&amp;diff=152852</id>
		<title>File:Swagger2.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Swagger2.png&amp;diff=152852"/>
		<updated>2023-12-08T18:07:40Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152851</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152851"/>
		<updated>2023-12-08T18:06:33Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* API Documentation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''list Action:'''&lt;br /&gt;
## The 'list' action will be reimplemented to handle listing of users.&lt;br /&gt;
## It will retrieve a list of users based on certain criteria, such as name, full name or email.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'list' and 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
==== ToDo Status: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done!&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|7&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `role` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;list and paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that list and paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that list and paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
The search_users method accepts three parameters: user_id, word, and search_by. If a valid user_id is provided, the method returns an array containing the user with that specific user_id.&lt;br /&gt;
In cases where user_id is empty or invalid, the method performs a search based on the provided word and search_by parameters.&lt;br /&gt;
To prevent SQL injection, the method validates search_by against a predefined set of acceptable fields, including 'name', 'full_name', 'email', and 'role'. The search is then conducted using a LIKE query on the specified field, ordering the results by the user's name.&lt;br /&gt;
&lt;br /&gt;
This flexible method accommodates various search scenarios, allowing users to query the system based on different criteria, enhancing the overall user experience.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
  def self.search_users(user_id, word, search_by)&lt;br /&gt;
    if user_id.present? &amp;amp;&amp;amp; (user = User.find_by(id: user_id))&lt;br /&gt;
      # If a valid user_id is provided, return the user with that specific user_id&lt;br /&gt;
      return [user]&lt;br /&gt;
    else&lt;br /&gt;
      # If user_id is empty or invalid, perform the search based on word and search_by parameters&lt;br /&gt;
&lt;br /&gt;
      # Validate search_by to avoid SQL injection&lt;br /&gt;
      valid_search_fields = %w[name full_name email role]&lt;br /&gt;
      search_by = valid_search_fields.include?(search_by) ? search_by : nil&lt;br /&gt;
&lt;br /&gt;
      # Perform the LIKE query on the specified field (name, full_name, email, role) and order by name&lt;br /&gt;
      if search_by.present?&lt;br /&gt;
        # Use search_by directly in the where clause&lt;br /&gt;
        users = User.joins(:role).where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name) if search_by == 'role'&lt;br /&gt;
        users ||= User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name)&lt;br /&gt;
        return users&lt;br /&gt;
      else&lt;br /&gt;
        # If search_by is not recognized, return an empty result&lt;br /&gt;
        return []&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The login method, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Attributes ====&lt;br /&gt;
&lt;br /&gt;
* '''name:''' The name of the user.&lt;br /&gt;
* '''full_name:''' The full name of the user, providing a comprehensive representation.&lt;br /&gt;
* '''email:''' The unique email address associated with each user, ensuring distinct identification.&lt;br /&gt;
* '''role:''' A relationship with the [[Role Model|Role model]], defining the user's role and access privileges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    user_id = params[:user_id]&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(user_id, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.present?&lt;br /&gt;
      # If the result is not empty, render the users as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is empty, render a message as JSON with a not found status&lt;br /&gt;
      render json: { error: 'User not found or no matching results' }, status: :not_found&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This method facilitates dynamic user searches, allowing users to query the system based on different parameters. Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
=== role Method ===&lt;br /&gt;
The role method provides the role of a specific user. It is used as a utility method in the controller.&lt;br /&gt;
  def role&lt;br /&gt;
    if @user&amp;amp;.role_id&lt;br /&gt;
      @role = Role.find(@user.role_id)&lt;br /&gt;
    elsif @user&lt;br /&gt;
      @role = Role.new(id: nil, name: '(none)')&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
=== find_by_login Method ===&lt;br /&gt;
The method provides the functionality to search user by email or name&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list Method ===&lt;br /&gt;
Method for displaying the list of users&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list and paginate_list Method ===&lt;br /&gt;
Method for filtering the users list with proper search and pagination.&lt;br /&gt;
  def list&lt;br /&gt;
    # code here&lt;br /&gt;
    letter = params[:letter]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
    # If search parameters present&lt;br /&gt;
    if letter.present? &amp;amp;&amp;amp; search_by.present?&lt;br /&gt;
      case search_by.to_i&lt;br /&gt;
      when 1 # Search by username&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('name LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 2 # Search by fullname&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('fullname LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 3 # Search by email&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('email LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      else&lt;br /&gt;
        @paginated_users = paginate_list&lt;br /&gt;
      end&lt;br /&gt;
    else # Display all users if no search parameters present&lt;br /&gt;
      @paginated_users = paginate_list&lt;br /&gt;
      if @paginated_users&lt;br /&gt;
        puts(&amp;quot;Not empty&amp;quot; + @paginated_users.to_s) else puts(&amp;quot;Empty&amp;quot;)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    render json: @paginated_users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def paginate_list&lt;br /&gt;
    paginate_options = { '1' =&amp;gt; 25, '2' =&amp;gt; 50, '3' =&amp;gt; 100 }&lt;br /&gt;
    # If the above hash does not have a value for the key,&lt;br /&gt;
    # it means that we need to show all the users on the page&lt;br /&gt;
    #&lt;br /&gt;
    # Just a point to remember, when we use pagination, the&lt;br /&gt;
    # 'users' variable should be an object, not an array&lt;br /&gt;
    # The type of condition for the search depends on what the user has selected from the search_by dropdown&lt;br /&gt;
    @search_by = params[:search_by]&lt;br /&gt;
    @per_page = params[:per_page] || 3&lt;br /&gt;
    # search for corresponding users&lt;br /&gt;
    # users = User.search_users(role, user_id, letter, @search_by)&lt;br /&gt;
    # paginate&lt;br /&gt;
    users = if paginate_options[@per_page.to_s].nil? # displaying all - no pagination&lt;br /&gt;
              User.all&lt;br /&gt;
            else # some pagination is active - use the per_page&lt;br /&gt;
              User.paginate(page: params[:page], per_page: paginate_options[@per_page.to_s])&lt;br /&gt;
            end&lt;br /&gt;
    # users = User.all&lt;br /&gt;
    users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Swagger API ===&lt;br /&gt;
&lt;br /&gt;
As shown in the image, we used Swagger to design and document our API application. Then, we exported the API data in JSON format to use Postman for development and testing. Postman is a tool that lets us send requests, check responses, write tests, and automate workflows for our API.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
&lt;br /&gt;
This endpoint allows you to search for users based on various fields. If the requesting user is a Super Administrator, the search can include 'name', 'email', 'full_name', 'id', or 'role'. If the user is not a Super Administrator, the search is limited to the user with the provided user_id.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id (optional)''': The ID of the requesting user. If provided, it is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field. If the requesting user is not a Super Administrator, it returns the user with the given user_id.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/list&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''search_by (optional)''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
* '''letter (optional)''': The field to indicate the starting letter of the user's 'name', 'email', or 'full_name'.&lt;br /&gt;
* '''per_page  (optional)''': The field to indicate how many users to be displayed per page.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET api/v1/users/list?letter=n&amp;amp;search_by=1&amp;amp;per_page=3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;newuser&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;new_user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:List_api.png|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
require 'rails_helper'&lt;br /&gt;
&lt;br /&gt;
RSpec.describe User, type: :model do&lt;br /&gt;
&lt;br /&gt;
  describe '.find_by_login' do&lt;br /&gt;
    context 'when login is an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      it 'returns the user with the matching email' do&lt;br /&gt;
        result = User.find_by_login('test@test.com')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      it 'returns nil when user with email doesnt exist' do&lt;br /&gt;
        result = User.find_by_login('test@unknown.com')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when login is not an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      let!(:user2) { build(:user, name: 'testname', email: 'test@test2.com') }&lt;br /&gt;
      it 'returns the user with the matching name' do&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return first user with the matching name' do&lt;br /&gt;
        user2.save&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return nil when no user with matching name' do&lt;br /&gt;
        result = User.find_by_login('unknown')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  describe '.search_users' do&lt;br /&gt;
    # Creating dummy objects for the test with the help of let statement&lt;br /&gt;
    let(:role) { Role.create(name: 'Instructor', parent_id: nil, id: 2, default_page_id: nil) }&lt;br /&gt;
    let(:instructor) do&lt;br /&gt;
      Instructor.create(id: 1234, name: 'testinstructor', email: 'test@test.com', full_name: 'Test Instructor',&lt;br /&gt;
                        password: '123456', role_id: 2)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by name' do&lt;br /&gt;
      it 'returns users with matching names' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'testins', 'name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by fullname' do&lt;br /&gt;
      it 'returns users with matching fullnames' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'Test', 'full_name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'UnknownName', 'full_name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by email' do&lt;br /&gt;
      it 'returns users with matching emails' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'test@test.com', 'email')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown@test.com', 'email')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by default' do&lt;br /&gt;
      it 'returns users with names starting with the specified id' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(instructor.id, nil, nil)&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(9999, nil, nil) # Use an invalid user_id&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    context 'when searching by role' do&lt;br /&gt;
      it 'returns users with matching roles' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'admin', 'role')&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.role_id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'role')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:Expertiza_tests.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;br /&gt;
* [https://github.com/expertiza/reimplementation-back-end/pull/60/files#diff-a05d611b9fb47bcea66e3aaf6f25dc71d8da9976c5ccc991ea5171e938605644 GITHUB PR]&lt;br /&gt;
* [https://www.youtube.com/watch?v=_aCqMjRwW_U VIDEO DEMO 5min]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152486</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152486"/>
		<updated>2023-12-05T02:50:23Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* RSpec Tests: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''list Action:'''&lt;br /&gt;
## The 'list' action will be reimplemented to handle listing of users.&lt;br /&gt;
## It will retrieve a list of users based on certain criteria, such as name, full name or email.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'list' and 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
==== ToDo Status: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done!&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|7&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `role` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;list and paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that list and paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that list and paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
The search_users method accepts three parameters: user_id, word, and search_by. If a valid user_id is provided, the method returns an array containing the user with that specific user_id.&lt;br /&gt;
In cases where user_id is empty or invalid, the method performs a search based on the provided word and search_by parameters.&lt;br /&gt;
To prevent SQL injection, the method validates search_by against a predefined set of acceptable fields, including 'name', 'full_name', 'email', and 'role'. The search is then conducted using a LIKE query on the specified field, ordering the results by the user's name.&lt;br /&gt;
&lt;br /&gt;
This flexible method accommodates various search scenarios, allowing users to query the system based on different criteria, enhancing the overall user experience.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
  def self.search_users(user_id, word, search_by)&lt;br /&gt;
    if user_id.present? &amp;amp;&amp;amp; (user = User.find_by(id: user_id))&lt;br /&gt;
      # If a valid user_id is provided, return the user with that specific user_id&lt;br /&gt;
      return [user]&lt;br /&gt;
    else&lt;br /&gt;
      # If user_id is empty or invalid, perform the search based on word and search_by parameters&lt;br /&gt;
&lt;br /&gt;
      # Validate search_by to avoid SQL injection&lt;br /&gt;
      valid_search_fields = %w[name full_name email role]&lt;br /&gt;
      search_by = valid_search_fields.include?(search_by) ? search_by : nil&lt;br /&gt;
&lt;br /&gt;
      # Perform the LIKE query on the specified field (name, full_name, email, role) and order by name&lt;br /&gt;
      if search_by.present?&lt;br /&gt;
        # Use search_by directly in the where clause&lt;br /&gt;
        users = User.joins(:role).where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name) if search_by == 'role'&lt;br /&gt;
        users ||= User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name)&lt;br /&gt;
        return users&lt;br /&gt;
      else&lt;br /&gt;
        # If search_by is not recognized, return an empty result&lt;br /&gt;
        return []&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The login method, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Attributes ====&lt;br /&gt;
&lt;br /&gt;
* '''name:''' The name of the user.&lt;br /&gt;
* '''full_name:''' The full name of the user, providing a comprehensive representation.&lt;br /&gt;
* '''email:''' The unique email address associated with each user, ensuring distinct identification.&lt;br /&gt;
* '''role:''' A relationship with the [[Role Model|Role model]], defining the user's role and access privileges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    user_id = params[:user_id]&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(user_id, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.present?&lt;br /&gt;
      # If the result is not empty, render the users as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is empty, render a message as JSON with a not found status&lt;br /&gt;
      render json: { error: 'User not found or no matching results' }, status: :not_found&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This method facilitates dynamic user searches, allowing users to query the system based on different parameters. Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
=== role Method ===&lt;br /&gt;
The role method provides the role of a specific user. It is used as a utility method in the controller.&lt;br /&gt;
  def role&lt;br /&gt;
    if @user&amp;amp;.role_id&lt;br /&gt;
      @role = Role.find(@user.role_id)&lt;br /&gt;
    elsif @user&lt;br /&gt;
      @role = Role.new(id: nil, name: '(none)')&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
=== find_by_login Method ===&lt;br /&gt;
The method provides the functionality to search user by email or name&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list Method ===&lt;br /&gt;
Method for displaying the list of users&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list and paginate_list Method ===&lt;br /&gt;
Method for filtering the users list with proper search and pagination.&lt;br /&gt;
  def list&lt;br /&gt;
    # code here&lt;br /&gt;
    letter = params[:letter]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
    # If search parameters present&lt;br /&gt;
    if letter.present? &amp;amp;&amp;amp; search_by.present?&lt;br /&gt;
      case search_by.to_i&lt;br /&gt;
      when 1 # Search by username&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('name LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 2 # Search by fullname&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('fullname LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 3 # Search by email&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('email LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      else&lt;br /&gt;
        @paginated_users = paginate_list&lt;br /&gt;
      end&lt;br /&gt;
    else # Display all users if no search parameters present&lt;br /&gt;
      @paginated_users = paginate_list&lt;br /&gt;
      if @paginated_users&lt;br /&gt;
        puts(&amp;quot;Not empty&amp;quot; + @paginated_users.to_s) else puts(&amp;quot;Empty&amp;quot;)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    render json: @paginated_users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def paginate_list&lt;br /&gt;
    paginate_options = { '1' =&amp;gt; 25, '2' =&amp;gt; 50, '3' =&amp;gt; 100 }&lt;br /&gt;
    # If the above hash does not have a value for the key,&lt;br /&gt;
    # it means that we need to show all the users on the page&lt;br /&gt;
    #&lt;br /&gt;
    # Just a point to remember, when we use pagination, the&lt;br /&gt;
    # 'users' variable should be an object, not an array&lt;br /&gt;
    # The type of condition for the search depends on what the user has selected from the search_by dropdown&lt;br /&gt;
    @search_by = params[:search_by]&lt;br /&gt;
    @per_page = params[:per_page] || 3&lt;br /&gt;
    # search for corresponding users&lt;br /&gt;
    # users = User.search_users(role, user_id, letter, @search_by)&lt;br /&gt;
    # paginate&lt;br /&gt;
    users = if paginate_options[@per_page.to_s].nil? # displaying all - no pagination&lt;br /&gt;
              User.all&lt;br /&gt;
            else # some pagination is active - use the per_page&lt;br /&gt;
              User.paginate(page: params[:page], per_page: paginate_options[@per_page.to_s])&lt;br /&gt;
            end&lt;br /&gt;
    # users = User.all&lt;br /&gt;
    users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
&lt;br /&gt;
This endpoint allows you to search for users based on various fields. If the requesting user is a Super Administrator, the search can include 'name', 'email', 'full_name', 'id', or 'role'. If the user is not a Super Administrator, the search is limited to the user with the provided user_id.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id (optional)''': The ID of the requesting user. If provided, it is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field. If the requesting user is not a Super Administrator, it returns the user with the given user_id.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/list&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''search_by (optional)''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
* '''letter (optional)''': The field to indicate the starting letter of the user's 'name', 'email', or 'full_name'.&lt;br /&gt;
* '''per_page  (optional)''': The field to indicate how many users to be displayed per page.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET api/v1/users/list?letter=n&amp;amp;search_by=1&amp;amp;per_page=3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;newuser&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;new_user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:List_api.png|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
require 'rails_helper'&lt;br /&gt;
&lt;br /&gt;
RSpec.describe User, type: :model do&lt;br /&gt;
&lt;br /&gt;
  describe '.find_by_login' do&lt;br /&gt;
    context 'when login is an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      it 'returns the user with the matching email' do&lt;br /&gt;
        result = User.find_by_login('test@test.com')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      it 'returns nil when user with email doesnt exist' do&lt;br /&gt;
        result = User.find_by_login('test@unknown.com')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when login is not an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      let!(:user2) { build(:user, name: 'testname', email: 'test@test2.com') }&lt;br /&gt;
      it 'returns the user with the matching name' do&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return first user with the matching name' do&lt;br /&gt;
        user2.save&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return nil when no user with matching name' do&lt;br /&gt;
        result = User.find_by_login('unknown')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  describe '.search_users' do&lt;br /&gt;
    # Creating dummy objects for the test with the help of let statement&lt;br /&gt;
    let(:role) { Role.create(name: 'Instructor', parent_id: nil, id: 2, default_page_id: nil) }&lt;br /&gt;
    let(:instructor) do&lt;br /&gt;
      Instructor.create(id: 1234, name: 'testinstructor', email: 'test@test.com', full_name: 'Test Instructor',&lt;br /&gt;
                        password: '123456', role_id: 2)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by name' do&lt;br /&gt;
      it 'returns users with matching names' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'testins', 'name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by fullname' do&lt;br /&gt;
      it 'returns users with matching fullnames' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'Test', 'full_name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'UnknownName', 'full_name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by email' do&lt;br /&gt;
      it 'returns users with matching emails' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'test@test.com', 'email')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown@test.com', 'email')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by default' do&lt;br /&gt;
      it 'returns users with names starting with the specified id' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(instructor.id, nil, nil)&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(9999, nil, nil) # Use an invalid user_id&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    context 'when searching by role' do&lt;br /&gt;
      it 'returns users with matching roles' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'admin', 'role')&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.role_id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'role')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:Expertiza_tests.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;br /&gt;
* [https://github.com/expertiza/reimplementation-back-end/pull/60/files#diff-a05d611b9fb47bcea66e3aaf6f25dc71d8da9976c5ccc991ea5171e938605644 GITHUB PR]&lt;br /&gt;
* [https://www.youtube.com/watch?v=_aCqMjRwW_U VIDEO DEMO 5min]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152471</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=152471"/>
		<updated>2023-12-05T02:39:36Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, list, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''list Action:'''&lt;br /&gt;
## The 'list' action will be reimplemented to handle listing of users.&lt;br /&gt;
## It will retrieve a list of users based on certain criteria, such as name, full name or email.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'list' and 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
# '''Instructor.rb Tests:'''&lt;br /&gt;
## Tests for the 'get_user_list' method in the 'Instructor' class will ensure it fetches the correct list of users based on the instructor's role and associated courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== ToDo Status: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done!&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|7&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|Done&lt;br /&gt;
|New tests will be written for the `role` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;list and paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that list and paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that list and paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
The search_users method accepts three parameters: user_id, word, and search_by. If a valid user_id is provided, the method returns an array containing the user with that specific user_id.&lt;br /&gt;
In cases where user_id is empty or invalid, the method performs a search based on the provided word and search_by parameters.&lt;br /&gt;
To prevent SQL injection, the method validates search_by against a predefined set of acceptable fields, including 'name', 'full_name', 'email', and 'role'. The search is then conducted using a LIKE query on the specified field, ordering the results by the user's name.&lt;br /&gt;
&lt;br /&gt;
This flexible method accommodates various search scenarios, allowing users to query the system based on different criteria, enhancing the overall user experience.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
  def self.search_users(user_id, word, search_by)&lt;br /&gt;
    if user_id.present? &amp;amp;&amp;amp; (user = User.find_by(id: user_id))&lt;br /&gt;
      # If a valid user_id is provided, return the user with that specific user_id&lt;br /&gt;
      return [user]&lt;br /&gt;
    else&lt;br /&gt;
      # If user_id is empty or invalid, perform the search based on word and search_by parameters&lt;br /&gt;
&lt;br /&gt;
      # Validate search_by to avoid SQL injection&lt;br /&gt;
      valid_search_fields = %w[name full_name email role]&lt;br /&gt;
      search_by = valid_search_fields.include?(search_by) ? search_by : nil&lt;br /&gt;
&lt;br /&gt;
      # Perform the LIKE query on the specified field (name, full_name, email, role) and order by name&lt;br /&gt;
      if search_by.present?&lt;br /&gt;
        # Use search_by directly in the where clause&lt;br /&gt;
        users = User.joins(:role).where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name) if search_by == 'role'&lt;br /&gt;
        users ||= User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name)&lt;br /&gt;
        return users&lt;br /&gt;
      else&lt;br /&gt;
        # If search_by is not recognized, return an empty result&lt;br /&gt;
        return []&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The login method, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Attributes ====&lt;br /&gt;
&lt;br /&gt;
* '''name:''' The name of the user.&lt;br /&gt;
* '''full_name:''' The full name of the user, providing a comprehensive representation.&lt;br /&gt;
* '''email:''' The unique email address associated with each user, ensuring distinct identification.&lt;br /&gt;
* '''role:''' A relationship with the [[Role Model|Role model]], defining the user's role and access privileges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    user_id = params[:user_id]&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(user_id, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.present?&lt;br /&gt;
      # If the result is not empty, render the users as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is empty, render a message as JSON with a not found status&lt;br /&gt;
      render json: { error: 'User not found or no matching results' }, status: :not_found&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This method facilitates dynamic user searches, allowing users to query the system based on different parameters. Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
=== role Method ===&lt;br /&gt;
The role method provides the role of a specific user. It is used as a utility method in the controller.&lt;br /&gt;
  def role&lt;br /&gt;
    if @user&amp;amp;.role_id&lt;br /&gt;
      @role = Role.find(@user.role_id)&lt;br /&gt;
    elsif @user&lt;br /&gt;
      @role = Role.new(id: nil, name: '(none)')&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
=== find_by_login Method ===&lt;br /&gt;
The method provides the functionality to search user by email or name&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list Method ===&lt;br /&gt;
Method for displaying the list of users&lt;br /&gt;
&lt;br /&gt;
  def self.find_by_login(login)&lt;br /&gt;
    user = User.find_by(email: login)&lt;br /&gt;
    if user.nil?&lt;br /&gt;
      items = login.split('@')&lt;br /&gt;
      short_name = items[0]&lt;br /&gt;
      user_list = User.where('name = ?', short_name)&lt;br /&gt;
      user = user_list.first if user_list.any? &amp;amp;&amp;amp; user_list.length == 1&lt;br /&gt;
    end&lt;br /&gt;
    user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== list and paginate_list Method ===&lt;br /&gt;
Method for filtering the users list with proper search and pagination.&lt;br /&gt;
  def list&lt;br /&gt;
    # code here&lt;br /&gt;
    letter = params[:letter]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
    # If search parameters present&lt;br /&gt;
    if letter.present? &amp;amp;&amp;amp; search_by.present?&lt;br /&gt;
      case search_by.to_i&lt;br /&gt;
      when 1 # Search by username&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('name LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 2 # Search by fullname&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('fullname LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      when 3 # Search by email&lt;br /&gt;
        @paginated_users = paginate_list&amp;amp;.where('email LIKE ?', &amp;quot;%#{letter}%&amp;quot;)&lt;br /&gt;
      else&lt;br /&gt;
        @paginated_users = paginate_list&lt;br /&gt;
      end&lt;br /&gt;
    else # Display all users if no search parameters present&lt;br /&gt;
      @paginated_users = paginate_list&lt;br /&gt;
      if @paginated_users&lt;br /&gt;
        puts(&amp;quot;Not empty&amp;quot; + @paginated_users.to_s) else puts(&amp;quot;Empty&amp;quot;)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    render json: @paginated_users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def paginate_list&lt;br /&gt;
    paginate_options = { '1' =&amp;gt; 25, '2' =&amp;gt; 50, '3' =&amp;gt; 100 }&lt;br /&gt;
    # If the above hash does not have a value for the key,&lt;br /&gt;
    # it means that we need to show all the users on the page&lt;br /&gt;
    #&lt;br /&gt;
    # Just a point to remember, when we use pagination, the&lt;br /&gt;
    # 'users' variable should be an object, not an array&lt;br /&gt;
    # The type of condition for the search depends on what the user has selected from the search_by dropdown&lt;br /&gt;
    @search_by = params[:search_by]&lt;br /&gt;
    @per_page = params[:per_page] || 3&lt;br /&gt;
    # search for corresponding users&lt;br /&gt;
    # users = User.search_users(role, user_id, letter, @search_by)&lt;br /&gt;
    # paginate&lt;br /&gt;
    users = if paginate_options[@per_page.to_s].nil? # displaying all - no pagination&lt;br /&gt;
              User.all&lt;br /&gt;
            else # some pagination is active - use the per_page&lt;br /&gt;
              User.paginate(page: params[:page], per_page: paginate_options[@per_page.to_s])&lt;br /&gt;
            end&lt;br /&gt;
    # users = User.all&lt;br /&gt;
    users&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
&lt;br /&gt;
This endpoint allows you to search for users based on various fields. If the requesting user is a Super Administrator, the search can include 'name', 'email', 'full_name', 'id', or 'role'. If the user is not a Super Administrator, the search is limited to the user with the provided user_id.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id (optional)''': The ID of the requesting user. If provided, it is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field. If the requesting user is not a Super Administrator, it returns the user with the given user_id.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/list&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''search_by (optional)''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
* '''letter (optional)''': The field to indicate the starting letter of the user's 'name', 'email', or 'full_name'.&lt;br /&gt;
* '''per_page  (optional)''': The field to indicate how many users to be displayed per page.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET api/v1/users/list?letter=n&amp;amp;search_by=1&amp;amp;per_page=3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;newuser&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;new_user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:List_api.png|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
require 'rails_helper'&lt;br /&gt;
&lt;br /&gt;
RSpec.describe User, type: :model do&lt;br /&gt;
&lt;br /&gt;
  describe '.find_by_login' do&lt;br /&gt;
    context 'when login is an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      it 'returns the user with the matching email' do&lt;br /&gt;
        result = User.find_by_login('test@test.com')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      it 'returns nil when user with email doesnt exist' do&lt;br /&gt;
        result = User.find_by_login('test@unknown.com')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when login is not an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      let!(:user2) { build(:user, name: 'testname', email: 'test@test2.com') }&lt;br /&gt;
      it 'returns the user with the matching name' do&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return first user with the matching name' do&lt;br /&gt;
        user2.save&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return nil when no user with matching name' do&lt;br /&gt;
        result = User.find_by_login('unknown')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  describe '.search_users' do&lt;br /&gt;
    # Creating dummy objects for the test with the help of let statement&lt;br /&gt;
    let(:role) { Role.create(name: 'Instructor', parent_id: nil, id: 2, default_page_id: nil) }&lt;br /&gt;
    let(:instructor) do&lt;br /&gt;
      Instructor.create(id: 1234, name: 'testinstructor', email: 'test@test.com', full_name: 'Test Instructor',&lt;br /&gt;
                        password: '123456', role_id: 2)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by name' do&lt;br /&gt;
      it 'returns users with matching names' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'testins', 'name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by fullname' do&lt;br /&gt;
      it 'returns users with matching fullnames' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'Test', 'full_name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'UnknownName', 'full_name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by email' do&lt;br /&gt;
      it 'returns users with matching emails' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'test@test.com', 'email')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown@test.com', 'email')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by default' do&lt;br /&gt;
      it 'returns users with names starting with the specified id' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(instructor.id, nil, nil)&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(9999, nil, nil) # Use an invalid user_id&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    context 'when searching by role' do&lt;br /&gt;
      it 'returns users with matching roles' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'admin', 'role')&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.role_id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'role')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:Expertiza_tests.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;br /&gt;
* [https://github.com/expertiza/reimplementation-back-end/pull/60/files#diff-a05d611b9fb47bcea66e3aaf6f25dc71d8da9976c5ccc991ea5171e938605644 GITHUB PR]&lt;br /&gt;
* [https://www.youtube.com/watch?v=_aCqMjRwW_U VIDEO DEMO 5min]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151979</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151979"/>
		<updated>2023-12-02T20:35:24Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== Revised Instructor.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''get_user_list Method:'''&lt;br /&gt;
## The 'get_user_list' method in the 'Instructor' class will be reimplemented to fetch a list of users based on the instructor's role.&lt;br /&gt;
## The method will likely consider the instructor's courses/assignments and return a list of users associated with those courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
# '''Instructor.rb Tests:'''&lt;br /&gt;
## Tests for the 'get_user_list' method in the 'Instructor' class will ensure it fetches the correct list of users based on the instructor's role and associated courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== ToDo Status: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done!&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `role` and `paginate_list` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
The search_users method accepts three parameters: user_id, word, and search_by. If a valid user_id is provided, the method returns an array containing the user with that specific user_id.&lt;br /&gt;
In cases where user_id is empty or invalid, the method performs a search based on the provided word and search_by parameters.&lt;br /&gt;
To prevent SQL injection, the method validates search_by against a predefined set of acceptable fields, including 'name', 'full_name', 'email', and 'role'. The search is then conducted using a LIKE query on the specified field, ordering the results by the user's name.&lt;br /&gt;
&lt;br /&gt;
This flexible method accommodates various search scenarios, allowing users to query the system based on different criteria, enhancing the overall user experience.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
  def self.search_users(user_id, word, search_by)&lt;br /&gt;
    if user_id.present? &amp;amp;&amp;amp; (user = User.find_by(id: user_id))&lt;br /&gt;
      # If a valid user_id is provided, return the user with that specific user_id&lt;br /&gt;
      return [user]&lt;br /&gt;
    else&lt;br /&gt;
      # If user_id is empty or invalid, perform the search based on word and search_by parameters&lt;br /&gt;
&lt;br /&gt;
      # Validate search_by to avoid SQL injection&lt;br /&gt;
      valid_search_fields = %w[name full_name email role]&lt;br /&gt;
      search_by = valid_search_fields.include?(search_by) ? search_by : nil&lt;br /&gt;
&lt;br /&gt;
      # Perform the LIKE query on the specified field (name, full_name, email, role) and order by name&lt;br /&gt;
      if search_by.present?&lt;br /&gt;
        # Use search_by directly in the where clause&lt;br /&gt;
        users = User.joins(:role).where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name) if search_by == 'role'&lt;br /&gt;
        users ||= User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name)&lt;br /&gt;
        return users&lt;br /&gt;
      else&lt;br /&gt;
        # If search_by is not recognized, return an empty result&lt;br /&gt;
        return []&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The login method, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Attributes ====&lt;br /&gt;
&lt;br /&gt;
* '''name:''' The name of the user.&lt;br /&gt;
* '''full_name:''' The full name of the user, providing a comprehensive representation.&lt;br /&gt;
* '''email:''' The unique email address associated with each user, ensuring distinct identification.&lt;br /&gt;
* '''role:''' A relationship with the [[Role Model|Role model]], defining the user's role and access privileges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    user_id = params[:user_id]&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(user_id, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.present?&lt;br /&gt;
      # If the result is not empty, render the users as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is empty, render a message as JSON with a not found status&lt;br /&gt;
      render json: { error: 'User not found or no matching results' }, status: :not_found&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This method facilitates dynamic user searches, allowing users to query the system based on different parameters. Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
&lt;br /&gt;
This endpoint allows you to search for users based on various fields. If the requesting user is a Super Administrator, the search can include 'name', 'email', 'full_name', 'id', or 'role'. If the user is not a Super Administrator, the search is limited to the user with the provided user_id.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id (optional)''': The ID of the requesting user. If provided, it is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field. If the requesting user is not a Super Administrator, it returns the user with the given user_id.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
require 'rails_helper'&lt;br /&gt;
&lt;br /&gt;
RSpec.describe User, type: :model do&lt;br /&gt;
&lt;br /&gt;
  describe '.find_by_login' do&lt;br /&gt;
    context 'when login is an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      it 'returns the user with the matching email' do&lt;br /&gt;
        result = User.find_by_login('test@test.com')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      it 'returns nil when user with email doesnt exist' do&lt;br /&gt;
        result = User.find_by_login('test@unknown.com')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when login is not an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      let!(:user2) { build(:user, name: 'testname', email: 'test@test2.com') }&lt;br /&gt;
      it 'returns the user with the matching name' do&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return first user with the matching name' do&lt;br /&gt;
        user2.save&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return nil when no user with matching name' do&lt;br /&gt;
        result = User.find_by_login('unknown')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  describe '.search_users' do&lt;br /&gt;
    # Creating dummy objects for the test with the help of let statement&lt;br /&gt;
    let(:role) { Role.create(name: 'Instructor', parent_id: nil, id: 2, default_page_id: nil) }&lt;br /&gt;
    let(:instructor) do&lt;br /&gt;
      Instructor.create(id: 1234, name: 'testinstructor', email: 'test@test.com', full_name: 'Test Instructor',&lt;br /&gt;
                        password: '123456', role_id: 2)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by name' do&lt;br /&gt;
      it 'returns users with matching names' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'testins', 'name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by fullname' do&lt;br /&gt;
      it 'returns users with matching fullnames' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'Test', 'full_name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'UnknownName', 'full_name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by email' do&lt;br /&gt;
      it 'returns users with matching emails' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'test@test.com', 'email')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown@test.com', 'email')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by default' do&lt;br /&gt;
      it 'returns users with names starting with the specified id' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(instructor.id, nil, nil)&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(9999, nil, nil) # Use an invalid user_id&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    context 'when searching by role' do&lt;br /&gt;
      it 'returns users with matching roles' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'admin', 'role')&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.role_id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'role')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:testSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;br /&gt;
* [https://github.com/expertiza/reimplementation-back-end/pull/60/files#diff-a05d611b9fb47bcea66e3aaf6f25dc71d8da9976c5ccc991ea5171e938605644 GITHUB PR]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151977</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151977"/>
		<updated>2023-12-02T20:33:03Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* user_spec.rb */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== Revised Instructor.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''get_user_list Method:'''&lt;br /&gt;
## The 'get_user_list' method in the 'Instructor' class will be reimplemented to fetch a list of users based on the instructor's role.&lt;br /&gt;
## The method will likely consider the instructor's courses/assignments and return a list of users associated with those courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
# '''Instructor.rb Tests:'''&lt;br /&gt;
## Tests for the 'get_user_list' method in the 'Instructor' class will ensure it fetches the correct list of users based on the instructor's role and associated courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== ToDo Status: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done!&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `role` and `paginate_list` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
The search_users method accepts three parameters: user_id, word, and search_by. If a valid user_id is provided, the method returns an array containing the user with that specific user_id.&lt;br /&gt;
In cases where user_id is empty or invalid, the method performs a search based on the provided word and search_by parameters.&lt;br /&gt;
To prevent SQL injection, the method validates search_by against a predefined set of acceptable fields, including 'name', 'full_name', 'email', and 'role'. The search is then conducted using a LIKE query on the specified field, ordering the results by the user's name.&lt;br /&gt;
&lt;br /&gt;
This flexible method accommodates various search scenarios, allowing users to query the system based on different criteria, enhancing the overall user experience.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
  def self.search_users(user_id, word, search_by)&lt;br /&gt;
    if user_id.present? &amp;amp;&amp;amp; (user = User.find_by(id: user_id))&lt;br /&gt;
      # If a valid user_id is provided, return the user with that specific user_id&lt;br /&gt;
      return [user]&lt;br /&gt;
    else&lt;br /&gt;
      # If user_id is empty or invalid, perform the search based on word and search_by parameters&lt;br /&gt;
&lt;br /&gt;
      # Validate search_by to avoid SQL injection&lt;br /&gt;
      valid_search_fields = %w[name full_name email role]&lt;br /&gt;
      search_by = valid_search_fields.include?(search_by) ? search_by : nil&lt;br /&gt;
&lt;br /&gt;
      # Perform the LIKE query on the specified field (name, full_name, email, role) and order by name&lt;br /&gt;
      if search_by.present?&lt;br /&gt;
        # Use search_by directly in the where clause&lt;br /&gt;
        users = User.joins(:role).where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name) if search_by == 'role'&lt;br /&gt;
        users ||= User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name)&lt;br /&gt;
        return users&lt;br /&gt;
      else&lt;br /&gt;
        # If search_by is not recognized, return an empty result&lt;br /&gt;
        return []&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The login method, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Attributes ====&lt;br /&gt;
&lt;br /&gt;
* '''name:''' The name of the user.&lt;br /&gt;
* '''full_name:''' The full name of the user, providing a comprehensive representation.&lt;br /&gt;
* '''email:''' The unique email address associated with each user, ensuring distinct identification.&lt;br /&gt;
* '''role:''' A relationship with the [[Role Model|Role model]], defining the user's role and access privileges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    user_id = params[:user_id]&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(user_id, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.present?&lt;br /&gt;
      # If the result is not empty, render the users as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is empty, render a message as JSON with a not found status&lt;br /&gt;
      render json: { error: 'User not found or no matching results' }, status: :not_found&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This method facilitates dynamic user searches, allowing users to query the system based on different parameters. Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
&lt;br /&gt;
This endpoint allows you to search for users based on various fields. If the requesting user is a Super Administrator, the search can include 'name', 'email', 'full_name', 'id', or 'role'. If the user is not a Super Administrator, the search is limited to the user with the provided user_id.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id (optional)''': The ID of the requesting user. If provided, it is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field. If the requesting user is not a Super Administrator, it returns the user with the given user_id.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
require 'rails_helper'&lt;br /&gt;
&lt;br /&gt;
RSpec.describe User, type: :model do&lt;br /&gt;
&lt;br /&gt;
  describe '.find_by_login' do&lt;br /&gt;
    context 'when login is an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      it 'returns the user with the matching email' do&lt;br /&gt;
        result = User.find_by_login('test@test.com')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      it 'returns nil when user with email doesnt exist' do&lt;br /&gt;
        result = User.find_by_login('test@unknown.com')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when login is not an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      let!(:user2) { build(:user, name: 'testname', email: 'test@test2.com') }&lt;br /&gt;
      it 'returns the user with the matching name' do&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return first user with the matching name' do&lt;br /&gt;
        user2.save&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return nil when no user with matching name' do&lt;br /&gt;
        result = User.find_by_login('unknown')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  describe '.search_users' do&lt;br /&gt;
    # Creating dummy objects for the test with the help of let statement&lt;br /&gt;
    let(:role) { Role.create(name: 'Instructor', parent_id: nil, id: 2, default_page_id: nil) }&lt;br /&gt;
    let(:instructor) do&lt;br /&gt;
      Instructor.create(id: 1234, name: 'testinstructor', email: 'test@test.com', full_name: 'Test Instructor',&lt;br /&gt;
                        password: '123456', role_id: 2)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by name' do&lt;br /&gt;
      it 'returns users with matching names' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'testins', 'name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by fullname' do&lt;br /&gt;
      it 'returns users with matching fullnames' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'Test', 'full_name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'UnknownName', 'full_name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by email' do&lt;br /&gt;
      it 'returns users with matching emails' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'test@test.com', 'email')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown@test.com', 'email')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by default' do&lt;br /&gt;
      it 'returns users with names starting with the specified id' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(instructor.id, nil, nil)&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(9999, nil, nil) # Use an invalid user_id&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    context 'when searching by role' do&lt;br /&gt;
      it 'returns users with matching roles' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'admin', 'role')&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.role_id)&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'role')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:testSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151976</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151976"/>
		<updated>2023-12-02T20:28:52Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* user_spec.rb */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== Revised Instructor.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''get_user_list Method:'''&lt;br /&gt;
## The 'get_user_list' method in the 'Instructor' class will be reimplemented to fetch a list of users based on the instructor's role.&lt;br /&gt;
## The method will likely consider the instructor's courses/assignments and return a list of users associated with those courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
# '''Instructor.rb Tests:'''&lt;br /&gt;
## Tests for the 'get_user_list' method in the 'Instructor' class will ensure it fetches the correct list of users based on the instructor's role and associated courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== ToDo Status: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done!&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `role` and `paginate_list` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
The search_users method accepts three parameters: user_id, word, and search_by. If a valid user_id is provided, the method returns an array containing the user with that specific user_id.&lt;br /&gt;
In cases where user_id is empty or invalid, the method performs a search based on the provided word and search_by parameters.&lt;br /&gt;
To prevent SQL injection, the method validates search_by against a predefined set of acceptable fields, including 'name', 'full_name', 'email', and 'role'. The search is then conducted using a LIKE query on the specified field, ordering the results by the user's name.&lt;br /&gt;
&lt;br /&gt;
This flexible method accommodates various search scenarios, allowing users to query the system based on different criteria, enhancing the overall user experience.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
  def self.search_users(user_id, word, search_by)&lt;br /&gt;
    if user_id.present? &amp;amp;&amp;amp; (user = User.find_by(id: user_id))&lt;br /&gt;
      # If a valid user_id is provided, return the user with that specific user_id&lt;br /&gt;
      return [user]&lt;br /&gt;
    else&lt;br /&gt;
      # If user_id is empty or invalid, perform the search based on word and search_by parameters&lt;br /&gt;
&lt;br /&gt;
      # Validate search_by to avoid SQL injection&lt;br /&gt;
      valid_search_fields = %w[name full_name email role]&lt;br /&gt;
      search_by = valid_search_fields.include?(search_by) ? search_by : nil&lt;br /&gt;
&lt;br /&gt;
      # Perform the LIKE query on the specified field (name, full_name, email, role) and order by name&lt;br /&gt;
      if search_by.present?&lt;br /&gt;
        # Use search_by directly in the where clause&lt;br /&gt;
        users = User.joins(:role).where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name) if search_by == 'role'&lt;br /&gt;
        users ||= User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name)&lt;br /&gt;
        return users&lt;br /&gt;
      else&lt;br /&gt;
        # If search_by is not recognized, return an empty result&lt;br /&gt;
        return []&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The login method, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Attributes ====&lt;br /&gt;
&lt;br /&gt;
* '''name:''' The name of the user.&lt;br /&gt;
* '''full_name:''' The full name of the user, providing a comprehensive representation.&lt;br /&gt;
* '''email:''' The unique email address associated with each user, ensuring distinct identification.&lt;br /&gt;
* '''role:''' A relationship with the [[Role Model|Role model]], defining the user's role and access privileges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    user_id = params[:user_id]&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(user_id, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.present?&lt;br /&gt;
      # If the result is not empty, render the users as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is empty, render a message as JSON with a not found status&lt;br /&gt;
      render json: { error: 'User not found or no matching results' }, status: :not_found&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This method facilitates dynamic user searches, allowing users to query the system based on different parameters. Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
&lt;br /&gt;
This endpoint allows you to search for users based on various fields. If the requesting user is a Super Administrator, the search can include 'name', 'email', 'full_name', 'id', or 'role'. If the user is not a Super Administrator, the search is limited to the user with the provided user_id.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id (optional)''': The ID of the requesting user. If provided, it is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field. If the requesting user is not a Super Administrator, it returns the user with the given user_id.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
  require 'rails_helper'&lt;br /&gt;
&lt;br /&gt;
RSpec.describe User, type: :model do&lt;br /&gt;
&lt;br /&gt;
  describe '.find_by_login' do&lt;br /&gt;
    context 'when login is an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      it 'returns the user with the matching email' do&lt;br /&gt;
        result = User.find_by_login('test@test.com')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
      it 'returns nil when user with email doesnt exist' do&lt;br /&gt;
        result = User.find_by_login('test@unknown.com')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when login is not an email' do&lt;br /&gt;
      let!(:user) { create(:user, name: 'testname', email: 'test@test.com') }&lt;br /&gt;
      let!(:user2) { build(:user, name: 'testname', email: 'test@test2.com') }&lt;br /&gt;
      it 'returns the user with the matching name' do&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return first user with the matching name' do&lt;br /&gt;
        user2.save&lt;br /&gt;
        result = User.find_by_login('testname')&lt;br /&gt;
        expect(result).to eq(user)&lt;br /&gt;
      end&lt;br /&gt;
      it 'return nil when no user with matching name' do&lt;br /&gt;
        result = User.find_by_login('unknown')&lt;br /&gt;
        expect(result).to eq(nil)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  describe '.search_users' do&lt;br /&gt;
    # Creating dummy objects for the test with the help of let statement&lt;br /&gt;
    let(:role) { Role.create(name: 'Instructor', parent_id: nil, id: 2, default_page_id: nil) }&lt;br /&gt;
    let(:instructor) do&lt;br /&gt;
      Instructor.create(id: 1234, name: 'testinstructor', email: 'test@test.com', full_name: 'Test Instructor',&lt;br /&gt;
                        password: '123456', role_id: 2)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by name' do&lt;br /&gt;
      it 'returns users with matching names' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'testins', 'name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by fullname' do&lt;br /&gt;
      it 'returns users with matching fullnames' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'Test', 'full_name')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'UnknownName', 'full_name')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by email' do&lt;br /&gt;
      it 'returns users with matching emails' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'test@test.com', 'email')&lt;br /&gt;
        expect(search_result).to include(instructor)&lt;br /&gt;
&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown@test.com', 'email')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    context 'when searching by default' do&lt;br /&gt;
      it 'returns users with names starting with the specified id' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(instructor.id, nil, nil)&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.id)&lt;br /&gt;
&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(9999, nil, nil) # Use an invalid user_id&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    context 'when searching by role' do&lt;br /&gt;
      it 'returns users with matching roles' do&lt;br /&gt;
        # Test scenario 1&lt;br /&gt;
        search_result = User.search_users(nil, 'admin', 'role')&lt;br /&gt;
        expect(search_result.map(&amp;amp;:id)).to include(instructor.role_id)&lt;br /&gt;
&lt;br /&gt;
        # Test scenario 2&lt;br /&gt;
        search_result = User.search_users(nil, 'unknown', 'role')&lt;br /&gt;
        expect(search_result).to be_empty&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:testSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151965</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151965"/>
		<updated>2023-12-02T18:04:27Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== Revised Instructor.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''get_user_list Method:'''&lt;br /&gt;
## The 'get_user_list' method in the 'Instructor' class will be reimplemented to fetch a list of users based on the instructor's role.&lt;br /&gt;
## The method will likely consider the instructor's courses/assignments and return a list of users associated with those courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
# '''Instructor.rb Tests:'''&lt;br /&gt;
## Tests for the 'get_user_list' method in the 'Instructor' class will ensure it fetches the correct list of users based on the instructor's role and associated courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== ToDo Status: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done!&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `role` and `paginate_list` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
The search_users method accepts three parameters: user_id, word, and search_by. If a valid user_id is provided, the method returns an array containing the user with that specific user_id.&lt;br /&gt;
In cases where user_id is empty or invalid, the method performs a search based on the provided word and search_by parameters.&lt;br /&gt;
To prevent SQL injection, the method validates search_by against a predefined set of acceptable fields, including 'name', 'full_name', 'email', and 'role'. The search is then conducted using a LIKE query on the specified field, ordering the results by the user's name.&lt;br /&gt;
&lt;br /&gt;
This flexible method accommodates various search scenarios, allowing users to query the system based on different criteria, enhancing the overall user experience.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
  def self.search_users(user_id, word, search_by)&lt;br /&gt;
    if user_id.present? &amp;amp;&amp;amp; (user = User.find_by(id: user_id))&lt;br /&gt;
      # If a valid user_id is provided, return the user with that specific user_id&lt;br /&gt;
      return [user]&lt;br /&gt;
    else&lt;br /&gt;
      # If user_id is empty or invalid, perform the search based on word and search_by parameters&lt;br /&gt;
&lt;br /&gt;
      # Validate search_by to avoid SQL injection&lt;br /&gt;
      valid_search_fields = %w[name full_name email role]&lt;br /&gt;
      search_by = valid_search_fields.include?(search_by) ? search_by : nil&lt;br /&gt;
&lt;br /&gt;
      # Perform the LIKE query on the specified field (name, full_name, email, role) and order by name&lt;br /&gt;
      if search_by.present?&lt;br /&gt;
        # Use search_by directly in the where clause&lt;br /&gt;
        users = User.joins(:role).where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name) if search_by == 'role'&lt;br /&gt;
        users ||= User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name)&lt;br /&gt;
        return users&lt;br /&gt;
      else&lt;br /&gt;
        # If search_by is not recognized, return an empty result&lt;br /&gt;
        return []&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The login method, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Attributes ====&lt;br /&gt;
&lt;br /&gt;
* '''name:''' The name of the user.&lt;br /&gt;
* '''full_name:''' The full name of the user, providing a comprehensive representation.&lt;br /&gt;
* '''email:''' The unique email address associated with each user, ensuring distinct identification.&lt;br /&gt;
* '''role:''' A relationship with the [[Role Model|Role model]], defining the user's role and access privileges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    user_id = params[:user_id]&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(user_id, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.present?&lt;br /&gt;
      # If the result is not empty, render the users as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is empty, render a message as JSON with a not found status&lt;br /&gt;
      render json: { error: 'User not found or no matching results' }, status: :not_found&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This method facilitates dynamic user searches, allowing users to query the system based on different parameters. Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
&lt;br /&gt;
This endpoint allows you to search for users based on various fields. If the requesting user is a Super Administrator, the search can include 'name', 'email', 'full_name', 'id', or 'role'. If the user is not a Super Administrator, the search is limited to the user with the provided user_id.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id (optional)''': The ID of the requesting user. If provided, it is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field. If the requesting user is not a Super Administrator, it returns the user with the given user_id.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;describe&amp;lt;/span&amp;gt; '.search_users' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
   let(:super_admin_role) { Role.find_or_create_by(name: 'Super Administrator') }&lt;br /&gt;
   let(:user) { create(:user, role: super_admin_role) }&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns users with matching roles' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When searching by role, it should return users with matching roles.&lt;br /&gt;
       result = User.search_users(user, 'Admin', 'role')&lt;br /&gt;
       expect(result).to include(user)&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns an empty array if no users match the criteria' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When no users match the specified criteria, it should return an empty array.&lt;br /&gt;
       result = User.search_users(user, 'Nonexistent', 'name')&lt;br /&gt;
       expect(result).to eq([])&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     let(:regular_user) { create(:user) }&lt;br /&gt;
     it 'returns unauthorized when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       result = User.search_users(regular_user, 'paolajones', 'name')&lt;br /&gt;
       expect(result).to eq('Not Authorized')&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:testSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151964</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151964"/>
		<updated>2023-12-02T18:03:17Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== Revised Instructor.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''get_user_list Method:'''&lt;br /&gt;
## The 'get_user_list' method in the 'Instructor' class will be reimplemented to fetch a list of users based on the instructor's role.&lt;br /&gt;
## The method will likely consider the instructor's courses/assignments and return a list of users associated with those courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
# '''Instructor.rb Tests:'''&lt;br /&gt;
## Tests for the 'get_user_list' method in the 'Instructor' class will ensure it fetches the correct list of users based on the instructor's role and associated courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== ToDo Status: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done!&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `role` and `paginate_list` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
The search_users method is a versatile search function within the User model. It accepts three parameters: user_id, word, and search_by. If a valid user_id is provided, indicating a Super Administrator, the method returns an array containing the user with that specific user_id.&lt;br /&gt;
In cases where user_id is empty or invalid, the method performs a search based on the provided word and search_by parameters. To prevent SQL injection, the method validates search_by against a predefined set of acceptable fields, including 'name', 'full_name', 'email', and 'role'. The search is then conducted using a LIKE query on the specified field, ordering the results by the user's name.&lt;br /&gt;
&lt;br /&gt;
This flexible method accommodates various search scenarios, allowing users to query the system based on different criteria, enhancing the overall user experience.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
  def self.search_users(user_id, word, search_by)&lt;br /&gt;
    if user_id.present? &amp;amp;&amp;amp; (user = User.find_by(id: user_id))&lt;br /&gt;
      # If a valid user_id is provided, return the user with that specific user_id&lt;br /&gt;
      return [user]&lt;br /&gt;
    else&lt;br /&gt;
      # If user_id is empty or invalid, perform the search based on word and search_by parameters&lt;br /&gt;
&lt;br /&gt;
      # Validate search_by to avoid SQL injection&lt;br /&gt;
      valid_search_fields = %w[name full_name email role]&lt;br /&gt;
      search_by = valid_search_fields.include?(search_by) ? search_by : nil&lt;br /&gt;
&lt;br /&gt;
      # Perform the LIKE query on the specified field (name, full_name, email, role) and order by name&lt;br /&gt;
      if search_by.present?&lt;br /&gt;
        # Use search_by directly in the where clause&lt;br /&gt;
        users = User.joins(:role).where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name) if search_by == 'role'&lt;br /&gt;
        users ||= User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name)&lt;br /&gt;
        return users&lt;br /&gt;
      else&lt;br /&gt;
        # If search_by is not recognized, return an empty result&lt;br /&gt;
        return []&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The login method, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Attributes ====&lt;br /&gt;
&lt;br /&gt;
* '''name:''' The name of the user.&lt;br /&gt;
* '''full_name:''' The full name of the user, providing a comprehensive representation.&lt;br /&gt;
* '''email:''' The unique email address associated with each user, ensuring distinct identification.&lt;br /&gt;
* '''role:''' A relationship with the [[Role Model|Role model]], defining the user's role and access privileges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    user_id = params[:user_id]&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(user_id, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.present?&lt;br /&gt;
      # If the result is not empty, render the users as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is empty, render a message as JSON with a not found status&lt;br /&gt;
      render json: { error: 'User not found or no matching results' }, status: :not_found&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This method facilitates dynamic user searches, allowing users to query the system based on different parameters. Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
&lt;br /&gt;
This endpoint allows you to search for users based on various fields. If the requesting user is a Super Administrator, the search can include 'name', 'email', 'full_name', 'id', or 'role'. If the user is not a Super Administrator, the search is limited to the user with the provided user_id.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id (optional)''': The ID of the requesting user. If provided, it is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field. If the requesting user is not a Super Administrator, it returns the user with the given user_id.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;describe&amp;lt;/span&amp;gt; '.search_users' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
   let(:super_admin_role) { Role.find_or_create_by(name: 'Super Administrator') }&lt;br /&gt;
   let(:user) { create(:user, role: super_admin_role) }&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns users with matching roles' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When searching by role, it should return users with matching roles.&lt;br /&gt;
       result = User.search_users(user, 'Admin', 'role')&lt;br /&gt;
       expect(result).to include(user)&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns an empty array if no users match the criteria' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When no users match the specified criteria, it should return an empty array.&lt;br /&gt;
       result = User.search_users(user, 'Nonexistent', 'name')&lt;br /&gt;
       expect(result).to eq([])&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     let(:regular_user) { create(:user) }&lt;br /&gt;
     it 'returns unauthorized when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       result = User.search_users(regular_user, 'paolajones', 'name')&lt;br /&gt;
       expect(result).to eq('Not Authorized')&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:testSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151963</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151963"/>
		<updated>2023-12-02T18:02:34Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== Revised Instructor.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''get_user_list Method:'''&lt;br /&gt;
## The 'get_user_list' method in the 'Instructor' class will be reimplemented to fetch a list of users based on the instructor's role.&lt;br /&gt;
## The method will likely consider the instructor's courses/assignments and return a list of users associated with those courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
# '''Instructor.rb Tests:'''&lt;br /&gt;
## Tests for the 'get_user_list' method in the 'Instructor' class will ensure it fetches the correct list of users based on the instructor's role and associated courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== ToDo Status: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done!&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `role` and `paginate_list` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
In cases where user_id is empty or invalid, the method performs a search based on the provided word and search_by parameters. To prevent SQL injection, the method validates search_by against a predefined set of acceptable fields, including 'name', 'full_name', 'email', and 'role'. The search is then conducted using a LIKE query on the specified field, ordering the results by the user's name.&lt;br /&gt;
&lt;br /&gt;
This flexible method accommodates various search scenarios, allowing users to query the system based on different criteria, enhancing the overall user experience.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
  def self.search_users(user_id, word, search_by)&lt;br /&gt;
    if user_id.present? &amp;amp;&amp;amp; (user = User.find_by(id: user_id))&lt;br /&gt;
      # If a valid user_id is provided, return the user with that specific user_id&lt;br /&gt;
      return [user]&lt;br /&gt;
    else&lt;br /&gt;
      # If user_id is empty or invalid, perform the search based on word and search_by parameters&lt;br /&gt;
&lt;br /&gt;
      # Validate search_by to avoid SQL injection&lt;br /&gt;
      valid_search_fields = %w[name full_name email role]&lt;br /&gt;
      search_by = valid_search_fields.include?(search_by) ? search_by : nil&lt;br /&gt;
&lt;br /&gt;
      # Perform the LIKE query on the specified field (name, full_name, email, role) and order by name&lt;br /&gt;
      if search_by.present?&lt;br /&gt;
        # Use search_by directly in the where clause&lt;br /&gt;
        users = User.joins(:role).where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name) if search_by == 'role'&lt;br /&gt;
        users ||= User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name)&lt;br /&gt;
        return users&lt;br /&gt;
      else&lt;br /&gt;
        # If search_by is not recognized, return an empty result&lt;br /&gt;
        return []&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The login method, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Attributes ====&lt;br /&gt;
&lt;br /&gt;
* '''name:''' The name of the user.&lt;br /&gt;
* '''full_name:''' The full name of the user, providing a comprehensive representation.&lt;br /&gt;
* '''email:''' The unique email address associated with each user, ensuring distinct identification.&lt;br /&gt;
* '''role:''' A relationship with the [[Role Model|Role model]], defining the user's role and access privileges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    user_id = params[:user_id]&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(user_id, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.present?&lt;br /&gt;
      # If the result is not empty, render the users as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is empty, render a message as JSON with a not found status&lt;br /&gt;
      render json: { error: 'User not found or no matching results' }, status: :not_found&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This method facilitates dynamic user searches, allowing users to query the system based on different parameters. Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
&lt;br /&gt;
This endpoint allows you to search for users based on various fields. If the requesting user is a Super Administrator, the search can include 'name', 'email', 'full_name', 'id', or 'role'. If the user is not a Super Administrator, the search is limited to the user with the provided user_id.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id (optional)''': The ID of the requesting user. If provided, it is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field. If the requesting user is not a Super Administrator, it returns the user with the given user_id.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;describe&amp;lt;/span&amp;gt; '.search_users' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
   let(:super_admin_role) { Role.find_or_create_by(name: 'Super Administrator') }&lt;br /&gt;
   let(:user) { create(:user, role: super_admin_role) }&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns users with matching roles' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When searching by role, it should return users with matching roles.&lt;br /&gt;
       result = User.search_users(user, 'Admin', 'role')&lt;br /&gt;
       expect(result).to include(user)&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns an empty array if no users match the criteria' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When no users match the specified criteria, it should return an empty array.&lt;br /&gt;
       result = User.search_users(user, 'Nonexistent', 'name')&lt;br /&gt;
       expect(result).to eq([])&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     let(:regular_user) { create(:user) }&lt;br /&gt;
     it 'returns unauthorized when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       result = User.search_users(regular_user, 'paolajones', 'name')&lt;br /&gt;
       expect(result).to eq('Not Authorized')&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:testSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151962</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151962"/>
		<updated>2023-12-02T17:58:31Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== Revised Instructor.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''get_user_list Method:'''&lt;br /&gt;
## The 'get_user_list' method in the 'Instructor' class will be reimplemented to fetch a list of users based on the instructor's role.&lt;br /&gt;
## The method will likely consider the instructor's courses/assignments and return a list of users associated with those courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
# '''Instructor.rb Tests:'''&lt;br /&gt;
## Tests for the 'get_user_list' method in the 'Instructor' class will ensure it fetches the correct list of users based on the instructor's role and associated courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== ToDo Status: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done!&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `role` and `paginate_list` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
This method, search_users, takes three parameters: user, word, and search_by.&lt;br /&gt;
It checks if the user has the role of &amp;quot;Super Administrator.&amp;quot;&lt;br /&gt;
If the user is a super admin, it performs a query on the User model based on the search_by parameter:&lt;br /&gt;
If search_by is 'role', it joins the Role model and looks for users with roles containing the specified word.&lt;br /&gt;
Otherwise, it looks for users with attributes specified by search_by containing the specified word.&lt;br /&gt;
If the user is not a super admin, it returns the string 'Not Authorized'.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
  def self.search_users(user_id, word, search_by)&lt;br /&gt;
    if user_id.present? &amp;amp;&amp;amp; (user = User.find_by(id: user_id))&lt;br /&gt;
      # If a valid user_id is provided, return the user with that specific user_id&lt;br /&gt;
      return [user]&lt;br /&gt;
    else&lt;br /&gt;
      # If user_id is empty or invalid, perform the search based on word and search_by parameters&lt;br /&gt;
&lt;br /&gt;
      # Validate search_by to avoid SQL injection&lt;br /&gt;
      valid_search_fields = %w[name full_name email role]&lt;br /&gt;
      search_by = valid_search_fields.include?(search_by) ? search_by : nil&lt;br /&gt;
&lt;br /&gt;
      # Perform the LIKE query on the specified field (name, full_name, email, role) and order by name&lt;br /&gt;
      if search_by.present?&lt;br /&gt;
        # Use search_by directly in the where clause&lt;br /&gt;
        users = User.joins(:role).where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name) if search_by == 'role'&lt;br /&gt;
        users ||= User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name)&lt;br /&gt;
        return users&lt;br /&gt;
      else&lt;br /&gt;
        # If search_by is not recognized, return an empty result&lt;br /&gt;
        return []&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The login method, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Attributes ====&lt;br /&gt;
&lt;br /&gt;
* '''name:''' The name of the user.&lt;br /&gt;
* '''full_name:''' The full name of the user, providing a comprehensive representation.&lt;br /&gt;
* '''email:''' The unique email address associated with each user, ensuring distinct identification.&lt;br /&gt;
* '''role:''' A relationship with the [[Role Model|Role model]], defining the user's role and access privileges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    user_id = params[:user_id]&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(user_id, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.present?&lt;br /&gt;
      # If the result is not empty, render the users as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is empty, render a message as JSON with a not found status&lt;br /&gt;
      render json: { error: 'User not found or no matching results' }, status: :not_found&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This method facilitates dynamic user searches, allowing users to query the system based on different parameters. Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
&lt;br /&gt;
This endpoint allows you to search for users based on various fields. If the requesting user is a Super Administrator, the search can include 'name', 'email', 'full_name', 'id', or 'role'. If the user is not a Super Administrator, the search is limited to the user with the provided user_id.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id (optional)''': The ID of the requesting user. If provided, it is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field. If the requesting user is not a Super Administrator, it returns the user with the given user_id.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;describe&amp;lt;/span&amp;gt; '.search_users' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
   let(:super_admin_role) { Role.find_or_create_by(name: 'Super Administrator') }&lt;br /&gt;
   let(:user) { create(:user, role: super_admin_role) }&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns users with matching roles' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When searching by role, it should return users with matching roles.&lt;br /&gt;
       result = User.search_users(user, 'Admin', 'role')&lt;br /&gt;
       expect(result).to include(user)&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns an empty array if no users match the criteria' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When no users match the specified criteria, it should return an empty array.&lt;br /&gt;
       result = User.search_users(user, 'Nonexistent', 'name')&lt;br /&gt;
       expect(result).to eq([])&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     let(:regular_user) { create(:user) }&lt;br /&gt;
     it 'returns unauthorized when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       result = User.search_users(regular_user, 'paolajones', 'name')&lt;br /&gt;
       expect(result).to eq('Not Authorized')&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:testSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151961</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151961"/>
		<updated>2023-12-02T17:57:50Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== Revised Instructor.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''get_user_list Method:'''&lt;br /&gt;
## The 'get_user_list' method in the 'Instructor' class will be reimplemented to fetch a list of users based on the instructor's role.&lt;br /&gt;
## The method will likely consider the instructor's courses/assignments and return a list of users associated with those courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
# '''Instructor.rb Tests:'''&lt;br /&gt;
## Tests for the 'get_user_list' method in the 'Instructor' class will ensure it fetches the correct list of users based on the instructor's role and associated courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== ToDo Status: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done!&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `role` and `paginate_list` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
This method, search_users, takes three parameters: user, word, and search_by.&lt;br /&gt;
It checks if the user has the role of &amp;quot;Super Administrator.&amp;quot;&lt;br /&gt;
If the user is a super admin, it performs a query on the User model based on the search_by parameter:&lt;br /&gt;
If search_by is 'role', it joins the Role model and looks for users with roles containing the specified word.&lt;br /&gt;
Otherwise, it looks for users with attributes specified by search_by containing the specified word.&lt;br /&gt;
If the user is not a super admin, it returns the string 'Not Authorized'.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
  def self.search_users(user_id, word, search_by)&lt;br /&gt;
    if user_id.present? &amp;amp;&amp;amp; (user = User.find_by(id: user_id))&lt;br /&gt;
      # If a valid user_id is provided, return the user with that specific user_id&lt;br /&gt;
      return [user]&lt;br /&gt;
    else&lt;br /&gt;
      # If user_id is empty or invalid, perform the search based on word and search_by parameters&lt;br /&gt;
&lt;br /&gt;
      # Validate search_by to avoid SQL injection&lt;br /&gt;
      valid_search_fields = %w[name full_name email role]&lt;br /&gt;
      search_by = valid_search_fields.include?(search_by) ? search_by : nil&lt;br /&gt;
&lt;br /&gt;
      # Perform the LIKE query on the specified field (name, full_name, email, role) and order by name&lt;br /&gt;
      if search_by.present?&lt;br /&gt;
        # Use search_by directly in the where clause&lt;br /&gt;
        users = User.joins(:role).where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name) if search_by == 'role'&lt;br /&gt;
        users ||= User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;).order(:name)&lt;br /&gt;
        return users&lt;br /&gt;
      else&lt;br /&gt;
        # If search_by is not recognized, return an empty result&lt;br /&gt;
        return []&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method, login, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Attributes ====&lt;br /&gt;
&lt;br /&gt;
* '''name:''' The name of the user.&lt;br /&gt;
* '''full_name:''' The full name of the user, providing a comprehensive representation.&lt;br /&gt;
* '''email:''' The unique email address associated with each user, ensuring distinct identification.&lt;br /&gt;
* '''role:''' A relationship with the [[Role Model|Role model]], defining the user's role and access privileges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    user_id = params[:user_id]&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(user_id, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.present?&lt;br /&gt;
      # If the result is not empty, render the users as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is empty, render a message as JSON with a not found status&lt;br /&gt;
      render json: { error: 'User not found or no matching results' }, status: :not_found&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This method facilitates dynamic user searches, allowing users to query the system based on different parameters. Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
&lt;br /&gt;
This endpoint allows you to search for users based on various fields. If the requesting user is a Super Administrator, the search can include 'name', 'email', 'full_name', 'id', or 'role'. If the user is not a Super Administrator, the search is limited to the user with the provided user_id.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id (optional)''': The ID of the requesting user. If provided, it is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field. If the requesting user is not a Super Administrator, it returns the user with the given user_id.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;describe&amp;lt;/span&amp;gt; '.search_users' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
   let(:super_admin_role) { Role.find_or_create_by(name: 'Super Administrator') }&lt;br /&gt;
   let(:user) { create(:user, role: super_admin_role) }&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns users with matching roles' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When searching by role, it should return users with matching roles.&lt;br /&gt;
       result = User.search_users(user, 'Admin', 'role')&lt;br /&gt;
       expect(result).to include(user)&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns an empty array if no users match the criteria' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When no users match the specified criteria, it should return an empty array.&lt;br /&gt;
       result = User.search_users(user, 'Nonexistent', 'name')&lt;br /&gt;
       expect(result).to eq([])&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     let(:regular_user) { create(:user) }&lt;br /&gt;
     it 'returns unauthorized when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       result = User.search_users(regular_user, 'paolajones', 'name')&lt;br /&gt;
       expect(result).to eq('Not Authorized')&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:testSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151960</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151960"/>
		<updated>2023-12-02T17:55:30Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* API Documentation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== Revised Instructor.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''get_user_list Method:'''&lt;br /&gt;
## The 'get_user_list' method in the 'Instructor' class will be reimplemented to fetch a list of users based on the instructor's role.&lt;br /&gt;
## The method will likely consider the instructor's courses/assignments and return a list of users associated with those courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
# '''Instructor.rb Tests:'''&lt;br /&gt;
## Tests for the 'get_user_list' method in the 'Instructor' class will ensure it fetches the correct list of users based on the instructor's role and associated courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== ToDo Status: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done!&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `role` and `paginate_list` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
This method, search_users, takes three parameters: user, word, and search_by.&lt;br /&gt;
It checks if the user has the role of &amp;quot;Super Administrator.&amp;quot;&lt;br /&gt;
If the user is a super admin, it performs a query on the User model based on the search_by parameter:&lt;br /&gt;
If search_by is 'role', it joins the Role model and looks for users with roles containing the specified word.&lt;br /&gt;
Otherwise, it looks for users with attributes specified by search_by containing the specified word.&lt;br /&gt;
If the user is not a super admin, it returns the string 'Not Authorized'.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def self.search_users(user, word, search_by)&lt;br /&gt;
   # Check if the user's role is &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
   if user.role.name == 'Super Administrator'&lt;br /&gt;
     # If the user is a super admin, perform the LIKE query on the specified field&lt;br /&gt;
     if search_by == 'role'&lt;br /&gt;
       User.joins(:role).where(&amp;quot;roles.name LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;)&lt;br /&gt;
     else&lt;br /&gt;
       User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;)&lt;br /&gt;
     end&lt;br /&gt;
   else&lt;br /&gt;
     # If the user is not a super admin, return a message indicating unauthorized&lt;br /&gt;
     'Not Authorized'&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method, login, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Attributes ====&lt;br /&gt;
&lt;br /&gt;
* '''name:''' The name of the user.&lt;br /&gt;
* '''full_name:''' The full name of the user, providing a comprehensive representation.&lt;br /&gt;
* '''email:''' The unique email address associated with each user, ensuring distinct identification.&lt;br /&gt;
* '''role:''' A relationship with the [[Role Model|Role model]], defining the user's role and access privileges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    user_id = params[:user_id]&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(user_id, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.present?&lt;br /&gt;
      # If the result is not empty, render the users as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is empty, render a message as JSON with a not found status&lt;br /&gt;
      render json: { error: 'User not found or no matching results' }, status: :not_found&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This method facilitates dynamic user searches, allowing users to query the system based on different parameters. Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
&lt;br /&gt;
This endpoint allows you to search for users based on various fields. If the requesting user is a Super Administrator, the search can include 'name', 'email', 'full_name', 'id', or 'role'. If the user is not a Super Administrator, the search is limited to the user with the provided user_id.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id (optional)''': The ID of the requesting user. If provided, it is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field. If the requesting user is not a Super Administrator, it returns the user with the given user_id.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;describe&amp;lt;/span&amp;gt; '.search_users' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
   let(:super_admin_role) { Role.find_or_create_by(name: 'Super Administrator') }&lt;br /&gt;
   let(:user) { create(:user, role: super_admin_role) }&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns users with matching roles' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When searching by role, it should return users with matching roles.&lt;br /&gt;
       result = User.search_users(user, 'Admin', 'role')&lt;br /&gt;
       expect(result).to include(user)&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns an empty array if no users match the criteria' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When no users match the specified criteria, it should return an empty array.&lt;br /&gt;
       result = User.search_users(user, 'Nonexistent', 'name')&lt;br /&gt;
       expect(result).to eq([])&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     let(:regular_user) { create(:user) }&lt;br /&gt;
     it 'returns unauthorized when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       result = User.search_users(regular_user, 'paolajones', 'name')&lt;br /&gt;
       expect(result).to eq('Not Authorized')&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:testSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151959</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151959"/>
		<updated>2023-12-02T17:52:59Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* search_users Method */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== Revised Instructor.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''get_user_list Method:'''&lt;br /&gt;
## The 'get_user_list' method in the 'Instructor' class will be reimplemented to fetch a list of users based on the instructor's role.&lt;br /&gt;
## The method will likely consider the instructor's courses/assignments and return a list of users associated with those courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
# '''Instructor.rb Tests:'''&lt;br /&gt;
## Tests for the 'get_user_list' method in the 'Instructor' class will ensure it fetches the correct list of users based on the instructor's role and associated courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== ToDo Status: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done!&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `role` and `paginate_list` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
This method, search_users, takes three parameters: user, word, and search_by.&lt;br /&gt;
It checks if the user has the role of &amp;quot;Super Administrator.&amp;quot;&lt;br /&gt;
If the user is a super admin, it performs a query on the User model based on the search_by parameter:&lt;br /&gt;
If search_by is 'role', it joins the Role model and looks for users with roles containing the specified word.&lt;br /&gt;
Otherwise, it looks for users with attributes specified by search_by containing the specified word.&lt;br /&gt;
If the user is not a super admin, it returns the string 'Not Authorized'.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def self.search_users(user, word, search_by)&lt;br /&gt;
   # Check if the user's role is &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
   if user.role.name == 'Super Administrator'&lt;br /&gt;
     # If the user is a super admin, perform the LIKE query on the specified field&lt;br /&gt;
     if search_by == 'role'&lt;br /&gt;
       User.joins(:role).where(&amp;quot;roles.name LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;)&lt;br /&gt;
     else&lt;br /&gt;
       User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;)&lt;br /&gt;
     end&lt;br /&gt;
   else&lt;br /&gt;
     # If the user is not a super admin, return a message indicating unauthorized&lt;br /&gt;
     'Not Authorized'&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method, login, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Attributes ====&lt;br /&gt;
&lt;br /&gt;
* '''name:''' The name of the user.&lt;br /&gt;
* '''full_name:''' The full name of the user, providing a comprehensive representation.&lt;br /&gt;
* '''email:''' The unique email address associated with each user, ensuring distinct identification.&lt;br /&gt;
* '''role:''' A relationship with the [[Role Model|Role model]], defining the user's role and access privileges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    user_id = params[:user_id]&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(user_id, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.present?&lt;br /&gt;
      # If the result is not empty, render the users as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is empty, render a message as JSON with a not found status&lt;br /&gt;
      render json: { error: 'User not found or no matching results' }, status: :not_found&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
This method facilitates dynamic user searches, allowing users to query the system based on different parameters. Users can search by `user_id`, and if it's empty or invalid, the search extends to attributes such as `name`, `full_name`, `email`, and `role`. The search results are ordered by the user's name.&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
This endpoint allows you to search for users based on various fields if the requesting user is a Super Administrator.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id''': The ID of the requesting user. This is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field. If the requesting user is not a Super Administrator, it returns the user with the given user_id.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;describe&amp;lt;/span&amp;gt; '.search_users' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
   let(:super_admin_role) { Role.find_or_create_by(name: 'Super Administrator') }&lt;br /&gt;
   let(:user) { create(:user, role: super_admin_role) }&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns users with matching roles' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When searching by role, it should return users with matching roles.&lt;br /&gt;
       result = User.search_users(user, 'Admin', 'role')&lt;br /&gt;
       expect(result).to include(user)&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns an empty array if no users match the criteria' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When no users match the specified criteria, it should return an empty array.&lt;br /&gt;
       result = User.search_users(user, 'Nonexistent', 'name')&lt;br /&gt;
       expect(result).to eq([])&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     let(:regular_user) { create(:user) }&lt;br /&gt;
     it 'returns unauthorized when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       result = User.search_users(regular_user, 'paolajones', 'name')&lt;br /&gt;
       expect(result).to eq('Not Authorized')&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:testSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151935</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151935"/>
		<updated>2023-12-01T17:13:05Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* ToDo Status: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== Revised Instructor.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''get_user_list Method:'''&lt;br /&gt;
## The 'get_user_list' method in the 'Instructor' class will be reimplemented to fetch a list of users based on the instructor's role.&lt;br /&gt;
## The method will likely consider the instructor's courses/assignments and return a list of users associated with those courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
# '''Instructor.rb Tests:'''&lt;br /&gt;
## Tests for the 'get_user_list' method in the 'Instructor' class will ensure it fetches the correct list of users based on the instructor's role and associated courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== ToDo Status: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|Done!&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `role` and `paginate_list` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
This method, search_users, takes three parameters: user, word, and search_by.&lt;br /&gt;
It checks if the user has the role of &amp;quot;Super Administrator.&amp;quot;&lt;br /&gt;
If the user is a super admin, it performs a query on the User model based on the search_by parameter:&lt;br /&gt;
If search_by is 'role', it joins the Role model and looks for users with roles containing the specified word.&lt;br /&gt;
Otherwise, it looks for users with attributes specified by search_by containing the specified word.&lt;br /&gt;
If the user is not a super admin, it returns the string 'Not Authorized'.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def self.search_users(user, word, search_by)&lt;br /&gt;
   # Check if the user's role is &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
   if user.role.name == 'Super Administrator'&lt;br /&gt;
     # If the user is a super admin, perform the LIKE query on the specified field&lt;br /&gt;
     if search_by == 'role'&lt;br /&gt;
       User.joins(:role).where(&amp;quot;roles.name LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;)&lt;br /&gt;
     else&lt;br /&gt;
       User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;)&lt;br /&gt;
     end&lt;br /&gt;
   else&lt;br /&gt;
     # If the user is not a super admin, return a message indicating unauthorized&lt;br /&gt;
     'Not Authorized'&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method, login, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
==== Parameters ====&lt;br /&gt;
- `user_id`: The identifier of the current user.&lt;br /&gt;
- `word`: The search term.&lt;br /&gt;
- `search_by`: The type of search (e.g., 'role', 'name', etc.).&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
- If the result is a message (e.g., 'Not Authorized'), it renders an error message as JSON with an HTTP status of `:unauthorized`.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    current_user = User.find(params[:user_id])&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(current_user, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.is_a?(ActiveRecord::Relation)&lt;br /&gt;
      # If the result is a collection of users, render them as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is a message (e.g., 'Not Authorized'), render it as JSON with an error status&lt;br /&gt;
      render json: { error: result }, status: :unauthorized&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
This endpoint allows you to search for users based on various fields if the requesting user is a Super Administrator.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id''': The ID of the requesting user. This is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field. If the requesting user is not a Super Administrator, it returns the user with the given user_id.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;describe&amp;lt;/span&amp;gt; '.search_users' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
   let(:super_admin_role) { Role.find_or_create_by(name: 'Super Administrator') }&lt;br /&gt;
   let(:user) { create(:user, role: super_admin_role) }&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns users with matching roles' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When searching by role, it should return users with matching roles.&lt;br /&gt;
       result = User.search_users(user, 'Admin', 'role')&lt;br /&gt;
       expect(result).to include(user)&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns an empty array if no users match the criteria' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When no users match the specified criteria, it should return an empty array.&lt;br /&gt;
       result = User.search_users(user, 'Nonexistent', 'name')&lt;br /&gt;
       expect(result).to eq([])&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     let(:regular_user) { create(:user) }&lt;br /&gt;
     it 'returns unauthorized when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       result = User.search_users(regular_user, 'paolajones', 'name')&lt;br /&gt;
       expect(result).to eq('Not Authorized')&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:testSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151933</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151933"/>
		<updated>2023-12-01T17:12:52Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* Development Strategy */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== Revised Instructor.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''get_user_list Method:'''&lt;br /&gt;
## The 'get_user_list' method in the 'Instructor' class will be reimplemented to fetch a list of users based on the instructor's role.&lt;br /&gt;
## The method will likely consider the instructor's courses/assignments and return a list of users associated with those courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
# '''Instructor.rb Tests:'''&lt;br /&gt;
## Tests for the 'get_user_list' method in the 'Instructor' class will ensure it fetches the correct list of users based on the instructor's role and associated courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== ToDo Status: ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `role` and `paginate_list` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
This method, search_users, takes three parameters: user, word, and search_by.&lt;br /&gt;
It checks if the user has the role of &amp;quot;Super Administrator.&amp;quot;&lt;br /&gt;
If the user is a super admin, it performs a query on the User model based on the search_by parameter:&lt;br /&gt;
If search_by is 'role', it joins the Role model and looks for users with roles containing the specified word.&lt;br /&gt;
Otherwise, it looks for users with attributes specified by search_by containing the specified word.&lt;br /&gt;
If the user is not a super admin, it returns the string 'Not Authorized'.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def self.search_users(user, word, search_by)&lt;br /&gt;
   # Check if the user's role is &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
   if user.role.name == 'Super Administrator'&lt;br /&gt;
     # If the user is a super admin, perform the LIKE query on the specified field&lt;br /&gt;
     if search_by == 'role'&lt;br /&gt;
       User.joins(:role).where(&amp;quot;roles.name LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;)&lt;br /&gt;
     else&lt;br /&gt;
       User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;)&lt;br /&gt;
     end&lt;br /&gt;
   else&lt;br /&gt;
     # If the user is not a super admin, return a message indicating unauthorized&lt;br /&gt;
     'Not Authorized'&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method, login, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
==== Parameters ====&lt;br /&gt;
- `user_id`: The identifier of the current user.&lt;br /&gt;
- `word`: The search term.&lt;br /&gt;
- `search_by`: The type of search (e.g., 'role', 'name', etc.).&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
- If the result is a message (e.g., 'Not Authorized'), it renders an error message as JSON with an HTTP status of `:unauthorized`.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    current_user = User.find(params[:user_id])&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(current_user, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.is_a?(ActiveRecord::Relation)&lt;br /&gt;
      # If the result is a collection of users, render them as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is a message (e.g., 'Not Authorized'), render it as JSON with an error status&lt;br /&gt;
      render json: { error: result }, status: :unauthorized&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
This endpoint allows you to search for users based on various fields if the requesting user is a Super Administrator.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id''': The ID of the requesting user. This is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field. If the requesting user is not a Super Administrator, it returns the user with the given user_id.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;describe&amp;lt;/span&amp;gt; '.search_users' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
   let(:super_admin_role) { Role.find_or_create_by(name: 'Super Administrator') }&lt;br /&gt;
   let(:user) { create(:user, role: super_admin_role) }&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns users with matching roles' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When searching by role, it should return users with matching roles.&lt;br /&gt;
       result = User.search_users(user, 'Admin', 'role')&lt;br /&gt;
       expect(result).to include(user)&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns an empty array if no users match the criteria' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When no users match the specified criteria, it should return an empty array.&lt;br /&gt;
       result = User.search_users(user, 'Nonexistent', 'name')&lt;br /&gt;
       expect(result).to eq([])&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     let(:regular_user) { create(:user) }&lt;br /&gt;
     it 'returns unauthorized when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       result = User.search_users(regular_user, 'paolajones', 'name')&lt;br /&gt;
       expect(result).to eq('Not Authorized')&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:testSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151923</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151923"/>
		<updated>2023-12-01T17:03:30Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* Create users Using rails Console */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== Revised Instructor.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''get_user_list Method:'''&lt;br /&gt;
## The 'get_user_list' method in the 'Instructor' class will be reimplemented to fetch a list of users based on the instructor's role.&lt;br /&gt;
## The method will likely consider the instructor's courses/assignments and return a list of users associated with those courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
# '''Instructor.rb Tests:'''&lt;br /&gt;
## Tests for the 'get_user_list' method in the 'Instructor' class will ensure it fetches the correct list of users based on the instructor's role and associated courses/assignments.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `role` and `paginate_list` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[[File:DBSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
This method, search_users, takes three parameters: user, word, and search_by.&lt;br /&gt;
It checks if the user has the role of &amp;quot;Super Administrator.&amp;quot;&lt;br /&gt;
If the user is a super admin, it performs a query on the User model based on the search_by parameter:&lt;br /&gt;
If search_by is 'role', it joins the Role model and looks for users with roles containing the specified word.&lt;br /&gt;
Otherwise, it looks for users with attributes specified by search_by containing the specified word.&lt;br /&gt;
If the user is not a super admin, it returns the string 'Not Authorized'.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def self.search_users(user, word, search_by)&lt;br /&gt;
   # Check if the user's role is &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
   if user.role.name == 'Super Administrator'&lt;br /&gt;
     # If the user is a super admin, perform the LIKE query on the specified field&lt;br /&gt;
     if search_by == 'role'&lt;br /&gt;
       User.joins(:role).where(&amp;quot;roles.name LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;)&lt;br /&gt;
     else&lt;br /&gt;
       User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;)&lt;br /&gt;
     end&lt;br /&gt;
   else&lt;br /&gt;
     # If the user is not a super admin, return a message indicating unauthorized&lt;br /&gt;
     'Not Authorized'&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method, login, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
==== Parameters ====&lt;br /&gt;
- `user_id`: The identifier of the current user.&lt;br /&gt;
- `word`: The search term.&lt;br /&gt;
- `search_by`: The type of search (e.g., 'role', 'name', etc.).&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
- If the result is a message (e.g., 'Not Authorized'), it renders an error message as JSON with an HTTP status of `:unauthorized`.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    current_user = User.find(params[:user_id])&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(current_user, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.is_a?(ActiveRecord::Relation)&lt;br /&gt;
      # If the result is a collection of users, render them as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is a message (e.g., 'Not Authorized'), render it as JSON with an error status&lt;br /&gt;
      render json: { error: result }, status: :unauthorized&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
This endpoint allows you to search for users based on various fields if the requesting user is a Super Administrator.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id''': The ID of the requesting user. This is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field. If the requesting user is not a Super Administrator, it returns the user with the given user_id.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;describe&amp;lt;/span&amp;gt; '.search_users' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
   let(:super_admin_role) { Role.find_or_create_by(name: 'Super Administrator') }&lt;br /&gt;
   let(:user) { create(:user, role: super_admin_role) }&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns users with matching roles' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When searching by role, it should return users with matching roles.&lt;br /&gt;
       result = User.search_users(user, 'Admin', 'role')&lt;br /&gt;
       expect(result).to include(user)&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns an empty array if no users match the criteria' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When no users match the specified criteria, it should return an empty array.&lt;br /&gt;
       result = User.search_users(user, 'Nonexistent', 'name')&lt;br /&gt;
       expect(result).to eq([])&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     let(:regular_user) { create(:user) }&lt;br /&gt;
     it 'returns unauthorized when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       result = User.search_users(regular_user, 'paolajones', 'name')&lt;br /&gt;
       expect(result).to eq('Not Authorized')&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:testSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151922</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151922"/>
		<updated>2023-12-01T17:03:04Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== Revised Instructor.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''get_user_list Method:'''&lt;br /&gt;
## The 'get_user_list' method in the 'Instructor' class will be reimplemented to fetch a list of users based on the instructor's role.&lt;br /&gt;
## The method will likely consider the instructor's courses/assignments and return a list of users associated with those courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
# '''Instructor.rb Tests:'''&lt;br /&gt;
## Tests for the 'get_user_list' method in the 'Instructor' class will ensure it fetches the correct list of users based on the instructor's role and associated courses/assignments.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `role` and `paginate_list` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[File:DBSV.png]&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
This method, search_users, takes three parameters: user, word, and search_by.&lt;br /&gt;
It checks if the user has the role of &amp;quot;Super Administrator.&amp;quot;&lt;br /&gt;
If the user is a super admin, it performs a query on the User model based on the search_by parameter:&lt;br /&gt;
If search_by is 'role', it joins the Role model and looks for users with roles containing the specified word.&lt;br /&gt;
Otherwise, it looks for users with attributes specified by search_by containing the specified word.&lt;br /&gt;
If the user is not a super admin, it returns the string 'Not Authorized'.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def self.search_users(user, word, search_by)&lt;br /&gt;
   # Check if the user's role is &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
   if user.role.name == 'Super Administrator'&lt;br /&gt;
     # If the user is a super admin, perform the LIKE query on the specified field&lt;br /&gt;
     if search_by == 'role'&lt;br /&gt;
       User.joins(:role).where(&amp;quot;roles.name LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;)&lt;br /&gt;
     else&lt;br /&gt;
       User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;)&lt;br /&gt;
     end&lt;br /&gt;
   else&lt;br /&gt;
     # If the user is not a super admin, return a message indicating unauthorized&lt;br /&gt;
     'Not Authorized'&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method, login, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
==== Parameters ====&lt;br /&gt;
- `user_id`: The identifier of the current user.&lt;br /&gt;
- `word`: The search term.&lt;br /&gt;
- `search_by`: The type of search (e.g., 'role', 'name', etc.).&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
- If the result is a message (e.g., 'Not Authorized'), it renders an error message as JSON with an HTTP status of `:unauthorized`.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    current_user = User.find(params[:user_id])&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(current_user, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.is_a?(ActiveRecord::Relation)&lt;br /&gt;
      # If the result is a collection of users, render them as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is a message (e.g., 'Not Authorized'), render it as JSON with an error status&lt;br /&gt;
      render json: { error: result }, status: :unauthorized&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
This endpoint allows you to search for users based on various fields if the requesting user is a Super Administrator.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id''': The ID of the requesting user. This is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field. If the requesting user is not a Super Administrator, it returns the user with the given user_id.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Postman Implementation ====&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;describe&amp;lt;/span&amp;gt; '.search_users' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
   let(:super_admin_role) { Role.find_or_create_by(name: 'Super Administrator') }&lt;br /&gt;
   let(:user) { create(:user, role: super_admin_role) }&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns users with matching roles' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When searching by role, it should return users with matching roles.&lt;br /&gt;
       result = User.search_users(user, 'Admin', 'role')&lt;br /&gt;
       expect(result).to include(user)&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns an empty array if no users match the criteria' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When no users match the specified criteria, it should return an empty array.&lt;br /&gt;
       result = User.search_users(user, 'Nonexistent', 'name')&lt;br /&gt;
       expect(result).to eq([])&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     let(:regular_user) { create(:user) }&lt;br /&gt;
     it 'returns unauthorized when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       result = User.search_users(regular_user, 'paolajones', 'name')&lt;br /&gt;
       expect(result).to eq('Not Authorized')&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:testSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151921</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151921"/>
		<updated>2023-12-01T17:02:24Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* Create users Using rails Console */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== Revised Instructor.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''get_user_list Method:'''&lt;br /&gt;
## The 'get_user_list' method in the 'Instructor' class will be reimplemented to fetch a list of users based on the instructor's role.&lt;br /&gt;
## The method will likely consider the instructor's courses/assignments and return a list of users associated with those courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
# '''Instructor.rb Tests:'''&lt;br /&gt;
## Tests for the 'get_user_list' method in the 'Instructor' class will ensure it fetches the correct list of users based on the instructor's role and associated courses/assignments.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `role` and `paginate_list` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
'''Configure Compose as a remote interpreter﻿'''&lt;br /&gt;
&lt;br /&gt;
'''Connect MySQL DB and Application''' &lt;br /&gt;
&lt;br /&gt;
[File:DBSV.png]&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
This method, search_users, takes three parameters: user, word, and search_by.&lt;br /&gt;
It checks if the user has the role of &amp;quot;Super Administrator.&amp;quot;&lt;br /&gt;
If the user is a super admin, it performs a query on the User model based on the search_by parameter:&lt;br /&gt;
If search_by is 'role', it joins the Role model and looks for users with roles containing the specified word.&lt;br /&gt;
Otherwise, it looks for users with attributes specified by search_by containing the specified word.&lt;br /&gt;
If the user is not a super admin, it returns the string 'Not Authorized'.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def self.search_users(user, word, search_by)&lt;br /&gt;
   # Check if the user's role is &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
   if user.role.name == 'Super Administrator'&lt;br /&gt;
     # If the user is a super admin, perform the LIKE query on the specified field&lt;br /&gt;
     if search_by == 'role'&lt;br /&gt;
       User.joins(:role).where(&amp;quot;roles.name LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;)&lt;br /&gt;
     else&lt;br /&gt;
       User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;)&lt;br /&gt;
     end&lt;br /&gt;
   else&lt;br /&gt;
     # If the user is not a super admin, return a message indicating unauthorized&lt;br /&gt;
     'Not Authorized'&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method, login, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
==== Parameters ====&lt;br /&gt;
- `user_id`: The identifier of the current user.&lt;br /&gt;
- `word`: The search term.&lt;br /&gt;
- `search_by`: The type of search (e.g., 'role', 'name', etc.).&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
- If the result is a message (e.g., 'Not Authorized'), it renders an error message as JSON with an HTTP status of `:unauthorized`.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    current_user = User.find(params[:user_id])&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(current_user, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.is_a?(ActiveRecord::Relation)&lt;br /&gt;
      # If the result is a collection of users, render them as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is a message (e.g., 'Not Authorized'), render it as JSON with an error status&lt;br /&gt;
      render json: { error: result }, status: :unauthorized&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
This endpoint allows you to search for users based on various fields if the requesting user is a Super Administrator.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id''': The ID of the requesting user. This is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field. If the requesting user is not a Super Administrator, it returns the user with the given user_id.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;describe&amp;lt;/span&amp;gt; '.search_users' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
   let(:super_admin_role) { Role.find_or_create_by(name: 'Super Administrator') }&lt;br /&gt;
   let(:user) { create(:user, role: super_admin_role) }&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns users with matching roles' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When searching by role, it should return users with matching roles.&lt;br /&gt;
       result = User.search_users(user, 'Admin', 'role')&lt;br /&gt;
       expect(result).to include(user)&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns an empty array if no users match the criteria' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When no users match the specified criteria, it should return an empty array.&lt;br /&gt;
       result = User.search_users(user, 'Nonexistent', 'name')&lt;br /&gt;
       expect(result).to eq([])&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     let(:regular_user) { create(:user) }&lt;br /&gt;
     it 'returns unauthorized when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       result = User.search_users(regular_user, 'paolajones', 'name')&lt;br /&gt;
       expect(result).to eq('Not Authorized')&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:testSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:DBSV.png&amp;diff=151920</id>
		<title>File:DBSV.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:DBSV.png&amp;diff=151920"/>
		<updated>2023-12-01T16:58:27Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151917</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151917"/>
		<updated>2023-12-01T16:53:10Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* Test Results */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== Revised Instructor.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''get_user_list Method:'''&lt;br /&gt;
## The 'get_user_list' method in the 'Instructor' class will be reimplemented to fetch a list of users based on the instructor's role.&lt;br /&gt;
## The method will likely consider the instructor's courses/assignments and return a list of users associated with those courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
# '''Instructor.rb Tests:'''&lt;br /&gt;
## Tests for the 'get_user_list' method in the 'Instructor' class will ensure it fetches the correct list of users based on the instructor's role and associated courses/assignments.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `role` and `paginate_list` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
This method, search_users, takes three parameters: user, word, and search_by.&lt;br /&gt;
It checks if the user has the role of &amp;quot;Super Administrator.&amp;quot;&lt;br /&gt;
If the user is a super admin, it performs a query on the User model based on the search_by parameter:&lt;br /&gt;
If search_by is 'role', it joins the Role model and looks for users with roles containing the specified word.&lt;br /&gt;
Otherwise, it looks for users with attributes specified by search_by containing the specified word.&lt;br /&gt;
If the user is not a super admin, it returns the string 'Not Authorized'.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def self.search_users(user, word, search_by)&lt;br /&gt;
   # Check if the user's role is &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
   if user.role.name == 'Super Administrator'&lt;br /&gt;
     # If the user is a super admin, perform the LIKE query on the specified field&lt;br /&gt;
     if search_by == 'role'&lt;br /&gt;
       User.joins(:role).where(&amp;quot;roles.name LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;)&lt;br /&gt;
     else&lt;br /&gt;
       User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;)&lt;br /&gt;
     end&lt;br /&gt;
   else&lt;br /&gt;
     # If the user is not a super admin, return a message indicating unauthorized&lt;br /&gt;
     'Not Authorized'&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method, login, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
==== Parameters ====&lt;br /&gt;
- `user_id`: The identifier of the current user.&lt;br /&gt;
- `word`: The search term.&lt;br /&gt;
- `search_by`: The type of search (e.g., 'role', 'name', etc.).&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
- If the result is a message (e.g., 'Not Authorized'), it renders an error message as JSON with an HTTP status of `:unauthorized`.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    current_user = User.find(params[:user_id])&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(current_user, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.is_a?(ActiveRecord::Relation)&lt;br /&gt;
      # If the result is a collection of users, render them as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is a message (e.g., 'Not Authorized'), render it as JSON with an error status&lt;br /&gt;
      render json: { error: result }, status: :unauthorized&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
This endpoint allows you to search for users based on various fields if the requesting user is a Super Administrator.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id''': The ID of the requesting user. This is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field. If the requesting user is not a Super Administrator, it returns the user with the given user_id.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;describe&amp;lt;/span&amp;gt; '.search_users' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
   let(:super_admin_role) { Role.find_or_create_by(name: 'Super Administrator') }&lt;br /&gt;
   let(:user) { create(:user, role: super_admin_role) }&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns users with matching roles' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When searching by role, it should return users with matching roles.&lt;br /&gt;
       result = User.search_users(user, 'Admin', 'role')&lt;br /&gt;
       expect(result).to include(user)&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns an empty array if no users match the criteria' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When no users match the specified criteria, it should return an empty array.&lt;br /&gt;
       result = User.search_users(user, 'Nonexistent', 'name')&lt;br /&gt;
       expect(result).to eq([])&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     let(:regular_user) { create(:user) }&lt;br /&gt;
     it 'returns unauthorized when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       result = User.search_users(regular_user, 'paolajones', 'name')&lt;br /&gt;
       expect(result).to eq('Not Authorized')&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
[[File:testSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151916</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151916"/>
		<updated>2023-12-01T16:53:01Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* Test Results */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== Revised Instructor.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''get_user_list Method:'''&lt;br /&gt;
## The 'get_user_list' method in the 'Instructor' class will be reimplemented to fetch a list of users based on the instructor's role.&lt;br /&gt;
## The method will likely consider the instructor's courses/assignments and return a list of users associated with those courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
# '''Instructor.rb Tests:'''&lt;br /&gt;
## Tests for the 'get_user_list' method in the 'Instructor' class will ensure it fetches the correct list of users based on the instructor's role and associated courses/assignments.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `role` and `paginate_list` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
This method, search_users, takes three parameters: user, word, and search_by.&lt;br /&gt;
It checks if the user has the role of &amp;quot;Super Administrator.&amp;quot;&lt;br /&gt;
If the user is a super admin, it performs a query on the User model based on the search_by parameter:&lt;br /&gt;
If search_by is 'role', it joins the Role model and looks for users with roles containing the specified word.&lt;br /&gt;
Otherwise, it looks for users with attributes specified by search_by containing the specified word.&lt;br /&gt;
If the user is not a super admin, it returns the string 'Not Authorized'.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def self.search_users(user, word, search_by)&lt;br /&gt;
   # Check if the user's role is &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
   if user.role.name == 'Super Administrator'&lt;br /&gt;
     # If the user is a super admin, perform the LIKE query on the specified field&lt;br /&gt;
     if search_by == 'role'&lt;br /&gt;
       User.joins(:role).where(&amp;quot;roles.name LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;)&lt;br /&gt;
     else&lt;br /&gt;
       User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;)&lt;br /&gt;
     end&lt;br /&gt;
   else&lt;br /&gt;
     # If the user is not a super admin, return a message indicating unauthorized&lt;br /&gt;
     'Not Authorized'&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method, login, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
==== Parameters ====&lt;br /&gt;
- `user_id`: The identifier of the current user.&lt;br /&gt;
- `word`: The search term.&lt;br /&gt;
- `search_by`: The type of search (e.g., 'role', 'name', etc.).&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
- If the result is a message (e.g., 'Not Authorized'), it renders an error message as JSON with an HTTP status of `:unauthorized`.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    current_user = User.find(params[:user_id])&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(current_user, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.is_a?(ActiveRecord::Relation)&lt;br /&gt;
      # If the result is a collection of users, render them as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is a message (e.g., 'Not Authorized'), render it as JSON with an error status&lt;br /&gt;
      render json: { error: result }, status: :unauthorized&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
This endpoint allows you to search for users based on various fields if the requesting user is a Super Administrator.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id''': The ID of the requesting user. This is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field. If the requesting user is not a Super Administrator, it returns the user with the given user_id.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;describe&amp;lt;/span&amp;gt; '.search_users' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
   let(:super_admin_role) { Role.find_or_create_by(name: 'Super Administrator') }&lt;br /&gt;
   let(:user) { create(:user, role: super_admin_role) }&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns users with matching roles' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When searching by role, it should return users with matching roles.&lt;br /&gt;
       result = User.search_users(user, 'Admin', 'role')&lt;br /&gt;
       expect(result).to include(user)&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns an empty array if no users match the criteria' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When no users match the specified criteria, it should return an empty array.&lt;br /&gt;
       result = User.search_users(user, 'Nonexistent', 'name')&lt;br /&gt;
       expect(result).to eq([])&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     let(:regular_user) { create(:user) }&lt;br /&gt;
     it 'returns unauthorized when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       result = User.search_users(regular_user, 'paolajones', 'name')&lt;br /&gt;
       expect(result).to eq('Not Authorized')&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
{{#invoke:Output|output}}&lt;br /&gt;
&lt;br /&gt;
[[File:testSV.png|800px]]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151915</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151915"/>
		<updated>2023-12-01T16:52:26Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* Test Results */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== Revised Instructor.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''get_user_list Method:'''&lt;br /&gt;
## The 'get_user_list' method in the 'Instructor' class will be reimplemented to fetch a list of users based on the instructor's role.&lt;br /&gt;
## The method will likely consider the instructor's courses/assignments and return a list of users associated with those courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
# '''Instructor.rb Tests:'''&lt;br /&gt;
## Tests for the 'get_user_list' method in the 'Instructor' class will ensure it fetches the correct list of users based on the instructor's role and associated courses/assignments.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `role` and `paginate_list` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
This method, search_users, takes three parameters: user, word, and search_by.&lt;br /&gt;
It checks if the user has the role of &amp;quot;Super Administrator.&amp;quot;&lt;br /&gt;
If the user is a super admin, it performs a query on the User model based on the search_by parameter:&lt;br /&gt;
If search_by is 'role', it joins the Role model and looks for users with roles containing the specified word.&lt;br /&gt;
Otherwise, it looks for users with attributes specified by search_by containing the specified word.&lt;br /&gt;
If the user is not a super admin, it returns the string 'Not Authorized'.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def self.search_users(user, word, search_by)&lt;br /&gt;
   # Check if the user's role is &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
   if user.role.name == 'Super Administrator'&lt;br /&gt;
     # If the user is a super admin, perform the LIKE query on the specified field&lt;br /&gt;
     if search_by == 'role'&lt;br /&gt;
       User.joins(:role).where(&amp;quot;roles.name LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;)&lt;br /&gt;
     else&lt;br /&gt;
       User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;)&lt;br /&gt;
     end&lt;br /&gt;
   else&lt;br /&gt;
     # If the user is not a super admin, return a message indicating unauthorized&lt;br /&gt;
     'Not Authorized'&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method, login, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
==== Parameters ====&lt;br /&gt;
- `user_id`: The identifier of the current user.&lt;br /&gt;
- `word`: The search term.&lt;br /&gt;
- `search_by`: The type of search (e.g., 'role', 'name', etc.).&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
- If the result is a message (e.g., 'Not Authorized'), it renders an error message as JSON with an HTTP status of `:unauthorized`.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    current_user = User.find(params[:user_id])&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(current_user, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.is_a?(ActiveRecord::Relation)&lt;br /&gt;
      # If the result is a collection of users, render them as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is a message (e.g., 'Not Authorized'), render it as JSON with an error status&lt;br /&gt;
      render json: { error: result }, status: :unauthorized&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
This endpoint allows you to search for users based on various fields if the requesting user is a Super Administrator.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id''': The ID of the requesting user. This is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field. If the requesting user is not a Super Administrator, it returns the user with the given user_id.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;describe&amp;lt;/span&amp;gt; '.search_users' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
   let(:super_admin_role) { Role.find_or_create_by(name: 'Super Administrator') }&lt;br /&gt;
   let(:user) { create(:user, role: super_admin_role) }&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns users with matching roles' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When searching by role, it should return users with matching roles.&lt;br /&gt;
       result = User.search_users(user, 'Admin', 'role')&lt;br /&gt;
       expect(result).to include(user)&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns an empty array if no users match the criteria' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When no users match the specified criteria, it should return an empty array.&lt;br /&gt;
       result = User.search_users(user, 'Nonexistent', 'name')&lt;br /&gt;
       expect(result).to eq([])&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     let(:regular_user) { create(:user) }&lt;br /&gt;
     it 'returns unauthorized when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       result = User.search_users(regular_user, 'paolajones', 'name')&lt;br /&gt;
       expect(result).to eq('Not Authorized')&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
{{#invoke:Output|output}}&lt;br /&gt;
&lt;br /&gt;
[File: testSV.png]&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:TestSV.png&amp;diff=151914</id>
		<title>File:TestSV.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:TestSV.png&amp;diff=151914"/>
		<updated>2023-12-01T16:51:56Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151913</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151913"/>
		<updated>2023-12-01T16:50:02Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* user_spec.rb */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== Revised Instructor.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''get_user_list Method:'''&lt;br /&gt;
## The 'get_user_list' method in the 'Instructor' class will be reimplemented to fetch a list of users based on the instructor's role.&lt;br /&gt;
## The method will likely consider the instructor's courses/assignments and return a list of users associated with those courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
# '''Instructor.rb Tests:'''&lt;br /&gt;
## Tests for the 'get_user_list' method in the 'Instructor' class will ensure it fetches the correct list of users based on the instructor's role and associated courses/assignments.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `role` and `paginate_list` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
This method, search_users, takes three parameters: user, word, and search_by.&lt;br /&gt;
It checks if the user has the role of &amp;quot;Super Administrator.&amp;quot;&lt;br /&gt;
If the user is a super admin, it performs a query on the User model based on the search_by parameter:&lt;br /&gt;
If search_by is 'role', it joins the Role model and looks for users with roles containing the specified word.&lt;br /&gt;
Otherwise, it looks for users with attributes specified by search_by containing the specified word.&lt;br /&gt;
If the user is not a super admin, it returns the string 'Not Authorized'.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def self.search_users(user, word, search_by)&lt;br /&gt;
   # Check if the user's role is &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
   if user.role.name == 'Super Administrator'&lt;br /&gt;
     # If the user is a super admin, perform the LIKE query on the specified field&lt;br /&gt;
     if search_by == 'role'&lt;br /&gt;
       User.joins(:role).where(&amp;quot;roles.name LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;)&lt;br /&gt;
     else&lt;br /&gt;
       User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;)&lt;br /&gt;
     end&lt;br /&gt;
   else&lt;br /&gt;
     # If the user is not a super admin, return a message indicating unauthorized&lt;br /&gt;
     'Not Authorized'&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method, login, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
==== Parameters ====&lt;br /&gt;
- `user_id`: The identifier of the current user.&lt;br /&gt;
- `word`: The search term.&lt;br /&gt;
- `search_by`: The type of search (e.g., 'role', 'name', etc.).&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
- If the result is a message (e.g., 'Not Authorized'), it renders an error message as JSON with an HTTP status of `:unauthorized`.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    current_user = User.find(params[:user_id])&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(current_user, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.is_a?(ActiveRecord::Relation)&lt;br /&gt;
      # If the result is a collection of users, render them as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is a message (e.g., 'Not Authorized'), render it as JSON with an error status&lt;br /&gt;
      render json: { error: result }, status: :unauthorized&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
This endpoint allows you to search for users based on various fields if the requesting user is a Super Administrator.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id''': The ID of the requesting user. This is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field. If the requesting user is not a Super Administrator, it returns the user with the given user_id.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;describe&amp;lt;/span&amp;gt; '.search_users' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
   let(:super_admin_role) { Role.find_or_create_by(name: 'Super Administrator') }&lt;br /&gt;
   let(:user) { create(:user, role: super_admin_role) }&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns users with matching roles' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When searching by role, it should return users with matching roles.&lt;br /&gt;
       result = User.search_users(user, 'Admin', 'role')&lt;br /&gt;
       expect(result).to include(user)&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns an empty array if no users match the criteria' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When no users match the specified criteria, it should return an empty array.&lt;br /&gt;
       result = User.search_users(user, 'Nonexistent', 'name')&lt;br /&gt;
       expect(result).to eq([])&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     let(:regular_user) { create(:user) }&lt;br /&gt;
     it 'returns unauthorized when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       result = User.search_users(regular_user, 'paolajones', 'name')&lt;br /&gt;
       expect(result).to eq('Not Authorized')&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Test Results ===&lt;br /&gt;
&lt;br /&gt;
{{#invoke:Output|output}}&lt;br /&gt;
RSpec:&lt;br /&gt;
'''{{{rspec ./spec/models/user_spec.rb:60 # User.search_users when the user is a Super Administrator returns users with matching roles}}}'''&lt;br /&gt;
'''{{{rspec ./spec/models/user_spec.rb:66 # User.search_users when the user is a Super Administrator returns an empty array if no users match the criteria}}}'''&lt;br /&gt;
'''{{{rspec ./spec/models/user_spec.rb:76 # User.search_users when the user is not a Super Administrator returns unauthorized when the user is not a Super Administrator}}}'''&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
'''{{{3}}}'''&lt;br /&gt;
Failures:&lt;br /&gt;
'''{{{0}}}'''&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151912</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151912"/>
		<updated>2023-12-01T16:47:00Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* Create users Using rails Console */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== Revised Instructor.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''get_user_list Method:'''&lt;br /&gt;
## The 'get_user_list' method in the 'Instructor' class will be reimplemented to fetch a list of users based on the instructor's role.&lt;br /&gt;
## The method will likely consider the instructor's courses/assignments and return a list of users associated with those courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
# '''Instructor.rb Tests:'''&lt;br /&gt;
## Tests for the 'get_user_list' method in the 'Instructor' class will ensure it fetches the correct list of users based on the instructor's role and associated courses/assignments.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `role` and `paginate_list` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
Ruby syntax for creating a new user in a Rails application using the User model.&lt;br /&gt;
&lt;br /&gt;
 User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
 )&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
This method, search_users, takes three parameters: user, word, and search_by.&lt;br /&gt;
It checks if the user has the role of &amp;quot;Super Administrator.&amp;quot;&lt;br /&gt;
If the user is a super admin, it performs a query on the User model based on the search_by parameter:&lt;br /&gt;
If search_by is 'role', it joins the Role model and looks for users with roles containing the specified word.&lt;br /&gt;
Otherwise, it looks for users with attributes specified by search_by containing the specified word.&lt;br /&gt;
If the user is not a super admin, it returns the string 'Not Authorized'.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def self.search_users(user, word, search_by)&lt;br /&gt;
   # Check if the user's role is &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
   if user.role.name == 'Super Administrator'&lt;br /&gt;
     # If the user is a super admin, perform the LIKE query on the specified field&lt;br /&gt;
     if search_by == 'role'&lt;br /&gt;
       User.joins(:role).where(&amp;quot;roles.name LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;)&lt;br /&gt;
     else&lt;br /&gt;
       User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;)&lt;br /&gt;
     end&lt;br /&gt;
   else&lt;br /&gt;
     # If the user is not a super admin, return a message indicating unauthorized&lt;br /&gt;
     'Not Authorized'&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method, login, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
==== Parameters ====&lt;br /&gt;
- `user_id`: The identifier of the current user.&lt;br /&gt;
- `word`: The search term.&lt;br /&gt;
- `search_by`: The type of search (e.g., 'role', 'name', etc.).&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
- If the result is a message (e.g., 'Not Authorized'), it renders an error message as JSON with an HTTP status of `:unauthorized`.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    current_user = User.find(params[:user_id])&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(current_user, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.is_a?(ActiveRecord::Relation)&lt;br /&gt;
      # If the result is a collection of users, render them as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is a message (e.g., 'Not Authorized'), render it as JSON with an error status&lt;br /&gt;
      render json: { error: result }, status: :unauthorized&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
This endpoint allows you to search for users based on various fields if the requesting user is a Super Administrator.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id''': The ID of the requesting user. This is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field. If the requesting user is not a Super Administrator, it returns the user with the given user_id.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;describe&amp;lt;/span&amp;gt; '.search_users' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
   let(:super_admin_role) { Role.find_or_create_by(name: 'Super Administrator') }&lt;br /&gt;
   let(:user) { create(:user, role: super_admin_role) }&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns users with matching roles' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When searching by role, it should return users with matching roles.&lt;br /&gt;
       result = User.search_users(user, 'Admin', 'role')&lt;br /&gt;
       expect(result).to include(user)&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns an empty array if no users match the criteria' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When no users match the specified criteria, it should return an empty array.&lt;br /&gt;
       result = User.search_users(user, 'Nonexistent', 'name')&lt;br /&gt;
       expect(result).to eq([])&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     let(:regular_user) { create(:user) }&lt;br /&gt;
     it 'returns unauthorized when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       result = User.search_users(regular_user, 'paolajones', 'name')&lt;br /&gt;
       expect(result).to eq('Not Authorized')&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151911</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151911"/>
		<updated>2023-12-01T16:43:49Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* Implementation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== Revised Instructor.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''get_user_list Method:'''&lt;br /&gt;
## The 'get_user_list' method in the 'Instructor' class will be reimplemented to fetch a list of users based on the instructor's role.&lt;br /&gt;
## The method will likely consider the instructor's courses/assignments and return a list of users associated with those courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
# '''Instructor.rb Tests:'''&lt;br /&gt;
## Tests for the 'get_user_list' method in the 'Instructor' class will ensure it fetches the correct list of users based on the instructor's role and associated courses/assignments.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `role` and `paginate_list` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
=== Create users Using rails Console ===&lt;br /&gt;
&lt;br /&gt;
User.create!(&lt;br /&gt;
  name: 'newuser', # lowercase name&lt;br /&gt;
  email: 'new_user@example.com',&lt;br /&gt;
  password: 'password123',&lt;br /&gt;
  full_name: 'New User',&lt;br /&gt;
  role: Role.find_by(name: 'Super Administrator')&lt;br /&gt;
)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
This method, search_users, takes three parameters: user, word, and search_by.&lt;br /&gt;
It checks if the user has the role of &amp;quot;Super Administrator.&amp;quot;&lt;br /&gt;
If the user is a super admin, it performs a query on the User model based on the search_by parameter:&lt;br /&gt;
If search_by is 'role', it joins the Role model and looks for users with roles containing the specified word.&lt;br /&gt;
Otherwise, it looks for users with attributes specified by search_by containing the specified word.&lt;br /&gt;
If the user is not a super admin, it returns the string 'Not Authorized'.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def self.search_users(user, word, search_by)&lt;br /&gt;
   # Check if the user's role is &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
   if user.role.name == 'Super Administrator'&lt;br /&gt;
     # If the user is a super admin, perform the LIKE query on the specified field&lt;br /&gt;
     if search_by == 'role'&lt;br /&gt;
       User.joins(:role).where(&amp;quot;roles.name LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;)&lt;br /&gt;
     else&lt;br /&gt;
       User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;)&lt;br /&gt;
     end&lt;br /&gt;
   else&lt;br /&gt;
     # If the user is not a super admin, return a message indicating unauthorized&lt;br /&gt;
     'Not Authorized'&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method, login, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
==== Parameters ====&lt;br /&gt;
- `user_id`: The identifier of the current user.&lt;br /&gt;
- `word`: The search term.&lt;br /&gt;
- `search_by`: The type of search (e.g., 'role', 'name', etc.).&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
- If the result is a message (e.g., 'Not Authorized'), it renders an error message as JSON with an HTTP status of `:unauthorized`.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    current_user = User.find(params[:user_id])&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(current_user, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.is_a?(ActiveRecord::Relation)&lt;br /&gt;
      # If the result is a collection of users, render them as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is a message (e.g., 'Not Authorized'), render it as JSON with an error status&lt;br /&gt;
      render json: { error: result }, status: :unauthorized&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
This endpoint allows you to search for users based on various fields if the requesting user is a Super Administrator.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id''': The ID of the requesting user. This is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field. If the requesting user is not a Super Administrator, it returns the user with the given user_id.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;describe&amp;lt;/span&amp;gt; '.search_users' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
   let(:super_admin_role) { Role.find_or_create_by(name: 'Super Administrator') }&lt;br /&gt;
   let(:user) { create(:user, role: super_admin_role) }&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns users with matching roles' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When searching by role, it should return users with matching roles.&lt;br /&gt;
       result = User.search_users(user, 'Admin', 'role')&lt;br /&gt;
       expect(result).to include(user)&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns an empty array if no users match the criteria' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When no users match the specified criteria, it should return an empty array.&lt;br /&gt;
       result = User.search_users(user, 'Nonexistent', 'name')&lt;br /&gt;
       expect(result).to eq([])&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     let(:regular_user) { create(:user) }&lt;br /&gt;
     it 'returns unauthorized when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       result = User.search_users(regular_user, 'paolajones', 'name')&lt;br /&gt;
       expect(result).to eq('Not Authorized')&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151909</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151909"/>
		<updated>2023-12-01T16:39:18Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* Implementation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== Revised Instructor.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''get_user_list Method:'''&lt;br /&gt;
## The 'get_user_list' method in the 'Instructor' class will be reimplemented to fetch a list of users based on the instructor's role.&lt;br /&gt;
## The method will likely consider the instructor's courses/assignments and return a list of users associated with those courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
# '''Instructor.rb Tests:'''&lt;br /&gt;
## Tests for the 'get_user_list' method in the 'Instructor' class will ensure it fetches the correct list of users based on the instructor's role and associated courses/assignments.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `role` and `paginate_list` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
This method, search_users, takes three parameters: user, word, and search_by.&lt;br /&gt;
It checks if the user has the role of &amp;quot;Super Administrator.&amp;quot;&lt;br /&gt;
If the user is a super admin, it performs a query on the User model based on the search_by parameter:&lt;br /&gt;
If search_by is 'role', it joins the Role model and looks for users with roles containing the specified word.&lt;br /&gt;
Otherwise, it looks for users with attributes specified by search_by containing the specified word.&lt;br /&gt;
If the user is not a super admin, it returns the string 'Not Authorized'.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def self.search_users(user, word, search_by)&lt;br /&gt;
   # Check if the user's role is &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
   if user.role.name == 'Super Administrator'&lt;br /&gt;
     # If the user is a super admin, perform the LIKE query on the specified field&lt;br /&gt;
     if search_by == 'role'&lt;br /&gt;
       User.joins(:role).where(&amp;quot;roles.name LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;)&lt;br /&gt;
     else&lt;br /&gt;
       User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;)&lt;br /&gt;
     end&lt;br /&gt;
   else&lt;br /&gt;
     # If the user is not a super admin, return a message indicating unauthorized&lt;br /&gt;
     'Not Authorized'&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method, login, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== search_users Method ===&lt;br /&gt;
&lt;br /&gt;
This method is designed to handle user search functionality in a web application. It retrieves parameters from the request to identify the current user, search criteria, and search type. It then calls the `search_users` method from the `User` model and renders the result accordingly.&lt;br /&gt;
&lt;br /&gt;
==== Parameters ====&lt;br /&gt;
- `user_id`: The identifier of the current user.&lt;br /&gt;
- `word`: The search term.&lt;br /&gt;
- `search_by`: The type of search (e.g., 'role', 'name', etc.).&lt;br /&gt;
&lt;br /&gt;
==== Result Handling ====&lt;br /&gt;
- If the result is a collection of users (`ActiveRecord::Relation`), it renders them as JSON.&lt;br /&gt;
- If the result is a message (e.g., 'Not Authorized'), it renders an error message as JSON with an HTTP status of `:unauthorized`.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    current_user = User.find(params[:user_id])&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(current_user, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.is_a?(ActiveRecord::Relation)&lt;br /&gt;
      # If the result is a collection of users, render them as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is a message (e.g., 'Not Authorized'), render it as JSON with an error status&lt;br /&gt;
      render json: { error: result }, status: :unauthorized&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
This endpoint allows you to search for users based on various fields if the requesting user is a Super Administrator.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id''': The ID of the requesting user. This is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field. If the requesting user is not a Super Administrator, it returns the user with the given user_id.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;describe&amp;lt;/span&amp;gt; '.search_users' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
   let(:super_admin_role) { Role.find_or_create_by(name: 'Super Administrator') }&lt;br /&gt;
   let(:user) { create(:user, role: super_admin_role) }&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns users with matching roles' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When searching by role, it should return users with matching roles.&lt;br /&gt;
       result = User.search_users(user, 'Admin', 'role')&lt;br /&gt;
       expect(result).to include(user)&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns an empty array if no users match the criteria' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When no users match the specified criteria, it should return an empty array.&lt;br /&gt;
       result = User.search_users(user, 'Nonexistent', 'name')&lt;br /&gt;
       expect(result).to eq([])&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     let(:regular_user) { create(:user) }&lt;br /&gt;
     it 'returns unauthorized when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       result = User.search_users(regular_user, 'paolajones', 'name')&lt;br /&gt;
       expect(result).to eq('Not Authorized')&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151906</id>
		<title>CSC/ECE 517 Fall 2023 - E2384. Reimplement user controller.rb, user.rb and its child classes</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2023_-_E2384._Reimplement_user_controller.rb,_user.rb_and_its_child_classes&amp;diff=151906"/>
		<updated>2023-12-01T16:35:30Z</updated>

		<summary type="html">&lt;p&gt;Svargas3: /* Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Description of Project==&lt;br /&gt;
&lt;br /&gt;
The project aims at reimplementing user controller.rb, user.rb and it's associated child classes. The project reimplementing functionalities specific to the above classes and write extensive rspec tests for the same. &lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
'''Background:''' Background: The [https://github.com/expertiza/expertiza/blob/main/app/models/user.rb 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.&lt;br /&gt;
&lt;br /&gt;
'''Reimplementation (What needs to be done): '''&lt;br /&gt;
To set up the project, follow [https://github.com/expertiza/reimplementation-back-end these] instructions.&lt;br /&gt;
&lt;br /&gt;
* Reimplement the search_users, find_user methods in user.rb&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb&lt;br /&gt;
* Write thorough RSpec tests for the newly implemented functionalities&lt;br /&gt;
&lt;br /&gt;
==Objectives==&lt;br /&gt;
&lt;br /&gt;
* Reimplement methods pertaining to controller.rb, user.rb and it's associated child classes. More specifically reimplement search_users, find_user methods in user.rb.&lt;br /&gt;
* Reimplement the role, paginate_list functionality in users_controller.rb.&lt;br /&gt;
* Ensure proper naming of the methods, variables.&lt;br /&gt;
* Incorporate suggestions from project3.&lt;br /&gt;
* Ensure the PR checks are successfull and all test cases are passing.&lt;br /&gt;
* Compose comprehensive RSpec tests for the changes done to ensure the robustness and reliability of the entire system.&lt;br /&gt;
&lt;br /&gt;
== Development Strategy ==&lt;br /&gt;
We have started the development activity using TDD approach. We start by creating test cases for the functionality in hand. Since it is reimplementation, the existing code and functionality makes it much easier to select comprehensive test cases. This is followed by writing clean and simple code to pass the test cases. We are also incorporating comments we received as part of project 3. We identified few more places where naming could be better and we will be incorporating the suggested changes in the reimplementation project.&lt;br /&gt;
&lt;br /&gt;
==== Revised User.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''search_users Method:'''&lt;br /&gt;
## The 'search_users' method will be reimplemented to accept parameters such as 'role', 'user_id', 'letter', and 'search_by'.&lt;br /&gt;
## It will construct a SQL query based on the provided parameters to filter users.&lt;br /&gt;
## The method will return a list of users that match the search criteria.&lt;br /&gt;
&lt;br /&gt;
# '''find_user Method:'''&lt;br /&gt;
## The 'find_user' method will be reimplemented to locate a user based on the provided login.&lt;br /&gt;
## It will first try to find the user by email. If not found, it will attempt to find the user by name.&lt;br /&gt;
## The method will return the found user or 'nil' if no matching user is found.&lt;br /&gt;
&lt;br /&gt;
==== Revised UsersController.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''role Action:'''&lt;br /&gt;
## The 'role' action will be reimplemented to handle requests related to user roles.&lt;br /&gt;
## It may return information about the role of a specific user or perform other role-related functionalities.&lt;br /&gt;
&lt;br /&gt;
# '''paginate_list Action:'''&lt;br /&gt;
## The 'paginate_list' action will be reimplemented to handle paginated listing of users.&lt;br /&gt;
## It will retrieve a paginated list of users based on certain criteria, making it easier to display users in chunks.&lt;br /&gt;
&lt;br /&gt;
==== Revised Instructor.rb: ====&lt;br /&gt;
&lt;br /&gt;
# '''get_user_list Method:'''&lt;br /&gt;
## The 'get_user_list' method in the 'Instructor' class will be reimplemented to fetch a list of users based on the instructor's role.&lt;br /&gt;
## The method will likely consider the instructor's courses/assignments and return a list of users associated with those courses/assignments.&lt;br /&gt;
&lt;br /&gt;
==== RSpec Tests: ====&lt;br /&gt;
&lt;br /&gt;
# '''User.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'search_users' method will ensure that the method correctly filters users based on the provided parameters.&lt;br /&gt;
## Tests for the 'find_user' method will validate that it successfully locates users by email or name.&lt;br /&gt;
&lt;br /&gt;
# '''UsersController.rb Tests:'''&lt;br /&gt;
## RSpec tests for the 'role' action will validate its behavior, ensuring it returns the expected role-related information.&lt;br /&gt;
## Tests for the 'paginate_list' action will check that it correctly paginates the list of users.&lt;br /&gt;
&lt;br /&gt;
# '''Instructor.rb Tests:'''&lt;br /&gt;
## Tests for the 'get_user_list' method in the 'Instructor' class will ensure it fetches the correct list of users based on the instructor's role and associated courses/assignments.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;width: 100%;&lt;br /&gt;
!  #  ''!! Task !! Status !! Notes&lt;br /&gt;
|-&lt;br /&gt;
|1&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;search_users&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to accept parameters such as `role`, `user_id`, `letter`, and `search_by`.&lt;br /&gt;
|-&lt;br /&gt;
|2&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;find_user&amp;lt;/code&amp;gt; method in User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This method will be updated to locate a user based on the provided login.&lt;br /&gt;
|-&lt;br /&gt;
|3&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;role&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle requests related to user roles.&lt;br /&gt;
|-&lt;br /&gt;
|4&lt;br /&gt;
|Reimplement &amp;lt;code&amp;gt;paginate_list&amp;lt;/code&amp;gt; action in UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|This action will be updated to handle paginated listing of users.&lt;br /&gt;
|-&lt;br /&gt;
|5&lt;br /&gt;
|Write RSpec tests for User.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `search_users` and `find_user` methods.&lt;br /&gt;
|-&lt;br /&gt;
|6&lt;br /&gt;
|Write RSpec tests for UsersController.rb&lt;br /&gt;
|To Do&lt;br /&gt;
|New tests will be written for the `role` and `paginate_list` actions.&lt;br /&gt;
|}''&lt;br /&gt;
&lt;br /&gt;
==Test Plan==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin-left:30px&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Sr No !! Test Description &lt;br /&gt;
|-&lt;br /&gt;
| 1 || &amp;lt;strong&amp;gt;seacrh_users method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 1.1 || Scenario 1: Test user search by name&lt;br /&gt;
|-&lt;br /&gt;
| 1.2 || Scenario 2: Test user search by email&lt;br /&gt;
|-&lt;br /&gt;
| 1.3 || Scenario 3: Test user search by fullname&lt;br /&gt;
|-&lt;br /&gt;
| 2 || &amp;lt;strong&amp;gt;find_user method in User.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 2.1 || Scenario 1: Test locate user by name&lt;br /&gt;
|-&lt;br /&gt;
| 2.2 || Scenario 2: Test locate user by email&lt;br /&gt;
|-&lt;br /&gt;
| 3 || &amp;lt;strong&amp;gt;Role method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 3.1 || Scenario 1: Role instance to be invalid scenario&lt;br /&gt;
|-&lt;br /&gt;
| 3.2 || Scenario 2: Role instance to be valid scenario for student&lt;br /&gt;
|-&lt;br /&gt;
| 3.3 || Scenario 3: Role instance to be valid scenario for instructor&lt;br /&gt;
|-&lt;br /&gt;
| 3.4 || Scenario 4: Role instance to be valid scenario for admin&lt;br /&gt;
|-&lt;br /&gt;
| 4 || &amp;lt;strong&amp;gt;paginate_list method in user_controller.rb&amp;lt;/strong&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| 4.1 || Scenario 1: checks that paginate_list does not fail with controller&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || Scenario 2: checks that paginate_list does not fail with post&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Code ===&lt;br /&gt;
&lt;br /&gt;
This method, search_users, takes three parameters: user, word, and search_by.&lt;br /&gt;
It checks if the user has the role of &amp;quot;Super Administrator.&amp;quot;&lt;br /&gt;
If the user is a super admin, it performs a query on the User model based on the search_by parameter:&lt;br /&gt;
If search_by is 'role', it joins the Role model and looks for users with roles containing the specified word.&lt;br /&gt;
Otherwise, it looks for users with attributes specified by search_by containing the specified word.&lt;br /&gt;
If the user is not a super admin, it returns the string 'Not Authorized'.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def self.search_users(user, word, search_by)&lt;br /&gt;
   # Check if the user's role is &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
   if user.role.name == 'Super Administrator'&lt;br /&gt;
     # If the user is a super admin, perform the LIKE query on the specified field&lt;br /&gt;
     if search_by == 'role'&lt;br /&gt;
       User.joins(:role).where(&amp;quot;roles.name LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;)&lt;br /&gt;
     else&lt;br /&gt;
       User.where(&amp;quot;#{search_by} LIKE ?&amp;quot;, &amp;quot;%#{word}%&amp;quot;)&lt;br /&gt;
     end&lt;br /&gt;
   else&lt;br /&gt;
     # If the user is not a super admin, return a message indicating unauthorized&lt;br /&gt;
     'Not Authorized'&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method, login, was updated to fix the NIL error&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
 def login&lt;br /&gt;
   user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name])&lt;br /&gt;
 &lt;br /&gt;
   if user&amp;amp;.authenticate(params[:password])&lt;br /&gt;
     payload = {&lt;br /&gt;
       id: user.id,&lt;br /&gt;
       name: user.name,&lt;br /&gt;
       full_name: user.full_name,&lt;br /&gt;
       role: user.role&amp;amp;.name,  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
       institution_id: user.institution&amp;amp;.id  # Use safe navigation operator (&amp;amp;.) to avoid nil error&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     token = JsonWebToken.encode(payload, 24.hours.from_now)&lt;br /&gt;
     render json: { token: token }, status: :ok  # Include the actual token value in the response&lt;br /&gt;
   else&lt;br /&gt;
     render json: { error: 'Invalid username/password combination' }, status: :unauthorized&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def search_users&lt;br /&gt;
    current_user = User.find(params[:user_id])&lt;br /&gt;
    word = params[:word]&lt;br /&gt;
    search_by = params[:search_by]&lt;br /&gt;
&lt;br /&gt;
    result = User.search_users(current_user, word, search_by)&lt;br /&gt;
&lt;br /&gt;
    if result.is_a?(ActiveRecord::Relation)&lt;br /&gt;
      # If the result is a collection of users, render them as JSON&lt;br /&gt;
      render json: result&lt;br /&gt;
    else&lt;br /&gt;
      # If the result is a message (e.g., 'Not Authorized'), render it as JSON with an error status&lt;br /&gt;
      render json: { error: result }, status: :unauthorized&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== API Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Endpoint: Search Users ===&lt;br /&gt;
&lt;br /&gt;
==== Description ====&lt;br /&gt;
This endpoint allows you to search for users based on various fields if the requesting user is a Super Administrator.&lt;br /&gt;
&lt;br /&gt;
==== Request ====&lt;br /&gt;
&lt;br /&gt;
'''Method:''' GET&lt;br /&gt;
&lt;br /&gt;
'''URL:''' /api/v1/users/search_users&lt;br /&gt;
&lt;br /&gt;
'''Parameters:'''&lt;br /&gt;
&lt;br /&gt;
* '''user_id''': The ID of the requesting user. This is used to check if the user is a Super Administrator.&lt;br /&gt;
* '''word''': The search term. This is used to perform a LIKE query on the specified field.&lt;br /&gt;
* '''search_by''': The field to search by. This can be 'name', 'email', 'full_name', 'id', or 'role'.&lt;br /&gt;
&lt;br /&gt;
==== Response ====&lt;br /&gt;
&lt;br /&gt;
'''Status Code:''' 200 OK&lt;br /&gt;
&lt;br /&gt;
'''Body:''' A list of users that match the search term in the specified field. If the requesting user is not a Super Administrator, it returns the user with the given user_id.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
'''Request:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GET /api/v1/users/search_users?user_id=1&amp;amp;word=r&amp;amp;search_by=role&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Response:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;New Super Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;root@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Super Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: null,&lt;br /&gt;
            &amp;quot;name&amp;quot;: null&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Administrator&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;admin@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: true,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: true,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Administrator&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;root&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 1,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 1&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
        &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
        &amp;quot;name&amp;quot;: &amp;quot;user&amp;quot;,&lt;br /&gt;
        &amp;quot;full_name&amp;quot;: &amp;quot;Regular User&amp;quot;,&lt;br /&gt;
        &amp;quot;email&amp;quot;: &amp;quot;user@example.com&amp;quot;,&lt;br /&gt;
        &amp;quot;email_on_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_submission&amp;quot;: false,&lt;br /&gt;
        &amp;quot;email_on_review_of_review&amp;quot;: false,&lt;br /&gt;
        &amp;quot;role&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 3,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Instructor&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;parent&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;admin&amp;quot;&lt;br /&gt;
        },&lt;br /&gt;
        &amp;quot;institution&amp;quot;: {&lt;br /&gt;
            &amp;quot;id&amp;quot;: 2,&lt;br /&gt;
            &amp;quot;name&amp;quot;: &amp;quot;Institution 2&amp;quot;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Postman1.jpg|800px]]&lt;br /&gt;
&lt;br /&gt;
== Test RSpec ==&lt;br /&gt;
&lt;br /&gt;
=== user_spec.rb===&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;describe&amp;lt;/span&amp;gt; '.search_users' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
   let(:super_admin_role) { Role.find_or_create_by(name: 'Super Administrator') }&lt;br /&gt;
   let(:user) { create(:user, role: super_admin_role) }&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns users with matching roles' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When searching by role, it should return users with matching roles.&lt;br /&gt;
       result = User.search_users(user, 'Admin', 'role')&lt;br /&gt;
       expect(result).to include(user)&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
     it 'returns an empty array if no users match the criteria' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       # Test scenario: When no users match the specified criteria, it should return an empty array.&lt;br /&gt;
       result = User.search_users(user, 'Nonexistent', 'name')&lt;br /&gt;
       expect(result).to eq([])&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   context 'when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
     let(:regular_user) { create(:user) }&lt;br /&gt;
     it 'returns unauthorized when the user is not a Super Administrator' &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;do&amp;lt;/span&amp;gt;&lt;br /&gt;
       result = User.search_users(regular_user, 'paolajones', 'name')&lt;br /&gt;
       expect(result).to eq('Not Authorized')&lt;br /&gt;
     &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#008000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;end&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Team==&lt;br /&gt;
=====Mentor===== &lt;br /&gt;
* Devashish Vachhani&lt;br /&gt;
&lt;br /&gt;
=====Members===== &lt;br /&gt;
* Doddaguni, Sachin R&lt;br /&gt;
* Mahesh, Amogh&lt;br /&gt;
* Villar, Sergio Vargas&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [[Object-Oriented Design and Programming]]&lt;br /&gt;
* [https://docs.google.com/document/d/14geQRUSR5q4W8RjwQdmvcGkp17Fl5vrbRMCUWJWOjg8/edit#heading=h.6i85pe7nlq5s Final Project]&lt;/div&gt;</summary>
		<author><name>Svargas3</name></author>
	</entry>
</feed>