• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

rero / sonar / 15972762752

30 Jun 2025 12:11PM UTC coverage: 96.376% (-0.002%) from 96.378%
15972762752

push

github

jma
fix(tests): fix tests

* Reorganizes imports with the isort black profile.

Co-Authored-by: Johnny Mariéthoz <Johnny.Mariethoz@rero.ch>

2634 of 2754 new or added lines in 197 files covered. (95.64%)

15 existing lines in 12 files now uncovered.

7871 of 8167 relevant lines covered (96.38%)

1.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.45
/sonar/modules/users/cli.py
1
# -*- coding: utf-8 -*-
2
#
3
# Swiss Open Access Repository
4
# Copyright (C) 2021 RERO
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU Affero General Public License as published by
8
# the Free Software Foundation, version 3 of the License.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU Affero General Public License for more details.
14
#
15
# You should have received a copy of the GNU Affero General Public License
16
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17

18
"""Click command-line interface for user management."""
19

20
from __future__ import absolute_import, print_function
2✔
21

22
import json
2✔
23

24
import click
2✔
25
from click.exceptions import ClickException
2✔
26
from flask import current_app
2✔
27
from flask.cli import with_appcontext
2✔
28
from flask_security.confirmable import confirm_user
2✔
29
from werkzeug.local import LocalProxy
2✔
30

31
from ..users.api import UserRecord
2✔
32

33
datastore = LocalProxy(lambda: current_app.extensions["security"].datastore)
2✔
34

35

36
@click.group()
2✔
37
def users():
2✔
38
    """Users CLI commands."""
39

40

41
@users.command("import")
2✔
42
@click.argument("infile", type=click.File("r"))
2✔
43
@with_appcontext
2✔
44
def import_users(infile):
2✔
45
    """Import users."""
46
    click.secho("Importing users from {file}".format(file=infile.name))
2✔
47

48
    data = json.load(infile)
2✔
49
    for user_data in data:
2✔
50
        try:
2✔
51
            email = user_data.get("email")
2✔
52

53
            # No email found in user's data, account cannot be created
54
            if not email:
2✔
55
                raise ClickException("Email not defined")
2✔
56

57
            user = datastore.find_user(email=email)
2✔
58

59
            # User already exists, skip account creation
60
            if user:
2✔
61
                raise ClickException(
2✔
62
                    "User with email {email} already exists".format(email=email)
63
                )
64

65
            password = user_data.get(
2✔
66
                "password",
67
                "$pbkdf2-sha512$25000$29ubk1KqFUJorTXmHAPAmA$ooj0RJyHyinmZw"
68
                "/.pNMXne8p70X/BDoX5Ypww24OIguSWEo3y.KT6hiwxwHS5OynZNkgnLvf"
69
                "R3m1mNVfsHgfgA",
70
            )
71
            del user_data["password"]
2✔
72

73
            if not user_data.get("role"):
2✔
74
                user_data["role"] = UserRecord.ROLE_USER
2✔
75

76
            if not datastore.find_role(user_data["role"]):
2✔
NEW
77
                datastore.create_role(name=user_data["role"])
×
UNCOV
78
                datastore.commit()
×
79

80
            # Create account and activate it
81
            datastore.create_user(
2✔
82
                email=email, password=password, roles=[user_data["role"]]
83
            )
84
            datastore.commit()
2✔
85
            user = datastore.find_user(email=email)
2✔
86
            confirm_user(user)
2✔
87
            datastore.commit()
2✔
88

89
            click.secho(
2✔
90
                "User {email} with ID #{id} created successfully".format(
91
                    email=email, id=user.id
92
                ),
93
                fg="green",
94
            )
95

96
            # Create user resource
97
            user = UserRecord.create(user_data, dbcommit=True)
2✔
98
            user.reindex()
2✔
99

100
        except Exception as error:
2✔
101
            click.secho(
2✔
102
                "User {user} could not be imported: {error}".format(
103
                    user=user_data, error=str(error)
104
                ),
105
                fg="red",
106
            )
107

108
    click.secho("Finished", fg="green")
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc