# Copyright © 2022 Aravinth Manivannan # # 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 . from django.contrib.auth import get_user_model from django.urls import reverse from django.test import TestCase, Client, override_settings from .views import CREATE_APP_CTX 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, ) self.superuser = get_user_model().objects.create_superuser( username="create_new_app_tests_superuser", email="create_new_app_tests_superuser@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_view_is_restricted_to_super_user(self): """ Tests if view is only accessible from superuser accounts """ 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, 404) def test_create_new_app_renders(self): """ Tests new_app template render """ c = Client() c.login(username=self.superuser.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.superuser.username, password=self.password) c.session.save() resp = c.post(reverse("oauth.integrations.new_app"), payload) self.assertEqual(resp.status_code, 200) def test_method_unavailable(self): """ Test new_app using unsupported HTTP method """ c = Client() c.login(username=self.superuser.username, password=self.password) c.session.save() resp = c.head(reverse("oauth.integrations.new_app")) self.assertEqual(resp.status_code, 404) @override_settings(RESTRICT_NEW_INTEGRATION_INSTALLATION=False) def test_unrestricted_app_creation(self): """ Test new_app using unsupported HTTP method """ 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)