Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 18 additions & 0 deletions src/onegov/election_day/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,24 @@ def update_archived_results(host: str, scheme: str) -> Processor:
""" Update the archive results, e.g. after a database transfer. """

def generate(request: ElectionDayRequest, app: ElectionDayApp) -> None:
if (
app.principal is not None
and app.principal.official_host is None
or host == 'localhost:8080'
):
click.secho(
'Official host is not set! Do not run this command on '
'staging environment. Uploading results later may create '
'duplicate archived result entries.',
fg='red',
)
click.secho(
'Use `/update-results` view or `update archive` '
'menu, both available for admins only on the UI',
fg='yellow',
)
return

click.secho(f'Updating {app.schema}', fg='yellow')
request.host = host
request.environ['wsgi.url_scheme'] = scheme
Expand Down
33 changes: 20 additions & 13 deletions tests/onegov/election_day/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
from onegov.election_day.models import ComplexVote
from onegov.election_day.models import Vote


from typing import Any, TYPE_CHECKING

if TYPE_CHECKING:
from click.testing import Result
from onegov.core.orm import SessionManager
Expand Down Expand Up @@ -429,34 +429,41 @@ def test_update_archived_results(
redis_url: str,
session_manager: SessionManager
) -> None:

session = session_manager.session()
cfg_path = os.path.join(temporary_directory, 'onegov.yml')
write_config(cfg_path, postgres_dsn, temporary_directory, redis_url)
write_principal(temporary_directory, 'Govikon', entity='1200')
write_principal(
temporary_directory,
"Govikon",
entity="1200",
params={"official_host": "https://wab.govikon.ch"},
)
assert run_command(cfg_path, 'govikon', ['add']).exit_code == 0

add_vote(1, session_manager)

assert run_command(
result = (run_command(
cfg_path,
'govikon',
['update-archived-results']
).exit_code == 0

session = session_manager.session()
assert session.query(ArchivedResult).one().url == (
'http://localhost:8080/onegov_election_day/govikon/vote/vote-1'
)
[
'update-archived-results',
]
))
assert result.exit_code == 0
assert 'Official host is not set!' in result.stdout
assert session.query(ArchivedResult).count() == 0

assert run_command(
result = run_command(
cfg_path,
'govikon',
[
'update-archived-results',
'--host', 'wab.govikon.ch',
'--scheme', 'https'
]
).exit_code == 0
)
assert result.exit_code == 0
assert 'Official host is not set!' not in result.stdout

assert session.query(ArchivedResult).one().url == (
'https://wab.govikon.ch/onegov_election_day/govikon/vote/vote-1'
Expand Down