Skip to content

Commit 713f3ec

Browse files
committed
Fix roles not being applied & update tests
1 parent c02e49e commit 713f3ec

File tree

5 files changed

+75
-14
lines changed

5 files changed

+75
-14
lines changed

src/plone/restapi/services/users/add.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ def reply(self):
156156
try:
157157
reader = csv.DictReader(stream)
158158
for row in reader:
159+
# convert to lists
160+
for key in ("roles", "groups"):
161+
if row.get(key):
162+
row[key] = [r.strip() for r in row[key].split(",")]
163+
# remove empty values
164+
for key in list(row.keys()):
165+
if not row[key]:
166+
del row[key]
159167
# validate important data
160168
self.validate_input_data(portal, row)
161169
data.append(row)

src/plone/restapi/tests/http-examples/users_add_csv_format.req

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0g
77
Content-Disposition: form-data; name="file"; filename="users.csv"
88
Content-Type: text/csv
99

10-
id,username,fullname,email,roles,location,password
11-
noam,noamchomsky,Noam Avran Chomsky,noam.chomsky@example.com,Contributor,"Cambridge, MA",password1234
10+
username,email,fullname,description,roles,home_page,password
11+
jdoe,jdoe@example.com,John Doe,Software developer from Berlin,"Member, Contributor",https://jdoe.dev,pass1234
12+
asmith,asmith@example.com,Alice Smith,Frontend engineer and designer,Member,https://alice.design,alicePwd!
13+
bwayne,bwayne@example.com,Bruce Wayne,Tech entrepreneur,,https://wayneenterprises.com,batman42
1214

1315
------WebKitFormBoundary7MA4YWxkTrZu0gW--
Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
HTTP/1.1 201 Created
22
Content-Type: application/json
3-
Location: http://localhost:55001/plone/@users/noamchomsky
3+
Location: http://localhost:55001/plone/@users/bwayne
44

55
[
66
{
7-
"@id": "http://localhost:55001/plone/@users/noamchomsky",
8-
"description": null,
9-
"email": "noam.chomsky@example.com",
10-
"fullname": "Noam Avran Chomsky",
7+
"@id": "http://localhost:55001/plone/@users/jdoe",
8+
"description": "Software developer from Berlin",
9+
"email": "jdoe@example.com",
10+
"fullname": "John Doe",
1111
"groups": {
1212
"@id": "http://localhost:55001/plone/@users",
1313
"items": [
@@ -18,11 +18,62 @@ Location: http://localhost:55001/plone/@users/noamchomsky
1818
],
1919
"items_total": 1
2020
},
21-
"home_page": null,
22-
"id": "noamchomsky",
23-
"location": "Cambridge, MA",
21+
"home_page": "https://jdoe.dev",
22+
"id": "jdoe",
23+
"location": null,
2424
"portrait": null,
25-
"roles": [],
26-
"username": "noamchomsky"
25+
"roles": [
26+
"Contributor",
27+
"Member"
28+
],
29+
"username": "jdoe"
30+
},
31+
{
32+
"@id": "http://localhost:55001/plone/@users/asmith",
33+
"description": "Frontend engineer and designer",
34+
"email": "asmith@example.com",
35+
"fullname": "Alice Smith",
36+
"groups": {
37+
"@id": "http://localhost:55001/plone/@users",
38+
"items": [
39+
{
40+
"id": "AuthenticatedUsers",
41+
"title": "AuthenticatedUsers"
42+
}
43+
],
44+
"items_total": 1
45+
},
46+
"home_page": "https://alice.design",
47+
"id": "asmith",
48+
"location": null,
49+
"portrait": null,
50+
"roles": [
51+
"Member"
52+
],
53+
"username": "asmith"
54+
},
55+
{
56+
"@id": "http://localhost:55001/plone/@users/bwayne",
57+
"description": "Tech entrepreneur",
58+
"email": "bwayne@example.com",
59+
"fullname": "Bruce Wayne",
60+
"groups": {
61+
"@id": "http://localhost:55001/plone/@users",
62+
"items": [
63+
{
64+
"id": "AuthenticatedUsers",
65+
"title": "AuthenticatedUsers"
66+
}
67+
],
68+
"items_total": 1
69+
},
70+
"home_page": "https://wayneenterprises.com",
71+
"id": "bwayne",
72+
"location": null,
73+
"portrait": null,
74+
"roles": [
75+
"Member"
76+
],
77+
"username": "bwayne"
2778
}
2879
]

src/plone/restapi/tests/test_documentation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,7 @@ def test_documentation_users_add(self):
10961096
def test_documentation_users_csv_format_add(self):
10971097
url = f"{self.portal.absolute_url()}/@users"
10981098

1099-
content = b'id,username,fullname,email,roles,location,password\r\nnoam,noamchomsky,Noam Avran Chomsky,noam.chomsky@example.com,Contributor,"Cambridge, MA",password1234\r\n'
1099+
content = b'username,email,fullname,description,roles,home_page,password\r\njdoe,jdoe@example.com,John Doe,Software developer from Berlin,"Member, Contributor",https://jdoe.dev,pass1234\nasmith,asmith@example.com,Alice Smith,Frontend engineer and designer,Member,https://alice.design,alicePwd!\r\nbwayne,bwayne@example.com,Bruce Wayne,Tech entrepreneur,,https://wayneenterprises.com,batman42\r\n'
11001100
csv_file = io.BytesIO(content)
11011101
csv_file.name = "users.csv"
11021102

src/plone/restapi/tests/test_services_users.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ def test_add_user_with_uuid_as_userid_enabled(self):
380380
def test_add_users_via_csv(self):
381381
"""Test POST /@users for CSV upload"""
382382

383-
content = b"username,email,fullname,description,home_page,password\njdoe,jdoe@example.com,John Doe,Software developer from Berlin,https://jdoe.dev,pass1234\nasmith,asmith@example.com,Alice Smith,Frontend engineer and designer,https://alice.design,alicePwd!\nbwayne,bwayne@example.com,Bruce Wayne,Tech entrepreneur,https://wayneenterprises.com,batman42\n"
383+
content = b'username,email,fullname,description,roles,home_page,password\njdoe,jdoe@example.com,John Doe,Software developer from Berlin,"Member, Contributor",https://jdoe.dev,pass1234\nasmith,asmith@example.com,Alice Smith,Frontend engineer and designer,,https://alice.design,alicePwd!\nbwayne,bwayne@example.com,Bruce Wayne,Tech entrepreneur,,https://wayneenterprises.com,batman42\n'
384384

385385
resp = self.api_session.post(
386386
"/@users",

0 commit comments

Comments
 (0)