urbanplatform / django-keycloak-auth

Middleware to allow authorization using Keycloak and Django for django-rest-framework (DRF). This package should only be used in projects starting from scratch, since it overrides the users' management.
MIT License
32 stars 14 forks source link

Django application with Integration tests #30

Closed rjvitorino closed 2 years ago

rjvitorino commented 2 years ago

Is your feature request related to a problem? Please describe. To properly review the merge requests and ensure that changes do not break the community's integrations and deployments, we need integration tests that would be run with the contributed change requests from merge requests etc.

Describe the solution you'd like One could have the integration tests on their own projects using the library, but it would be useful to have a sample Django application in the project that integrates the library and runs the tests.

Describe alternatives you've considered This requires the setup of Keycloak. Will it make the project too heavy or complex to test?

Additional context How can we leverage Github pipelines and runners to optimise this effort? What experience from other projects we can replicate here?

/cc @simao-silva @moritz89 @jaimeventura

moritz89 commented 2 years ago

I agree that this would be very useful in streamlining the development process. A good reference project would be python-keycloak. They have tests, linting, deploys all running for merge requests.

As an asside, in my local tests and GitLab pipeline I actually use a Keycloak instance to validate the code parts that interact with Keycloak. An example would be:

import os
from django.test import TestCase
from django.contrib.auth import get_user_model
from django_keycloak.mixins import KeycloakTestMixin

class UserManagementTests(KeycloakTestMixin, TestCase):
    def setUp(self):
        self.keycloak_init()

    def tearDown(self):
        self.keycloak_cleanup()

    def test_create_users(self):
        """Test creating different types of users"""

        user_manager = get_user_model().objects
        jake = user_manager.create_keycloak_user(
            username="jake", email="jake@example.com", password="Jakes1_passwd"
        )
        self.assertEqual(jake.id, str(user_manager.get(username=jake.username).id))
        self.assertTrue(jake.is_active)
        self.assertFalse(jake.is_staff)
        self.assertFalse(jake.is_superuser)

        superuser_username = os.environ.get("DJANGO_SUPERUSER_USERNAME")
        superuser_password = os.environ.get("DJANGO_SUPERUSER_PASSWORD")
        superuser = user_manager.create_superuser(
            username=superuser_username,
            email="super@example.com",
            password=superuser_password,
        )
        superuser_id = str(user_manager.get(username=superuser.username).id)
        self.assertEqual(superuser.id, superuser_id)
        self.assertTrue(superuser.is_active)
        self.assertTrue(superuser.is_staff)
        self.assertTrue(superuser.is_superuser)
moritz89 commented 2 years ago

@rjfv I've created a setup to be able to run tests in a CI against multiple versions of Python and Keycloak in #33

I've only added a very basic test. Nonetheless, let's merge #33 and add tests in subsequent MRs. I'll mainly be focusing on the functionality that I use, and I'd invite your team to cover aspects that are of importance to you.

moritz89 commented 2 years ago

Has been added to the v2 branch