CMU-313 / NodeBB

GNU General Public License v3.0
0 stars 223 forks source link

Refactoring helper formatApiResponse in src/controllers/helpers.js #576

Open etong11 opened 2 months ago

etong11 commented 2 months ago

Refactoring helpers.formatApiResponse in src/controllers/helpers.js to reduce its Cognitive Complexity from 20 to the 15 allowed. This was done by replacing the nested switch and if statements with function calls.

Resolves #341

Sonar Cloud link: https://sonarcloud.io/project/issues?open=AZFmi__BybYwxy-_uEXS&id=CMU-313_NodeBB

Execution from UI

formatApiResponse can be triggered when the code returns an API response. For example, the function can be triggered from the home page of NodeBB when clicking on one of the categories displayed ("General Discussion").

Screenshot 2024-09-05 at 8 50 46 PM Screenshot 2024-09-05 at 8 46 27 PM
sonarcloud[bot] commented 2 months ago

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
0.0% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarCloud

coveralls commented 2 months ago

Pull Request Test Coverage Report for Build 10730488514

Details


Changes Missing Coverage Covered Lines Changed/Added Lines %
src/controllers/helpers.js 33 39 84.62%
<!-- Total: 33 39 84.62% -->
Totals Coverage Status
Change from base Build 10607818019: 0.006%
Covered Lines: 22336
Relevant Lines: 25595

💛 - Coveralls
etong11 commented 2 months ago

Code coverage is not 100% in the refactored code due to two spots:

Screenshot 2024-09-05 at 11 44 41 PM Screenshot 2024-09-05 at 11 45 02 PM

When looking through the existing tests, I could not find an existing file that contained tests for helper.formatApiResponse. There is an existing test file, api.js, that tests the API but only aspects such as whether it has a valid request body and routes, not whether the correct formatting was applied to the API response. I attempted to get the case of when the message is '[[error:user-banned]]' to run by creating a test in which a user had a ban applied to them but this did not invoke formatApiResponse.