Skip to content

Commit 3b6f0b9

Browse files
authored
šŸ‘”(backend) update logic to retrieve drive main_workspace (#449)
The previous route we were using to retrieve the user's Drive main workspace through drive resource server is not working anymore. But we are able to retrieve this workspace through the `/me` api route.
1 parent 5161978 commit 3b6f0b9

2 files changed

Lines changed: 74 additions & 106 deletions

File tree

ā€Žsrc/backend/core/api/viewsets/drive.pyā€Ž

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
DriveAPIView.
33
"""
44

5+
import logging
6+
57
from django.conf import settings
68
from django.utils.decorators import method_decorator
79

@@ -22,6 +24,8 @@
2224
from core.api import utils
2325
from core.api.serializers import PartialDriveItemSerializer
2426

27+
logger = logging.getLogger(__name__)
28+
2529

2630
class DriveAPIView(APIView):
2731
"""
@@ -38,25 +42,29 @@ def __init__(self, *args, **kwargs):
3842
)
3943

4044
def _retrieve_main_workspace(self, access_token):
45+
"""
46+
Retrieve the main workspace for the authenticated user.
47+
"""
4148
response = requests.get(
42-
f"{self.drive_external_api}/items/",
49+
f"{self.drive_external_api}/users/me/",
4350
headers={
4451
"Authorization": f"Bearer {access_token}",
4552
"Content-Type": "application/json",
4653
},
4754
timeout=5,
4855
)
49-
response.raise_for_status()
50-
data = response.json()
51-
items = data.get("results", [])
52-
main_workspace = None
5356

54-
for item in items:
55-
if item["main_workspace"] is True:
56-
main_workspace = item
57-
break
57+
if not response.ok:
58+
logger.warning(
59+
"Failed to retrieve main workspace. (%s - %s)",
60+
response.status_code,
61+
response.text,
62+
exc_info=True,
63+
)
64+
return None
5865

59-
return main_workspace
66+
data = response.json()
67+
return data.get("main_workspace")
6068

6169
@extend_schema(
6270
tags=["third-party/drive"],
@@ -104,7 +112,9 @@ def get(self, request):
104112
if not main_workspace:
105113
return Response(
106114
status=status.HTTP_404_NOT_FOUND,
107-
data={"error": "No Drive main workspace found"},
115+
data={
116+
"error": f"No {settings.DRIVE_CONFIG.get('app_name')} main workspace found"
117+
},
108118
)
109119

110120
# Search for files at the root of the main workspace
@@ -166,7 +176,9 @@ def post(self, request):
166176
if not main_workspace:
167177
return Response(
168178
status=status.HTTP_404_NOT_FOUND,
169-
data={"error": "No Drive main workspace found"},
179+
data={
180+
"error": f"No {settings.DRIVE_CONFIG.get('app_name')} main workspace found"
181+
},
170182
)
171183

172184
# Create a new file in the main workspace

ā€Žsrc/backend/core/tests/api/test_drive.pyā€Ž

Lines changed: 50 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,8 @@ def test_api_third_party_drive_get_should_refresh_token(
118118

119119
responses.add(
120120
responses.GET,
121-
"http://drive.test/external_api/v1.0/items/",
122-
status=status.HTTP_200_OK,
123-
json={
124-
"count": 0,
125-
"next": None,
126-
"previous": None,
127-
"results": [],
128-
},
121+
"http://drive.test/external_api/v1.0/users/me/",
122+
status=status.HTTP_401_UNAUTHORIZED,
129123
)
130124
client.get(reverse("drive") + "?title=test_document")
131125
assert mock.call_count == 1
@@ -145,19 +139,16 @@ def test_api_third_party_drive_get_search_by_title(
145139
workspace_id = str(uuid.uuid4())
146140
responses.add(
147141
responses.GET,
148-
"http://drive.test/external_api/v1.0/items/",
142+
"http://drive.test/external_api/v1.0/users/me/",
149143
json={
150-
"count": 1,
151-
"next": None,
152-
"previous": None,
153-
"results": [
154-
{
155-
"id": workspace_id,
156-
"title": "My Workspace",
157-
"main_workspace": True,
158-
"type": "workspace",
159-
}
160-
],
144+
"id": "123",
145+
"email": "john.doe@test.local",
146+
"main_workspace": {
147+
"id": workspace_id,
148+
"type": "folder",
149+
"title": "My Workspace",
150+
"main_workspace": True,
151+
},
161152
},
162153
status=status.HTTP_200_OK,
163154
)
@@ -197,10 +188,10 @@ def test_api_third_party_drive_get_search_by_title(
197188
# Verify the requests were made with correct parameters
198189
assert len(responses.calls) == 2
199190

200-
# First request should be to get workspaces
191+
# First request should be to main workspace
201192
assert (
202193
responses.calls[0].request.url
203-
== "http://drive.test/external_api/v1.0/items/"
194+
== "http://drive.test/external_api/v1.0/users/me/"
204195
)
205196
assert (
206197
responses.calls[0].request.headers["Authorization"]
@@ -227,21 +218,8 @@ def test_api_third_party_drive_get_no_main_workspace(
227218
# Mock the workspace listing with no main workspace
228219
responses.add(
229220
responses.GET,
230-
"http://drive.test/external_api/v1.0/items/",
231-
json={
232-
"count": 1,
233-
"next": None,
234-
"previous": None,
235-
"results": [
236-
{
237-
"id": str(uuid.uuid4()),
238-
"title": "Some Workspace",
239-
"main_workspace": False,
240-
"type": "workspace",
241-
}
242-
],
243-
},
244-
status=status.HTTP_200_OK,
221+
"http://drive.test/external_api/v1.0/users/me/",
222+
status=status.HTTP_401_UNAUTHORIZED,
245223
)
246224

247225
response = client.get(reverse("drive") + "?title=test")
@@ -262,22 +240,19 @@ def test_api_third_party_drive_get_without_title_filter(
262240

263241
workspace_id = str(uuid.uuid4())
264242

265-
# Mock the workspace listing response
243+
# Mock the users me response
266244
responses.add(
267245
responses.GET,
268-
"http://drive.test/external_api/v1.0/items/",
246+
"http://drive.test/external_api/v1.0/users/me/",
269247
json={
270-
"count": 1,
271-
"next": None,
272-
"previous": None,
273-
"results": [
274-
{
275-
"id": workspace_id,
276-
"title": "My Workspace",
277-
"main_workspace": True,
278-
"type": "workspace",
279-
}
280-
],
248+
"id": "123",
249+
"email": "john.doe@test.local",
250+
"main_workspace": {
251+
"id": workspace_id,
252+
"type": "folder",
253+
"title": "My Workspace",
254+
"main_workspace": True,
255+
},
281256
},
282257
status=status.HTTP_200_OK,
283258
)
@@ -453,22 +428,19 @@ def test_api_third_party_drive_post_success(
453428
file_id = str(uuid.uuid4())
454429
presigned_url = "http://s3.test/presigned-upload-url"
455430

456-
# Mock the workspace listing response
431+
# Mock the users me response
457432
responses.add(
458433
responses.GET,
459-
"http://drive.test/external_api/v1.0/items/",
434+
"http://drive.test/external_api/v1.0/users/me/",
460435
json={
461-
"count": 1,
462-
"next": None,
463-
"previous": None,
464-
"results": [
465-
{
466-
"id": workspace_id,
467-
"title": "My Workspace",
468-
"main_workspace": True,
469-
"type": "workspace",
470-
}
471-
],
436+
"id": "123",
437+
"email": "john.doe@test.local",
438+
"main_workspace": {
439+
"id": workspace_id,
440+
"type": "folder",
441+
"title": "My Workspace",
442+
"main_workspace": True,
443+
},
472444
},
473445
status=status.HTTP_200_OK,
474446
)
@@ -527,7 +499,7 @@ def test_api_third_party_drive_post_success(
527499
# Verify workspace listing request
528500
assert (
529501
responses.calls[0].request.url
530-
== "http://drive.test/external_api/v1.0/items/"
502+
== "http://drive.test/external_api/v1.0/users/me/"
531503
)
532504
assert (
533505
responses.calls[0].request.headers["Authorization"]
@@ -561,24 +533,11 @@ def test_api_third_party_drive_post_no_main_workspace(
561533

562534
blob_id = f"msg_{message.id}_0"
563535

564-
# Mock the workspace listing with no main workspace
536+
# Mock the users me as unauthenticated
565537
responses.add(
566538
responses.GET,
567-
"http://drive.test/external_api/v1.0/items/",
568-
json={
569-
"count": 1,
570-
"next": None,
571-
"previous": None,
572-
"results": [
573-
{
574-
"id": str(uuid.uuid4()),
575-
"title": "Some Workspace",
576-
"main_workspace": False,
577-
"type": "workspace",
578-
}
579-
],
580-
},
581-
status=status.HTTP_200_OK,
539+
"http://drive.test/external_api/v1.0/users/me/",
540+
status=status.HTTP_401_UNAUTHORIZED,
582541
)
583542

584543
response = client.post(
@@ -653,24 +612,21 @@ def test_api_third_party_drive_post_upload_to_s3_fails(
653612
file_id = str(uuid.uuid4())
654613
presigned_url = "http://s3.test/presigned-upload-url"
655614

656-
# Mock the workspace listing response
615+
# Mock the users me response
657616
responses.add(
658617
responses.GET,
659-
"http://drive.test/external_api/v1.0/items/",
618+
"http://drive.test/external_api/v1.0/users/me/",
660619
json={
661-
"count": 1,
662-
"next": None,
663-
"previous": None,
664-
"results": [
665-
{
666-
"id": workspace_id,
667-
"title": "My Workspace",
668-
"main_workspace": True,
669-
"type": "workspace",
670-
}
671-
],
620+
"id": "123",
621+
"email": "john.doe@test.local",
622+
"main_workspace": {
623+
"id": workspace_id,
624+
"type": "folder",
625+
"title": "My Workspace",
626+
"main_workspace": True,
627+
},
672628
},
673-
status=200,
629+
status=status.HTTP_200_OK,
674630
)
675631

676632
# Mock the file creation response

0 commit comments

Comments
Ā (0)
⚔