Skip to content

Commit

Permalink
Merge branch 'async' into SDESK-7275
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkLark86 committed Sep 5, 2024
2 parents 2664e79 + 938774b commit de95581
Show file tree
Hide file tree
Showing 39 changed files with 884 additions and 476 deletions.
50 changes: 50 additions & 0 deletions .github/workflows/test-instance.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: "Spawn Test Instance"

on: [push, pull_request]

jobs:
pytest:
runs-on: ubuntu-latest

strategy:
matrix:
python-version: ['3.10']

services:
mongodb:
image: mongo:6
ports:
- 27017:27017
redis:
image: redis:alpine
ports:
- 6379:6379
elastic:
image: docker.elastic.co/elasticsearch/elasticsearch:7.17.22
ports:
- 9200:9200
env:
discovery.type: single-node

steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- run: ./scripts/tests_setup

- name: Install dependencies
working-directory: ./tests/test_instance/server
run: |
pip install -U pip wheel setuptools
pip install -r requirements.txt
- name: Prepare instance
working-directory: ./tests/test_instance/server
run: |
python manage.py app:initialize_data
python manage.py users:create -u admin -p admin -e [email protected] --admin
# python manage.py data:upgrade # until we decide what to do
python manage.py schema:migrate
2 changes: 1 addition & 1 deletion apps/auth/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from .reset_password import ResetPasswordService, ResetPasswordResource, ActiveTokensResource
import superdesk
from .db import DbAuthService
from .commands import CreateUserCommand, HashUserPasswordsCommand # noqa
from .commands import create_user_command, HashUserPasswordsCommand # noqa
from superdesk.services import BaseService
from apps.auth.db.change_password import ChangePasswordService, ChangePasswordResource

Expand Down
82 changes: 42 additions & 40 deletions apps/auth/db/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,33 @@
# AUTHORS and LICENSE files distributed with this source code, or
# at https://www.sourcefabric.org/superdesk/license

import logging
import json
import csv
import json
import click
import logging

import superdesk

from pathlib import Path
from base64 import b64encode
from superdesk.core import get_app_config, get_current_app
import superdesk

from superdesk.flask import Flask
from superdesk.commands import cli
from superdesk.utils import get_hash, is_hashed


logger = logging.getLogger(__name__)
USER_FIELDS_NAMES = {"username", "email", "password", "first_name", "last_name", "sign_off", "role"}


class CreateUserCommand(superdesk.Command):
@cli.register_async_command("users:create", with_appcontext=True)
@click.option("--username", "-u", required=True, help="Username for the new user.")
@click.option("--password", "-p", required=True, help="Password for the new user.")
@click.option("--email", "-e", required=True, help="Email address for the new user.")
@click.option("--admin", "-a", is_flag=True, help="Specify if the user is an administrator.")
@click.option("--support", "-s", is_flag=True, help="Specify if the user is a support user.")
async def create_user_command(*args, **kwargs):
"""Create a user with given username, password and email.
If user with given username exists it's noop.
Expand All @@ -33,46 +45,37 @@ class CreateUserCommand(superdesk.Command):
$ python manage.py users:create -u admin -p admin -e '[email protected]' --admin
"""
return await create_user_command_handler(*args, **kwargs)

option_list = [
superdesk.Option("--username", "-u", dest="username", required=True),
superdesk.Option("--password", "-p", dest="password", required=True),
superdesk.Option("--email", "-e", dest="email", required=True),
superdesk.Option("--admin", "-a", dest="admin", required=False, action="store_true"),
superdesk.Option("--support", "-s", dest="support", required=False, action="store_true"),
]

async def run(self, username, password, email, admin=False, support=False):
# force type conversion to boolean
user_type = "administrator" if admin else "user"

userdata = {
"username": username,
"password": password,
"email": email,
"user_type": user_type,
"is_active": admin,
"is_support": support,
"needs_activation": not admin,
}

app = get_current_app().as_any()
async with app.test_request_context("/users", method="POST"):
if userdata.get("password", None) and not is_hashed(userdata.get("password")):
userdata["password"] = get_hash(
userdata.get("password"), get_app_config("BCRYPT_GENSALT_WORK_FACTOR", 12)
)
async def create_user_command_handler(username: str, password: str, email: str, admin=False, support=False):
user_type = "administrator" if admin else "user"
userdata = {
"username": username,
"password": password,
"email": email,
"user_type": user_type,
"is_active": admin,
"is_support": support,
"needs_activation": not admin,
}

app = get_current_app().as_any()

user = superdesk.get_resource_service("users").find_one(username=userdata.get("username"), req=None)
async with app.test_request_context("/users", method="POST"):
if userdata.get("password", None) and not is_hashed(userdata.get("password")):
userdata["password"] = get_hash(userdata.get("password"), get_app_config("BCRYPT_GENSALT_WORK_FACTOR", 12))

if user:
logger.info("user already exists %s" % (userdata))
else:
logger.info("creating user %s" % (userdata))
superdesk.get_resource_service("users").post([userdata])
logger.info("user saved %s" % (userdata))
user = superdesk.get_resource_service("users").find_one(username=userdata.get("username"), req=None)

if user:
logger.info("user already exists %s" % (userdata))
else:
logger.info("creating user %s" % (userdata))
superdesk.get_resource_service("users").post([userdata])
logger.info("user saved %s" % (userdata))

return userdata
return userdata


class ImportUsersCommand(superdesk.Command):
Expand Down Expand Up @@ -279,7 +282,6 @@ def run(self, username, password):
return encoded_token


superdesk.command("users:create", CreateUserCommand())
superdesk.command("users:import", ImportUsersCommand())
superdesk.command("users:hash_passwords", HashUserPasswordsCommand())
superdesk.command("users:get_auth_token", GetAuthTokenCommand())
2 changes: 1 addition & 1 deletion apps/prepopulate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from .app_prepopulate import PrepopulateService, PrepopulateResource
from .app_populate import AppPopulateCommand # NOQA
from .app_initialize import AppInitializeWithDataCommand # NOQA
from .app_initialize import app_initialize_data_command # NOQA
from .app_scaffold_data import AppScaffoldDataCommand # NOQA


Expand Down
Loading

0 comments on commit de95581

Please sign in to comment.