def test_bundle_invalid_format(self): """Test bundle with invalid format""" importer = FlowImporter('{"version": 3}') self.assertFalse(importer.validate()) importer = FlowImporter( ('{"version": 1,"entries":[{"identifiers":{},"attrs":{},' '"model": "authentik_core.User"}]}')) self.assertFalse(importer.validate())
def test_export_validate_import_policies(self): """Test export and validate it""" flow_slug = generate_client_id() stage_name = generate_client_id() with transaction_rollback(): flow_policy = ExpressionPolicy.objects.create( name=generate_client_id(), expression="return True", ) flow = Flow.objects.create( slug=flow_slug, designation=FlowDesignation.AUTHENTICATION, name=generate_client_id(), title=generate_client_id(), ) PolicyBinding.objects.create(policy=flow_policy, target=flow, order=0) user_login = UserLoginStage.objects.create(name=stage_name) fsb = FlowStageBinding.objects.create( target=flow, stage=user_login, order=0 ) PolicyBinding.objects.create(policy=flow_policy, target=fsb, order=0) exporter = FlowExporter(flow) export = exporter.export() export_json = dumps(export, cls=DataclassEncoder) importer = FlowImporter(export_json) self.assertTrue(importer.validate()) self.assertTrue(importer.apply()) self.assertTrue(UserLoginStage.objects.filter(name=stage_name).exists()) self.assertTrue(Flow.objects.filter(slug=flow_slug).exists())
def test_export_validate_import(self): """Test export and validate it""" flow_slug = generate_client_id() with transaction_rollback(): login_stage = UserLoginStage.objects.create(name=generate_client_id()) flow = Flow.objects.create( slug=flow_slug, designation=FlowDesignation.AUTHENTICATION, name=generate_client_id(), title=generate_client_id(), ) FlowStageBinding.objects.update_or_create( target=flow, stage=login_stage, order=0, ) exporter = FlowExporter(flow) export = exporter.export() self.assertEqual(len(export.entries), 3) export_json = exporter.export_to_string() importer = FlowImporter(export_json) self.assertTrue(importer.validate()) self.assertTrue(importer.apply()) self.assertTrue(Flow.objects.filter(slug=flow_slug).exists())
def handle(self, *args, **options): """Apply all flows in order, abort when one fails to import""" for flow_path in options.get("flows", []): with open(flow_path, "r", encoding="utf8") as flow_file: importer = FlowImporter(flow_file.read()) valid = importer.validate() if not valid: raise ValueError("Flow invalid") importer.apply()
def import_flow(self, request: Request) -> Response: """Import flow from .akflow file""" file = request.FILES.get("file", None) if not file: return HttpResponseBadRequest() importer = FlowImporter(file.read().decode()) valid = importer.validate() if not valid: return HttpResponseBadRequest() successful = importer.apply() if not successful: return HttpResponseBadRequest() return Response(status=204)
def test_export_validate_import_prompt(self): """Test export and validate it""" with transaction_rollback(): # 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, ) # Stages first_stage = PromptStage.objects.create(name=generate_id()) first_stage.fields.set( [username_prompt, password, password_repeat]) first_stage.save() flow = Flow.objects.create( name=generate_id(), slug=generate_id(), designation=FlowDesignation.ENROLLMENT, title=generate_id(), ) FlowStageBinding.objects.create(target=flow, stage=first_stage, order=0) exporter = FlowExporter(flow) export = exporter.export() export_json = dumps(export, cls=DataclassEncoder) importer = FlowImporter(export_json) self.assertTrue(importer.validate()) self.assertTrue(importer.apply())
def tester(self: TestTransferDocs): with open(file_name, "r", encoding="utf8") as flow_json: importer = FlowImporter(flow_json.read()) self.assertTrue(importer.validate()) self.assertTrue(importer.apply())