airqo-platform / airqo-platform/AirQo-api

Add unit tests for cohort validator — updateCohort and updateCohortName

未关闭
#6,209 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
device-registry good first issue testing
主要语言
JavaScript
星标
26
派生
24
平均合并
5 小时 36 分钟
30 天内合并 PR
81

描述

# 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:

### `updateCohort` — `PUT /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

### `updateCohortName` — `PUT /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

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。