airqo-platform/AirQo-api

Add unit tests for cohort validator — updateCohort and updateCohortName

Open

#6,209 opened on Mar 10, 2026

 (1 comment) (0 reactions) (0 assignees)JavaScript (24 forks)auto 404
device-registrygood first issuetesting

Repository metrics

Stars
 (26 stars)
PR merge metrics
 (PR metrics pending)

Description

Add unit tests for cohort validator — updateCohort and updateCohortName

🏷️ Labels

good first issue · testing · device-registry


📋 Background

As part of the hotfix in fix-cohort-name (which added an explicit rejection of the name field on PUT /cohorts/:cohort_id), a Copilot reviewer flagged that there are no unit tests covering the cohort validators in src/device-registry/validators/cohorts.validators.js.

Other validators in the same service already have test coverage (e.g., validators/test/ut_device.validators.js). Cohort validators should follow the same pattern to prevent regressions.


🎯 What needs to be done

Create a new test file:

src/device-registry/validators/test/ut_cohort.validators.js

At minimum, the following cases should be covered:

updateCohortPUT /cohorts/:cohort_id

  • Returns 400 with message "name cannot be updated via this endpoint; use PUT /cohorts/:cohort_id/name instead" when name is present in the request body
  • Returns 200 (passes validation) when name is absent and other valid fields are provided (description, visibility, cohort_tags, groups, network)
  • Returns 400 when cohort_id param is not a valid MongoDB ObjectId

updateCohortNamePUT /cohorts/:cohort_id/name

  • Returns 400 when name is missing
  • Returns 400 when confirm_update is missing or set to false
  • Returns 400 when update_reason is missing, empty, or shorter than 10 characters
  • Returns 400 when update_reason exceeds 500 characters
  • Passes validation when all required fields (name, confirm_update: true, update_reason) are correctly provided

General

  • Returns 400 when tenant query param is provided but is not in the allowed tenants list

📁 Reference

  • Validators file: src/device-registry/validators/cohorts.validators.js
  • Existing test example to follow: src/device-registry/validators/test/ut_device.validators.js
  • Related hotfix PR: fix-cohort-name

✅ Acceptance Criteria

  • New test file exists at the path above
  • All cases listed are covered with assertions on both the HTTP status code and the error message
  • All existing tests continue to pass
  • No changes to production code are required for this issue

Contributor guide