Skip to content

Commit

Permalink
Merge pull request #218 from FAIRDataPipeline/hotfix-remote
Browse files Browse the repository at this point in the history
Hotfix remote
  • Loading branch information
RyanJField authored Apr 30, 2024
2 parents 3ba8701 + 62490d7 commit 26e5164
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 17 deletions.
6 changes: 5 additions & 1 deletion data_management/rest/renderers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from rest_framework import renderers, serializers
from rest_framework.utils.field_mapping import ClassLookupDict

from data_management import settings

class HTMLFormRenderer(renderers.HTMLFormRenderer):
"""
Expand Down Expand Up @@ -89,3 +89,7 @@ class BrowsableAPIRenderer(renderers.BrowsableAPIRenderer):
Subclassing the BrowsableAPIRenderer to use our custom HTMLFormRenderer.
"""
form_renderer_class = HTMLFormRenderer
def get_context(self, *args, **kwargs):
context = super(BrowsableAPIRenderer, self).get_context(*args, **kwargs)
context["remote_registry"] = settings.REMOTE_REGISTRY
return(context)
15 changes: 15 additions & 0 deletions data_management/rest/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,4 +569,19 @@ def auth_provider(request):
_data = {
"auth_provider":conf_settings.AUTH_METHOD
}
return JsonResponse(_data)

def auth_url(request):
"""Returns Auth Provider URL in Json Format"""
auth_url = None
if hasattr(conf_settings, 'SOCIAL_AUTH_GITLAB_API_URL'):
if conf_settings.SOCIAL_AUTH_GITLAB_API_URL:
auth_url = conf_settings.SOCIAL_AUTH_GITLAB_API_URL
if conf_settings.AUTH_METHOD == 'GitLab' and not auth_url:
auth_url = "https://gitlab.com"
if conf_settings.AUTH_METHOD == 'GitHub' and not auth_url:
auth_url = "https://github.com"
_data = {
"auth_url":auth_url
}
return JsonResponse(_data)
3 changes: 1 addition & 2 deletions data_management/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -827,8 +827,7 @@ def test_filter_by_key(self):
self.assertEqual(response['Content-Type'], 'application/json')
results = response.json()['results']
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['key'], 'TestKey2')

self.assertEqual(results[0]['key'], 'TestKey2')

class ProvAPITests(TestCase):

Expand Down
64 changes: 64 additions & 0 deletions data_management/tests/test_custom_urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from django.conf import settings
from django.test import TestCase
from django.urls import reverse
from django.contrib.auth import get_user_model
from rest_framework.test import APIClient

from .initdb import init_db

class CustomURLTests(TestCase):
def setUp(self):
settings.AUTH_METHOD = "GitLab"
settings.SOCIAL_AUTH_GITLAB_API_URL = "https://test.com"
self.user = get_user_model().objects.create(username='Test User')
init_db()

def test_auth_provider_url(self):
client = APIClient()
url = reverse('auth-provider')
response = client.get(url, format='json')

self.assertEqual(response.status_code, 200)
self.assertEqual(response['Content-Type'], 'application/json')
self.assertEqual(response.json()['auth_provider'], 'GitLab')

def test_auth_url(self):
client = APIClient()
url = reverse('auth-url')
response = client.get(url, format='json')

self.assertEqual(response.status_code, 200)
self.assertEqual(response['Content-Type'], 'application/json')
self.assertEqual(response.json()['auth_url'], 'https://test.com')

def test_auth_url_github(self):
client = APIClient()
settings.AUTH_METHOD = "GitHub"
settings.SOCIAL_AUTH_GITLAB_API_URL = None
url = reverse('auth-url')
response = client.get(url, format='json')

self.assertEqual(response.status_code, 200)
self.assertEqual(response['Content-Type'], 'application/json')
self.assertEqual(response.json()['auth_url'], 'https://github.com')

def test_auth_url_github_no_api(self):
client = APIClient()
settings.AUTH_METHOD = "GitLab"
settings.SOCIAL_AUTH_GITLAB_API_URL = None
url = reverse('auth-url')
response = client.get(url, format='json')

self.assertEqual(response.status_code, 200)
self.assertEqual(response['Content-Type'], 'application/json')
self.assertEqual(response.json()['auth_url'], 'https://gitlab.com')

def test_auth_provider_url_github(self):
client = APIClient()
settings.AUTH_METHOD = "GitHub"
url = reverse('auth-provider')
response = client.get(url, format='json')

self.assertEqual(response.status_code, 200)
self.assertEqual(response['Content-Type'], 'application/json')
self.assertEqual(response.json()['auth_provider'], 'GitHub')
4 changes: 3 additions & 1 deletion data_management/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
path('data/<str:name>', views.get_data),
path('api/data/<str:checksum>', api_views.ObjectStorageView.as_view()),
path('api/data', api_views.ObjectStorageView.as_view()),
path('api/auth-provider', api_views.auth_provider),
path('api/auth-provider/', api_views.auth_provider, name='auth-provider'),
path('api/auth-url/', api_views.auth_url, name='auth-url'),
path('api/logout', views.logout),
path('logout', views.logout),
]

Expand Down
2 changes: 1 addition & 1 deletion data_management/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,4 @@ def external_object(request, alternate_identifier, title, version):
def logout(request):
"""Logs out user"""
auth_logout(request)
return redirect("/")
return redirect(request.META.get('HTTP_REFERER', '/'))
2 changes: 1 addition & 1 deletion drams/base_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
ACCOUNT_UNIQUE_EMAIL = False

# Redirect authenticated users to this URL
LOGIN_REDIRECT_URL = 'index'
#LOGIN_REDIRECT_URL = 'index'

CRISPY_TEMPLATE_PACK = 'bootstrap3'

Expand Down
2 changes: 2 additions & 0 deletions drams/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

ALLOWED_HOSTS = ['data.fairdatapipeline.org', '127.0.0.1', 'localhost']

DOMAIN_URL = 'https://data.fairdatapipeline.org/'

REMOTE = True

DATABASES = {
Expand Down
4 changes: 2 additions & 2 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@
{% else %}
{% is_auth_method "GitHub" as is_github %}
{% if is_github %}
<a href="{% url 'social:begin' 'github' %}">Login / Signup with GitHub </a>
<a href="{% url 'social:begin' 'github' %}?next={{ request.get_full_path|urlencode }}">Login / Signup with GitHub </a>
{% endif %}
{% is_auth_method "GitLab" as is_gitlab %}
{% if is_gitlab %}
<a href="{% url 'social:begin' 'gitlab' %}">Login / Signup with GitLab </a>
<a href="{% url 'social:begin' 'gitlab' %}?next={{ request.get_full_path|urlencode }}">Login / Signup with GitLab </a>
{% endif %}
{% endif %}
</span>
Expand Down
25 changes: 16 additions & 9 deletions templates/rest_framework/api.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% extends "rest_framework/base.html" %}
{% load auth_tags %}

{% load static %}

Expand All @@ -19,15 +20,21 @@

{% block userlinks %}
{% if remote_registry %}
<span class="navbar-text">
{% if user.is_authenticated %}
Logged in as: {{ user.username }}
<a href="/logout">logout</a>
{% else %}
<a href="{% url 'social:begin' 'github' %}">Login / Signup with GitHub </a>
<a href="{% url 'social:begin' 'gitlab' %}">Login / Signup with GitLab </a>
{% endif %}
</span>
<span class="navbar-text">
{% if user.is_authenticated %}
Logged in as: {{ user.username }}
<a href="logout">logout</a>
{% else %}
{% is_auth_method "GitHub" as is_github %}
{% if is_github %}
<a href="{% url 'social:begin' 'github' %}?next={{ request.get_full_path|urlencode }}">Login / Signup with GitHub </a>
{% endif %}
{% is_auth_method "GitLab" as is_gitlab %}
{% if is_gitlab %}
<a href="{% url 'social:begin' 'gitlab' %}?next={{ request.get_full_path|urlencode }}">Login / Signup with GitLab </a>
{% endif %}
{% endif %}
</span>
{% endif %}
{% endblock %}

Expand Down

0 comments on commit 26e5164

Please sign in to comment.