feat: view OAuth2 app registration form and handle POST

wip-payments
Aravinth Manivannan 2022-06-06 04:18:31 +05:30
parent 493268c62b
commit 2ed3786fad
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
8 changed files with 219 additions and 9 deletions

View File

@ -0,0 +1,25 @@
# Generated by Django 4.0.3 on 2022-06-05 21:32
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("integrations", "0005_alter_oauthintegration_client_secret_text"),
]
operations = [
migrations.AlterField(
model_name="oauthintegration",
name="client_secret_text",
field=models.CharField(
blank=True,
default="nfZH00oFFZw7nj9o8zCXleNBBwqiMrgs",
editable=False,
max_length=32,
unique=True,
verbose_name="client secret",
),
),
]

View File

@ -0,0 +1,25 @@
# Generated by Django 4.0.3 on 2022-06-05 21:49
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("integrations", "0006_alter_oauthintegration_client_secret_text"),
]
operations = [
migrations.AlterField(
model_name="oauthintegration",
name="client_secret_text",
field=models.CharField(
blank=True,
default="zc5jGzAvl32522k2bK2AGBRyjjuQ7XCS",
editable=False,
max_length=32,
unique=True,
verbose_name="client secret",
),
),
]

View File

@ -0,0 +1,35 @@
# Generated by Django 4.0.3 on 2022-06-05 22:45
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("integrations", "0007_alter_oauthintegration_client_secret_text"),
]
operations = [
migrations.AlterField(
model_name="oauthintegration",
name="client_secret_text",
field=models.CharField(
blank=True,
default="Wfr9q7bWzgnbBFzBhEhPv7mpCRvyfAnc",
editable=False,
max_length=32,
unique=True,
verbose_name="client secret",
),
),
migrations.AlterField(
model_name="oauthintegration",
name="privacy_policy_uri",
field=models.URLField(
blank=True,
default=None,
null=True,
verbose_name="privacy policy of the application",
),
),
]

View File

@ -37,7 +37,7 @@ class OauthIntegration(models.Model):
editable=False,
)
privacy_policy_uri = models.URLField(
"privacy policy of the application", default=None, blank=True
"privacy policy of the application", default=None, blank=True, null=True
)
redirect_uri = models.URLField("uri where user is to be redirected", unique=True)

View File

@ -0,0 +1,24 @@
<form action="{% url 'oauth.integrations.new_app' %}" method="POST">
{% csrf_token %}
<legend>
<h1>{{ create_app.function }} </h1>
</legend>
<label for="name">
{{ create_app.name }}
<input required type="text" name="name" id="name" />
</label>
<label for="redirect_uri">
{{ create_app.redirect_uri }}
<input required type="text" name="redirect_uri" id="redirect_uri" />
</label>
<label for="privacy_policy">
{{ create_app.privacy_policy }}
<input type="text" name="privacy_policy" id="privacy_policy" />
</label>
<button type="submit">Create App</button>
</form>

View File

@ -1,3 +1,70 @@
from django.test import TestCase
# Copyright © 2022 Aravinth Manivannan <realaravinth@batsense.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Create your tests here.
from django.contrib.auth import get_user_model
from django.urls import reverse
from django.test import TestCase, Client
from .views import CREATE_APP_CTX, new_app
class CreateNewAppTests(TestCase):
"""
Tests create new app view
"""
def setUp(self):
self.password = "password121231"
self.user = get_user_model().objects.create_user(
username="create_new_app_tests",
email="create_new_app_tests@example.org",
password=self.password,
)
def test_create_new_app_unauthenticated_user(self):
"""
Tests if new_app is accessible only when user is authenticated
"""
resp = self.client.get(reverse("oauth.integrations.new_app"))
self.assertEqual(resp.status_code, 302)
def test_create_new_app_renders(self):
"""
Tests new_app template render
"""
c = Client()
c.login(username=self.user.username, password=self.password)
c.session.save()
resp = c.get(reverse("oauth.integrations.new_app"))
self.assertEqual(resp.status_code, 200)
for (_, value) in CREATE_APP_CTX.items():
self.assertContains(resp, value)
def test_new_app_submission(self):
"""
Tests new_app template render
"""
payload = {
"name": "test_new_app_submission",
"redirect_uri": "https://test_new_app_submission.example.org",
}
c = Client()
c.login(username=self.user.username, password=self.password)
c.session.save()
resp = c.post(reverse("oauth.integrations.new_app"), payload)
self.assertEqual(resp.status_code, 200)

View File

@ -17,4 +17,6 @@ from django.urls import path, include
from . import views
urlpatterns = [path("", views.index, name="index")]
urlpatterns = [
path("new/", views.new_app, name="oauth.integrations.new_app"),
]

View File

@ -14,10 +14,42 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django.shortcuts import render
from django.http import HttpResponse
from django.http import HttpResponse, Http404
from django.contrib.auth import authenticate
from django.contrib.auth.decorators import login_required
# Create your views here.
def index(request):
return HttpResponse("Integrations bar")
from django.views.decorators.csrf import csrf_protect
from .models import OauthIntegration
CREATE_APP_CTX = {
"function": "Create new OAuth2 Application",
"name": "Application Name",
"redirect_uri": "Redirect URI",
"privacy_policy": "Privacy Policy URI",
}
@login_required
@csrf_protect
def new_app(request):
"""
Create new OAuth integration APP
"""
if request.method == "GET":
return render(request, "integrations/new.html", {"create_app": CREATE_APP_CTX})
if request.method == "POST":
app = OauthIntegration(
owned_by=request.user,
name_text=request.POST["name"],
redirect_uri=request.POST["redirect_uri"],
)
if "privacy_policy" in request.POST:
app.privacy_policy_uri = request.POST["privacy_policy"]
print("OK")
app.save()
return HttpResponse("OK")
return Http404("Method not supported")