shardeum / shardus-core

Other
10 stars 2 forks source link

SYS-332, SYS-375, SYS-376, SYS-377, SYS-378: Add `validateJoinRequest.test.ts` #244

Open muni-corn opened 4 weeks ago

muni-corn commented 4 weeks ago

Adds unit tests for testing validateJoinRequest and some of its components.

Refactors were required in order to eliminate any circular dependencies that prevented mock functions from working as intended.

To see this work, just run npx jest "validateJoinRequest".

github-actions[bot] commented 4 weeks ago

PR Reviewer Guide ๐Ÿ”

โฑ๏ธ Estimated effort to review: 4 ๐Ÿ”ต๐Ÿ”ต๐Ÿ”ต๐Ÿ”ตโšช
๐Ÿงช PR contains tests
๐Ÿ”’ No security concerns identified
โšก Key issues to review

Possible Bug
The function `validateJoinRequestHost` in `validate.ts` might incorrectly handle IP validation when bogons are allowed. It checks for reserved IPs even if bogons are allowed, which might not be the intended behavior. Consider revising the logic to ensure it aligns with the intended use cases. Redundant Code
The function `verifyUnseen` in `validate.ts` adds public keys to the seen set even if they are already in it. This might lead to redundant operations and could be optimized. Error Handling
The function `validateJoinRequestHost` catches exceptions but does not log them, which could hinder debugging. Consider adding logging for exceptions.
chrypnotoad commented 4 weeks ago
Summary of all failing tests
FAIL test/unit/src/validateJoinRequest.test.ts (21.152 s)
  โ— verifyJoinRequestSigner โ€บ should fail with wrong signature owner

    expect(received).toStrictEqual(expected) // deep equality

    Expected: {"fatal": true, "reason": "Bad signature, sign owner and node attempted joining mismatched", "success": false}
    Received: null

      124 |         expect(result).toBeNull()
      125 |       } else {
    > 126 |         expect(result).toStrictEqual(test.expectedError)
          |                        ^
      127 |       }
      128 |     })
      129 |   }

      at Object.<anonymous> (test/unit/src/validateJoinRequest.test.ts:126:24)

  โ— verifyJoinRequestSigner โ€บ should fail with wrong node public key

    expect(received).toStrictEqual(expected) // deep equality

    Expected: {"fatal": true, "reason": "Bad signature, sign owner and node attempted joining mismatched", "success": false}
    Received: null

      124 |         expect(result).toBeNull()
      125 |       } else {
    > 126 |         expect(result).toStrictEqual(test.expectedError)
          |                        ^
      127 |       }
      128 |     })
      129 |   }

      at Object.<anonymous> (test/unit/src/validateJoinRequest.test.ts:126:24)

two of the new tests failed in the PR check https://github.com/shardeum/shardus-core/actions/runs/10410903643/job/28833708776?pr=244

BelfordZ commented 3 weeks ago

Can you point out the circular dependency that existed, and where it was removed?

I noticed you didnt have any commits that add tests without refactoring. Which test was not possible without needing a refactor?

muni-corn commented 3 weeks ago

Can you point out the circular dependency that existed, and where it was removed?

I noticed you didnt have any commits that add tests without refactoring. Which test was not possible without needing a refactor?

These tests required mocking fields and functions that were all included in the root of the Join module. Because the actual functions we wanted to test were also in the root module, this sort of self-dependency would upset mocking with jest. (Because the functions' actual implementations are imported as-is before jest can replace any calls with mock implementations.)

For example, all tests using logging functions like info, warn, or error fail with their actual implementations because p2pLogger is uninitialized (and I haven't found a decent way of actually initializing it with log4js yet; it felt unnecessary).

It also became impossible to change allowBogon or seen as needed.

So...

This fixes any tests that depended on allowBogon, seen, info(), warn(), or error() (e.g. the IP address tests). The issue was the self-dependency of the root module. The goal of the refactor was to separate any mocked functions (and their dependencies, if necessary) into their own modules so that the modules can be mocked separately from the root module.