Skip to content

Conversation

@Git-HimanshuRathi
Copy link
Collaborator

@Git-HimanshuRathi Git-HimanshuRathi commented Dec 11, 2025

This PR refactors the stream access tests in stream_data.test.cjs by replacing the separate
has_metadata_access, has_content_access, and can_preview test blocks with a single,
cleaner table-driven test.

Why this change?

As described in issue #33600:

  • The previous tests repeated large amounts of setup code.
  • Behavior differences between the access checks were not easy to compare.
  • Zulip prefers a table-driven pattern for test clarity (e.g., filter.test.cjs).

This update applies that approach to the stream access tests.

What this PR does

  • Introduces a unified table-driven test ("stream access table-driven test").
  • Tests combinations of:
    • public / private / web-public streams
    • subscribed vs. unsubscribed user states
    • admin / moderator / guest / normal user roles
    • spectator behavior
    • history_public_to_subscribers conditions
  • Asserts all three access functions together:
    • has_metadata_access(stream)
    • has_content_access(stream)
    • can_preview(stream)

This removes duplication and significantly improves readability and maintainability.

Test results

All relevant tests passed locally:
✔ ./tools/test-js-with-node web/tests/stream_data.test.cjs — passed
✔ No regressions in other JS tests
✔ Backend + webhook tests unaffected (no backend code touched)

Issue

Fixes: #33600

Self-review checklist
  • Self-reviewed the changes for clarity and maintainability.
  • Communicate decisions, questions, and potential concerns.
  • Explains differences from previous plans (e.g., issue description).
  • Highlights technical choices and bugs encountered.
  • Calls out remaining decisions and concerns.
  • Automated tests verify logic where appropriate.
  • Individual commits are ready for review (see commit discipline).
  • Each commit is a coherent idea.
  • Commit message(s) explain reasoning and motivation for changes.
  • Visual appearance of the changes.
  • Responsiveness and internationalization.
  • Strings and tooltips.
  • End-to-end functionality of buttons, interactions and flows.
  • Corner cases, error conditions, and easily imagined bugs.

@alya
Copy link
Contributor

alya commented Dec 11, 2025

Thanks! Please fill out the self-review checklist, which is for some reason missing from the PR description.

Your PR will also need to pass tests.

@alya alya added the not ready Needs work to be ready for maintainer review. label Dec 11, 2025
@zulipbot
Copy link
Member

Hello @zulip/server-testing members, this pull request was labeled with the "area: testing-coverage" label, so you may want to check it out!

@Git-HimanshuRathi
Copy link
Collaborator Author

@alya

Thanks! Please fill out the self-review checklist, which is for some reason missing from the PR description.

Your PR will also need to pass tests.

I’ve now added the full self-review checklist to the PR description, as requested.
I also pushed a new commit fixing the previously failing CI test case.

@alya
Copy link
Contributor

alya commented Dec 16, 2025

Please clean up your commit history and post again to request a review. See here for guidelines.

@Git-HimanshuRathi Git-HimanshuRathi force-pushed the issue-33600-stream-data-test-refactor branch from ccb0810 to 7efe85a Compare December 19, 2025 09:48
@Git-HimanshuRathi
Copy link
Collaborator Author

@alya

I’ve cleaned up the commit history using an interactive rebase and force-pushed a single coherent commit, following the guidelines that you've shared .

All tests are passing. Please let me know if anything else is needed before review.

@alya alya added maintainer review PR is ready for review by Zulip maintainers. and removed not ready Needs work to be ready for maintainer review. labels Dec 20, 2025
@alya
Copy link
Contributor

alya commented Dec 20, 2025

@shubham-padia Could you please take a look at this one?

Copy link
Member

@shubham-padia shubham-padia left a comment

Choose a reason for hiding this comment

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

Had a brief look and posted some comments. Can you explain how did you make sure that all the previous test cases are included in the new test. I could find some missing cases on a brief look, so the first thing to ensure here is that we don't lose any test cases during this change.

assert.equal(stream_data.has_metadata_access(social), true);
user: {is_admin: false, is_guest: false},
spectator: false,
expect: {meta: true, content: true, preview: false},
Copy link
Member

Choose a reason for hiding this comment

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

Let's use the full names here, e.g has_content_access, has_metadata_access and can_preview so it's clear what we are testing just from the test case.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

];

run_test("stream access table-driven test", ({override}) => {
for (const tc of access_cases) {
Copy link
Member

Choose a reason for hiding this comment

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

let's use test_case instead of tc

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

Copy link
Member

Choose a reason for hiding this comment

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

Why are we removing all these testcases for can_access_stream_email and shortening the test?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Restored can_access_stream_email test
The full original test is now restored with all cases:

Archived stream → false
Spectator → false
User in permission group → true
Bot in permission group → true
Unknown user → false
Guest → false


override_rewire(stream_data, "has_content_access", () => true);
assert.equal(stream_data.can_preview(social), true);
social.history_public_to_subscribers = false;
Copy link
Member

Choose a reason for hiding this comment

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

Same here with can_toggle_subscription, why are we shortening the test?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Restored
can_toggle_subscription
All original cases are now included:

Spectator → false
Subscribed user → true
Guest subscribed → true (can unsubscribe)
Unsubscribed public channel → true
Unsubscribed private channel → false
Admin on private → false
User in can_add_subscribers_group → true
User in can_subscribe_group → true

const social = {
subscribed: true,
color: "red",
name: "social",
Copy link
Member

Choose a reason for hiding this comment

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

We don't have a test for this and can_subscribe_group

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added a dedicated standalone test can_subscribe_group grants access that verifies users in can_subscribe_group get metadata, content, and preview access.

Also added coverage in stream access with group permissions test that explicitly tests both can_add_subscribers_group and can_subscribe_group with guest user behavior.

@Git-HimanshuRathi Git-HimanshuRathi force-pushed the issue-33600-stream-data-test-refactor branch 2 times, most recently from 4c7d4b2 to c221339 Compare December 22, 2025 22:24
@Git-HimanshuRathi
Copy link
Collaborator Author

CI is currently failing during provisioning due to a 503 error while downloading shellcheck (tools/setup/install-shellcheck).
This appears to be an external infrastructure issue rather than a test failure.
I’ll retrigger CI once the service is available again.

This commit refactors the has_metadata_access, has_content_access, and
can_preview tests into a single table-driven test suite, making it
easier to compare behavioral differences across various stream configs.

Changes:
- Combined related tests into 12-case table-driven test
- Uses full function names in expect objects (e.g., has_metadata_access)
- Added tests for guest users, spectators, and group permissions
- Added standalone tests for can_add_subscribers_group,
  can_subscribe_group, and can_administer_channel_group
- Restored full can_toggle_subscription and can_access_stream_email tests

Fixes zulip#33600.
@Git-HimanshuRathi Git-HimanshuRathi force-pushed the issue-33600-stream-data-test-refactor branch from c221339 to 76cb107 Compare December 23, 2025 09:21
@Git-HimanshuRathi
Copy link
Collaborator Author

I compared the new tests against the original main branch using git diff main -- web/tests/stream_data.test.cjs to ensure no test cases were lost. The table-driven test now has 12 cases (up from 7), and all original standalone test logic has been preserved or expanded.

All tests pass and linting is clean.

@Git-HimanshuRathi
Copy link
Collaborator Author

@shubham-padia

Could you please take a look and let me know if there’s anything that needs to be changed ?

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve stream_data.test.cjs to share more test setup code

4 participants