<?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=Mrjain</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=Mrjain"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Mrjain"/>
	<updated>2026-08-20T01:06:22Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2228._Refactor_JavaScript_on_Expertiza&amp;diff=145615</id>
		<title>CSC/ECE 517 Spring 2022 - E2228. Refactor JavaScript on Expertiza</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2228._Refactor_JavaScript_on_Expertiza&amp;diff=145615"/>
		<updated>2022-05-02T01:25:14Z</updated>

		<summary type="html">&lt;p&gt;Mrjain: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Problem Statement==&lt;br /&gt;
Refactor JavaScript on Expertiza for but not limited to the Assignment View&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
Expertiza is a software project that uses peer review to produce reusable learning items. It also allows for the uploading of practically any document type, including URLs and wiki pages, as well as team projects. Expertiza's primary language for managing views is JavaScript. However, these scripts are not well structured and present a large scope of improvement in terms of code quality and optimization.&lt;br /&gt;
&lt;br /&gt;
== Files to be modified ==&lt;br /&gt;
* assignments/edit/_due_dates.html.erb&lt;br /&gt;
* assignments/edit/_rubrics.html.erb &lt;br /&gt;
* assignments/edit/_general.html.erb &lt;br /&gt;
* assignments/edit.html.erb &lt;br /&gt;
&lt;br /&gt;
== Plan of Action ==&lt;br /&gt;
* Refactor the existing methods to ensure that it follows DRY, ensure functions are modular and follow Single Responsibility which stipulates that a method or function should only be used for one reason and thereby increases code reuse.. &lt;br /&gt;
* Use an npm package called erb to inject erb code into JavaScript code.&lt;br /&gt;
* Provide descriptive comments for each method as well as make code readable by adding proper indentation.&lt;br /&gt;
* Identify and remove functions do not serve any purpose as well as remove unnecessary documentation/methods that have been commented out.&lt;br /&gt;
* Change deprecated JavaScript syntax to ES6 syntax.&lt;br /&gt;
&lt;br /&gt;
== Issues and potential fixes ==&lt;br /&gt;
&lt;br /&gt;
===1. Code comment and fix indentation ===&lt;br /&gt;
&lt;br /&gt;
All files under consideration are poorly indented and commented. Unindented HTML code may contain bugs that are difficult to trace and make the code less readable. Additionally, it becomes hard to identify repeating codes and enforce DRY. Uncommented code makes it extremely difficult for new programmers to get hang of the application and its components. A snapshot of the existing code to illustrate this issue is shown below:&lt;br /&gt;
&lt;br /&gt;
[[File:comments.jpeg||frame|center]] &lt;br /&gt;
&lt;br /&gt;
Indent codes and add comments might fix this problem. &lt;br /&gt;
&lt;br /&gt;
===2. Ensure DRY is enforced ===&lt;br /&gt;
&lt;br /&gt;
This requires that repeating code is put in functions. There are many places throughout the code where this principle is being violated. Repeated code reduces the quality of the code, increases the file size and makes the program run slower.A snapshot of the existing code to illustrate this issue is shown below:&lt;br /&gt;
&lt;br /&gt;
[[File:dry.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
Create a parameterized function that can be called wherever the code is being repeated in order to maintain the DRY Principle. This will help in improving the code quality and make it more clean and readable.&lt;br /&gt;
&lt;br /&gt;
=== 3. Isolate Javascript ===&lt;br /&gt;
&lt;br /&gt;
For Javascript methods that do not use any erb tags, we isolate them and keep them in a separate file. This is done since we do not need embedded ruby in the Javascript function and the logic can be separated out. Performing this fix makes the code more readable and easier to handle as well as understand. A snapshot of the existing code to illustrate this issue is shown below:&lt;br /&gt;
&lt;br /&gt;
[[File:jsq.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
As can be seen in the code snippet above, all the javascript functions have been included in the .html.erb file. These can be moved to a separate directory, for instance app/assets/javascript/&lt;br /&gt;
&lt;br /&gt;
=== 4. Violating Single Responsibility for each function. ===&lt;br /&gt;
&lt;br /&gt;
Split big functions into smaller functions to ensure Single Responsibility for each function. There are many tasks that this function is performing which can be broken down into smaller functions that perform a single task. This will also help us write test cases for each function which will improve the code quality. A snapshot of the existing code to illustrate this issue is shown below:&lt;br /&gt;
&lt;br /&gt;
[[File:sr1.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:sr2.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:sr3.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:sr4.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:sr5.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:sr6.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
Break the function into smaller parts to ensure that the DRY Principle and single responsibility principle is not violated and code quality can be improved.&lt;br /&gt;
&lt;br /&gt;
=== 5. Have css in separate files ===&lt;br /&gt;
&lt;br /&gt;
The CSS code is cluttering erb files. The CSS code should be moved into a different file so that in future if there are any changes to be made in the UI which will need the CSS code to be modified, it will not affect the main erb files. A snapshot of the existing code to illustrate this issue is shown below:&lt;br /&gt;
&lt;br /&gt;
[[File:css1.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
[[File:css2.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
We plan on defining classes for different components and moving these to a separate stylesheet.&lt;br /&gt;
&lt;br /&gt;
=== 6. Remove unused and useless code ===&lt;br /&gt;
&lt;br /&gt;
Instances of unused code found in some files. Removing code blocks which have no code in them. A snapshot of the existing code to illustrate this issue is shown below:&lt;br /&gt;
&lt;br /&gt;
[[File:unused.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
=== 7. Refactor variable, function and class names ===&lt;br /&gt;
&lt;br /&gt;
Variables, classes and functions have been defined using names that do not explain their functionalities in full capacity.A snapshot of the existing code to illustrate this issue is shown below:&lt;br /&gt;
&lt;br /&gt;
[[File:changenames.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Goals Acheived ==&lt;br /&gt;
&lt;br /&gt;
* Pure js functions have been moved to assests/javascripts folder.&lt;br /&gt;
&lt;br /&gt;
* Unused functions that have been declared but not used anywhere have been removed.&lt;br /&gt;
&lt;br /&gt;
* The indentations have been fixed and comments have been added.&lt;br /&gt;
&lt;br /&gt;
* The code containing CSS script has been moved.&lt;br /&gt;
&lt;br /&gt;
== Roadblocks ==&lt;br /&gt;
&lt;br /&gt;
* The JavaScript functions take in embedded ruby content as variables. This ruby content is actually ruby objects which are accessing database content. Copying these into JavaScript is not possible, because the changes made on these JavaScript variables will not reflect on the database, as we desire (since we are working on &amp;quot;edit&amp;quot; action). &lt;br /&gt;
&lt;br /&gt;
* Secondly, Javascript is client side and Ruby is server side. Converting variables from language to another will add to network load, as this requires a client-server communication, and impact the performance. &lt;br /&gt;
&lt;br /&gt;
* Repeating codes could not be converted into callable functions because these repeating code lines have javascript and ruby variables intermingling in the javascript function. Hence it has been left as is. &lt;br /&gt;
&lt;br /&gt;
== Testing Plan ==&lt;br /&gt;
&lt;br /&gt;
This project was a refactoring project where we had to separate JavaScript code from html.erb files. All the JS code was used to render the frontend which is why there is no test cases possible.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We analyzed the entire codebase and identified potential problems with the JavaScript code. We plan on using the above mentioned solutions to fix the same. Subsequently, we plan on adding new fixes to the code as and when we find new issues.&lt;br /&gt;
&lt;br /&gt;
== Useful Links ==&lt;br /&gt;
&lt;br /&gt;
* https://expertiza.csc.ncsu.edu/index.php/CSC/ECE_517_Spring_2022_-_E2228._Refactor_JavaScript_on_Expertiza&lt;br /&gt;
* https://github.com/expertiza/expertiza/pull/2404&lt;br /&gt;
* https://github.com/KrishnaSatya10/expertiza/tree/beta&lt;br /&gt;
* https://go.ncsu.edu/e2228-refactor-javascript-on-expertiza&lt;/div&gt;</summary>
		<author><name>Mrjain</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2228._Refactor_JavaScript_on_Expertiza&amp;diff=145614</id>
		<title>CSC/ECE 517 Spring 2022 - E2228. Refactor JavaScript on Expertiza</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2228._Refactor_JavaScript_on_Expertiza&amp;diff=145614"/>
		<updated>2022-05-02T00:20:16Z</updated>

		<summary type="html">&lt;p&gt;Mrjain: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Problem Statement==&lt;br /&gt;
Refactor JavaScript on Expertiza for but not limited to the Assignment View&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
Expertiza is a software project that uses peer review to produce reusable learning items. It also allows for the uploading of practically any document type, including URLs and wiki pages, as well as team projects. Expertiza's primary language for managing views is JavaScript. However, these scripts are not well structured and present a large scope of improvement in terms of code quality and optimization.&lt;br /&gt;
&lt;br /&gt;
== Files to be modified ==&lt;br /&gt;
* assignments/edit/_due_dates.html.erb&lt;br /&gt;
* assignments/edit/_rubrics.html.erb &lt;br /&gt;
* assignments/edit/_general.html.erb &lt;br /&gt;
* assignments/edit.html.erb &lt;br /&gt;
&lt;br /&gt;
== Plan of Action ==&lt;br /&gt;
* Refactor the existing methods to ensure that it follows DRY, ensure functions are modular and follow Single Responsibility which stipulates that a method or function should only be used for one reason and thereby increases code reuse.. &lt;br /&gt;
* Use an npm package called erb to inject erb code into JavaScript code.&lt;br /&gt;
* Provide descriptive comments for each method as well as make code readable by adding proper indentation.&lt;br /&gt;
* Identify and remove functions do not serve any purpose as well as remove unnecessary documentation/methods that have been commented out.&lt;br /&gt;
* Change deprecated JavaScript syntax to ES6 syntax.&lt;br /&gt;
&lt;br /&gt;
== Issues and potential fixes ==&lt;br /&gt;
&lt;br /&gt;
===1. Code comment and fix indentation ===&lt;br /&gt;
&lt;br /&gt;
All files under consideration are poorly indented and commented. Unindented HTML code may contain bugs that are difficult to trace and make the code less readable. Additionally, it becomes hard to identify repeating codes and enforce DRY. Uncommented code makes it extremely difficult for new programmers to get hang of the application and its components. A snapshot of the existing code to illustrate this issue is shown below:&lt;br /&gt;
&lt;br /&gt;
[[File:comments.jpeg||frame|center]] &lt;br /&gt;
&lt;br /&gt;
Indent codes and add comments might fix this problem. &lt;br /&gt;
&lt;br /&gt;
===2. Ensure DRY is enforced ===&lt;br /&gt;
&lt;br /&gt;
This requires that repeating code is put in functions. There are many places throughout the code where this principle is being violated. Repeated code reduces the quality of the code, increases the file size and makes the program run slower.A snapshot of the existing code to illustrate this issue is shown below:&lt;br /&gt;
&lt;br /&gt;
[[File:dry.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
Create a parameterized function that can be called wherever the code is being repeated in order to maintain the DRY Principle. This will help in improving the code quality and make it more clean and readable.&lt;br /&gt;
&lt;br /&gt;
=== 3. Isolate Javascript ===&lt;br /&gt;
&lt;br /&gt;
For Javascript methods that do not use any erb tags, we isolate them and keep them in a separate file. This is done since we do not need embedded ruby in the Javascript function and the logic can be separated out. Performing this fix makes the code more readable and easier to handle as well as understand. A snapshot of the existing code to illustrate this issue is shown below:&lt;br /&gt;
&lt;br /&gt;
[[File:jsq.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
As can be seen in the code snippet above, all the javascript functions have been included in the .html.erb file. These can be moved to a separate directory, for instance app/assets/javascript/&lt;br /&gt;
&lt;br /&gt;
=== 4. Violating Single Responsibility for each function. ===&lt;br /&gt;
&lt;br /&gt;
Split big functions into smaller functions to ensure Single Responsibility for each function. There are many tasks that this function is performing which can be broken down into smaller functions that perform a single task. This will also help us write test cases for each function which will improve the code quality. A snapshot of the existing code to illustrate this issue is shown below:&lt;br /&gt;
&lt;br /&gt;
[[File:sr1.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:sr2.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:sr3.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:sr4.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:sr5.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:sr6.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
Break the function into smaller parts to ensure that the DRY Principle and single responsibility principle is not violated and code quality can be improved.&lt;br /&gt;
&lt;br /&gt;
=== 5. Have css in separate files ===&lt;br /&gt;
&lt;br /&gt;
The CSS code is cluttering erb files. The CSS code should be moved into a different file so that in future if there are any changes to be made in the UI which will need the CSS code to be modified, it will not affect the main erb files. A snapshot of the existing code to illustrate this issue is shown below:&lt;br /&gt;
&lt;br /&gt;
[[File:css1.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
[[File:css2.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
We plan on defining classes for different components and moving these to a separate stylesheet.&lt;br /&gt;
&lt;br /&gt;
=== 6. Remove unused and useless code ===&lt;br /&gt;
&lt;br /&gt;
Instances of unused code found in some files. Removing code blocks which have no code in them. A snapshot of the existing code to illustrate this issue is shown below:&lt;br /&gt;
&lt;br /&gt;
[[File:unused.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
=== 7. Refactor variable, function and class names ===&lt;br /&gt;
&lt;br /&gt;
Variables, classes and functions have been defined using names that do not explain their functionalities in full capacity.A snapshot of the existing code to illustrate this issue is shown below:&lt;br /&gt;
&lt;br /&gt;
[[File:changenames.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Goals Acheived ==&lt;br /&gt;
&lt;br /&gt;
* Pure js functions have been moved to assests/javascripts folder.&lt;br /&gt;
&lt;br /&gt;
* Unused functions that have been declared but not used anywhere have been removed.&lt;br /&gt;
&lt;br /&gt;
* The indentations have been fixed and comments have been added.&lt;br /&gt;
&lt;br /&gt;
* The code containing CSS script has been moved.&lt;br /&gt;
&lt;br /&gt;
== Roadblocks ==&lt;br /&gt;
&lt;br /&gt;
* The JavaScript functions take in embedded ruby content as variables. This ruby content is actually ruby objects which are accessing database content. Copying these into JavaScript is not possible, because the changes made on these JavaScript variables will not reflect on the database, as we desire (since we are working on &amp;quot;edit&amp;quot; action). &lt;br /&gt;
&lt;br /&gt;
* Secondly, Javascript is client side and Ruby is server side. Converting variables from language to another will add to network load, as this requires a client-server communication, and impact the performance. &lt;br /&gt;
&lt;br /&gt;
* Repeating codes could not be converted into callable functions because these repeating code lines have javascript and ruby variables intermingling in the javascript function. Hence it has been left as is. &lt;br /&gt;
&lt;br /&gt;
== Testing Plan ==&lt;br /&gt;
&lt;br /&gt;
This project was a refactoring project where we had to separate JavaScript code from html.erb files. All the JS code was used to render the frontend which is why there is no test cases possible.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We analyzed the entire codebase and identified potential problems with the JavaScript code. We plan on using the above mentioned solutions to fix the same. Subsequently, we plan on adding new fixes to the code as and when we find new issues.&lt;br /&gt;
&lt;br /&gt;
== Useful Links ==&lt;br /&gt;
&lt;br /&gt;
* https://expertiza.csc.ncsu.edu/index.php/CSC/ECE_517_Spring_2022_-_E2228._Refactor_JavaScript_on_Expertiza&lt;br /&gt;
* https://github.com/expertiza/expertiza/pull/2404&lt;br /&gt;
* https://github.com/KrishnaSatya10/expertiza/tree/beta&lt;/div&gt;</summary>
		<author><name>Mrjain</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2228._Refactor_JavaScript_on_Expertiza&amp;diff=145613</id>
		<title>CSC/ECE 517 Spring 2022 - E2228. Refactor JavaScript on Expertiza</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_E2228._Refactor_JavaScript_on_Expertiza&amp;diff=145613"/>
		<updated>2022-05-02T00:11:09Z</updated>

		<summary type="html">&lt;p&gt;Mrjain: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Problem Statement==&lt;br /&gt;
Refactor JavaScript on Expertiza for but not limited to the Assignment View&lt;br /&gt;
&lt;br /&gt;
== Background ==&lt;br /&gt;
Expertiza is a software project that uses peer review to produce reusable learning items. It also allows for the uploading of practically any document type, including URLs and wiki pages, as well as team projects. Expertiza's primary language for managing views is JavaScript. However, these scripts are not well structured and present a large scope of improvement in terms of code quality and optimization.&lt;br /&gt;
&lt;br /&gt;
== Files to be modified ==&lt;br /&gt;
* assignments/edit/_due_dates.html.erb&lt;br /&gt;
* assignments/edit/_rubrics.html.erb &lt;br /&gt;
* assignments/edit/_general.html.erb &lt;br /&gt;
* assignments/edit.html.erb &lt;br /&gt;
&lt;br /&gt;
== Plan of Action ==&lt;br /&gt;
* Refactor the existing methods to ensure that it follows DRY, ensure functions are modular and follow Single Responsibility which stipulates that a method or function should only be used for one reason and thereby increases code reuse.. &lt;br /&gt;
* Use an npm package called erb to inject erb code into JavaScript code.&lt;br /&gt;
* Provide descriptive comments for each method as well as make code readable by adding proper indentation.&lt;br /&gt;
* Identify and remove functions do not serve any purpose as well as remove unnecessary documentation/methods that have been commented out.&lt;br /&gt;
* Change deprecated JavaScript syntax to ES6 syntax.&lt;br /&gt;
&lt;br /&gt;
== Issues and potential fixes ==&lt;br /&gt;
&lt;br /&gt;
===1. Code comment and fix indentation ===&lt;br /&gt;
&lt;br /&gt;
All files under consideration are poorly indented and commented. Unindented HTML code may contain bugs that are difficult to trace and make the code less readable. Additionally, it becomes hard to identify repeating codes and enforce DRY. Uncommented code makes it extremely difficult for new programmers to get hang of the application and its components. A snapshot of the existing code to illustrate this issue is shown below:&lt;br /&gt;
&lt;br /&gt;
[[File:comments.jpeg||frame|center]] &lt;br /&gt;
&lt;br /&gt;
Indent codes and add comments might fix this problem. &lt;br /&gt;
&lt;br /&gt;
===2. Ensure DRY is enforced ===&lt;br /&gt;
&lt;br /&gt;
This requires that repeating code is put in functions. There are many places throughout the code where this principle is being violated. Repeated code reduces the quality of the code, increases the file size and makes the program run slower.A snapshot of the existing code to illustrate this issue is shown below:&lt;br /&gt;
&lt;br /&gt;
[[File:dry.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
Create a parameterized function that can be called wherever the code is being repeated in order to maintain the DRY Principle. This will help in improving the code quality and make it more clean and readable.&lt;br /&gt;
&lt;br /&gt;
=== 3. Isolate Javascript ===&lt;br /&gt;
&lt;br /&gt;
For Javascript methods that do not use any erb tags, we isolate them and keep them in a separate file. This is done since we do not need embedded ruby in the Javascript function and the logic can be separated out. Performing this fix makes the code more readable and easier to handle as well as understand. A snapshot of the existing code to illustrate this issue is shown below:&lt;br /&gt;
&lt;br /&gt;
[[File:jsq.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
As can be seen in the code snippet above, all the javascript functions have been included in the .html.erb file. These can be moved to a separate directory, for instance app/assets/javascript/&lt;br /&gt;
&lt;br /&gt;
=== 4. Violating Single Responsibility for each function. ===&lt;br /&gt;
&lt;br /&gt;
Split big functions into smaller functions to ensure Single Responsibility for each function. There are many tasks that this function is performing which can be broken down into smaller functions that perform a single task. This will also help us write test cases for each function which will improve the code quality. A snapshot of the existing code to illustrate this issue is shown below:&lt;br /&gt;
&lt;br /&gt;
[[File:sr1.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:sr2.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:sr3.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:sr4.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:sr5.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:sr6.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
Break the function into smaller parts to ensure that the DRY Principle and single responsibility principle is not violated and code quality can be improved.&lt;br /&gt;
&lt;br /&gt;
=== 5. Have css in separate files ===&lt;br /&gt;
&lt;br /&gt;
The CSS code is cluttering erb files. The CSS code should be moved into a different file so that in future if there are any changes to be made in the UI which will need the CSS code to be modified, it will not affect the main erb files. A snapshot of the existing code to illustrate this issue is shown below:&lt;br /&gt;
&lt;br /&gt;
[[File:css1.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
[[File:css2.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
We plan on defining classes for different components and moving these to a separate stylesheet.&lt;br /&gt;
&lt;br /&gt;
=== 6. Remove unused and useless code ===&lt;br /&gt;
&lt;br /&gt;
Instances of unused code found in some files. Removing code blocks which have no code in them. A snapshot of the existing code to illustrate this issue is shown below:&lt;br /&gt;
&lt;br /&gt;
[[File:unused.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
=== 7. Refactor variable, function and class names ===&lt;br /&gt;
&lt;br /&gt;
Variables, classes and functions have been defined using names that do not explain their functionalities in full capacity.A snapshot of the existing code to illustrate this issue is shown below:&lt;br /&gt;
&lt;br /&gt;
[[File:changenames.jpeg||frame|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Goals Acheived ==&lt;br /&gt;
&lt;br /&gt;
* Pure js functions have been moved to assests/javascripts folder.&lt;br /&gt;
&lt;br /&gt;
* Unused functions that have been declared but not used anywhere have been removed.&lt;br /&gt;
&lt;br /&gt;
* The indentations have been fixed and comments have been added.&lt;br /&gt;
&lt;br /&gt;
* The code containing CSS script has been moved.&lt;br /&gt;
&lt;br /&gt;
== Roadblocks ==&lt;br /&gt;
&lt;br /&gt;
* The JavaScript functions take in embedded ruby content as variables. This ruby content is actually ruby objects which are accessing database content. Copying these into JavaScript is not possible, because the changes made on these JavaScript variables will not reflect on the database, as we desire (since we are working on &amp;quot;edit&amp;quot; action). &lt;br /&gt;
&lt;br /&gt;
* Secondly, Javascript is client side and Ruby is server side. Converting variables from language to another will add to network load, as this requires a client-server communication, and impact the performance. &lt;br /&gt;
&lt;br /&gt;
* Repeating codes could not be converted into callable functions because these repeating code lines have javascript and ruby variables intermingling in the javascript function. Hence it has been left as is. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We analyzed the entire codebase and identified potential problems with the JavaScript code. We plan on using the above mentioned solutions to fix the same. Subsequently, we plan on adding new fixes to the code as and when we find new issues.&lt;br /&gt;
&lt;br /&gt;
== Useful Links ==&lt;br /&gt;
&lt;br /&gt;
* https://expertiza.csc.ncsu.edu/index.php/CSC/ECE_517_Spring_2022_-_E2228._Refactor_JavaScript_on_Expertiza&lt;br /&gt;
* https://github.com/expertiza/expertiza/pull/2404&lt;br /&gt;
* https://github.com/KrishnaSatya10/expertiza/tree/beta&lt;/div&gt;</summary>
		<author><name>Mrjain</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_S2222:_Refactor_impersonate_controller&amp;diff=143684</id>
		<title>CSC/ECE 517 Spring 2022 - S2222: Refactor impersonate controller</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_S2222:_Refactor_impersonate_controller&amp;diff=143684"/>
		<updated>2022-03-28T00:39:50Z</updated>

		<summary type="html">&lt;p&gt;Mrjain: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This wiki page describes the work done under E2222 OSS Program for Spring 2022, in the CSC/ECE 517 course.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==About Expertiza==&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is an open-source project based on [http://rubyonrails.org/ Ruby on Rails] framework. Expertiza is a complete instructor-student usage website where the instructor can assign assignments, deadlines, grades, etc that is required for the course(s) under him and the students can use this website to perform the tasks required as part of the course like project or assignments submission, forming groups and collaborating with them, as well as reviewing projects and teammates.&lt;br /&gt;
&lt;br /&gt;
This project focuses on a specific feature of expertiza which allows Administrators, Instructors or Teaching Assistants to impersonate other and access their account. &lt;br /&gt;
The demonstration for the feature is as shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:img1.jpeg|Figure 1|frame|center]]&lt;br /&gt;
[[File:img2.jpeg|Figure 2|frame|center]]&lt;br /&gt;
[[File:img3.jpeg|Figure 3|frame|center]]&lt;br /&gt;
         &lt;br /&gt;
== What does this controller do? ==&lt;br /&gt;
Login: We can only login as an instructor(only instructor login details are available) &lt;br /&gt;
username -&amp;gt; instructor6, password -&amp;gt; password&lt;br /&gt;
&lt;br /&gt;
* When logged in as an instructor, under the manage option as shown in Figure 1, select impersonate user. &lt;br /&gt;
* Upon redirected to impersonate page, enter the account which needs to be impersonated. &lt;br /&gt;
* It impersonates that user provided that user can be impersonated. &lt;br /&gt;
* As shown in figure 3, this can be used to revert the impersonation and return to the instructor profile.&lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
Expertiza allows administrators, instructors and Teaching Assistants to impersonate other users like a student. This allows the impersonator to view assignments, deadlines and submissions of other students. The rules to impersonating a user is, the impersonator has to be an ancestor of the impersonate. The hierarchy of impersonation is as follows: &lt;br /&gt;
&lt;br /&gt;
super administrator -&amp;gt; Administrator -&amp;gt; Instructor -&amp;gt; Teaching Assistant -&amp;gt; Student&lt;br /&gt;
&lt;br /&gt;
Note: impersonation cannot happen within the same level of hierarchy. For Example, a Super Administrator can impersonate any user apart from other Super Administrators, an Administrator can impersonate Instructors, TA, Students and not other Admins and so on. The aim of this project is to refactor the impersonate controller.&lt;br /&gt;
&lt;br /&gt;
== Major issues in the previous version of code ==&lt;br /&gt;
&lt;br /&gt;
* Long single lines of code throughout the controller.&lt;br /&gt;
* Repeated code written multiple times in different methods throughout the controller&lt;br /&gt;
* Method names used were not informative and readable.&lt;br /&gt;
* Undiscovered bug that was breaking the application(Has been reported in Expertiza).&lt;br /&gt;
* Unused method discovered.&lt;br /&gt;
&lt;br /&gt;
==What needs to be done==&lt;br /&gt;
&lt;br /&gt;
* Introduce Single Responsibility Principle - A method/function should only have one purpose to fulfill (Read more about it here). Read more about the ideal length of a function here. &lt;br /&gt;
* You’re supposed to figure out different things a method is doing, break that method into smaller methods such that each resulting method is doing only one thing.&lt;br /&gt;
* Throughout the file, the user is being initialized but never used. This is bad coding practice as it slows down runtime.&lt;br /&gt;
* Code comments are needed for each and every function. Figure out what the function is doing and write at-least 50 word descriptions for each function. &lt;br /&gt;
* Your code comments should also include the information about what the parameters are for, and what the method returns (not counted for 50 word description). &lt;br /&gt;
* Read more about how to write meaningful code comments here.&lt;br /&gt;
* Rename the methods to a more informative name. For example, There can be better names for check_if_user_impersonateable method, do_main_operation, check_if_special_char methods&lt;br /&gt;
* Refactor very long lines of code to make it more readable&lt;br /&gt;
* There are many unnecessary nesting of if statements. Convert them to elif. For example, Line 36, Line 139.&lt;br /&gt;
* Use a guard clause instead of wrapping the code inside a conditional expression. Read more about guard clauses here.&lt;br /&gt;
* A generalized exception is being rescued everywhere, which is against the coding principles that are supposed to be followed. Find what exceptions could a piece of code generate and rescue a more specialized exception.&lt;br /&gt;
* Test Functions and increase test code coverage.&lt;br /&gt;
&lt;br /&gt;
== Solution ==&lt;br /&gt;
&lt;br /&gt;
=== Original Code ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Method to overwrite the session details that are corresponding to the user or one being impersonated&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def overwrite_session&lt;br /&gt;
    # If not impersonatable, then original user's session remains&lt;br /&gt;
    if params[:impersonate].nil?&lt;br /&gt;
      # E1991 : check whether instructor is currently in anonymized view&lt;br /&gt;
      user = User.anonymized_view?(session[:ip]) ? User.real_user_from_anonymized_name(params[:user][:name]) : User.find_by(name: params[:user][:name])&lt;br /&gt;
      session[:super_user] = session[:user] if session[:super_user].nil?&lt;br /&gt;
      AuthController.clear_user_info(session, nil)&lt;br /&gt;
      session[:original_user] = @original_user&lt;br /&gt;
      session[:impersonate] = true&lt;br /&gt;
      session[:user] = user&lt;br /&gt;
    elsif !params[:impersonate][:name].empty?&lt;br /&gt;
      # E1991 : check whether instructor is currently in anonymized view&lt;br /&gt;
      user = User.anonymized_view?(session[:ip]) ? User.real_user_from_anonymized_name(params[:impersonate][:name]) : User.find_by(name: params[:impersonate][:name])&lt;br /&gt;
      AuthController.clear_user_info(session, nil)&lt;br /&gt;
      session[:user] = user&lt;br /&gt;
      session[:impersonate] = true&lt;br /&gt;
      session[:original_user] = @original_user&lt;br /&gt;
    else&lt;br /&gt;
      # E1991 : check whether instructor is currently in anonymized view&lt;br /&gt;
      AuthController.clear_user_info(session, nil)&lt;br /&gt;
      session[:user] = session[:super_user]&lt;br /&gt;
      session[:super_user] = nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def check_if_special_char&lt;br /&gt;
    if params[:user]&lt;br /&gt;
      if warn_for_special_chars(params[:user][:name], 'Username')&lt;br /&gt;
        redirect_back&lt;br /&gt;
        return&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if params[:impersonate]&lt;br /&gt;
      if warn_for_special_chars(params[:impersonate][:name], 'Username')&lt;br /&gt;
        redirect_back&lt;br /&gt;
        return&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Checking if the username provided can be impersonated or not&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def check_if_user_impersonateable&lt;br /&gt;
    if params[:impersonate].nil?&lt;br /&gt;
      # E1991 : check whether instructor is currently in anonymized view&lt;br /&gt;
      user = User.anonymized_view?(session[:ip]) ? User.real_user_from_anonymized_name(params[:user][:name]) : User.find_by(name: params[:user][:name])&lt;br /&gt;
      if !@original_user.can_impersonate? user&lt;br /&gt;
        @message = &amp;quot;You cannot impersonate '#{params[:user][:name]}'.&amp;quot;&lt;br /&gt;
        temp&lt;br /&gt;
        AuthController.clear_user_info(session, nil)&lt;br /&gt;
      else&lt;br /&gt;
        overwrite_session&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      unless params[:impersonate][:name].empty?&lt;br /&gt;
        # E1991 : check whether instructor is currently in anonymized view&lt;br /&gt;
        overwrite_session&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Main operation&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def do_main_operation(user)&lt;br /&gt;
    if user&lt;br /&gt;
      check_if_user_impersonateable&lt;br /&gt;
    else&lt;br /&gt;
      display_error_msg&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Main operation, method used to break the functions in impersonate controller and bring out 2 functionalities at same level,&lt;br /&gt;
* checking if user impersonate-able, if not throw corresponding error message&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def impersonate&lt;br /&gt;
    # Initial check to see if the username exists&lt;br /&gt;
    display_error_msg&lt;br /&gt;
    begin&lt;br /&gt;
      @original_user = session[:super_user] || session[:user]&lt;br /&gt;
      # Impersonate using form on /impersonate/start, based on the username provided, this method looks to see if that's possible by calling the do_main_operation method&lt;br /&gt;
      if params[:impersonate].nil?&lt;br /&gt;
        # Check if special chars /\?&amp;lt;&amp;gt;|&amp;amp;$# are used to avoid html tags or system command&lt;br /&gt;
        check_if_special_char&lt;br /&gt;
        # E1991 : check whether instructor is currently in anonymized view&lt;br /&gt;
        user = User.anonymized_view?(session[:ip]) ? User.real_user_from_anonymized_name(params[:user][:name]) : user = User.find_by(name: params[:user][:name])&lt;br /&gt;
        do_main_operation(user)&lt;br /&gt;
      else&lt;br /&gt;
        # Impersonate a new account&lt;br /&gt;
        if !params[:impersonate][:name].empty?&lt;br /&gt;
          # check if special chars /\?&amp;lt;&amp;gt;|&amp;amp;$# are used to avoid html tags or system command&lt;br /&gt;
          check_if_special_char&lt;br /&gt;
          # E1991 : check whether instructor is currently in anonymized view&lt;br /&gt;
          user = User.anonymized_view?(session[:ip]) ? User.real_user_from_anonymized_name(params[:impersonate][:name]) : User.find_by(name: params[:impersonate][:name])&lt;br /&gt;
          do_main_operation(user)&lt;br /&gt;
          # Revert to original account when currently in the impersonated session&lt;br /&gt;
        else&lt;br /&gt;
          if !session[:super_user].nil?&lt;br /&gt;
            AuthController.clear_user_info(session, nil)&lt;br /&gt;
            session[:user] = session[:super_user]&lt;br /&gt;
            user = session[:user]&lt;br /&gt;
            session[:super_user] = nil&lt;br /&gt;
          else&lt;br /&gt;
            display_error_msg&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      # Navigate to user's home location as the default landing page after impersonating or reverting&lt;br /&gt;
      AuthController.set_current_role(user.role_id, session)&lt;br /&gt;
      redirect_to action: AuthHelper.get_home_action(session[:user]),&lt;br /&gt;
                  controller: AuthHelper.get_home_controller(session[:user])&lt;br /&gt;
    rescue StandardError&lt;br /&gt;
      flash[:error] = @message&lt;br /&gt;
      redirect_to :back&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== New Code ===&lt;br /&gt;
* This function checks if the logged in user is a student or not. If it is a student, do not allow the impersonate mode.&lt;br /&gt;
* If the logged in user has the role or anything other than the student, we allow that user to use the impersonate mode.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
before_action :check_if_input_is_valid&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Refactored Code ===&lt;br /&gt;
&lt;br /&gt;
* Method to Generate Overwrite Session when the user tries to Impersonate another User&lt;br /&gt;
* Function created to generate session whenever needed.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def generate_session(user)&lt;br /&gt;
    AuthController.clear_user_info(session, nil)&lt;br /&gt;
    session[:original_user] = @original_user&lt;br /&gt;
    session[:impersonate] = true&lt;br /&gt;
    session[:user] = user&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Method to overwrite the session details that are corresponding to the user or one being impersonated&lt;br /&gt;
* The first 'if' statement is executed if the logged in user tried to access the impersonate feature from his account.&lt;br /&gt;
* The 'elsif' statement is executed if the user is impersonating someone and then tried to impersonate another person.&lt;br /&gt;
* Function Refactored to call the generate session method to reduce the length of the code and reduced long lines of code&lt;br /&gt;
  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def overwrite_session&lt;br /&gt;
    if params[:impersonate].nil?&lt;br /&gt;
      user = get_real_user(params[:user][:name]) &lt;br /&gt;
      session[:super_user] = session[:user] if session[:super_user].nil?&lt;br /&gt;
      generate_session(user)&lt;br /&gt;
    elsif !params[:impersonate][:name].empty?&lt;br /&gt;
      user = get_real_user(params[:impersonate][:name])&lt;br /&gt;
      generate_session(user)&lt;br /&gt;
    else&lt;br /&gt;
      session[:user] = session[:super_user]&lt;br /&gt;
      session[:super_user] = nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Checks if special characters are present in the username provided, only alphanumeric should be used&lt;br /&gt;
* warn_for_special_chars is a method in SecurityHelper class.SecurityHelper class has methods to handle this.&lt;br /&gt;
* special_chars method-Initialises string with special characters /\?&amp;lt;&amp;gt;|&amp;amp;$# .&lt;br /&gt;
* contains_special_chars method-converts it to regex and compares with the string&lt;br /&gt;
* warn_for_special_chars takes the output from above method and flashes an error if there are any special characters(/\?&amp;lt;&amp;gt;|&amp;amp;$#) in the string&lt;br /&gt;
  &lt;br /&gt;
* Function refactored to use guard clauses instead of if else nesting.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def check_if_special_char&lt;br /&gt;
    redirect_back if params[:user] &amp;amp;&amp;amp; warn_for_special_chars(params[:user][:name], 'Username')&lt;br /&gt;
    redirect_back if params[:impersonate] &amp;amp;&amp;amp; warn_for_special_chars(params[:impersonate][:name], 'Username')&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Function refactored to reduce long lines of code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def check_if_user_impersonateable&lt;br /&gt;
    if params[:impersonate].nil?&lt;br /&gt;
      user = get_real_user(params[:user][:name]) &lt;br /&gt;
      if !@original_user.can_impersonate? user&lt;br /&gt;
        @message = &amp;quot;You cannot impersonate '#{params[:user][:name]}'.&amp;quot;&lt;br /&gt;
        AuthController.clear_user_info(session, nil)&lt;br /&gt;
      else&lt;br /&gt;
        overwrite_session&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      unless params[:impersonate][:name].empty?&lt;br /&gt;
        overwrite_session&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Function name refactored to a better name&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def do_impersonate_operation(user)&lt;br /&gt;
    check_if_user_impersonateable if user&lt;br /&gt;
    display_error_msg&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Function refactored to reduce long lines of code&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def impersonate&lt;br /&gt;
    # Initial check to see if the username exists&lt;br /&gt;
    display_error_msg&lt;br /&gt;
    begin&lt;br /&gt;
      @original_user = session[:super_user] || session[:user]&lt;br /&gt;
      # Impersonate using form on /impersonate/start, based on the username provided, this method looks to see if that's possible by calling the do_main_operation method&lt;br /&gt;
      if params[:impersonate].nil?&lt;br /&gt;
        # Check if special chars /\?&amp;lt;&amp;gt;|&amp;amp;$# are used to avoid html tags or system command&lt;br /&gt;
        check_if_special_char&lt;br /&gt;
        user = get_real_user(params[:user][:name]) &lt;br /&gt;
        do_impersonate_operation(user)&lt;br /&gt;
      else&lt;br /&gt;
        # Impersonate a new account&lt;br /&gt;
        if !params[:impersonate][:name].empty?&lt;br /&gt;
          # check if special chars /\?&amp;lt;&amp;gt;|&amp;amp;$# are used to avoid html tags or system command&lt;br /&gt;
          check_if_special_char&lt;br /&gt;
          user = get_real_user(params[:impersonate][:name])&lt;br /&gt;
          do_impersonate_operation(user)&lt;br /&gt;
          # Revert to original account when currently in the impersonated session&lt;br /&gt;
        else&lt;br /&gt;
          if !session[:super_user].nil?&lt;br /&gt;
            AuthController.clear_user_info(session, nil)&lt;br /&gt;
            session[:user] = session[:super_user]&lt;br /&gt;
            user = session[:user]&lt;br /&gt;
            session[:super_user] = nil&lt;br /&gt;
          else&lt;br /&gt;
            display_error_msg&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      # Navigate to user's home location as the default landing page after impersonating or reverting&lt;br /&gt;
      AuthController.set_current_role(user.role_id, session)&lt;br /&gt;
      redirect_to action: AuthHelper.get_home_action(session[:user]),&lt;br /&gt;
                  controller: AuthHelper.get_home_controller(session[:user])&lt;br /&gt;
    rescue StandardError&lt;br /&gt;
      flash[:error] = @message&lt;br /&gt;
      redirect_to :back&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Function created to reduce long lines of code used throughout the code&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def get_real_user(name)&lt;br /&gt;
    if User.anonymized_view?(session[:ip])&lt;br /&gt;
      user = User.real_user_from_anonymized_name(name)&lt;br /&gt;
    else&lt;br /&gt;
      user = User.find_by(name: name)&lt;br /&gt;
    end&lt;br /&gt;
    return user&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Testing Plan ==&lt;br /&gt;
&lt;br /&gt;
* instructor should not be able to impersonate a super admin user with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when instructor tries to impersonate super_admin in which case parameter 'impersonate' will be nil in session parameters&lt;br /&gt;
 &lt;br /&gt;
    &amp;lt;pre&amp;gt;&lt;br /&gt;
    it 'instructor should not be able to impersonate a super admin user with their real name' do&lt;br /&gt;
      allow(User).to receive(:find_by).with(name: super_admin.name).and_return(super_admin)&lt;br /&gt;
      allow(instructor).to receive(:can_impersonate?).with(super_admin).and_return(false)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: super_admin.name } }&lt;br /&gt;
      @session = { user: instructor }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:impersonate]).to be nil&lt;br /&gt;
    end&lt;br /&gt;
    &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* instructor should be able to impersonate a user while already impersonating a user but from nav bar&lt;br /&gt;
&lt;br /&gt;
This test is to ascertain the functionality of the user being able to impersonate another user(obeying hierarchy) through the navigation bar on the top right hand corner.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
  it 'instructor should be able to impersonate a user while already impersonating a user but from nav bar' do&lt;br /&gt;
    allow(User).to receive(:find_by).with(name: student1.name).and_return(student1)&lt;br /&gt;
    allow(User).to receive(:find_by).with(name: student2.name).and_return(student2)&lt;br /&gt;
    allow(instructor).to receive(:can_impersonate?).with(student1).and_return(true)&lt;br /&gt;
    allow(instructor).to receive(:can_impersonate?).with(student2).and_return(true)&lt;br /&gt;
    request.env[&amp;quot;HTTP_REFERER&amp;quot;] = &amp;quot;http://www.example.com&amp;quot;&lt;br /&gt;
    @params = { user: { name: student1.name } }&lt;br /&gt;
    @session = { user: instructor }&lt;br /&gt;
    post :impersonate, @params, @session&lt;br /&gt;
    # nav bar uses the :impersonate as the param name, so let make sure it always works from there too.&lt;br /&gt;
    @params = { impersonate: { name: student2.name } }&lt;br /&gt;
    post :impersonate, @params, @session&lt;br /&gt;
    expect(session[:super_user]).to eq instructor&lt;br /&gt;
    expect(session[:user]).to eq student2&lt;br /&gt;
    expect(session[:original_user]).to eq instructor&lt;br /&gt;
    expect(session[:impersonate]).to be true&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* instructor should be able to impersonate a user with their anonymized name&lt;br /&gt;
&lt;br /&gt;
This test verifies the functionality that is available with from the Anonymized view. It checks for the impersonation from both the ends - Through the Manage tab as well as the Navigation Bar.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
it ‘instructor should be able to impersonate a user with their anonymized name’ do&lt;br /&gt;
    allow(User).to receive(:find_by).with(name: student1.name).and_return(student1)&lt;br /&gt;
    allow(instructor).to receive(:can_impersonate?).with(student1).and_return(true)&lt;br /&gt;
    allow(User).to receive(:anonymized_view?).and_return(true)&lt;br /&gt;
    allow(User).to receive(:real_user_from_anonymized_name).with(“Student30”).and_return(student1)&lt;br /&gt;
    request.env[“HTTP_REFERER”] = “http://www.example.com”&lt;br /&gt;
    @params = { user: { name: “Student30&amp;quot; } }&lt;br /&gt;
    @session = { user: instructor }&lt;br /&gt;
    post :impersonate, @params, @session&lt;br /&gt;
    expect(session[:super_user]).to eq instructor&lt;br /&gt;
    expect(session[:user]).to eq student1&lt;br /&gt;
    expect(session[:original_user]).to eq instructor&lt;br /&gt;
    expect(session[:impersonate]).to be true&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* instructor should not be able to impersonate an admin user with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when instructor tries to impersonate admin in which case parameter 'impersonate' will be nil in session parameters&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
it 'instructor should not be able to impersonate an admin user with their real name' do&lt;br /&gt;
      allow(User).to receive(:find_by).with(name: admin.name).and_return(admin)&lt;br /&gt;
      allow(instructor).to receive(:can_impersonate?).with(admin).and_return(false)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: admin.name } }&lt;br /&gt;
      @session = { user: instructor }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:impersonate]).to be nil&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* instructor should be able to impersonate a teaching assistant user with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when instructor tries to impersonate a teaching assistant in which case parameter 'impersonate' will be true in session parameters&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
it 'instructor should be able to impersonate a teaching assistant user with their real name' do&lt;br /&gt;
      allow(User).to receive(:find_by).with(name: teaching_assistant.name).and_return(teaching_assistant)&lt;br /&gt;
      allow(instructor).to receive(:can_impersonate?).with(teaching_assistant).and_return(true)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: teaching_assistant.name } }&lt;br /&gt;
      @session = { user: instructor }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:super_user]).to eq instructor&lt;br /&gt;
      expect(session[:user]).to eq teaching_assistant&lt;br /&gt;
      expect(session[:original_user]).to eq instructor&lt;br /&gt;
      expect(session[:impersonate]).to be true&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* teaching assistant should be able to impersonate a student with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when teaching assistant tries to impersonate a student in which case parameter 'impersonate' will be true in session parameters&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
it 'teaching assistant should be able to impersonate a student with their real name' do&lt;br /&gt;
      stub_current_user(teaching_assistant, teaching_assistant.role.name, teaching_assistant.role)&lt;br /&gt;
      allow(User).to receive(:find_by).with(name: student1.name).and_return(student1)&lt;br /&gt;
      allow(teaching_assistant).to receive(:can_impersonate?).with(student1).and_return(true)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: student1.name } }&lt;br /&gt;
      @session = { user: teaching_assistant }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:super_user]).to eq teaching_assistant&lt;br /&gt;
      expect(session[:user]).to eq student1&lt;br /&gt;
      expect(session[:original_user]).to eq teaching_assistant&lt;br /&gt;
      expect(session[:impersonate]).to be true&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* teaching assistant should not be able to impersonate an instructor with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when teaching assistant tries to impersonate an instructor in which case parameter 'impersonate' will be nil in session parameters&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
it 'teaching assistant should not be able to impersonate an instructor with their real name' do&lt;br /&gt;
      stub_current_user(teaching_assistant, teaching_assistant.role.name, teaching_assistant.role)&lt;br /&gt;
      allow(User).to receive(:find_by).with(name: instructor.name).and_return(instructor)&lt;br /&gt;
      allow(teaching_assistant).to receive(:can_impersonate?).with(instructor).and_return(false)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: instructor.name } }&lt;br /&gt;
      @session = { user: teaching_assistant }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:impersonate]).to be nil&lt;br /&gt;
    end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* teaching assistant should not be able to impersonate an admin with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when teaching assistant tries to impersonate an admin in which case parameter 'impersonate' will be nil in session parameters&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
it 'teaching assistant should not be able to impersonate an admin with their real name' do&lt;br /&gt;
      stub_current_user(teaching_assistant, teaching_assistant.role.name, teaching_assistant.role)&lt;br /&gt;
      allow(User).to receive(:find_by).with(name: admin.name).and_return(admin)&lt;br /&gt;
      allow(teaching_assistant).to receive(:can_impersonate?).with(admin).and_return(false)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: admin.name } }&lt;br /&gt;
      @session = { user: teaching_assistant }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:impersonate]).to be nil&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* teaching assistant should not be able to impersonate a super admin with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when teaching assistant tries to impersonate a super admin in which case parameter 'impersonate' will be nil in session parameters&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
it 'teaching assistant should not be able to impersonate an super admin with their real name' do&lt;br /&gt;
      stub_current_user(teaching_assistant, teaching_assistant.role.name, teaching_assistant.role)&lt;br /&gt;
      allow(User).to receive(:find_by).with(name: super_admin.name).and_return(super_admin)&lt;br /&gt;
      allow(teaching_assistant).to receive(:can_impersonate?).with(super_admin).and_return(false)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: super_admin.name } }&lt;br /&gt;
      @session = { user: teaching_assistant }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:impersonate]).to be nil&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* admin should be able to impersonate a student with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when admin tries to impersonate a student in which case parameter 'impersonate' will be true in session parameters&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
it 'admin should be able to impersonate a student with their real name' do&lt;br /&gt;
      stub_current_user(admin, admin.role.name, admin.role)       &lt;br /&gt;
      allow(User).to receive(:find_by).with(name: student1.name).and_return(student1)&lt;br /&gt;
      allow(admin).to receive(:can_impersonate?).with(student1).and_return(true)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: student1.name } }&lt;br /&gt;
      @session = { user: admin }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:super_user]).to eq admin&lt;br /&gt;
      expect(session[:user]).to eq student1&lt;br /&gt;
      expect(session[:original_user]).to eq admin&lt;br /&gt;
      expect(session[:impersonate]).to be true&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* admin should be able to impersonate a teaching assistant with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when admin tries to impersonate a teaching assistant in which case parameter 'impersonate' will be true in session parameters&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
it 'admin should be able to impersonate a teaching assistant with their real name' do&lt;br /&gt;
      stub_current_user(admin, admin.role.name, admin.role)&lt;br /&gt;
      allow(User).to receive(:find_by).with(name: teaching_assistant.name).and_return(teaching_assistant)&lt;br /&gt;
      allow(admin).to receive(:can_impersonate?).with(teaching_assistant).and_return(true)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: teaching_assistant.name } }&lt;br /&gt;
      @session = { user: admin }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:super_user]).to eq admin&lt;br /&gt;
      expect(session[:user]).to eq teaching_assistant&lt;br /&gt;
      expect(session[:original_user]).to eq admin&lt;br /&gt;
      expect(session[:impersonate]).to be true&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* admin should be able to impersonate an instructor with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when admin tries to impersonate an instructor in which case parameter 'impersonate' will be true in session parameters&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
it 'admin should be able to impersonate an instructor with their real name' do&lt;br /&gt;
      stub_current_user(admin, admin.role.name, admin.role)&lt;br /&gt;
      allow(User).to receive(:find_by).with(name: instructor.name).and_return(instructor)&lt;br /&gt;
      allow(admin).to receive(:can_impersonate?).with(instructor).and_return(true)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: instructor.name } }&lt;br /&gt;
      @session = { user: admin }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:super_user]).to eq admin&lt;br /&gt;
      expect(session[:user]).to eq instructor&lt;br /&gt;
      expect(session[:original_user]).to eq admin&lt;br /&gt;
      expect(session[:impersonate]).to be true&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* admin should not be able to impersonate a super admin with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when admin tries to impersonate a super admin in which case parameter 'impersonate' will be nil in session parameters&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
it 'admin should not be able to impersonate a super admin with their real name' do&lt;br /&gt;
      stub_current_user(admin, admin.role.name, admin.role)&lt;br /&gt;
      allow(User).to receive(:find_by).with(name: super_admin.name).and_return(super_admin)&lt;br /&gt;
      allow(admin).to receive(:can_impersonate?).with(super_admin).and_return(false)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: super_admin.name } }&lt;br /&gt;
      @session = { user: admin }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:impersonate]).to be nil&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* super admin should be able to impersonate a student with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when super admin tries to impersonate a student in which case parameter 'impersonate' will be true in session parameters&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
it 'super admin should be able to impersonate a student with their real name' do&lt;br /&gt;
      stub_current_user(super_admin, super_admin.role.name, super_admin.role)       &lt;br /&gt;
      allow(User).to receive(:find_by).with(name: student1.name).and_return(student1)&lt;br /&gt;
      allow(super_admin).to receive(:can_impersonate?).with(student1).and_return(true)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: student1.name } }&lt;br /&gt;
      @session = { user: super_admin }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:super_user]).to eq super_admin&lt;br /&gt;
      expect(session[:user]).to eq student1&lt;br /&gt;
      expect(session[:original_user]).to eq super_admin&lt;br /&gt;
      expect(session[:impersonate]).to be true&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* super admin should be able to impersonate a teaching assistant with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when super admin tries to impersonate a teaching assistant in which case parameter 'impersonate' will be true in session parameters&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
it 'super admin should be able to impersonate a teaching assistant with their real name' do&lt;br /&gt;
      stub_current_user(super_admin, super_admin.role.name, super_admin.role)&lt;br /&gt;
      allow(User).to receive(:find_by).with(name: teaching_assistant.name).and_return(teaching_assistant)&lt;br /&gt;
      allow(super_admin).to receive(:can_impersonate?).with(teaching_assistant).and_return(true)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: teaching_assistant.name } }&lt;br /&gt;
      @session = { user: super_admin }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:super_user]).to eq super_admin&lt;br /&gt;
      expect(session[:user]).to eq teaching_assistant&lt;br /&gt;
      expect(session[:original_user]).to eq super_admin&lt;br /&gt;
      expect(session[:impersonate]).to be true&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* super admin should be able to impersonate an instructor with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when super admin tries to impersonate an instructor in which case parameter 'impersonate' will be true in session parameters&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
it 'super admin should be able to impersonate an instructor with their real name' do&lt;br /&gt;
      stub_current_user(super_admin, super_admin.role.name, super_admin.role)&lt;br /&gt;
      allow(User).to receive(:find_by).with(name: instructor.name).and_return(instructor)&lt;br /&gt;
      allow(super_admin).to receive(:can_impersonate?).with(instructor).and_return(true)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: instructor.name } }&lt;br /&gt;
      @session = { user: super_admin }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:super_user]).to eq super_admin&lt;br /&gt;
      expect(session[:user]).to eq instructor&lt;br /&gt;
      expect(session[:original_user]).to eq super_admin&lt;br /&gt;
      expect(session[:impersonate]).to be true&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* super admin should be able to impersonate an admin with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when super admin tries to impersonate an admin in which case parameter 'impersonate' will be true in session parameters&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
it 'super admin should be able to impersonate an admin with their real name' do&lt;br /&gt;
      stub_current_user(super_admin, super_admin.role.name, super_admin.role)&lt;br /&gt;
      allow(User).to receive(:find_by).with(name: admin.name).and_return(admin)&lt;br /&gt;
      allow(super_admin).to receive(:can_impersonate?).with(admin).and_return(true)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: admin.name } }&lt;br /&gt;
      @session = { user: super_admin }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:super_user]).to eq super_admin&lt;br /&gt;
      expect(session[:user]).to eq admin&lt;br /&gt;
      expect(session[:original_user]).to eq super_admin&lt;br /&gt;
      expect(session[:impersonate]).to be true&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Useful Links ===&lt;br /&gt;
The following are the links to the useful pages in understanding this project(as of Spring 2022)&lt;br /&gt;
* [https://expertiza.csc.ncsu.edu/index.php/Expertiza_documentation] - General wiki describing the Expertiza project.&lt;br /&gt;
* [https://github.com/expertiza/expertiza] - GitHub repo that maintains code for Expertiza.&lt;br /&gt;
* [https://github.com/moksh98/expertiza/tree/E2222-beta] - GitHub repo for the work done in Spring 2022 for the refactoring of impersonate controller.&lt;br /&gt;
* [https://expertiza.csc.ncsu.edu/index.php/CSE/ECE_517_Spring_2021_-_E2108._Impersonate_controller.rb] - Spring 2021's wiki for the refactoring of impersonate controller.&lt;br /&gt;
* [https://github.com/expertiza/expertiza/pull/2347] - Pull request for the work done in Spring 2022.&lt;br /&gt;
* [https://github.com/expertiza/expertiza/issues/2342] - Issue that has been raised for the undiscovered bug that was breaking the application.&lt;br /&gt;
* [http://152.7.98.229:8080/] - Application Link&lt;/div&gt;</summary>
		<author><name>Mrjain</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_S2222:_Refactor_impersonate_controller&amp;diff=143682</id>
		<title>CSC/ECE 517 Spring 2022 - S2222: Refactor impersonate controller</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_S2222:_Refactor_impersonate_controller&amp;diff=143682"/>
		<updated>2022-03-28T00:37:22Z</updated>

		<summary type="html">&lt;p&gt;Mrjain: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This wiki page describes the work done under E2222 OSS Program for Spring 2022, in the CSC/ECE 517 course.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==About Expertiza==&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is an open-source project based on [http://rubyonrails.org/ Ruby on Rails] framework. Expertiza is a complete instructor-student usage website where the instructor can assign assignments, deadlines, grades, etc that is required for the course(s) under him and the students can use this website to perform the tasks required as part of the course like project or assignments submission, forming groups and collaborating with them, as well as reviewing projects and teammates.&lt;br /&gt;
&lt;br /&gt;
This project focuses on a specific feature of expertiza which allows Administrators, Instructors or Teaching Assistants to impersonate other and access their account. &lt;br /&gt;
The demonstration for the feature is as shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:img1.jpeg|Figure 1|frame|center]]&lt;br /&gt;
[[File:img2.jpeg|Figure 2|frame|center]]&lt;br /&gt;
[[File:img3.jpeg|Figure 3|frame|center]]&lt;br /&gt;
         &lt;br /&gt;
== What does this controller do? ==&lt;br /&gt;
Login: We can only login as an instructor(only instructor login details are available) &lt;br /&gt;
username -&amp;gt; instructor6, password -&amp;gt; password&lt;br /&gt;
&lt;br /&gt;
* When logged in as an instructor, under the manage option as shown in Figure 1, select impersonate user. &lt;br /&gt;
* Upon redirected to impersonate page, enter the account which needs to be impersonated. &lt;br /&gt;
* It impersonates that user provided that user can be impersonated. &lt;br /&gt;
* As shown in figure 3, this can be used to revert the impersonation and return to the instructor profile.&lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
Expertiza allows administrators, instructors and Teaching Assistants to impersonate other users like a student. This allows the impersonator to view assignments, deadlines and submissions of other students. The rules to impersonating a user is, the impersonator has to be an ancestor of the impersonate. The hierarchy of impersonation is as follows: &lt;br /&gt;
&lt;br /&gt;
super administrator -&amp;gt; Administrator -&amp;gt; Instructor -&amp;gt; Teaching Assistant -&amp;gt; Student&lt;br /&gt;
&lt;br /&gt;
Note: impersonation cannot happen within the same level of hierarchy. For Example, a Super Administrator can impersonate any user apart from other Super Administrators, an Administrator can impersonate Instructors, TA, Students and not other Admins and so on. The aim of this project is to refactor the impersonate controller.&lt;br /&gt;
&lt;br /&gt;
== Major issues in the previous version of code ==&lt;br /&gt;
&lt;br /&gt;
* Long single lines of code throughout the controller.&lt;br /&gt;
* Repeated code written multiple times in different methods throughout the controller&lt;br /&gt;
* Method names used were not informative and readable.&lt;br /&gt;
* Undiscovered bug that was breaking the application(Has been reported in Expertiza).&lt;br /&gt;
* Unused method discovered.&lt;br /&gt;
&lt;br /&gt;
==What needs to be done==&lt;br /&gt;
&lt;br /&gt;
* Introduce Single Responsibility Principle - A method/function should only have one purpose to fulfill (Read more about it here). Read more about the ideal length of a function here. &lt;br /&gt;
* You’re supposed to figure out different things a method is doing, break that method into smaller methods such that each resulting method is doing only one thing.&lt;br /&gt;
* Throughout the file, the user is being initialized but never used. This is bad coding practice as it slows down runtime.&lt;br /&gt;
* Code comments are needed for each and every function. Figure out what the function is doing and write at-least 50 word descriptions for each function. &lt;br /&gt;
* Your code comments should also include the information about what the parameters are for, and what the method returns (not counted for 50 word description). &lt;br /&gt;
* Read more about how to write meaningful code comments here.&lt;br /&gt;
* Rename the methods to a more informative name. For example, There can be better names for check_if_user_impersonateable method, do_main_operation, check_if_special_char methods&lt;br /&gt;
* Refactor very long lines of code to make it more readable&lt;br /&gt;
* There are many unnecessary nesting of if statements. Convert them to elif. For example, Line 36, Line 139.&lt;br /&gt;
* Use a guard clause instead of wrapping the code inside a conditional expression. Read more about guard clauses here.&lt;br /&gt;
* A generalized exception is being rescued everywhere, which is against the coding principles that are supposed to be followed. Find what exceptions could a piece of code generate and rescue a more specialized exception.&lt;br /&gt;
* Test Functions and increase test code coverage.&lt;br /&gt;
&lt;br /&gt;
== Solution ==&lt;br /&gt;
&lt;br /&gt;
=== Original Code ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Method to overwrite the session details that are corresponding to the user or one being impersonated&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def overwrite_session&lt;br /&gt;
    # If not impersonatable, then original user's session remains&lt;br /&gt;
    if params[:impersonate].nil?&lt;br /&gt;
      # E1991 : check whether instructor is currently in anonymized view&lt;br /&gt;
      user = User.anonymized_view?(session[:ip]) ? User.real_user_from_anonymized_name(params[:user][:name]) : User.find_by(name: params[:user][:name])&lt;br /&gt;
      session[:super_user] = session[:user] if session[:super_user].nil?&lt;br /&gt;
      AuthController.clear_user_info(session, nil)&lt;br /&gt;
      session[:original_user] = @original_user&lt;br /&gt;
      session[:impersonate] = true&lt;br /&gt;
      session[:user] = user&lt;br /&gt;
    elsif !params[:impersonate][:name].empty?&lt;br /&gt;
      # E1991 : check whether instructor is currently in anonymized view&lt;br /&gt;
      user = User.anonymized_view?(session[:ip]) ? User.real_user_from_anonymized_name(params[:impersonate][:name]) : User.find_by(name: params[:impersonate][:name])&lt;br /&gt;
      AuthController.clear_user_info(session, nil)&lt;br /&gt;
      session[:user] = user&lt;br /&gt;
      session[:impersonate] = true&lt;br /&gt;
      session[:original_user] = @original_user&lt;br /&gt;
    else&lt;br /&gt;
      # E1991 : check whether instructor is currently in anonymized view&lt;br /&gt;
      AuthController.clear_user_info(session, nil)&lt;br /&gt;
      session[:user] = session[:super_user]&lt;br /&gt;
      session[:super_user] = nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def check_if_special_char&lt;br /&gt;
    if params[:user]&lt;br /&gt;
      if warn_for_special_chars(params[:user][:name], 'Username')&lt;br /&gt;
        redirect_back&lt;br /&gt;
        return&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if params[:impersonate]&lt;br /&gt;
      if warn_for_special_chars(params[:impersonate][:name], 'Username')&lt;br /&gt;
        redirect_back&lt;br /&gt;
        return&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Checking if the username provided can be impersonated or not&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def check_if_user_impersonateable&lt;br /&gt;
    if params[:impersonate].nil?&lt;br /&gt;
      # E1991 : check whether instructor is currently in anonymized view&lt;br /&gt;
      user = User.anonymized_view?(session[:ip]) ? User.real_user_from_anonymized_name(params[:user][:name]) : User.find_by(name: params[:user][:name])&lt;br /&gt;
      if !@original_user.can_impersonate? user&lt;br /&gt;
        @message = &amp;quot;You cannot impersonate '#{params[:user][:name]}'.&amp;quot;&lt;br /&gt;
        temp&lt;br /&gt;
        AuthController.clear_user_info(session, nil)&lt;br /&gt;
      else&lt;br /&gt;
        overwrite_session&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      unless params[:impersonate][:name].empty?&lt;br /&gt;
        # E1991 : check whether instructor is currently in anonymized view&lt;br /&gt;
        overwrite_session&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Main operation&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def do_main_operation(user)&lt;br /&gt;
    if user&lt;br /&gt;
      check_if_user_impersonateable&lt;br /&gt;
    else&lt;br /&gt;
      display_error_msg&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Main operation, method used to break the functions in impersonate controller and bring out 2 functionalities at same level,&lt;br /&gt;
* checking if user impersonate-able, if not throw corresponding error message&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def impersonate&lt;br /&gt;
    # Initial check to see if the username exists&lt;br /&gt;
    display_error_msg&lt;br /&gt;
    begin&lt;br /&gt;
      @original_user = session[:super_user] || session[:user]&lt;br /&gt;
      # Impersonate using form on /impersonate/start, based on the username provided, this method looks to see if that's possible by calling the do_main_operation method&lt;br /&gt;
      if params[:impersonate].nil?&lt;br /&gt;
        # Check if special chars /\?&amp;lt;&amp;gt;|&amp;amp;$# are used to avoid html tags or system command&lt;br /&gt;
        check_if_special_char&lt;br /&gt;
        # E1991 : check whether instructor is currently in anonymized view&lt;br /&gt;
        user = User.anonymized_view?(session[:ip]) ? User.real_user_from_anonymized_name(params[:user][:name]) : user = User.find_by(name: params[:user][:name])&lt;br /&gt;
        do_main_operation(user)&lt;br /&gt;
      else&lt;br /&gt;
        # Impersonate a new account&lt;br /&gt;
        if !params[:impersonate][:name].empty?&lt;br /&gt;
          # check if special chars /\?&amp;lt;&amp;gt;|&amp;amp;$# are used to avoid html tags or system command&lt;br /&gt;
          check_if_special_char&lt;br /&gt;
          # E1991 : check whether instructor is currently in anonymized view&lt;br /&gt;
          user = User.anonymized_view?(session[:ip]) ? User.real_user_from_anonymized_name(params[:impersonate][:name]) : User.find_by(name: params[:impersonate][:name])&lt;br /&gt;
          do_main_operation(user)&lt;br /&gt;
          # Revert to original account when currently in the impersonated session&lt;br /&gt;
        else&lt;br /&gt;
          if !session[:super_user].nil?&lt;br /&gt;
            AuthController.clear_user_info(session, nil)&lt;br /&gt;
            session[:user] = session[:super_user]&lt;br /&gt;
            user = session[:user]&lt;br /&gt;
            session[:super_user] = nil&lt;br /&gt;
          else&lt;br /&gt;
            display_error_msg&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      # Navigate to user's home location as the default landing page after impersonating or reverting&lt;br /&gt;
      AuthController.set_current_role(user.role_id, session)&lt;br /&gt;
      redirect_to action: AuthHelper.get_home_action(session[:user]),&lt;br /&gt;
                  controller: AuthHelper.get_home_controller(session[:user])&lt;br /&gt;
    rescue StandardError&lt;br /&gt;
      flash[:error] = @message&lt;br /&gt;
      redirect_to :back&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== New Code ===&lt;br /&gt;
* This function checks if the logged in user is a student or not. If it is a student, do not allow the impersonate mode.&lt;br /&gt;
* If the logged in user has the role or anything other than the student, we allow that user to use the impersonate mode.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
before_action :check_if_input_is_valid&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Refactored Code ===&lt;br /&gt;
&lt;br /&gt;
* Method to Generate Overwrite Session when the user tries to Impersonate another User&lt;br /&gt;
* Function created to generate session whenever needed.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def generate_session(user)&lt;br /&gt;
    AuthController.clear_user_info(session, nil)&lt;br /&gt;
    session[:original_user] = @original_user&lt;br /&gt;
    session[:impersonate] = true&lt;br /&gt;
    session[:user] = user&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Method to overwrite the session details that are corresponding to the user or one being impersonated&lt;br /&gt;
* The first 'if' statement is executed if the logged in user tried to access the impersonate feature from his account.&lt;br /&gt;
* The 'elsif' statement is executed if the user is impersonating someone and then tried to impersonate another person.&lt;br /&gt;
* Function Refactored to call the generate session method to reduce the length of the code and reduced long lines of code&lt;br /&gt;
  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def overwrite_session&lt;br /&gt;
    if params[:impersonate].nil?&lt;br /&gt;
      user = get_real_user(params[:user][:name]) &lt;br /&gt;
      session[:super_user] = session[:user] if session[:super_user].nil?&lt;br /&gt;
      generate_session(user)&lt;br /&gt;
    elsif !params[:impersonate][:name].empty?&lt;br /&gt;
      user = get_real_user(params[:impersonate][:name])&lt;br /&gt;
      generate_session(user)&lt;br /&gt;
    else&lt;br /&gt;
      session[:user] = session[:super_user]&lt;br /&gt;
      session[:super_user] = nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Checks if special characters are present in the username provided, only alphanumeric should be used&lt;br /&gt;
* warn_for_special_chars is a method in SecurityHelper class.SecurityHelper class has methods to handle this.&lt;br /&gt;
* special_chars method-Initialises string with special characters /\?&amp;lt;&amp;gt;|&amp;amp;$# .&lt;br /&gt;
* contains_special_chars method-converts it to regex and compares with the string&lt;br /&gt;
* warn_for_special_chars takes the output from above method and flashes an error if there are any special characters(/\?&amp;lt;&amp;gt;|&amp;amp;$#) in the string&lt;br /&gt;
  &lt;br /&gt;
* Function refactored to use guard clauses instead of if else nesting.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def check_if_special_char&lt;br /&gt;
    redirect_back if params[:user] &amp;amp;&amp;amp; warn_for_special_chars(params[:user][:name], 'Username')&lt;br /&gt;
    redirect_back if params[:impersonate] &amp;amp;&amp;amp; warn_for_special_chars(params[:impersonate][:name], 'Username')&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Function refactored to reduce long lines of code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def check_if_user_impersonateable&lt;br /&gt;
    if params[:impersonate].nil?&lt;br /&gt;
      user = get_real_user(params[:user][:name]) &lt;br /&gt;
      if !@original_user.can_impersonate? user&lt;br /&gt;
        @message = &amp;quot;You cannot impersonate '#{params[:user][:name]}'.&amp;quot;&lt;br /&gt;
        AuthController.clear_user_info(session, nil)&lt;br /&gt;
      else&lt;br /&gt;
        overwrite_session&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      unless params[:impersonate][:name].empty?&lt;br /&gt;
        overwrite_session&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Function name refactored to a better name&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def do_impersonate_operation(user)&lt;br /&gt;
    check_if_user_impersonateable if user&lt;br /&gt;
    display_error_msg&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Function refactored to reduce long lines of code&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def impersonate&lt;br /&gt;
    # Initial check to see if the username exists&lt;br /&gt;
    display_error_msg&lt;br /&gt;
    begin&lt;br /&gt;
      @original_user = session[:super_user] || session[:user]&lt;br /&gt;
      # Impersonate using form on /impersonate/start, based on the username provided, this method looks to see if that's possible by calling the do_main_operation method&lt;br /&gt;
      if params[:impersonate].nil?&lt;br /&gt;
        # Check if special chars /\?&amp;lt;&amp;gt;|&amp;amp;$# are used to avoid html tags or system command&lt;br /&gt;
        check_if_special_char&lt;br /&gt;
        user = get_real_user(params[:user][:name]) &lt;br /&gt;
        do_impersonate_operation(user)&lt;br /&gt;
      else&lt;br /&gt;
        # Impersonate a new account&lt;br /&gt;
        if !params[:impersonate][:name].empty?&lt;br /&gt;
          # check if special chars /\?&amp;lt;&amp;gt;|&amp;amp;$# are used to avoid html tags or system command&lt;br /&gt;
          check_if_special_char&lt;br /&gt;
          user = get_real_user(params[:impersonate][:name])&lt;br /&gt;
          do_impersonate_operation(user)&lt;br /&gt;
          # Revert to original account when currently in the impersonated session&lt;br /&gt;
        else&lt;br /&gt;
          if !session[:super_user].nil?&lt;br /&gt;
            AuthController.clear_user_info(session, nil)&lt;br /&gt;
            session[:user] = session[:super_user]&lt;br /&gt;
            user = session[:user]&lt;br /&gt;
            session[:super_user] = nil&lt;br /&gt;
          else&lt;br /&gt;
            display_error_msg&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      # Navigate to user's home location as the default landing page after impersonating or reverting&lt;br /&gt;
      AuthController.set_current_role(user.role_id, session)&lt;br /&gt;
      redirect_to action: AuthHelper.get_home_action(session[:user]),&lt;br /&gt;
                  controller: AuthHelper.get_home_controller(session[:user])&lt;br /&gt;
    rescue StandardError&lt;br /&gt;
      flash[:error] = @message&lt;br /&gt;
      redirect_to :back&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Function created to reduce long lines of code used throughout the code&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def get_real_user(name)&lt;br /&gt;
    if User.anonymized_view?(session[:ip])&lt;br /&gt;
      user = User.real_user_from_anonymized_name(name)&lt;br /&gt;
    else&lt;br /&gt;
      user = User.find_by(name: name)&lt;br /&gt;
    end&lt;br /&gt;
    return user&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Testing Plan ==&lt;br /&gt;
&lt;br /&gt;
* instructor should not be able to impersonate a super admin user with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when instructor tries to impersonate super_admin in which case parameter 'impersonate' will be nil in session parameters&lt;br /&gt;
 &lt;br /&gt;
    &amp;lt;pre&amp;gt;&lt;br /&gt;
    it 'instructor should not be able to impersonate a super admin user with their real name' do&lt;br /&gt;
      allow(User).to receive(:find_by).with(name: super_admin.name).and_return(super_admin)&lt;br /&gt;
      allow(instructor).to receive(:can_impersonate?).with(super_admin).and_return(false)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: super_admin.name } }&lt;br /&gt;
      @session = { user: instructor }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:impersonate]).to be nil&lt;br /&gt;
    end&lt;br /&gt;
    &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* instructor should be able to impersonate a user while already impersonating a user but from nav bar&lt;br /&gt;
&lt;br /&gt;
This test is to ascertain the functionality of the user being able to impersonate another user(obeying hierarchy) through the navigation bar on the top right hand corner.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
  it 'instructor should be able to impersonate a user while already impersonating a user but from nav bar' do&lt;br /&gt;
    allow(User).to receive(:find_by).with(name: student1.name).and_return(student1)&lt;br /&gt;
    allow(User).to receive(:find_by).with(name: student2.name).and_return(student2)&lt;br /&gt;
    allow(instructor).to receive(:can_impersonate?).with(student1).and_return(true)&lt;br /&gt;
    allow(instructor).to receive(:can_impersonate?).with(student2).and_return(true)&lt;br /&gt;
    request.env[&amp;quot;HTTP_REFERER&amp;quot;] = &amp;quot;http://www.example.com&amp;quot;&lt;br /&gt;
    @params = { user: { name: student1.name } }&lt;br /&gt;
    @session = { user: instructor }&lt;br /&gt;
    post :impersonate, @params, @session&lt;br /&gt;
    # nav bar uses the :impersonate as the param name, so let make sure it always works from there too.&lt;br /&gt;
    @params = { impersonate: { name: student2.name } }&lt;br /&gt;
    post :impersonate, @params, @session&lt;br /&gt;
    expect(session[:super_user]).to eq instructor&lt;br /&gt;
    expect(session[:user]).to eq student2&lt;br /&gt;
    expect(session[:original_user]).to eq instructor&lt;br /&gt;
    expect(session[:impersonate]).to be true&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* instructor should be able to impersonate a user with their anonymized name&lt;br /&gt;
&lt;br /&gt;
This test verifies the functionality that is available with from the Anonymized view. It checks for the impersonation from both the ends - Through the Manage tab as well as the Navigation Bar.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
it ‘instructor should be able to impersonate a user with their anonymized name’ do&lt;br /&gt;
    allow(User).to receive(:find_by).with(name: student1.name).and_return(student1)&lt;br /&gt;
    allow(instructor).to receive(:can_impersonate?).with(student1).and_return(true)&lt;br /&gt;
    allow(User).to receive(:anonymized_view?).and_return(true)&lt;br /&gt;
    allow(User).to receive(:real_user_from_anonymized_name).with(“Student30”).and_return(student1)&lt;br /&gt;
    request.env[“HTTP_REFERER”] = “http://www.example.com”&lt;br /&gt;
    @params = { user: { name: “Student30&amp;quot; } }&lt;br /&gt;
    @session = { user: instructor }&lt;br /&gt;
    post :impersonate, @params, @session&lt;br /&gt;
    expect(session[:super_user]).to eq instructor&lt;br /&gt;
    expect(session[:user]).to eq student1&lt;br /&gt;
    expect(session[:original_user]).to eq instructor&lt;br /&gt;
    expect(session[:impersonate]).to be true&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* instructor should not be able to impersonate an admin user with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when instructor tries to impersonate admin in which case parameter 'impersonate' will be nil in session parameters&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
it 'instructor should not be able to impersonate an admin user with their real name' do&lt;br /&gt;
      allow(User).to receive(:find_by).with(name: admin.name).and_return(admin)&lt;br /&gt;
      allow(instructor).to receive(:can_impersonate?).with(admin).and_return(false)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: admin.name } }&lt;br /&gt;
      @session = { user: instructor }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:impersonate]).to be nil&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* instructor should be able to impersonate a teaching assistant user with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when instructor tries to impersonate a teaching assistant in which case parameter 'impersonate' will be true in session parameters&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
it 'instructor should be able to impersonate a teaching assistant user with their real name' do&lt;br /&gt;
      allow(User).to receive(:find_by).with(name: teaching_assistant.name).and_return(teaching_assistant)&lt;br /&gt;
      allow(instructor).to receive(:can_impersonate?).with(teaching_assistant).and_return(true)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: teaching_assistant.name } }&lt;br /&gt;
      @session = { user: instructor }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:super_user]).to eq instructor&lt;br /&gt;
      expect(session[:user]).to eq teaching_assistant&lt;br /&gt;
      expect(session[:original_user]).to eq instructor&lt;br /&gt;
      expect(session[:impersonate]).to be true&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* teaching assistant should be able to impersonate a student with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when teaching assistant tries to impersonate a student in which case parameter 'impersonate' will be true in session parameters&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
it 'teaching assistant should be able to impersonate a student with their real name' do&lt;br /&gt;
      stub_current_user(teaching_assistant, teaching_assistant.role.name, teaching_assistant.role)&lt;br /&gt;
      allow(User).to receive(:find_by).with(name: student1.name).and_return(student1)&lt;br /&gt;
      allow(teaching_assistant).to receive(:can_impersonate?).with(student1).and_return(true)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: student1.name } }&lt;br /&gt;
      @session = { user: teaching_assistant }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:super_user]).to eq teaching_assistant&lt;br /&gt;
      expect(session[:user]).to eq student1&lt;br /&gt;
      expect(session[:original_user]).to eq teaching_assistant&lt;br /&gt;
      expect(session[:impersonate]).to be true&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* teaching assistant should not be able to impersonate an instructor with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when teaching assistant tries to impersonate an instructor in which case parameter 'impersonate' will be nil in session parameters&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
it 'teaching assistant should not be able to impersonate an instructor with their real name' do&lt;br /&gt;
      stub_current_user(teaching_assistant, teaching_assistant.role.name, teaching_assistant.role)&lt;br /&gt;
      allow(User).to receive(:find_by).with(name: instructor.name).and_return(instructor)&lt;br /&gt;
      allow(teaching_assistant).to receive(:can_impersonate?).with(instructor).and_return(false)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: instructor.name } }&lt;br /&gt;
      @session = { user: teaching_assistant }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:impersonate]).to be nil&lt;br /&gt;
    end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* teaching assistant should not be able to impersonate an admin with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when teaching assistant tries to impersonate an admin in which case parameter 'impersonate' will be nil in session parameters&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
it 'teaching assistant should not be able to impersonate an admin with their real name' do&lt;br /&gt;
      stub_current_user(teaching_assistant, teaching_assistant.role.name, teaching_assistant.role)&lt;br /&gt;
      allow(User).to receive(:find_by).with(name: admin.name).and_return(admin)&lt;br /&gt;
      allow(teaching_assistant).to receive(:can_impersonate?).with(admin).and_return(false)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: admin.name } }&lt;br /&gt;
      @session = { user: teaching_assistant }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:impersonate]).to be nil&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* teaching assistant should not be able to impersonate a super admin with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when teaching assistant tries to impersonate a super admin in which case parameter 'impersonate' will be nil in session parameters&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
it 'teaching assistant should not be able to impersonate an super admin with their real name' do&lt;br /&gt;
      stub_current_user(teaching_assistant, teaching_assistant.role.name, teaching_assistant.role)&lt;br /&gt;
      allow(User).to receive(:find_by).with(name: super_admin.name).and_return(super_admin)&lt;br /&gt;
      allow(teaching_assistant).to receive(:can_impersonate?).with(super_admin).and_return(false)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: super_admin.name } }&lt;br /&gt;
      @session = { user: teaching_assistant }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:impersonate]).to be nil&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* admin should be able to impersonate a student with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when admin tries to impersonate a student in which case parameter 'impersonate' will be true in session parameters&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
it 'admin should be able to impersonate a student with their real name' do&lt;br /&gt;
      stub_current_user(admin, admin.role.name, admin.role)       &lt;br /&gt;
      allow(User).to receive(:find_by).with(name: student1.name).and_return(student1)&lt;br /&gt;
      allow(admin).to receive(:can_impersonate?).with(student1).and_return(true)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: student1.name } }&lt;br /&gt;
      @session = { user: admin }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:super_user]).to eq admin&lt;br /&gt;
      expect(session[:user]).to eq student1&lt;br /&gt;
      expect(session[:original_user]).to eq admin&lt;br /&gt;
      expect(session[:impersonate]).to be true&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* admin should be able to impersonate a teaching assistant with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when admin tries to impersonate a teaching assistant in which case parameter 'impersonate' will be true in session parameters&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
it 'admin should be able to impersonate a teaching assistant with their real name' do&lt;br /&gt;
      stub_current_user(admin, admin.role.name, admin.role)&lt;br /&gt;
      allow(User).to receive(:find_by).with(name: teaching_assistant.name).and_return(teaching_assistant)&lt;br /&gt;
      allow(admin).to receive(:can_impersonate?).with(teaching_assistant).and_return(true)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: teaching_assistant.name } }&lt;br /&gt;
      @session = { user: admin }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:super_user]).to eq admin&lt;br /&gt;
      expect(session[:user]).to eq teaching_assistant&lt;br /&gt;
      expect(session[:original_user]).to eq admin&lt;br /&gt;
      expect(session[:impersonate]).to be true&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* admin should be able to impersonate an instructor with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when admin tries to impersonate an instructor in which case parameter 'impersonate' will be true in session parameters&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
it 'admin should be able to impersonate an instructor with their real name' do&lt;br /&gt;
      stub_current_user(admin, admin.role.name, admin.role)&lt;br /&gt;
      allow(User).to receive(:find_by).with(name: instructor.name).and_return(instructor)&lt;br /&gt;
      allow(admin).to receive(:can_impersonate?).with(instructor).and_return(true)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: instructor.name } }&lt;br /&gt;
      @session = { user: admin }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:super_user]).to eq admin&lt;br /&gt;
      expect(session[:user]).to eq instructor&lt;br /&gt;
      expect(session[:original_user]).to eq admin&lt;br /&gt;
      expect(session[:impersonate]).to be true&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* admin should not be able to impersonate a super admin with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when admin tries to impersonate a super admin in which case parameter 'impersonate' will be nil in session parameters&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
it 'admin should not be able to impersonate a super admin with their real name' do&lt;br /&gt;
      stub_current_user(admin, admin.role.name, admin.role)&lt;br /&gt;
      allow(User).to receive(:find_by).with(name: super_admin.name).and_return(super_admin)&lt;br /&gt;
      allow(admin).to receive(:can_impersonate?).with(super_admin).and_return(false)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: super_admin.name } }&lt;br /&gt;
      @session = { user: admin }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:impersonate]).to be nil&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* super admin should be able to impersonate a student with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when super admin tries to impersonate a student in which case parameter 'impersonate' will be true in session parameters&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
it 'super admin should be able to impersonate a student with their real name' do&lt;br /&gt;
      stub_current_user(super_admin, super_admin.role.name, super_admin.role)       &lt;br /&gt;
      allow(User).to receive(:find_by).with(name: student1.name).and_return(student1)&lt;br /&gt;
      allow(super_admin).to receive(:can_impersonate?).with(student1).and_return(true)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: student1.name } }&lt;br /&gt;
      @session = { user: super_admin }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:super_user]).to eq super_admin&lt;br /&gt;
      expect(session[:user]).to eq student1&lt;br /&gt;
      expect(session[:original_user]).to eq super_admin&lt;br /&gt;
      expect(session[:impersonate]).to be true&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* super admin should be able to impersonate a teaching assistant with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when super admin tries to impersonate a teaching assistant in which case parameter 'impersonate' will be true in session parameters&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
it 'super admin should be able to impersonate a teaching assistant with their real name' do&lt;br /&gt;
      stub_current_user(super_admin, super_admin.role.name, super_admin.role)&lt;br /&gt;
      allow(User).to receive(:find_by).with(name: teaching_assistant.name).and_return(teaching_assistant)&lt;br /&gt;
      allow(super_admin).to receive(:can_impersonate?).with(teaching_assistant).and_return(true)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: teaching_assistant.name } }&lt;br /&gt;
      @session = { user: super_admin }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:super_user]).to eq super_admin&lt;br /&gt;
      expect(session[:user]).to eq teaching_assistant&lt;br /&gt;
      expect(session[:original_user]).to eq super_admin&lt;br /&gt;
      expect(session[:impersonate]).to be true&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* super admin should be able to impersonate an instructor with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when super admin tries to impersonate an instructor in which case parameter 'impersonate' will be true in session parameters&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
it 'super admin should be able to impersonate an instructor with their real name' do&lt;br /&gt;
      stub_current_user(super_admin, super_admin.role.name, super_admin.role)&lt;br /&gt;
      allow(User).to receive(:find_by).with(name: instructor.name).and_return(instructor)&lt;br /&gt;
      allow(super_admin).to receive(:can_impersonate?).with(instructor).and_return(true)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: instructor.name } }&lt;br /&gt;
      @session = { user: super_admin }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:super_user]).to eq super_admin&lt;br /&gt;
      expect(session[:user]).to eq instructor&lt;br /&gt;
      expect(session[:original_user]).to eq super_admin&lt;br /&gt;
      expect(session[:impersonate]).to be true&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* super admin should be able to impersonate an admin with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when super admin tries to impersonate an admin in which case parameter 'impersonate' will be true in session parameters&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
it 'super admin should be able to impersonate an admin with their real name' do&lt;br /&gt;
      stub_current_user(super_admin, super_admin.role.name, super_admin.role)&lt;br /&gt;
      allow(User).to receive(:find_by).with(name: admin.name).and_return(admin)&lt;br /&gt;
      allow(super_admin).to receive(:can_impersonate?).with(admin).and_return(true)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: admin.name } }&lt;br /&gt;
      @session = { user: super_admin }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:super_user]).to eq super_admin&lt;br /&gt;
      expect(session[:user]).to eq admin&lt;br /&gt;
      expect(session[:original_user]).to eq super_admin&lt;br /&gt;
      expect(session[:impersonate]).to be true&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Useful Links ===&lt;br /&gt;
The following are the links to the useful pages in understanding this project(as of Spring 2022)&lt;br /&gt;
* [https://expertiza.csc.ncsu.edu/index.php/Expertiza_documentation] - General wiki describing the Expertiza project.&lt;br /&gt;
* [https://github.com/expertiza/expertiza] - GitHub repo that maintains code for Expertiza.&lt;br /&gt;
* [https://github.com/moksh98/expertiza/tree/E2222-beta] - GitHub repo for the work done in Spring 2022 for the refactoring of impersonate controller.&lt;br /&gt;
* [https://expertiza.csc.ncsu.edu/index.php/CSE/ECE_517_Spring_2021_-_E2108._Impersonate_controller.rb] - Spring 2021's wiki for the refactoring of impersonate controller.&lt;br /&gt;
* [https://github.com/expertiza/expertiza/pull/2347] - Pull request for the work done in Spring 2022.&lt;br /&gt;
* [https://github.com/expertiza/expertiza/issues/2342]- Issue that has been raised for the undiscovered bug that was breaking the application.&lt;br /&gt;
* [http://152.7.98.229:8080/] - Application Link&lt;/div&gt;</summary>
		<author><name>Mrjain</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_S2222:_Refactor_impersonate_controller&amp;diff=143671</id>
		<title>CSC/ECE 517 Spring 2022 - S2222: Refactor impersonate controller</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2022_-_S2222:_Refactor_impersonate_controller&amp;diff=143671"/>
		<updated>2022-03-28T00:28:28Z</updated>

		<summary type="html">&lt;p&gt;Mrjain: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This wiki page describes the work done under E2222 OSS Program for Spring 2022, in the CSC/ECE 517 course.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==About Expertiza==&lt;br /&gt;
&lt;br /&gt;
[http://expertiza.ncsu.edu/ Expertiza] is an open-source project based on [http://rubyonrails.org/ Ruby on Rails] framework. Expertiza is a complete instructor-student usage website where the instructor can assign assignments, deadlines, grades, etc that is required for the course(s) under him and the students can use this website to perform the tasks required as part of the course like project or assignments submission, forming groups and collaborating with them, as well as reviewing projects and teammates.&lt;br /&gt;
&lt;br /&gt;
This project focuses on a specific feature of expertiza which allows Administrators, Instructors or Teaching Assistants to impersonate other and access their account. &lt;br /&gt;
The demonstration for the feature is as shown below.&lt;br /&gt;
&lt;br /&gt;
[[File:img1.jpeg|Figure 1|frame|center]]&lt;br /&gt;
[[File:img2.jpeg|Figure 2|frame|center]]&lt;br /&gt;
[[File:img3.jpeg|Figure 3|frame|center]]&lt;br /&gt;
         &lt;br /&gt;
== What does this controller do? ==&lt;br /&gt;
Login: We can only login as an instructor(only instructor login details are available) &lt;br /&gt;
username -&amp;gt; instructor6, password -&amp;gt; password&lt;br /&gt;
&lt;br /&gt;
* When logged in as an instructor, under the manage option as shown in Figure 1, select impersonate user. &lt;br /&gt;
* Upon redirected to impersonate page, enter the account which needs to be impersonated. &lt;br /&gt;
* It impersonates that user provided that user can be impersonated. &lt;br /&gt;
* As shown in figure 3, this can be used to revert the impersonation and return to the instructor profile.&lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
Expertiza allows administrators, instructors and Teaching Assistants to impersonate other users like a student. This allows the impersonator to view assignments, deadlines and submissions of other students. The rules to impersonating a user is, the impersonator has to be an ancestor of the impersonate. The hierarchy of impersonation is as follows: &lt;br /&gt;
&lt;br /&gt;
super administrator -&amp;gt; Administrator -&amp;gt; Instructor -&amp;gt; Teaching Assistant -&amp;gt; Student&lt;br /&gt;
&lt;br /&gt;
Note: impersonation cannot happen within the same level of hierarchy. For Example, a Super Administrator can impersonate any user apart from other Super Administrators, an Administrator can impersonate Instructors, TA, Students and not other Admins and so on. The aim of this project is to refactor the impersonate controller.&lt;br /&gt;
&lt;br /&gt;
== Major issues in the previous version of code ==&lt;br /&gt;
&lt;br /&gt;
* Long single lines of code throughout the controller.&lt;br /&gt;
* Repeated code written multiple times in different methods throughout the controller&lt;br /&gt;
* Method names used were not informative and readable.&lt;br /&gt;
* Undiscovered bug that was breaking the application(Has been reported in Expertiza).&lt;br /&gt;
* Unused method discovered.&lt;br /&gt;
&lt;br /&gt;
==What needs to be done==&lt;br /&gt;
&lt;br /&gt;
* Introduce Single Responsibility Principle - A method/function should only have one purpose to fulfill (Read more about it here). Read more about the ideal length of a function here. &lt;br /&gt;
* You’re supposed to figure out different things a method is doing, break that method into smaller methods such that each resulting method is doing only one thing.&lt;br /&gt;
* Throughout the file, the user is being initialized but never used. This is bad coding practice as it slows down runtime.&lt;br /&gt;
* Code comments are needed for each and every function. Figure out what the function is doing and write at-least 50 word descriptions for each function. &lt;br /&gt;
* Your code comments should also include the information about what the parameters are for, and what the method returns (not counted for 50 word description). &lt;br /&gt;
* Read more about how to write meaningful code comments here.&lt;br /&gt;
* Rename the methods to a more informative name. For example, There can be better names for check_if_user_impersonateable method, do_main_operation, check_if_special_char methods&lt;br /&gt;
* Refactor very long lines of code to make it more readable&lt;br /&gt;
* There are many unnecessary nesting of if statements. Convert them to elif. For example, Line 36, Line 139.&lt;br /&gt;
* Use a guard clause instead of wrapping the code inside a conditional expression. Read more about guard clauses here.&lt;br /&gt;
* A generalized exception is being rescued everywhere, which is against the coding principles that are supposed to be followed. Find what exceptions could a piece of code generate and rescue a more specialized exception.&lt;br /&gt;
* Test Functions and increase test code coverage.&lt;br /&gt;
&lt;br /&gt;
== Solution ==&lt;br /&gt;
&lt;br /&gt;
=== Original Code ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Method to overwrite the session details that are corresponding to the user or one being impersonated&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def overwrite_session&lt;br /&gt;
    # If not impersonatable, then original user's session remains&lt;br /&gt;
    if params[:impersonate].nil?&lt;br /&gt;
      # E1991 : check whether instructor is currently in anonymized view&lt;br /&gt;
      user = User.anonymized_view?(session[:ip]) ? User.real_user_from_anonymized_name(params[:user][:name]) : User.find_by(name: params[:user][:name])&lt;br /&gt;
      session[:super_user] = session[:user] if session[:super_user].nil?&lt;br /&gt;
      AuthController.clear_user_info(session, nil)&lt;br /&gt;
      session[:original_user] = @original_user&lt;br /&gt;
      session[:impersonate] = true&lt;br /&gt;
      session[:user] = user&lt;br /&gt;
    elsif !params[:impersonate][:name].empty?&lt;br /&gt;
      # E1991 : check whether instructor is currently in anonymized view&lt;br /&gt;
      user = User.anonymized_view?(session[:ip]) ? User.real_user_from_anonymized_name(params[:impersonate][:name]) : User.find_by(name: params[:impersonate][:name])&lt;br /&gt;
      AuthController.clear_user_info(session, nil)&lt;br /&gt;
      session[:user] = user&lt;br /&gt;
      session[:impersonate] = true&lt;br /&gt;
      session[:original_user] = @original_user&lt;br /&gt;
    else&lt;br /&gt;
      # E1991 : check whether instructor is currently in anonymized view&lt;br /&gt;
      AuthController.clear_user_info(session, nil)&lt;br /&gt;
      session[:user] = session[:super_user]&lt;br /&gt;
      session[:super_user] = nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def check_if_special_char&lt;br /&gt;
    if params[:user]&lt;br /&gt;
      if warn_for_special_chars(params[:user][:name], 'Username')&lt;br /&gt;
        redirect_back&lt;br /&gt;
        return&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if params[:impersonate]&lt;br /&gt;
      if warn_for_special_chars(params[:impersonate][:name], 'Username')&lt;br /&gt;
        redirect_back&lt;br /&gt;
        return&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Checking if the username provided can be impersonated or not&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def check_if_user_impersonateable&lt;br /&gt;
    if params[:impersonate].nil?&lt;br /&gt;
      # E1991 : check whether instructor is currently in anonymized view&lt;br /&gt;
      user = User.anonymized_view?(session[:ip]) ? User.real_user_from_anonymized_name(params[:user][:name]) : User.find_by(name: params[:user][:name])&lt;br /&gt;
      if !@original_user.can_impersonate? user&lt;br /&gt;
        @message = &amp;quot;You cannot impersonate '#{params[:user][:name]}'.&amp;quot;&lt;br /&gt;
        temp&lt;br /&gt;
        AuthController.clear_user_info(session, nil)&lt;br /&gt;
      else&lt;br /&gt;
        overwrite_session&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      unless params[:impersonate][:name].empty?&lt;br /&gt;
        # E1991 : check whether instructor is currently in anonymized view&lt;br /&gt;
        overwrite_session&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Main operation&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  def do_main_operation(user)&lt;br /&gt;
    if user&lt;br /&gt;
      check_if_user_impersonateable&lt;br /&gt;
    else&lt;br /&gt;
      display_error_msg&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Main operation, method used to break the functions in impersonate controller and bring out 2 functionalities at same level,&lt;br /&gt;
* checking if user impersonate-able, if not throw corresponding error message&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
def impersonate&lt;br /&gt;
    # Initial check to see if the username exists&lt;br /&gt;
    display_error_msg&lt;br /&gt;
    begin&lt;br /&gt;
      @original_user = session[:super_user] || session[:user]&lt;br /&gt;
      # Impersonate using form on /impersonate/start, based on the username provided, this method looks to see if that's possible by calling the do_main_operation method&lt;br /&gt;
      if params[:impersonate].nil?&lt;br /&gt;
        # Check if special chars /\?&amp;lt;&amp;gt;|&amp;amp;$# are used to avoid html tags or system command&lt;br /&gt;
        check_if_special_char&lt;br /&gt;
        # E1991 : check whether instructor is currently in anonymized view&lt;br /&gt;
        user = User.anonymized_view?(session[:ip]) ? User.real_user_from_anonymized_name(params[:user][:name]) : user = User.find_by(name: params[:user][:name])&lt;br /&gt;
        do_main_operation(user)&lt;br /&gt;
      else&lt;br /&gt;
        # Impersonate a new account&lt;br /&gt;
        if !params[:impersonate][:name].empty?&lt;br /&gt;
          # check if special chars /\?&amp;lt;&amp;gt;|&amp;amp;$# are used to avoid html tags or system command&lt;br /&gt;
          check_if_special_char&lt;br /&gt;
          # E1991 : check whether instructor is currently in anonymized view&lt;br /&gt;
          user = User.anonymized_view?(session[:ip]) ? User.real_user_from_anonymized_name(params[:impersonate][:name]) : User.find_by(name: params[:impersonate][:name])&lt;br /&gt;
          do_main_operation(user)&lt;br /&gt;
          # Revert to original account when currently in the impersonated session&lt;br /&gt;
        else&lt;br /&gt;
          if !session[:super_user].nil?&lt;br /&gt;
            AuthController.clear_user_info(session, nil)&lt;br /&gt;
            session[:user] = session[:super_user]&lt;br /&gt;
            user = session[:user]&lt;br /&gt;
            session[:super_user] = nil&lt;br /&gt;
          else&lt;br /&gt;
            display_error_msg&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      # Navigate to user's home location as the default landing page after impersonating or reverting&lt;br /&gt;
      AuthController.set_current_role(user.role_id, session)&lt;br /&gt;
      redirect_to action: AuthHelper.get_home_action(session[:user]),&lt;br /&gt;
                  controller: AuthHelper.get_home_controller(session[:user])&lt;br /&gt;
    rescue StandardError&lt;br /&gt;
      flash[:error] = @message&lt;br /&gt;
      redirect_to :back&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== New Code ===&lt;br /&gt;
* This function checks if the logged in user is a student or not. If it is a student, do not allow the impersonate mode.&lt;br /&gt;
* If the logged in user has the role or anything other than the student, we allow that user to use the impersonate mode.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
before_action :check_if_input_is_valid&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Refactored Code ===&lt;br /&gt;
&lt;br /&gt;
* Method to Generate Overwrite Session when the user tries to Impersonate another User&lt;br /&gt;
* Function created to generate session whenever needed.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def generate_session(user)&lt;br /&gt;
    AuthController.clear_user_info(session, nil)&lt;br /&gt;
    session[:original_user] = @original_user&lt;br /&gt;
    session[:impersonate] = true&lt;br /&gt;
    session[:user] = user&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Method to overwrite the session details that are corresponding to the user or one being impersonated&lt;br /&gt;
* The first 'if' statement is executed if the logged in user tried to access the impersonate feature from his account.&lt;br /&gt;
* The 'elsif' statement is executed if the user is impersonating someone and then tried to impersonate another person.&lt;br /&gt;
* Function Refactored to call the generate session method to reduce the length of the code and reduced long lines of code&lt;br /&gt;
  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def overwrite_session&lt;br /&gt;
    if params[:impersonate].nil?&lt;br /&gt;
      user = get_real_user(params[:user][:name]) &lt;br /&gt;
      session[:super_user] = session[:user] if session[:super_user].nil?&lt;br /&gt;
      generate_session(user)&lt;br /&gt;
    elsif !params[:impersonate][:name].empty?&lt;br /&gt;
      user = get_real_user(params[:impersonate][:name])&lt;br /&gt;
      generate_session(user)&lt;br /&gt;
    else&lt;br /&gt;
      session[:user] = session[:super_user]&lt;br /&gt;
      session[:super_user] = nil&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Checks if special characters are present in the username provided, only alphanumeric should be used&lt;br /&gt;
* warn_for_special_chars is a method in SecurityHelper class.SecurityHelper class has methods to handle this.&lt;br /&gt;
* special_chars method-Initialises string with special characters /\?&amp;lt;&amp;gt;|&amp;amp;$# .&lt;br /&gt;
* contains_special_chars method-converts it to regex and compares with the string&lt;br /&gt;
* warn_for_special_chars takes the output from above method and flashes an error if there are any special characters(/\?&amp;lt;&amp;gt;|&amp;amp;$#) in the string&lt;br /&gt;
  &lt;br /&gt;
* Function refactored to use guard clauses instead of if else nesting.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def check_if_special_char&lt;br /&gt;
    redirect_back if params[:user] &amp;amp;&amp;amp; warn_for_special_chars(params[:user][:name], 'Username')&lt;br /&gt;
    redirect_back if params[:impersonate] &amp;amp;&amp;amp; warn_for_special_chars(params[:impersonate][:name], 'Username')&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Function refactored to reduce long lines of code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def check_if_user_impersonateable&lt;br /&gt;
    if params[:impersonate].nil?&lt;br /&gt;
      user = get_real_user(params[:user][:name]) &lt;br /&gt;
      if !@original_user.can_impersonate? user&lt;br /&gt;
        @message = &amp;quot;You cannot impersonate '#{params[:user][:name]}'.&amp;quot;&lt;br /&gt;
        AuthController.clear_user_info(session, nil)&lt;br /&gt;
      else&lt;br /&gt;
        overwrite_session&lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      unless params[:impersonate][:name].empty?&lt;br /&gt;
        overwrite_session&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Function name refactored to a better name&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def do_impersonate_operation(user)&lt;br /&gt;
    check_if_user_impersonateable if user&lt;br /&gt;
    display_error_msg&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Function refactored to reduce long lines of code&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def impersonate&lt;br /&gt;
    # Initial check to see if the username exists&lt;br /&gt;
    display_error_msg&lt;br /&gt;
    begin&lt;br /&gt;
      @original_user = session[:super_user] || session[:user]&lt;br /&gt;
      # Impersonate using form on /impersonate/start, based on the username provided, this method looks to see if that's possible by calling the do_main_operation method&lt;br /&gt;
      if params[:impersonate].nil?&lt;br /&gt;
        # Check if special chars /\?&amp;lt;&amp;gt;|&amp;amp;$# are used to avoid html tags or system command&lt;br /&gt;
        check_if_special_char&lt;br /&gt;
        user = get_real_user(params[:user][:name]) &lt;br /&gt;
        do_impersonate_operation(user)&lt;br /&gt;
      else&lt;br /&gt;
        # Impersonate a new account&lt;br /&gt;
        if !params[:impersonate][:name].empty?&lt;br /&gt;
          # check if special chars /\?&amp;lt;&amp;gt;|&amp;amp;$# are used to avoid html tags or system command&lt;br /&gt;
          check_if_special_char&lt;br /&gt;
          user = get_real_user(params[:impersonate][:name])&lt;br /&gt;
          do_impersonate_operation(user)&lt;br /&gt;
          # Revert to original account when currently in the impersonated session&lt;br /&gt;
        else&lt;br /&gt;
          if !session[:super_user].nil?&lt;br /&gt;
            AuthController.clear_user_info(session, nil)&lt;br /&gt;
            session[:user] = session[:super_user]&lt;br /&gt;
            user = session[:user]&lt;br /&gt;
            session[:super_user] = nil&lt;br /&gt;
          else&lt;br /&gt;
            display_error_msg&lt;br /&gt;
          end&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      # Navigate to user's home location as the default landing page after impersonating or reverting&lt;br /&gt;
      AuthController.set_current_role(user.role_id, session)&lt;br /&gt;
      redirect_to action: AuthHelper.get_home_action(session[:user]),&lt;br /&gt;
                  controller: AuthHelper.get_home_controller(session[:user])&lt;br /&gt;
    rescue StandardError&lt;br /&gt;
      flash[:error] = @message&lt;br /&gt;
      redirect_to :back&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Function created to reduce long lines of code used throughout the code&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def get_real_user(name)&lt;br /&gt;
    if User.anonymized_view?(session[:ip])&lt;br /&gt;
      user = User.real_user_from_anonymized_name(name)&lt;br /&gt;
    else&lt;br /&gt;
      user = User.find_by(name: name)&lt;br /&gt;
    end&lt;br /&gt;
    return user&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Testing Plan ==&lt;br /&gt;
&lt;br /&gt;
* instructor should not be able to impersonate a super admin user with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when instructor tries to impersonate super_admin in which case parameter 'impersonate' will be nil in session parameters&lt;br /&gt;
 &lt;br /&gt;
    &amp;lt;pre&amp;gt;&lt;br /&gt;
    it 'instructor should not be able to impersonate a super admin user with their real name' do&lt;br /&gt;
      allow(User).to receive(:find_by).with(name: super_admin.name).and_return(super_admin)&lt;br /&gt;
      allow(instructor).to receive(:can_impersonate?).with(super_admin).and_return(false)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: super_admin.name } }&lt;br /&gt;
      @session = { user: instructor }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:impersonate]).to be nil&lt;br /&gt;
    end&lt;br /&gt;
    &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* instructor should be able to impersonate a user while already impersonating a user but from nav bar&lt;br /&gt;
&lt;br /&gt;
This test is to ascertain the functionality of the user being able to impersonate another user(obeying hierarchy) through the navigation bar on the top right hand corner.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
  it 'instructor should be able to impersonate a user while already impersonating a user but from nav bar' do&lt;br /&gt;
    allow(User).to receive(:find_by).with(name: student1.name).and_return(student1)&lt;br /&gt;
    allow(User).to receive(:find_by).with(name: student2.name).and_return(student2)&lt;br /&gt;
    allow(instructor).to receive(:can_impersonate?).with(student1).and_return(true)&lt;br /&gt;
    allow(instructor).to receive(:can_impersonate?).with(student2).and_return(true)&lt;br /&gt;
    request.env[&amp;quot;HTTP_REFERER&amp;quot;] = &amp;quot;http://www.example.com&amp;quot;&lt;br /&gt;
    @params = { user: { name: student1.name } }&lt;br /&gt;
    @session = { user: instructor }&lt;br /&gt;
    post :impersonate, @params, @session&lt;br /&gt;
    # nav bar uses the :impersonate as the param name, so let make sure it always works from there too.&lt;br /&gt;
    @params = { impersonate: { name: student2.name } }&lt;br /&gt;
    post :impersonate, @params, @session&lt;br /&gt;
    expect(session[:super_user]).to eq instructor&lt;br /&gt;
    expect(session[:user]).to eq student2&lt;br /&gt;
    expect(session[:original_user]).to eq instructor&lt;br /&gt;
    expect(session[:impersonate]).to be true&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* instructor should be able to impersonate a user with their anonymized name&lt;br /&gt;
&lt;br /&gt;
This test verifies the functionality that is available with from the Anonymized view. It checks for the impersonation from both the ends - Through the Manage tab as well as the Navigation Bar.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
it ‘instructor should be able to impersonate a user with their anonymized name’ do&lt;br /&gt;
    allow(User).to receive(:find_by).with(name: student1.name).and_return(student1)&lt;br /&gt;
    allow(instructor).to receive(:can_impersonate?).with(student1).and_return(true)&lt;br /&gt;
    allow(User).to receive(:anonymized_view?).and_return(true)&lt;br /&gt;
    allow(User).to receive(:real_user_from_anonymized_name).with(“Student30”).and_return(student1)&lt;br /&gt;
    request.env[“HTTP_REFERER”] = “http://www.example.com”&lt;br /&gt;
    @params = { user: { name: “Student30&amp;quot; } }&lt;br /&gt;
    @session = { user: instructor }&lt;br /&gt;
    post :impersonate, @params, @session&lt;br /&gt;
    expect(session[:super_user]).to eq instructor&lt;br /&gt;
    expect(session[:user]).to eq student1&lt;br /&gt;
    expect(session[:original_user]).to eq instructor&lt;br /&gt;
    expect(session[:impersonate]).to be true&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* instructor should not be able to impersonate an admin user with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when instructor tries to impersonate admin in which case parameter 'impersonate' will be nil in session parameters&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
it 'instructor should not be able to impersonate an admin user with their real name' do&lt;br /&gt;
      allow(User).to receive(:find_by).with(name: admin.name).and_return(admin)&lt;br /&gt;
      allow(instructor).to receive(:can_impersonate?).with(admin).and_return(false)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: admin.name } }&lt;br /&gt;
      @session = { user: instructor }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:impersonate]).to be nil&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* instructor should be able to impersonate a teaching assistant user with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when instructor tries to impersonate a teaching assistant in which case parameter 'impersonate' will be true in session parameters&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
it 'instructor should be able to impersonate a teaching assistant user with their real name' do&lt;br /&gt;
      allow(User).to receive(:find_by).with(name: teaching_assistant.name).and_return(teaching_assistant)&lt;br /&gt;
      allow(instructor).to receive(:can_impersonate?).with(teaching_assistant).and_return(true)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: teaching_assistant.name } }&lt;br /&gt;
      @session = { user: instructor }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:super_user]).to eq instructor&lt;br /&gt;
      expect(session[:user]).to eq teaching_assistant&lt;br /&gt;
      expect(session[:original_user]).to eq instructor&lt;br /&gt;
      expect(session[:impersonate]).to be true&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* teaching assistant should be able to impersonate a student with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when teaching assistant tries to impersonate a student in which case parameter 'impersonate' will be true in session parameters&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
it 'teaching assistant should be able to impersonate a student with their real name' do&lt;br /&gt;
      stub_current_user(teaching_assistant, teaching_assistant.role.name, teaching_assistant.role)&lt;br /&gt;
      allow(User).to receive(:find_by).with(name: student1.name).and_return(student1)&lt;br /&gt;
      allow(teaching_assistant).to receive(:can_impersonate?).with(student1).and_return(true)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: student1.name } }&lt;br /&gt;
      @session = { user: teaching_assistant }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:super_user]).to eq teaching_assistant&lt;br /&gt;
      expect(session[:user]).to eq student1&lt;br /&gt;
      expect(session[:original_user]).to eq teaching_assistant&lt;br /&gt;
      expect(session[:impersonate]).to be true&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* teaching assistant should not be able to impersonate an instructor with their real name&lt;br /&gt;
&lt;br /&gt;
This tests for the case when teaching assistant tries to impersonate an instructor in which case parameter 'impersonate' will be nil in session parameters&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
it 'teaching assistant should not be able to impersonate an instructor with their real name' do&lt;br /&gt;
      stub_current_user(teaching_assistant, teaching_assistant.role.name, teaching_assistant.role)&lt;br /&gt;
      allow(User).to receive(:find_by).with(name: instructor.name).and_return(instructor)&lt;br /&gt;
      allow(teaching_assistant).to receive(:can_impersonate?).with(instructor).and_return(false)&lt;br /&gt;
      request.env['HTTP_REFERER'] = 'http://www.example.com'&lt;br /&gt;
      @params = { user: { name: instructor.name } }&lt;br /&gt;
      @session = { user: teaching_assistant }&lt;br /&gt;
      post :impersonate, @params, @session&lt;br /&gt;
      expect(session[:impersonate]).to be nil&lt;br /&gt;
    end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Useful Links ===&lt;br /&gt;
The following are the links to the useful pages in understanding this project(as of Spring 2022)&lt;br /&gt;
* [https://expertiza.csc.ncsu.edu/index.php/Expertiza_documentation] - General wiki describing the Expertiza project.&lt;br /&gt;
* [https://github.com/expertiza/expertiza] - GitHub repo that maintains code for Expertiza.&lt;br /&gt;
* [https://github.com/moksh98/expertiza/tree/E2222-beta] - GitHub repo for the work done in Spring 2022 for the refactoring of impersonate controller.&lt;br /&gt;
* [https://expertiza.csc.ncsu.edu/index.php/CSE/ECE_517_Spring_2021_-_E2108._Impersonate_controller.rb] - Spring 2021's wiki for the refactoring of impersonate controller.&lt;br /&gt;
* [https://github.com/expertiza/expertiza/pull/2347] - Pull request for the work done in Spring 2022.&lt;br /&gt;
* [https://github.com/expertiza/expertiza/issues/2342]- Issue that has been raised for the undiscovered bug that was breaking the application.&lt;br /&gt;
* [http://152.7.98.229:8080/]-&lt;/div&gt;</summary>
		<author><name>Mrjain</name></author>
	</entry>
</feed>