Skip to content

Commit

Permalink
addressed changes
Browse files Browse the repository at this point in the history
  • Loading branch information
codethulu committed Jun 13, 2024
1 parent 300d64f commit a2c0d7d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 27 deletions.
4 changes: 2 additions & 2 deletions discourse_rock/patches/discourse-charm.patch
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
+ exit 0
+ end
+ puts "ERROR: User with email #{email} not found"
+ exit 1
+ exit 2
+end
+
+desc "Activate a user account"
Expand All @@ -20,7 +20,7 @@
+ user = User.find_by_email(email)
+ if !user
+ puts "User with email #{email} does not exist"
+ exit 1
+ exit 2
+ end
+ user.email_tokens.update_all(confirmed: true)
+ puts "User with email #{email} activated"
Expand Down
2 changes: 1 addition & 1 deletion docs/how-to/contribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ the registry:

```shell
cd [project_dir]/discourse_rock && rockcraft pack rockcraft.yaml
skopeo --insecure-policy copy --dest-tls-verify=false oci-archive:discourse_1.0_amd64.rock docker://localhost:32000/discourse:latest
rockcraft.skopeo --insecure-policy copy --dest-tls-verify=false oci-archive:discourse_1.0_amd64.rock docker://localhost:32000/discourse:latest
```

### Deploy
Expand Down
32 changes: 27 additions & 5 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,8 +718,13 @@ def _on_promote_user_action(self, event: ActionEvent) -> None:
)
try:
user_exists.wait_output()
except ExecError:
event.fail(f"User with email {email} does not exist")
except ExecError as ex:
if ex.exit_code == 2:
event.fail(f"User with email {email} does not exist")
else:
event.fail(
f"Error checking if user with email {email} exists: {ex.stdout}" # type: ignore
)
return

process = container.exec(
Expand Down Expand Up @@ -766,8 +771,12 @@ def _on_create_user_action(self, event: ActionEvent) -> None:
user_exists.wait_output()
event.fail(f"User with email {email} already exists")
return
except ExecError:
pass
except ExecError as ex:
if ex.exit_code == 2:
pass
else:
event.fail(f"Error checking if user with email {email} exists: {ex.stdout}")
return
# Admin flag is optional, if it is true, the user will be created as an admin
admin_flag = "Y" if event.params.get("admin") else "N"
process = container.exec(
Expand All @@ -788,6 +797,7 @@ def _on_create_user_action(self, event: ActionEvent) -> None:
event.set_results({"user": email, "password": password})
except ExecError as ex:
event.fail(f"Failed to make user with email {email}: {ex.stdout}") # type: ignore
return

if event.params.get("admin") or not event.params.get("active"):
return
Expand All @@ -803,7 +813,19 @@ def _on_create_user_action(self, event: ActionEvent) -> None:
user=CONTAINER_APP_USERNAME,
environment=self._create_discourse_environment_settings(),
)
activate_process.wait_output()
try:
activate_process.wait_output()
except ExecError as ex:
if ex.exit_code == 2:
event.fail(
f"Could not find user {email} for activation: {ex.stdout}" # type: ignore
)
return
event.fail(
# Parameter validation errors are printed to stdout
# Ignore mypy warning when formatting stdout
f"Failed to activate user with email {email}: {ex.stdout}" # type: ignore
)

def _generate_password(self, length: int) -> str:
"""Generate a random password.
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ async def app_fixture(
async with ops_test.fast_forward():
await model.wait_for_idle(apps=[postgres_app.name], status="active")

# Revision 28 is being used due to https://github.com/canonical/redis-k8s-operator/issues/87.
redis_app = await model.deploy("redis-k8s", series="jammy", channel="latest/edge", revision=28)
redis_app = await model.deploy("redis-k8s", series="jammy", channel="latest/edge")
await model.wait_for_idle(apps=[redis_app.name], status="active")

await model.deploy("nginx-ingress-integrator", series="focal", trust=True)
Expand Down
17 changes: 0 additions & 17 deletions tests/integration/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from juju.action import Action
from juju.application import Application
from juju.unit import Unit
from pytest_operator.plugin import Model

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -44,23 +43,13 @@ async def test_create_user(
async def test_promote_user(
app: Application,
discourse_address: str,
model: Model,
requests_timeout: float,
):
"""
arrange: A discourse application
act: Promote a user to admin
assert: User cannot access the admin API before being promoted
"""

def test_discourse_srv_status_ok():
response = requests.get(f"{discourse_address}/srv/status", timeout=requests_timeout)
assert response.status_code == 200

# The charm should be active when starting this test
await model.wait_for_idle(status="active")
test_discourse_srv_status_ok()

with requests.session() as session:

def get_api_key(csrf_token: str) -> bool:
Expand Down Expand Up @@ -107,12 +96,6 @@ def get_api_key(csrf_token: str) -> bool:
},
)

try:
"error" not in response.json()
except Exception as e:
logger.error("Error in response: %s", response.text)
raise e

assert response.ok, response.text # pylint: disable=no-member
assert "error" not in response.json()

Expand Down

0 comments on commit a2c0d7d

Please sign in to comment.