Skip to content

Commit 05795a5

Browse files
author
Stefan Negru
committed
Merge branch 'bugfix/absent-paths-backend' into 'devel'
add missing paths in backend Closes #1065 See merge request sds-dev/sd-connect/swift-browser-ui!96
2 parents 7b0eb39 + 205c468 commit 05795a5

File tree

7 files changed

+131
-79
lines changed

7 files changed

+131
-79
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
159159
- fixed `REQUEST_DB_PORT` instead of `SHARING_DB_PORT` in sharing db
160160
- fixed typing for python code in db so that ports are int instead of string - required for deployment
161161
- (GL #1056) Fix creating a pseudo-container (forward slash) returning 403 and logging user out
162+
- (GL #1050) fixed missing paths in backend: `unauth`, `forbid`, `uidown`, `badrequest`, `notfound` and front-end api responses
162163

163164
### Removed
164165

swift_browser_ui/ui/front.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,69 @@ async def loginpassword(
116116
return aiohttp.web.FileResponse(
117117
str(setd["static_directory"]) + "/loginpassword.html"
118118
)
119+
120+
121+
async def unauth(_: aiohttp.web.Request) -> aiohttp.web.FileResponse:
122+
"""Serve worker js in worker scope."""
123+
return aiohttp.web.FileResponse(
124+
path=str(f"{setd['static_directory']}/401.html"),
125+
status=401,
126+
headers={
127+
"Cache-Control": "no-cache, no-store, must-revalidate",
128+
"Pragma": "no-cache",
129+
"Expires": "0",
130+
"WWW-Authenticate": 'Bearer realm="/", charset="UTF-8"',
131+
},
132+
)
133+
134+
135+
async def forbid(_: aiohttp.web.Request) -> aiohttp.web.FileResponse:
136+
"""Serve worker js in worker scope."""
137+
return aiohttp.web.FileResponse(
138+
path=str(f"{setd['static_directory']}/403.html"),
139+
status=403,
140+
headers={
141+
"Cache-Control": "no-cache, no-store, must-revalidate",
142+
"Pragma": "no-cache",
143+
"Expires": "0",
144+
},
145+
)
146+
147+
148+
async def uidown(_: aiohttp.web.Request) -> aiohttp.web.FileResponse:
149+
"""Serve worker js in worker scope."""
150+
return aiohttp.web.FileResponse(
151+
path=str(f"{setd['static_directory']}/503.html"),
152+
status=503,
153+
headers={
154+
"Cache-Control": "no-cache, no-store, must-revalidate",
155+
"Pragma": "no-cache",
156+
"Expires": "0",
157+
},
158+
)
159+
160+
161+
async def badrequest(_: aiohttp.web.Request) -> aiohttp.web.FileResponse:
162+
"""Serve worker js in worker scope."""
163+
return aiohttp.web.FileResponse(
164+
path=str(f"{setd['static_directory']}/400.html"),
165+
status=400,
166+
headers={
167+
"Cache-Control": "no-cache, no-store, must-revalidate",
168+
"Pragma": "no-cache",
169+
"Expires": "0",
170+
},
171+
)
172+
173+
174+
async def notfound(_: aiohttp.web.Request) -> aiohttp.web.FileResponse:
175+
"""Serve worker js in worker scope."""
176+
return aiohttp.web.FileResponse(
177+
path=str(f"{setd['static_directory']}/404.html"),
178+
status=404,
179+
headers={
180+
"Cache-Control": "no-cache, no-store, must-revalidate",
181+
"Pragma": "no-cache",
182+
"Expires": "0",
183+
},
184+
)

swift_browser_ui/ui/middlewares.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,17 @@
1515
]
1616

1717

18-
def return_error_response(error_code: int) -> web.Response:
18+
def return_error_response(error_code: int) -> web.FileResponse:
1919
"""Return the correct error page with correct status code."""
20-
with open(str(setd["static_directory"]) + "/" + str(error_code) + ".html") as f:
21-
resp = web.Response(
22-
body="".join(f.readlines()),
23-
status=error_code,
24-
content_type="text/html",
25-
headers={
26-
"Cache-Control": "no-cache, no-store, must-revalidate",
27-
"Pragma": "no-cache",
28-
"Expires": "0",
29-
},
30-
)
20+
resp = web.FileResponse(
21+
path=str(f"{setd['static_directory']}/{str(error_code)}.html"),
22+
status=error_code,
23+
headers={
24+
"Cache-Control": "no-cache, no-store, must-revalidate",
25+
"Pragma": "no-cache",
26+
"Expires": "0",
27+
},
28+
)
3129
if error_code == 401:
3230
resp.headers["WWW-Authenticate"] = 'Bearer realm="/", charset="UTF-8"'
3331
return resp
@@ -124,7 +122,9 @@ async def check_session_taintness(
124122

125123

126124
@web.middleware
127-
async def error_middleware(request: web.Request, handler: AiohttpHandler) -> web.Response:
125+
async def error_middleware(
126+
request: web.Request, handler: AiohttpHandler
127+
) -> web.Response | web.FileResponse:
128128
"""Return the correct HTTP Error page."""
129129
to_process = {400, 401, 403, 404}
130130
container_errors = {405, 409}

swift_browser_ui/ui/server.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,15 @@
4646
from swift_browser_ui.ui.discover import handle_discover
4747
from swift_browser_ui.ui.front import (
4848
browse,
49+
forbid,
4950
index,
5051
loginpassword,
52+
notfound,
5153
select,
5254
swasm,
5355
swjs,
56+
uidown,
57+
unauth,
5458
)
5559
from swift_browser_ui.ui.health import handle_health_check
5660
from swift_browser_ui.ui.login import (
@@ -194,6 +198,11 @@ async def on_prepare(
194198
# an spa
195199
aiohttp.web.get("/browse/{tail:.*}", browse),
196200
aiohttp.web.get("/select", select),
201+
aiohttp.web.get("/unauth", unauth),
202+
aiohttp.web.get("/forbid", forbid),
203+
aiohttp.web.get("/uidown", uidown),
204+
aiohttp.web.get("/badrequest", uidown),
205+
aiohttp.web.get("/notfound", notfound),
197206
]
198207
)
199208

swift_browser_ui_frontend/src/common/api.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ async function fetchWithCookie({method, url, body, signal}) {
1616
.then(response => {
1717
switch (response.status) {
1818
case 401:
19-
case 403:
2019
window.location.pathname = "/unauth";
2120
break;
22-
case 500:
21+
case 403:
22+
window.location.pathname = "/forbid";
23+
break;
24+
case 503:
25+
window.location.pathname = "/uidown";
2326
break;
2427
}
2528

tests/ui_unit/test_middlewares.py

Lines changed: 37 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,36 @@ async def return_401_handler(with_exception):
1717
"""Return an HTTP401 error."""
1818
if with_exception:
1919
raise HTTPUnauthorized()
20-
return Response(status=401)
20+
return FileResponse(
21+
status=401, path=f"{os.getcwd()}/swift_browser_ui_frontend/dist/401.html"
22+
)
2123

2224

2325
async def return_403_handler(with_exception):
2426
"""Return an HTTP403 error."""
2527
if with_exception:
2628
raise HTTPForbidden()
27-
return Response(status=403)
29+
return FileResponse(
30+
status=403, path=f"{os.getcwd()}/swift_browser_ui_frontend/dist/403.html"
31+
)
2832

2933

3034
async def return_404_handler(with_exception):
3135
"""Return or raise an HTTP404 error."""
3236
if with_exception:
3337
raise HTTPNotFound()
34-
return Response(status=404)
38+
return FileResponse(
39+
status=404, path=f"{os.getcwd()}/swift_browser_ui_frontend/dist/404.html"
40+
)
3541

3642

3743
async def return_400_handler(with_exception):
3844
"""Return or raise an HTTP400 error."""
3945
if with_exception:
4046
raise HTTPClientError()
41-
return Response(status=400)
47+
return FileResponse(
48+
status=400, path=f"{os.getcwd()}/swift_browser_ui_frontend/dist/400.html"
49+
)
4250

4351

4452
async def return_200_handler(_):
@@ -89,81 +97,51 @@ async def test_check_session_fail_no_raise(self):
8997

9098
async def test_401_return(self):
9199
"""Test 401 middleware when the 401 status is returned."""
92-
patch_setd = unittest.mock.patch(
93-
"swift_browser_ui.ui.middlewares.setd",
94-
new={"static_directory": os.getcwd() + "/swift_browser_ui_frontend/dist"},
100+
resp = await swift_browser_ui.ui.middlewares.error_middleware(
101+
None, return_401_handler
95102
)
96-
with patch_setd:
97-
resp = await swift_browser_ui.ui.middlewares.error_middleware(
98-
None, return_401_handler
99-
)
100-
self.assertEqual(resp.status, 401)
101-
self.assertIsInstance(resp, Response)
103+
self.assertEqual(resp.status, 401)
104+
self.assertIsInstance(resp, FileResponse)
102105

103106
async def test_401_exception(self):
104107
"""Test 401 middleware when the 401 status is risen."""
105-
patch_setd = unittest.mock.patch(
106-
"swift_browser_ui.ui.middlewares.setd",
107-
new={"static_directory": os.getcwd() + "/swift_browser_ui_frontend/dist"},
108+
resp = await swift_browser_ui.ui.middlewares.error_middleware(
109+
True, return_401_handler
108110
)
109-
with patch_setd:
110-
resp = await swift_browser_ui.ui.middlewares.error_middleware(
111-
True, return_401_handler
112-
)
113-
self.assertEqual(resp.status, 401)
114-
self.assertIsInstance(resp, Response)
111+
self.assertEqual(resp.status, 401)
112+
self.assertIsInstance(resp, FileResponse)
115113

116114
async def test_403_return(self):
117115
"""Test 403 middleware when the 403 status is returned."""
118-
patch_setd = unittest.mock.patch(
119-
"swift_browser_ui.ui.middlewares.setd",
120-
new={"static_directory": os.getcwd() + "/swift_browser_ui_frontend/dist"},
116+
resp = await swift_browser_ui.ui.middlewares.error_middleware(
117+
None, return_403_handler
121118
)
122-
with patch_setd:
123-
resp = await swift_browser_ui.ui.middlewares.error_middleware(
124-
None, return_403_handler
125-
)
126-
self.assertEqual(resp.status, 403)
127-
self.assertIsInstance(resp, Response)
119+
self.assertEqual(resp.status, 403)
120+
self.assertIsInstance(resp, FileResponse)
128121

129122
async def test_403_exception(self):
130123
"""Test 403 middleware when the 403 status is risen."""
131-
patch_setd = unittest.mock.patch(
132-
"swift_browser_ui.ui.middlewares.setd",
133-
new={"static_directory": os.getcwd() + "/swift_browser_ui_frontend/dist"},
124+
resp = await swift_browser_ui.ui.middlewares.error_middleware(
125+
True, return_403_handler
134126
)
135-
with patch_setd:
136-
resp = await swift_browser_ui.ui.middlewares.error_middleware(
137-
True, return_403_handler
138-
)
139-
self.assertEqual(resp.status, 403)
140-
self.assertIsInstance(resp, Response)
127+
self.assertEqual(resp.status, 403)
128+
self.assertIsInstance(resp, FileResponse)
141129

142130
async def test_404_return(self):
143131
"""Test 404 middleware when the 404 status is returned."""
144-
patch_setd = unittest.mock.patch(
145-
"swift_browser_ui.ui.middlewares.setd",
146-
new={"static_directory": os.getcwd() + "/swift_browser_ui_frontend/dist"},
132+
resp = await swift_browser_ui.ui.middlewares.error_middleware(
133+
None, return_404_handler
147134
)
148-
with patch_setd:
149-
resp = await swift_browser_ui.ui.middlewares.error_middleware(
150-
None, return_404_handler
151-
)
152-
self.assertEqual(resp.status, 404)
153-
self.assertIsInstance(resp, Response)
135+
self.assertEqual(resp.status, 404)
136+
self.assertIsInstance(resp, FileResponse)
154137

155138
async def test_404_exception(self):
156139
"""Test 404 middlewrae when the 404 status is risen."""
157-
patch_setd = unittest.mock.patch(
158-
"swift_browser_ui.ui.middlewares.setd",
159-
new={"static_directory": os.getcwd() + "/swift_browser_ui_frontend/dist"},
140+
resp = await swift_browser_ui.ui.middlewares.error_middleware(
141+
True, return_404_handler
160142
)
161-
with patch_setd:
162-
resp = await swift_browser_ui.ui.middlewares.error_middleware(
163-
True, return_404_handler
164-
)
165-
self.assertEqual(resp.status, 404)
166-
self.assertIsInstance(resp, Response)
143+
self.assertEqual(resp.status, 404)
144+
self.assertIsInstance(resp, FileResponse)
167145

168146
async def test_error_middleware_no_error(self):
169147
"""Test the general error middleware with correct status."""
@@ -178,11 +156,7 @@ async def test_error_middleware_no_error(self):
178156

179157
async def test_error_middleware_non_handled_raise(self):
180158
"""Test the general error middleware with other status code."""
181-
patch_setd = unittest.mock.patch(
182-
"swift_browser_ui.ui.middlewares.setd",
183-
new={"static_directory": os.getcwd() + "/swift_browser_ui_frontend/dist"},
184-
)
185-
with self.assertRaises(HTTPClientError), patch_setd:
159+
with self.assertRaises(HTTPClientError):
186160
await swift_browser_ui.ui.middlewares.error_middleware(
187161
True, return_400_handler
188162
)

tests/upload/test_cryptupload.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Unit tests for swift_browser_ui.upload.cryptupload module."""
22

3-
import asyncio
43
import aiohttp.web
54
import unittest.mock
65

0 commit comments

Comments
 (0)