Browse Source

Added possibility of automatic generation of test users during project installation and upgrade using *bin/install.sh*

TonyKurts 1 week ago
parent
commit
08ddad8f87
3 changed files with 91 additions and 2 deletions
  1. 7 1
      README.md
  2. 13 0
      bin/install.sh
  3. 71 1
      sharix_admin/signals.py

+ 7 - 1
README.md

@@ -2,10 +2,16 @@
 
 The base Django project of a service web application to which other modules are connected.
 
-## Installation
+## Installation / Upgrade
 
 Download or clone repository. For the initial configuration, run *bin/install.sh*.
 
+During the installation, you can pass the `--test-users` parameter to automatically create test users. These users will be assigned to the **TEST** group. Additionally, if the `DEBUG` setting is enabled (`DEBUG=True` in *core/settings_vars.py*), these test users will be marked as active and can log in. If `DEBUG` is disabled (`DEBUG=False`), the users will be inactive and unable to log in.
+
+```bash
+./bin/install.sh --test-users
+```
+
 ## Configuration
 
 For basic project configuration when deploying, use the *core/settings_vars.py*. This file is automatically created during the installation script execution, in case this file has not been created yet. It is a copy of *core/_settings_vars.py* and contains some default settings that allow the project to run locally and is more suitable for development.

+ 13 - 0
bin/install.sh

@@ -2,6 +2,19 @@
 
 source bin/install.cfg
 
+# Command line argument handler
+while [[ "$#" -gt 0 ]]; do
+    case $1 in
+        --test-users) 
+            export TEST_USERS=true
+            echo "Test users flag set"
+            shift ;;
+        *) 
+            echo "Unknown parameter: $1"
+            shift ;;
+    esac
+done
+
 # Function to check if a repository exists and perform git pull or git clone
 update_repository() {
     local repo_url="$1"

+ 71 - 1
sharix_admin/signals.py

@@ -1,10 +1,18 @@
+import os
+
 from django.contrib.auth.models import Group
 from django.dispatch import receiver
 from django.db.models.signals import post_migrate
+from django.contrib.auth import get_user_model
+from django.contrib.auth.hashers import make_password
+from django.conf import settings
 
 from tickets.models import TicketList
 
 
+User = get_user_model()
+
+
 @receiver(post_migrate)
 def create_initial_groups(sender, **kwargs):
     Group.objects.get_or_create(pk=21, name='METASERVICE-ADMIN')
@@ -21,6 +29,60 @@ def create_initial_groups(sender, **kwargs):
     Group.objects.get_or_create(pk=51, name='CLIENT')
     Group.objects.get_or_create(pk=61, name='GUEST')
 
+    Group.objects.get_or_create(pk=99, name='TEST')
+
+
+@receiver(post_migrate)
+def create_test_users(sender, **kwargs):
+    """
+    Создает тестовых пользователей, но только если установочный скрипт был запущен с параметром --test-users
+    или была задана переменная окружения TEST_USERS=true.
+    """
+    if os.getenv('TEST_USERS') == 'true':
+        password = make_password("sharix-open-test")
+        test_group = Group.objects.get(pk=99)
+
+        # Группа, в которой надо создать пользователя и их количество
+        test_users = [
+            ('METASERVICE-ADMIN', 1),
+            ('METASERVICE-SUPERVISOR', 3),
+            ('METASERVICE-SUPPORT', 3),
+            ('METASERVICE-TECHSUPPORT', 3),
+
+            ('PARTNER-ADMIN', 1),
+            ('PARTNER-SUPERVISOR', 3),
+            ('PARTNER-SUPPORT', 3),
+            ('PARTNER-TECHSUPPORT', 3),
+
+            ('PROVIDER', 3),
+            ('CLIENT', 3),
+            ('GUEST', 3)
+        ]
+
+        for test_user in test_users:
+            group_name = test_user[0]        
+            group = Group.objects.get(name=group_name)
+
+            for i in range(1, test_user[1] + 1):
+                user, created = User.objects.get_or_create(
+                    phone_number=f"{group.pk}0{i}",
+                    defaults={
+                        'last_name': i,
+                        'first_name': group_name,
+                        'email': f"test-{group_name.lower()}-{i}@domain.org",
+                        'username': f"test-{group_name.lower()}-{i}",
+                        'middle_name': "Test",
+                        'password': password
+                    }
+                )
+
+                if created:
+                    user.groups.add(
+                        test_group,
+                        group
+                    )
+                    print(f"Test user created: {user}")
+
 
 @receiver(post_migrate)
 def create_initial_ticket_lists(sender, **kwargs):
@@ -61,4 +123,12 @@ def create_initial_ticket_lists(sender, **kwargs):
         TicketList.objects.get_or_create(pk=3207, name='Ручное подтверждение заявок (ACCESS-REQUEST)', group=Group.objects.get(name='PARTNER-SUPERVISOR'))
         
         ## TECHSUPPORT
-        TicketList.objects.get_or_create(pk=3401, name='Входящие технические заявки (ST_REQUEST)', group=Group.objects.get(name='PARTNER-TECHSUPPORT'))
+        TicketList.objects.get_or_create(pk=3401, name='Входящие технические заявки (ST_REQUEST)', group=Group.objects.get(name='PARTNER-TECHSUPPORT'))
+
+
+# Откючаем тестовых пользователей, если DEBUG=False и наоброт
+test_group = Group.objects.get(name="TEST")
+users_in_test_group = User.objects.filter(groups=test_group)
+
+if users_in_test_group:
+    users_in_test_group.update(is_active=settings.DEBUG)