2020-06-29 19:39:04 +00:00
|
|
|
"""Test Enroll flow"""
|
2020-09-11 21:21:11 +00:00
|
|
|
from sys import platform
|
2021-03-24 19:19:10 +00:00
|
|
|
from time import sleep
|
2021-02-18 12:41:03 +00:00
|
|
|
from typing import Any, Optional
|
2020-09-11 21:21:11 +00:00
|
|
|
from unittest.case import skipUnless
|
2020-06-29 19:39:04 +00:00
|
|
|
|
2020-09-09 22:14:59 +00:00
|
|
|
from docker.types import Healthcheck
|
2020-06-29 19:39:04 +00:00
|
|
|
from selenium.webdriver.common.by import By
|
|
|
|
from selenium.webdriver.support import expected_conditions as ec
|
2021-02-21 22:30:31 +00:00
|
|
|
from selenium.webdriver.support.wait import WebDriverWait
|
2020-06-29 19:39:04 +00:00
|
|
|
|
2022-08-01 21:05:58 +00:00
|
|
|
from authentik.blueprints import apply_blueprint
|
2020-12-05 21:08:42 +00:00
|
|
|
from authentik.core.models import User
|
2022-05-29 19:45:25 +00:00
|
|
|
from authentik.core.tests.utils import create_test_flow
|
|
|
|
from authentik.flows.models import FlowDesignation, FlowStageBinding
|
|
|
|
from authentik.lib.generators import generate_id
|
2020-12-05 21:08:42 +00:00
|
|
|
from authentik.stages.email.models import EmailStage, EmailTemplates
|
|
|
|
from authentik.stages.identification.models import IdentificationStage
|
|
|
|
from authentik.stages.prompt.models import FieldTypes, Prompt, PromptStage
|
|
|
|
from authentik.stages.user_login.models import UserLoginStage
|
|
|
|
from authentik.stages.user_write.models import UserWriteStage
|
2022-08-01 21:05:58 +00:00
|
|
|
from tests.e2e.utils import SeleniumTestCase, retry
|
2020-06-29 19:39:04 +00:00
|
|
|
|
|
|
|
|
2020-09-11 21:21:11 +00:00
|
|
|
@skipUnless(platform.startswith("linux"), "requires local docker")
|
2020-06-29 19:39:04 +00:00
|
|
|
class TestFlowsEnroll(SeleniumTestCase):
|
|
|
|
"""Test Enroll flow"""
|
|
|
|
|
2021-02-18 12:41:03 +00:00
|
|
|
def get_container_specs(self) -> Optional[dict[str, Any]]:
|
2020-09-11 21:21:11 +00:00
|
|
|
return {
|
2021-02-03 20:18:31 +00:00
|
|
|
"image": "mailhog/mailhog:v1.0.1",
|
2020-09-11 21:21:11 +00:00
|
|
|
"detach": True,
|
|
|
|
"network_mode": "host",
|
|
|
|
"auto_remove": True,
|
|
|
|
"healthcheck": Healthcheck(
|
2020-08-14 15:55:44 +00:00
|
|
|
test=["CMD", "wget", "--spider", "http://localhost:8025"],
|
2020-06-29 19:39:04 +00:00
|
|
|
interval=5 * 100 * 1000000,
|
|
|
|
start_period=1 * 100 * 1000000,
|
|
|
|
),
|
2020-09-11 21:21:11 +00:00
|
|
|
}
|
2020-06-29 19:39:04 +00:00
|
|
|
|
2020-10-20 16:42:26 +00:00
|
|
|
@retry()
|
2022-08-01 21:05:58 +00:00
|
|
|
@apply_blueprint(
|
|
|
|
"blueprints/default/10-flow-default-authentication-flow.yaml",
|
|
|
|
"blueprints/default/10-flow-default-invalidation-flow.yaml",
|
|
|
|
)
|
2020-06-29 19:39:04 +00:00
|
|
|
def test_enroll_2_step(self):
|
|
|
|
"""Test 2-step enroll flow"""
|
|
|
|
# First stage fields
|
|
|
|
username_prompt = Prompt.objects.create(
|
|
|
|
field_key="username", label="Username", order=0, type=FieldTypes.TEXT
|
|
|
|
)
|
|
|
|
password = Prompt.objects.create(
|
|
|
|
field_key="password", label="Password", order=1, type=FieldTypes.PASSWORD
|
|
|
|
)
|
|
|
|
password_repeat = Prompt.objects.create(
|
|
|
|
field_key="password_repeat",
|
|
|
|
label="Password (repeat)",
|
|
|
|
order=2,
|
|
|
|
type=FieldTypes.PASSWORD,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Second stage fields
|
|
|
|
name_field = Prompt.objects.create(
|
|
|
|
field_key="name", label="Name", order=0, type=FieldTypes.TEXT
|
|
|
|
)
|
|
|
|
email = Prompt.objects.create(
|
|
|
|
field_key="email", label="E-Mail", order=1, type=FieldTypes.EMAIL
|
|
|
|
)
|
|
|
|
|
|
|
|
# Stages
|
2022-05-29 19:45:25 +00:00
|
|
|
first_stage = PromptStage.objects.create(name=generate_id())
|
2020-06-29 19:39:04 +00:00
|
|
|
first_stage.fields.set([username_prompt, password, password_repeat])
|
|
|
|
first_stage.save()
|
2022-05-29 19:45:25 +00:00
|
|
|
second_stage = PromptStage.objects.create(name=generate_id())
|
2020-06-29 19:39:04 +00:00
|
|
|
second_stage.fields.set([name_field, email])
|
|
|
|
second_stage.save()
|
2022-05-29 19:45:25 +00:00
|
|
|
user_write = UserWriteStage.objects.create(name=generate_id())
|
|
|
|
user_login = UserLoginStage.objects.create(name=generate_id())
|
|
|
|
|
|
|
|
flow = create_test_flow(FlowDesignation.ENROLLMENT)
|
2020-06-29 19:39:04 +00:00
|
|
|
|
|
|
|
# Attach enrollment flow to identification stage
|
|
|
|
ident_stage: IdentificationStage = IdentificationStage.objects.first()
|
|
|
|
ident_stage.enrollment_flow = flow
|
|
|
|
ident_stage.save()
|
|
|
|
|
2020-07-03 21:34:44 +00:00
|
|
|
FlowStageBinding.objects.create(target=flow, stage=first_stage, order=0)
|
|
|
|
FlowStageBinding.objects.create(target=flow, stage=second_stage, order=1)
|
|
|
|
FlowStageBinding.objects.create(target=flow, stage=user_write, order=2)
|
|
|
|
FlowStageBinding.objects.create(target=flow, stage=user_login, order=3)
|
2020-06-29 19:39:04 +00:00
|
|
|
|
|
|
|
self.driver.get(self.live_server_url)
|
2021-02-21 22:30:31 +00:00
|
|
|
|
2021-02-27 21:09:40 +00:00
|
|
|
self.initial_stages()
|
2020-06-29 19:39:04 +00:00
|
|
|
|
2021-09-16 15:30:16 +00:00
|
|
|
interface_user = self.get_shadow_root("ak-interface-user")
|
|
|
|
wait = WebDriverWait(interface_user, self.wait_timeout)
|
2021-03-16 17:24:31 +00:00
|
|
|
|
2021-09-16 20:48:34 +00:00
|
|
|
wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, ".pf-c-page__header")))
|
|
|
|
self.driver.get(self.if_user_url("/settings"))
|
2020-06-29 19:39:04 +00:00
|
|
|
|
2020-11-23 13:24:42 +00:00
|
|
|
user = User.objects.get(username="foo")
|
|
|
|
self.assertEqual(user.username, "foo")
|
|
|
|
self.assertEqual(user.name, "some name")
|
|
|
|
self.assertEqual(user.email, "foo@bar.baz")
|
2020-06-29 19:39:04 +00:00
|
|
|
|
2020-10-20 16:42:26 +00:00
|
|
|
@retry()
|
2022-08-01 21:05:58 +00:00
|
|
|
@apply_blueprint(
|
|
|
|
"blueprints/default/10-flow-default-authentication-flow.yaml",
|
|
|
|
"blueprints/default/10-flow-default-invalidation-flow.yaml",
|
|
|
|
)
|
2020-06-29 19:39:04 +00:00
|
|
|
def test_enroll_email(self):
|
|
|
|
"""Test enroll with Email verification"""
|
|
|
|
# First stage fields
|
|
|
|
username_prompt = Prompt.objects.create(
|
|
|
|
field_key="username", label="Username", order=0, type=FieldTypes.TEXT
|
|
|
|
)
|
|
|
|
password = Prompt.objects.create(
|
|
|
|
field_key="password", label="Password", order=1, type=FieldTypes.PASSWORD
|
|
|
|
)
|
|
|
|
password_repeat = Prompt.objects.create(
|
|
|
|
field_key="password_repeat",
|
|
|
|
label="Password (repeat)",
|
|
|
|
order=2,
|
|
|
|
type=FieldTypes.PASSWORD,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Second stage fields
|
|
|
|
name_field = Prompt.objects.create(
|
|
|
|
field_key="name", label="Name", order=0, type=FieldTypes.TEXT
|
|
|
|
)
|
|
|
|
email = Prompt.objects.create(
|
|
|
|
field_key="email", label="E-Mail", order=1, type=FieldTypes.EMAIL
|
|
|
|
)
|
|
|
|
|
|
|
|
# Stages
|
2022-05-29 19:45:25 +00:00
|
|
|
first_stage = PromptStage.objects.create(name=generate_id())
|
2020-06-29 19:39:04 +00:00
|
|
|
first_stage.fields.set([username_prompt, password, password_repeat])
|
|
|
|
first_stage.save()
|
2022-05-29 19:45:25 +00:00
|
|
|
second_stage = PromptStage.objects.create(name=generate_id())
|
2020-06-29 19:39:04 +00:00
|
|
|
second_stage.fields.set([name_field, email])
|
|
|
|
second_stage.save()
|
|
|
|
email_stage = EmailStage.objects.create(
|
2022-05-29 19:45:25 +00:00
|
|
|
name=generate_id(),
|
2020-06-29 19:39:04 +00:00
|
|
|
host="localhost",
|
|
|
|
port=1025,
|
|
|
|
template=EmailTemplates.ACCOUNT_CONFIRM,
|
|
|
|
)
|
2022-05-29 19:45:25 +00:00
|
|
|
user_write = UserWriteStage.objects.create(name=generate_id())
|
|
|
|
user_login = UserLoginStage.objects.create(name=generate_id())
|
|
|
|
|
|
|
|
flow = create_test_flow(FlowDesignation.ENROLLMENT)
|
2020-06-29 19:39:04 +00:00
|
|
|
|
|
|
|
# Attach enrollment flow to identification stage
|
|
|
|
ident_stage: IdentificationStage = IdentificationStage.objects.first()
|
|
|
|
ident_stage.enrollment_flow = flow
|
|
|
|
ident_stage.save()
|
|
|
|
|
2020-07-03 21:34:44 +00:00
|
|
|
FlowStageBinding.objects.create(target=flow, stage=first_stage, order=0)
|
|
|
|
FlowStageBinding.objects.create(target=flow, stage=second_stage, order=1)
|
|
|
|
FlowStageBinding.objects.create(target=flow, stage=user_write, order=2)
|
|
|
|
FlowStageBinding.objects.create(target=flow, stage=email_stage, order=3)
|
|
|
|
FlowStageBinding.objects.create(target=flow, stage=user_login, order=4)
|
2020-06-29 19:39:04 +00:00
|
|
|
|
|
|
|
self.driver.get(self.live_server_url)
|
2021-02-27 21:09:40 +00:00
|
|
|
self.initial_stages()
|
|
|
|
|
|
|
|
# Email stage
|
|
|
|
flow_executor = self.get_shadow_root("ak-flow-executor")
|
|
|
|
email_stage = self.get_shadow_root("ak-stage-email", flow_executor)
|
|
|
|
|
|
|
|
wait = WebDriverWait(email_stage, self.wait_timeout)
|
|
|
|
|
2020-09-11 21:21:11 +00:00
|
|
|
# Wait for the success message so we know the email is sent
|
2021-02-27 21:09:40 +00:00
|
|
|
wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, ".pf-c-form p")))
|
2020-06-29 19:39:04 +00:00
|
|
|
|
|
|
|
# Open Mailhog
|
|
|
|
self.driver.get("http://localhost:8025")
|
|
|
|
|
|
|
|
# Click on first message
|
2021-08-03 15:45:16 +00:00
|
|
|
self.wait.until(ec.presence_of_element_located((By.CLASS_NAME, "msglist-message")))
|
2020-06-29 19:39:04 +00:00
|
|
|
self.driver.find_element(By.CLASS_NAME, "msglist-message").click()
|
|
|
|
self.driver.switch_to.frame(self.driver.find_element(By.CLASS_NAME, "tab-pane"))
|
|
|
|
self.driver.find_element(By.ID, "confirm").click()
|
|
|
|
self.driver.close()
|
|
|
|
self.driver.switch_to.window(self.driver.window_handles[0])
|
|
|
|
|
2021-03-24 19:19:10 +00:00
|
|
|
sleep(2)
|
2020-06-29 19:39:04 +00:00
|
|
|
# We're now logged in
|
2021-09-16 15:30:16 +00:00
|
|
|
wait = WebDriverWait(self.get_shadow_root("ak-interface-user"), self.wait_timeout)
|
2021-03-16 17:24:31 +00:00
|
|
|
|
2021-09-16 20:48:34 +00:00
|
|
|
wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, ".pf-c-page__header")))
|
|
|
|
self.driver.get(self.if_user_url("/settings"))
|
2020-06-29 19:39:04 +00:00
|
|
|
|
2020-11-23 13:24:42 +00:00
|
|
|
self.assert_user(User.objects.get(username="foo"))
|
2021-02-27 21:09:40 +00:00
|
|
|
|
|
|
|
def initial_stages(self):
|
|
|
|
"""Fill out initial stages"""
|
|
|
|
# Identification stage, click enroll
|
|
|
|
flow_executor = self.get_shadow_root("ak-flow-executor")
|
2021-08-03 15:45:16 +00:00
|
|
|
identification_stage = self.get_shadow_root("ak-stage-identification", flow_executor)
|
2021-02-27 21:09:40 +00:00
|
|
|
wait = WebDriverWait(identification_stage, self.wait_timeout)
|
|
|
|
|
|
|
|
wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, "#enroll")))
|
|
|
|
identification_stage.find_element(By.CSS_SELECTOR, "#enroll").click()
|
|
|
|
|
|
|
|
# First prompt stage
|
|
|
|
flow_executor = self.get_shadow_root("ak-flow-executor")
|
|
|
|
prompt_stage = self.get_shadow_root("ak-stage-prompt", flow_executor)
|
|
|
|
wait = WebDriverWait(prompt_stage, self.wait_timeout)
|
|
|
|
|
2021-08-03 15:45:16 +00:00
|
|
|
wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, "input[name=username]")))
|
|
|
|
prompt_stage.find_element(By.CSS_SELECTOR, "input[name=username]").send_keys("foo")
|
2021-02-27 21:09:40 +00:00
|
|
|
prompt_stage.find_element(By.CSS_SELECTOR, "input[name=password]").send_keys(
|
2021-11-23 20:30:02 +00:00
|
|
|
self.user.username
|
2021-02-27 21:09:40 +00:00
|
|
|
)
|
2021-08-03 15:45:16 +00:00
|
|
|
prompt_stage.find_element(By.CSS_SELECTOR, "input[name=password_repeat]").send_keys(
|
2021-11-23 20:30:02 +00:00
|
|
|
self.user.username
|
2021-08-03 15:45:16 +00:00
|
|
|
)
|
2021-02-27 21:09:40 +00:00
|
|
|
prompt_stage.find_element(By.CSS_SELECTOR, ".pf-c-button").click()
|
|
|
|
|
|
|
|
# Second prompt stage
|
|
|
|
flow_executor = self.get_shadow_root("ak-flow-executor")
|
|
|
|
prompt_stage = self.get_shadow_root("ak-stage-prompt", flow_executor)
|
2021-02-27 22:33:15 +00:00
|
|
|
wait = WebDriverWait(prompt_stage, self.wait_timeout)
|
2021-02-27 21:09:40 +00:00
|
|
|
|
2021-08-03 15:45:16 +00:00
|
|
|
wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, "input[name=name]")))
|
|
|
|
prompt_stage.find_element(By.CSS_SELECTOR, "input[name=name]").send_keys("some name")
|
|
|
|
prompt_stage.find_element(By.CSS_SELECTOR, "input[name=email]").send_keys("foo@bar.baz")
|
2021-02-27 21:09:40 +00:00
|
|
|
prompt_stage.find_element(By.CSS_SELECTOR, ".pf-c-button").click()
|