152 lines
4.8 KiB
Python
152 lines
4.8 KiB
Python
# Create your tests here.
|
|
|
|
# Copyright © 2022 Aravinth Manivannan <realaravinth@batsense.net>
|
|
# Copyright © 2022 Alan Alexander Thomas <alan2000alex@gmail.com>
|
|
#
|
|
# 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/>.
|
|
import time
|
|
import os
|
|
from io import StringIO
|
|
from urllib.parse import urlparse, urlunparse
|
|
|
|
import requests
|
|
|
|
from django.core import mail
|
|
from django.contrib.auth import get_user_model
|
|
from django.core.management import call_command
|
|
from django.urls import reverse
|
|
from django.test import TestCase, Client, override_settings
|
|
from django.utils.http import urlencode
|
|
from django.contrib.auth import authenticate
|
|
from django.conf import settings
|
|
|
|
|
|
from oauth2_provider.models import get_application_model
|
|
|
|
|
|
def register_util(t: TestCase, username: str):
|
|
t.password = "asdklfja;ldkfja;df"
|
|
t.email = f"{username}@vitap.ac.in"
|
|
t.user = get_user_model().objects.create(
|
|
username=username,
|
|
email=t.email,
|
|
)
|
|
t.user.set_password(t.password)
|
|
t.user.save()
|
|
|
|
|
|
def login_util(t: TestCase, c: Client, redirect_to: str):
|
|
payload = {
|
|
"login": t.email,
|
|
"password": t.password,
|
|
}
|
|
resp = c.post(reverse("accounts.login"), payload)
|
|
t.assertEqual(resp.status_code, 302)
|
|
t.assertEqual(resp.headers["location"], reverse(redirect_to))
|
|
|
|
|
|
class CreateOidCApplicaiton(TestCase):
|
|
"""
|
|
Test command: manage.py create_oidc
|
|
"""
|
|
|
|
def setUp(self):
|
|
self.username = "oidcadmin"
|
|
register_util(t=self, username=self.username)
|
|
|
|
def test_cmd(self):
|
|
|
|
Application = get_application_model()
|
|
|
|
stdout = StringIO()
|
|
stderr = StringIO()
|
|
|
|
redirect_uri = "http://example.org"
|
|
app_name = "test_cmd_oidc"
|
|
|
|
# username exists
|
|
call_command(
|
|
"create_oidc",
|
|
app_name,
|
|
self.username,
|
|
redirect_uri,
|
|
stdout=stdout,
|
|
stderr=stderr,
|
|
)
|
|
out = stdout.getvalue()
|
|
|
|
self.assertIn(f"New application {app_name} created successfully", out)
|
|
|
|
client_id = out.split("\n")[1].split(" ")[1]
|
|
|
|
self.assertEqual(
|
|
get_application_model().objects.filter(client_id=client_id).exists(), True
|
|
)
|
|
app = get_application_model().objects.get(name=app_name)
|
|
self.assertEqual(app.client_id, client_id)
|
|
self.assertEqual(app.name, app_name)
|
|
self.assertEqual(app.user_id, self.user.id)
|
|
self.assertEqual(app.redirect_uris, redirect_uri)
|
|
self.assertEqual(app.skip_authorization, True)
|
|
self.assertEqual(app.client_type, "confidential")
|
|
self.assertEqual(app.authorization_grant_type, "authorization-code")
|
|
self.assertEqual(app.algorithm, "HS256")
|
|
|
|
|
|
class RegistrationTest(TestCase):
|
|
def setUp(self):
|
|
self.username = "register_user"
|
|
self.password = "2i3j4;1qlk2asdf"
|
|
self.email = "register_user@vitap.ac.in"
|
|
|
|
def test_register_template_works(self):
|
|
"""
|
|
Tests if register template renders
|
|
"""
|
|
resp = self.client.get(reverse("accounts:register"))
|
|
self.assertEqual(b"Register" in resp.content, True)
|
|
|
|
def test_register_works(self):
|
|
"""
|
|
Tests if register works
|
|
"""
|
|
c = Client()
|
|
|
|
# passwords don't match
|
|
msg = {
|
|
"password": self.password,
|
|
"email": self.email,
|
|
"password-confirm": self.email,
|
|
}
|
|
resp = c.post(reverse("accounts:register"), msg)
|
|
self.assertEqual(resp.status_code, 400)
|
|
|
|
# register user
|
|
msg["password-confirm"] = self.password
|
|
resp = c.post(reverse("accounts:register"), msg)
|
|
self.assertEqual(resp.status_code, 200)
|
|
|
|
user = get_user_model().objects.get(username=self.username)
|
|
|
|
# duplicate email
|
|
resp = c.post(reverse("accounts:register"), msg)
|
|
self.assertEqual(resp.status_code, 400)
|
|
self.assertEqual(b"This email is already registered." in resp.content, True)
|
|
|
|
msg["email"] = "12345@gmail.com"
|
|
resp = c.post(reverse("accounts:register"), msg)
|
|
self.assertEqual(resp.status_code, 400)
|
|
self.assertEqual(
|
|
b"We do not provide services for this domain yet." in resp.content, True
|
|
)
|