Skip to content
This repository was archived by the owner on Mar 12, 2025. It is now read-only.

Commit c4d11bd

Browse files
committed
fix: filter email from user endopints
1 parent 8653984 commit c4d11bd

File tree

2 files changed

+209
-4
lines changed

2 files changed

+209
-4
lines changed

apps/backend/src/protected-populate/index.json

+130-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
],
2222
"populate": {
2323
"feature_image": {
24-
"fields": ["url", "id"],
24+
"fields": ["url", "id", "formats"],
2525
"populate": {}
2626
},
2727
"tags": {
@@ -110,7 +110,7 @@
110110
],
111111
"populate": {
112112
"feature_image": {
113-
"fields": ["url", "id"]
113+
"fields": ["url", "id", "formats"]
114114
},
115115
"tags": {
116116
"fields": [
@@ -171,7 +171,7 @@
171171
],
172172
"populate": {
173173
"feature_image": {
174-
"fields": ["url", "id"],
174+
"fields": ["url", "id", "formats"],
175175
"populate": {}
176176
},
177177
"tags": {
@@ -260,7 +260,7 @@
260260
],
261261
"populate": {
262262
"feature_image": {
263-
"fields": ["url", "id"]
263+
"fields": ["url", "id", "formats"]
264264
},
265265
"tags": {
266266
"fields": [
@@ -684,5 +684,131 @@
684684
]
685685
}
686686
}
687+
},
688+
"GET /api/users": {
689+
"content-type": "plugin::users-permissions.user",
690+
"roles": {
691+
"contributor": {
692+
"fields": [
693+
"provider",
694+
"confirmed",
695+
"blocked",
696+
"slug",
697+
"name",
698+
"bio",
699+
"website",
700+
"location",
701+
"facebook",
702+
"twitter",
703+
"last_seen",
704+
"ghost_id",
705+
"status",
706+
"createdAt",
707+
"updatedAt",
708+
"id"
709+
],
710+
"populate": {
711+
"role": {
712+
"fields": ["name", "type", "id", "description"]
713+
},
714+
"profile_image": {
715+
"fields": ["url"]
716+
}
717+
}
718+
},
719+
"authenticated": {
720+
"fields": [
721+
"email",
722+
"provider",
723+
"confirmed",
724+
"blocked",
725+
"slug",
726+
"name",
727+
"bio",
728+
"website",
729+
"location",
730+
"facebook",
731+
"twitter",
732+
"last_seen",
733+
"ghost_id",
734+
"status",
735+
"createdAt",
736+
"updatedAt",
737+
"id"
738+
],
739+
"populate": {
740+
"profile_image": {
741+
"fields": ["url"]
742+
},
743+
"role": {
744+
"fields": ["name", "description", "type", "id"]
745+
}
746+
}
747+
},
748+
"public": {}
749+
}
750+
},
751+
"GET /api/users/:id": {
752+
"content-type": "plugin::users-permissions.user",
753+
"roles": {
754+
"contributor": {
755+
"fields": [
756+
"provider",
757+
"confirmed",
758+
"blocked",
759+
"slug",
760+
"name",
761+
"bio",
762+
"website",
763+
"location",
764+
"facebook",
765+
"twitter",
766+
"last_seen",
767+
"ghost_id",
768+
"status",
769+
"createdAt",
770+
"updatedAt",
771+
"id"
772+
],
773+
"populate": {
774+
"role": {
775+
"fields": ["name", "type", "id", "description"]
776+
},
777+
"profile_image": {
778+
"fields": ["url"]
779+
}
780+
}
781+
},
782+
"authenticated": {
783+
"fields": [
784+
"email",
785+
"provider",
786+
"confirmed",
787+
"blocked",
788+
"slug",
789+
"name",
790+
"bio",
791+
"website",
792+
"location",
793+
"facebook",
794+
"twitter",
795+
"last_seen",
796+
"ghost_id",
797+
"status",
798+
"createdAt",
799+
"updatedAt",
800+
"id"
801+
],
802+
"populate": {
803+
"profile_image": {
804+
"fields": ["url"]
805+
},
806+
"role": {
807+
"fields": ["name", "description", "type", "id"]
808+
}
809+
}
810+
},
811+
"public": {}
812+
}
687813
}
688814
}

apps/backend/tests/user/index.js

+79
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const {
33
deleteUser,
44
getUserByRole,
55
getAllRoles,
6+
getUser,
7+
getUserJWT,
68
} = require("../helpers/helpers");
79

810
// user mock data
@@ -15,6 +17,15 @@ const mockUserData = {
1517
blocked: null,
1618
};
1719

20+
let contributorJWT = "";
21+
let editorJWT = "";
22+
23+
beforeAll(async () => {
24+
// Prepare user token
25+
contributorJWT = await getUserJWT("contributor-user");
26+
editorJWT = await getUserJWT("editor-user");
27+
});
28+
1829
describe("user", () => {
1930
// Example test taken from https://docs.strapi.io/dev-docs/testing
2031
// This test should pass if the test environment is set up properly
@@ -55,4 +66,72 @@ describe("user", () => {
5566
});
5667
}
5768
});
69+
70+
describe("Contributors getting user data", () => {
71+
// Due to Strapi's permission system, if we desable the /users or /users/:id endpoint,
72+
// it will also disable population of the user data in other endpoints.
73+
// (e.g. /posts?populate[0]=author)
74+
// Therefore, we are filtering out the email field in the response
75+
// instead of disabling the entire endpoint.
76+
77+
it("GET /users should not return email to contributors", async () => {
78+
const response = await request(strapi.server.httpServer)
79+
.get(`/api/users`)
80+
.set("Content-Type", "application/json")
81+
.set("Authorization", `Bearer ${contributorJWT}`)
82+
.send();
83+
84+
expect(response.status).toBe(200);
85+
// check that email and username are not present in the response
86+
expect(response.body.every((user) => !user.email && !user.username)).toBe(
87+
true,
88+
);
89+
});
90+
91+
it("GET /users/:id should not return email to contributors", async () => {
92+
const editorUser = await getUser("editor-user");
93+
94+
const response = await request(strapi.server.httpServer)
95+
.get(`/api/users/${editorUser.id}`)
96+
.set("Content-Type", "application/json")
97+
.set("Authorization", `Bearer ${contributorJWT}`)
98+
.send();
99+
100+
expect(response.status).toBe(200);
101+
102+
// check that email and username are not present in the response
103+
expect(response.body).not.toHaveProperty("email");
104+
expect(response.body).not.toHaveProperty("username");
105+
});
106+
});
107+
108+
describe("Editors getting user data", () => {
109+
it("GET /users should return email to editors", async () => {
110+
const response = await request(strapi.server.httpServer)
111+
.get(`/api/users`)
112+
.set("Content-Type", "application/json")
113+
.set("Authorization", `Bearer ${editorJWT}`)
114+
.send();
115+
116+
expect(response.status).toBe(200);
117+
expect(response.body.every((user) => user.email && !user.username)).toBe(
118+
true,
119+
);
120+
});
121+
122+
it("GET /users/:id should return email to editors", async () => {
123+
const contributorUser = await getUser("contributor-user");
124+
125+
const response = await request(strapi.server.httpServer)
126+
.get(`/api/users/${contributorUser.id}`)
127+
.set("Content-Type", "application/json")
128+
.set("Authorization", `Bearer ${editorJWT}`)
129+
.send();
130+
131+
expect(response.status).toBe(200);
132+
133+
expect(response.body).toHaveProperty("email");
134+
expect(response.body).not.toHaveProperty("username");
135+
});
136+
});
58137
});

0 commit comments

Comments
 (0)