Skip to content

Commit

Permalink
Local status template
Browse files Browse the repository at this point in the history
  • Loading branch information
raftmsohani committed Jul 11, 2024
1 parent 410ce8f commit 8274434
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 5 deletions.
2 changes: 2 additions & 0 deletions tdrs-backend/tdpservice/settings/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ class Local(Common):
}

REDIS_SERVER_LOCAL = bool(strtobool(os.getenv("REDIS_SERVER_LOCAL", "TRUE")))

AMS_CONFIGURATION_ENDPOINT = "http://localhost:8080/.well-known/openid-configuration"
21 changes: 16 additions & 5 deletions tdrs-backend/tdpservice/users/api/login_redirect_oidc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from urllib.parse import quote_plus, urlencode

from django.conf import settings
from django.http import HttpResponseRedirect
from django.http import HttpResponseRedirect, HttpResponse
from django.views.generic.base import RedirectView

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -93,19 +93,30 @@ def get_ams_configuration():
Includes currently published URLs for authorization, token, etc.
"""
return None
r = requests.get(settings.AMS_CONFIGURATION_ENDPOINT)
data = r.json()
return data
if r.status_code != 200:
logger.error(
f"Failed to get AMS configuration: {r.status_code} - {r.text}"
)
return None
else:
data = r.json()
return data

def get(self, request, *args, **kwargs):
"""Handle login workflow based on request origin."""
# Create state and nonce to track requests
logger.info("--------------- 1")
state = secrets.token_hex(32)
nonce = secrets.token_hex(32)

logger.info("--------------- 2")
"""Get request and manage login information with AMS OpenID."""
configuration = self.get_ams_configuration()

logger.info("--------------- 3")
if not configuration:
return HttpResponse("Failed to get AMS configuration", status=500)
logger.info("--------------- 4")
auth_params = {
"client_id": settings.AMS_CLIENT_ID,
"nonce": nonce,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#LoginRedirectAMS
from unittest import mock
from tdpservice.users.api.login_redirect_oidc import LoginRedirectAMS

@mock.patch("requests.get")
def test_get_ams_configuration(requests_get_mock):
"""Test the LoginRedirectAMS class."""
requests_get_mock.return_value.status_code = 200
requests_get_mock.return_value.json.return_value = {"key": "test"}
returned_value = LoginRedirectAMS.get_ams_configuration()
assert returned_value == {"key": "test"}

# Test if the configuration is not returned
requests_get_mock.return_value.status_code = 500
returned_value = LoginRedirectAMS.get_ams_configuration()
assert returned_value is None

@mock.patch("requests.get")
@mock.patch("secrets.token_hex")
def test_LoginRedirectAMS_get(secrets_token_hex_mock, requests_get_mock):
"""Test the LoginRedirectAMS class."""
class DummyRequest:
session = {
"state_nonce_tracker": "dummy_state_nonce_tracker"
}
requests_get_mock.return_value.status_code = 200
requests_get_mock.return_value.json.return_value = {"authorization_endpoint": "dummy_authorization_endpoint"}

secrets_token_hex_mock.return_value = "dummy_state_nonce"

login_redirect_ams = LoginRedirectAMS()

response = login_redirect_ams.get(DummyRequest)
assert response.url == "dummy_authorization_endpoint?client_id=&nonce=dummy_state_nonce&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fv1%2Foidc%2Fams&response_type=code&state=dummy_state_nonce&scope=openid+email"


# Test if the AMS server is down
requests_get_mock.return_value.status_code = 500
login_redirect_ams = LoginRedirectAMS()
response = login_redirect_ams.get("request")
print('____ response', response.__dict__)
assert response.status_code == 500


1 change: 1 addition & 0 deletions tdrs-frontend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ COPY nginx/local/default.conf.template /etc/nginx/default.conf.template
COPY nginx/local/locations.conf /etc/nginx/locations.conf
#COPY nginx/local/under_maintenance.html /etc/nginx/under_maintenance.html
COPY nginx/src/503.html /usr/share/nginx/html/503_.html
COPY nginx/src/50x.html /usr/share/nginx/html/50x.html
COPY nginx/src/static/ /usr/share/nginx/html/static/
8 changes: 8 additions & 0 deletions tdrs-frontend/nginx/local/locations.conf
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ location ~ ^/(v1|admin|static/admin|static/drf-yasg|swagger|redocs) {
proxy_buffer_size 4k;
proxy_temp_file_write_size 64k;
proxy_pass_header x-csrftoken;

}

location ~* ^/kibana/(.*)$ {
Expand Down Expand Up @@ -86,6 +87,13 @@ location = / {
try_files $uri $uri/ /index.html;
}

proxy_intercept_errors on;
error_page 500 502 504 /50x.html;
location = /50x.html {
sub_filter '##STATUS##' $status;
root /usr/share/nginx/html;
try_files /50x.html @error;
internal;
}

error_page 404 /index.html;
17 changes: 17 additions & 0 deletions tdrs-frontend/nginx/src/50x.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Your Business</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<!--# if expr="$status = 502" -->
<h1>We are updating our website "##STATUS##"</h1>
<p>This is only for a few seconds, you will be redirected. $status</p>
<!--# else -->
<h1><!--# echo var="status" default="" --> <!--# echo var="status_text" default="Something goes wrong" --></h1>
<!--# endif -->

</body>
</html>

0 comments on commit 8274434

Please sign in to comment.