Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an add-admin action #220

Merged
merged 32 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
2a864cc
Add an add-user action
nrobinaubertin Jan 22, 2023
4f81db7
Add unit tests for add-user action
nrobinaubertin Jan 25, 2023
0fc1ebb
Change the CLI patch into a plugin
nrobinaubertin Jan 25, 2023
bb43d21
Fix unit tests
nrobinaubertin Jan 25, 2023
0098aca
Remove unused file
nrobinaubertin Jan 25, 2023
de9945f
Split unit tests in two files
nrobinaubertin Jan 26, 2023
77dc42e
Use tpying.List instead of list
nrobinaubertin Jan 26, 2023
5f087f2
Remove unused files
nrobinaubertin Jan 26, 2023
20992f7
Fix docstring
nrobinaubertin Jan 26, 2023
a05206e
Add 'expected_' prefix to a variable's name
nrobinaubertin Jan 26, 2023
ce5bbe6
Add stdout message when something goes wrong
nrobinaubertin Jan 26, 2023
9f3c9c4
Remove unused import
nrobinaubertin Jan 26, 2023
f08fb27
Remove ignores
nrobinaubertin Jan 27, 2023
c9c9e4f
Change action into add-admin (only adds admins)
nrobinaubertin Jan 30, 2023
c6cc492
Add integration test for add-admin
nrobinaubertin Jan 30, 2023
083e21f
Fix author and email for autocreate plugin
nrobinaubertin Jan 30, 2023
0acccc2
Add missing comments and copyright notices
nrobinaubertin Jan 31, 2023
4128088
Fix docstring of the added integ test
nrobinaubertin Jan 31, 2023
bc35a06
Add comment explaining #nosec
nrobinaubertin Jan 31, 2023
af94b49
Add plugins files to the linting tests
nrobinaubertin Jan 31, 2023
8c905aa
Add missing copyright
nrobinaubertin Jan 31, 2023
c348acb
Log exception's stdout
nrobinaubertin Jan 31, 2023
42b8374
Check that the created user is admin before returning it
nrobinaubertin Feb 2, 2023
99b04a9
Change var names in dict comprehension
nrobinaubertin Feb 2, 2023
06140cb
Fix unit test
nrobinaubertin Feb 2, 2023
b3713e7
Fix typo
nrobinaubertin Feb 2, 2023
6337e55
Merge origin/main
nrobinaubertin Feb 2, 2023
03c46a3
Remove pylint disable by renaming var
nrobinaubertin Feb 3, 2023
f0ee5f2
Add a .mypy_cache to be ignored
nrobinaubertin Feb 3, 2023
e505648
Merge remote-tracking branch 'origin/main' into add-user
nrobinaubertin Feb 3, 2023
ec13f9e
Add a tox env and github ci job for the plugins
nrobinaubertin Feb 3, 2023
d7db4c4
Merge remote-tracking branch 'origin/main' into add-user
nrobinaubertin Feb 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ __pycache__/
.idea
.vscode
.mypy_cache
*.egg-info/
1 change: 0 additions & 1 deletion indico.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ RUN apt-get update \
# Add our plugins
COPY --chown=indico:indico plugins /srv/indico/plugins

USER indico
RUN python3 -m pip install --no-cache-dir --no-warn-script-location --prefer-binary \
indico==3.2.0 \
indico-plugin-piwik \
Expand Down
6 changes: 4 additions & 2 deletions plugins/autocreate/autocreate/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from indico.util.i18n import make_bound_gettext
#!/usr/bin/env python3
# Copyright 2023 Canonical Ltd.
# See LICENSE file for licensing details.

_ = make_bound_gettext("autocreate")
"""Create users non-interactively."""
nrobinaubertin marked this conversation as resolved.
Show resolved Hide resolved
20 changes: 17 additions & 3 deletions plugins/autocreate/autocreate/cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#!/usr/bin/env python3
# Copyright 2023 Canonical Ltd.
# See LICENSE file for licensing details.

"""Create users non-interactively."""

import click
from indico.cli.core import cli_group
from indico.core.db import db
Expand Down Expand Up @@ -45,8 +51,10 @@ def create_admin(ctx, email, password):
)
user.is_admin = True

db.session.add(user)
db.session.commit()
# db.session has add()
db.session.add(user) # pylint: disable=no-member
nrobinaubertin marked this conversation as resolved.
Show resolved Hide resolved
# db.session has commit()
db.session.commit() # pylint: disable=no-member

# search the created user
res = search_users(
Expand All @@ -63,4 +71,10 @@ def create_admin(ctx, email, password):
click.secho("Admin was not correctly created", fg="red")
ctx.exit(1)

click.secho(f'Admin with email "{res.pop().email}" correctly created', fg="green")
user = res.pop()

if not user.is_admin:
nrobinaubertin marked this conversation as resolved.
Show resolved Hide resolved
click.secho("Created user is not admin", fg="red")
ctx.exit(1)

click.secho(f'Admin with email "{user.email}" correctly created', fg="green")
8 changes: 7 additions & 1 deletion plugins/autocreate/autocreate/plugin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#!/usr/bin/env python3
# Copyright 2023 Canonical Ltd.
# See LICENSE file for licensing details.

"""Connect our CLI plugin to the existing Indico's CLI."""

from autocreate.cli import cli
Expand All @@ -15,5 +19,7 @@ def init(self):
super().init()
self.connect(signals.plugin.cli, self._extend_indico_cli)

def _extend_indico_cli(self, sender, **kwargs):
# We need those arguments when extending indico's CLI
# pylint: disable=unused-argument
def _extend_indico_cli(self, *args, **kwargs):
nrobinaubertin marked this conversation as resolved.
Show resolved Hide resolved
return cli
6 changes: 3 additions & 3 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ def _get_indico_env_config(self, container: Container) -> Dict:

def _get_indico_env_config_str(self, container: Container) -> Dict[str, str]:
nrobinaubertin marked this conversation as resolved.
Show resolved Hide resolved
indico_env_config = self._get_indico_env_config(container)
return {k: str(v) for k, v in indico_env_config.items()}
return {env_name: str(value) for env_name, value in indico_env_config.items()}

def _get_http_proxy_configuration(self) -> Dict[str, str]:
"""Generate http proxy config.
Expand Down Expand Up @@ -924,11 +924,11 @@ def _add_admin_action(self, event: ActionEvent) -> None:
output = process.wait_output()
event.set_results({"user": f"{event.params['email']}", "output": output})
except ExecError as ex:
logger.exception("Action add-admin failed")
logger.exception("Action add-admin failed: %s", ex.stdout)

event.fail(
# Parameter validation errors are printed to stdout
f"Failed to create admin {event.params['email']}: {ex.stdout}" # type: ignore
f"Failed to create admin {event.params['email']}: {ex.stdout!r}"
)


Expand Down
12 changes: 8 additions & 4 deletions tests/integration/test_charm.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python3
# Copyright 2022 Canonical Ltd.
# Copyright 2023 Canonical Ltd.
# See LICENSE file for licensing details.

"""Indico charm integration tests."""
Expand Down Expand Up @@ -100,16 +100,20 @@ async def test_health_checks(app: Application):
@pytest.mark.asyncio
@pytest.mark.abort_on_fail
async def test_add_admin(app: Application):
"""Test the add-admin action.

Assume that the charm has already been built and is running.
"""
arrange: given charm in its initial state
act: run the add-admin action
assert: check the output in the action result
"""

# Application actually does have units
assert app.units[0] # type: ignore
nrobinaubertin marked this conversation as resolved.
Show resolved Hide resolved

email = "[email protected]"
# This is a test password
password = "somepassword" # nosec

# Application actually does have units
action: juju.action.Action = await app.units[0].run_action( # type: ignore
"add-admin", email=email, password=password
)
Expand Down
6 changes: 4 additions & 2 deletions tests/unit/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,12 @@ def test_add_admin_fail(self, mock_exec):
return_value=("", None),
)

stdout_mock = "CRASH"

# I'm disabling unused-argument here because some could be passed to the mock
def mock_wo_side_effect(*args, **kwargs): # pylint: disable=unused-argument
if isinstance(mock_wo.cmd, list) and "autocreate" in mock_wo.cmd:
raise ExecError(command=mock_wo.cmd, exit_code=42, stdout="CRASH", stderr="")
raise ExecError(command=mock_wo.cmd, exit_code=42, stdout=stdout_mock, stderr="")
return DEFAULT

mock_wo.side_effect = mock_wo_side_effect
Expand Down Expand Up @@ -193,7 +195,7 @@ def event_store_failure(arg):
]

charm._add_admin_action(event)
assert event.fail_message == "Failed to create admin sample@email.com: CRASH"
assert event.fail_message == f"Failed to create admin {email}: '{stdout_mock}'"

mock_exec.assert_any_call(
expected_cmd,
Expand Down
5 changes: 4 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ envlist = lint, unit, static, coverage-report
[vars]
src_path = {toxinidir}/src/
tst_path = {toxinidir}/tests/
plugins_path = {toxinidir}/plugins/
;lib_path = {toxinidir}/lib/charms/operator_name_with_underscores
all_path = {[vars]src_path} {[vars]tst_path}
all_path = {[vars]src_path} {[vars]tst_path} {[vars]plugins_path}

[testenv]
setenv =
Expand Down Expand Up @@ -42,9 +43,11 @@ deps =
flake8-docstrings>=1.6.0
flake8-docstrings-complete>=1.0.3
flake8-test-docs>=1.0
indico==3.2.0
isort
mypy
pep8-naming
plugins/autocreate
pydocstyle>=2.10
pylint
pyproject-flake8<6.0.0
Expand Down