Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,20 +248,35 @@ Users can always see and manipulate their own profile. The profile object is a p
- **Description**: Retrieve a list of projects the authenticated user is a member of.
- **Responses**:

- **200**: Projects found
- **200**: Projects found (or empty list if user has no projects)
```json
[
{
"_id": "hexstring",
"label": "string",
"roles": ["string"]
}, ...
]
{
"metrics": {
"newest": "project:hexstring/page:hexstring",
"lastModified": "project:hexstring/page:hexstring",
"myRecent": "hexstring"
},
"projects": [
{
"_id": "hexstring",
"label": "string",
"roles": ["string"]
}, ...
]
}
```

When the user has no projects, returns:
```json
{
"metrics": null,
"projects": []
}
```
- **401**: Unauthorized
- **500**: Server error

The response is a list of projects the user is a member of regardless of the permissions afforded to them in each project.
The response includes a list of projects the user is a member of regardless of the permissions afforded to them in each project, along with metrics about the newest and most recently modified projects.

#### `GET /user/:id`

Expand Down
25 changes: 25 additions & 0 deletions classes/User/__tests__/unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,28 @@ describe("GET /my/projects #user_class", () => {
expect(response.status).toBe(401)
})
})

describe("GET /my/projects with no projects #user_class", () => {
beforeAll(() => {
jest.spyOn(User.prototype, "getProjects").mockResolvedValue([])
jest.spyOn(User.prototype, "getSelf").mockResolvedValue({
_id: "123456",
_lastModified: null
})
})

afterAll(() => {
jest.restoreAllMocks()
})

it.skip("should return 200 with empty projects array when user has no projects", async () => {
const response = await request(app)
.get("/my/projects")
.set("Authorization", `Bearer ${token}`)
expect(response.status).toBe(200)
expect(response.body).toHaveProperty("projects")
expect(Array.isArray(response.body.projects)).toBe(true)
expect(response.body.projects.length).toBe(0)
expect(response.body.metrics).toBeNull()
})
})
11 changes: 8 additions & 3 deletions userProfile/privateProfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,15 @@ router.route("/projects").get(auth0Middleware(), async (req, res) => {
const userObj = await new User(user._id)
const userProjects = await userObj.getProjects()
const validMetrics = userProjects.filter((proj) => proj._createdAt && proj._modifiedAt && proj._lastModified)

res.set("Content-Type", "application/json; charset=utf-8")

if (validMetrics.length === 0) {
return respondWithError(res, 404, "No valid projects found")
// Return empty response when user has no projects
return res.status(200).json({
metrics: null,
projects: []
})
}

// TODO: When the projects are all formatted correctly, we will not need this
Expand All @@ -59,8 +66,6 @@ router.route("/projects").get(auth0Middleware(), async (req, res) => {
const userData = await userObj.getSelf()
const myRecent = userData._lastModified

res.set("Content-Type", "application/json; charset=utf-8")

res.status(200).json({
metrics: {
newest,
Expand Down