Skip to content

test: add unit test suite for platform role and permission management…#78

Open
Oluwasuyi-Timilehin wants to merge 3 commits into
Fundable-Protocol:devfrom
Oluwasuyi-Timilehin:test/64-platform-role-permission-specs
Open

test: add unit test suite for platform role and permission management…#78
Oluwasuyi-Timilehin wants to merge 3 commits into
Fundable-Protocol:devfrom
Oluwasuyi-Timilehin:test/64-platform-role-permission-specs

Conversation

@Oluwasuyi-Timilehin

@Oluwasuyi-Timilehin Oluwasuyi-Timilehin commented Jun 30, 2026

Copy link
Copy Markdown

#closes #64

Context

Resolves #64. The platform role and permission controllers lacked focused unit/regression tests under src/__tests__ to validate authorization setup constraints, validation logic, and constraint enforcement.

Scope & Changes

  • Added a robust unit test suite under src/__tests__/platform.test.ts to validate platform operations.
  • Implemented tests to reject duplicate permissions with a ConflictError.
  • Implemented tests to prevent the deletion of permissions actively assigned to roles with an InvalidRequestError.
  • Implemented tests for role creation combinations: valid payloads, missing requirements (BadRequestError), payload duplicates (ConflictError), and non-existent permissions (NotFoundError).
  • Implemented tests protecting the super-admin role from deletion while verifying that non-super-admin roles can be deleted.
  • Covered platform.utils.ts text-transformation string normalization behavior.
  • Leveraged in-memory state mapping to fully satisfy the requirement that tests must not require a live database connection.

Verification & Testing Results

All local verification validations pass seamlessly on Windows environments running Bun 1.3.14:

  1. Linting (bun run lint): Passed with 0 errors.
  2. Type-Checking (bun run type-check): Completed cleanly with zero type compilation errors.
  3. Unit Tests & Coverage (bun run test): All 13 newly targeted specifications pass with 100% statement and line coverage metrics inside the isolated platform workspace module.
ℹ tests 13
ℹ suites 4
ℹ pass 13
ℹ fail 0
------------------|---------|----------|---------|---------|-------------------
File              | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
------------------|---------|----------|---------|---------|-------------------
All files         |     100 |    88.33 |   94.44 |     100 |                   
 platform.test.ts |     100 |    88.33 |   94.44 |     100 | 
------------------|---------|----------|---------|---------|-------------------
#closes 

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

* **Tests**
  * Added Node.js test coverage for platform role and permission management using mocked persistence and response handling.
  * Verified permission flows: adding succeeds, duplicates return conflict, and deleting handles both protected and missing permissions.
  * Verified role flows: creating/editing succeeds with valid inputs, and failures cover bad requests, missing/invalid related records, and protected super-admin deletion.
  * Removed reliance on permission text normalization checks in favor of controller outcome verification.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

@drips-wave

drips-wave Bot commented Jun 30, 2026

Copy link
Copy Markdown

@Oluwasuyi-Timilehin Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@coderabbitai

coderabbitai Bot commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@Oluwasuyi-Timilehin, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 50 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 5831d0f9-d588-4507-b77f-f86871dbd911

📥 Commits

Reviewing files that changed from the base of the PR and between 3218107 and 18c2500.

📒 Files selected for processing (1)
  • src/__tests__/platform.test.ts
📝 Walkthrough

Walkthrough

Adds src/__tests__/platform.test.ts as a controller-level test suite for platform permission and role flows, using mocked repositories, a mocked Response, and project error types to cover success and rejection paths.

Changes

Platform Role and Permission Test Suite

Layer / File(s) Summary
Test setup and helpers
src/__tests__/platform.test.ts
Sets database-related environment variables, imports controller and TypeORM modules, defines Express response and entity-like helpers, and resets mocks before each test.
Permission controller tests
src/__tests__/platform.test.ts
Expects controller success for adding a unique permission and rejections for duplicate adds, attached-permission deletion, and missing-permission deletion.
Role controller tests
src/__tests__/platform.test.ts
Expects controller success for creating and editing roles and rejections for invalid permission data, missing related records, and super-admin or missing-role deletion.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐇 Hop hop, the tests spring new,
Roles and permissions checked on cue.
No live database in sight today,
Mocks lead the way, hooray, hooray!
Tiny paws tap through each path so bright,
Guardrails now glimmer in the night.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: adding unit tests for platform role and permission management.
Description check ✅ Passed The description is mostly complete and includes summary, scope, and verification, though it doesn't fully match the template sections.
Linked Issues check ✅ Passed The tests cover the linked issue's core scenarios: permissions, roles, expected errors, and no-live-database behavior.
Out of Scope Changes check ✅ Passed No unrelated code changes are evident beyond the platform test suite and its supporting mocks.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/__tests__/platform.test.ts`:
- Around line 5-95: The platform test suite is using local stand-ins instead of
the real production entrypoints, so it can pass while the actual
controllers/utilities are broken. Update platform.test.ts to import and exercise
the real permissionController/roleController and platformUtils (especially
transformPermissions), and mock the underlying repository/query dependencies
they use rather than reimplementing their behavior. Make sure the tests reflect
the real transformPermissions contract from platform.utils and keep
normalization expectations aligned with platform.validations instead of the mock
lowercasing/trimming logic.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 998ff6ba-0867-4d3d-8b73-720207cb32d3

📥 Commits

Reviewing files that changed from the base of the PR and between a91569c and 2cca2a2.

📒 Files selected for processing (1)
  • src/__tests__/platform.test.ts

Comment thread src/__tests__/platform.test.ts Outdated
@pragmaticAweds

Copy link
Copy Markdown
Contributor

Hi @Oluwasuyi-Timilehin

Thank you for your awesome contribution, however after analyzing your implementation, there is a minor fix to be done. Kindly fix it to merge your PR asap.

Also do not forget to use fundable.finance to offramp.

@Oluwasuyi-Timilehin

Copy link
Copy Markdown
Author

will get on that now, thank you

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/__tests__/platform.test.ts (1)

44-48: 📐 Maintainability & Code Quality | 🔵 Trivial

createMockEntity hardcodes RoleEntity.prototype for permission mocks too.

Lines 64/73/81/105/117/128 use this helper for permission records, so they're stamped with RoleEntity.prototype. This is harmless if the controllers only read plain fields, but will misbehave under any instanceof / prototype-method checks. Consider parameterizing the prototype.

♻️ Optional: accept the entity class
-const createMockEntity = (data: any) => {
-  const entity = Object.create(RoleEntity.prototype);
-  return Object.assign(entity, data);
-};
+const createMockEntity = (data: any, EntityClass: { prototype: object } = RoleEntity) => {
+  const entity = Object.create(EntityClass.prototype);
+  return Object.assign(entity, data);
+};
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/__tests__/platform.test.ts` around lines 44 - 48, The helper
createMockEntity currently always builds objects with RoleEntity.prototype, but
it is also used for permission records in platform.test.ts, so those mocks get
the wrong prototype. Update createMockEntity to accept the entity class or
prototype as a parameter, and then use the appropriate entity prototype at each
call site where permission mocks are created so the records match the intended
type.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/__tests__/platform.test.ts`:
- Around line 44-48: The helper createMockEntity currently always builds objects
with RoleEntity.prototype, but it is also used for permission records in
platform.test.ts, so those mocks get the wrong prototype. Update
createMockEntity to accept the entity class or prototype as a parameter, and
then use the appropriate entity prototype at each call site where permission
mocks are created so the records match the intended type.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 5d5f5fdb-e471-4de9-853d-f4ff763b68df

📥 Commits

Reviewing files that changed from the base of the PR and between 2cca2a2 and 3218107.

📒 Files selected for processing (1)
  • src/__tests__/platform.test.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add platform role and permission service tests

2 participants