factories.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. """FactoryBoy factories for all models."""
  2. from datetime import datetime, timezone
  3. from uuid import uuid4
  4. import factory
  5. from factory.alchemy import SQLAlchemyModelFactory
  6. from flowsint_core.core.enums import EventLevel
  7. from flowsint_core.core.models import (
  8. Analysis,
  9. Chat,
  10. ChatMessage,
  11. CustomType,
  12. EnricherTemplate,
  13. Flow,
  14. Investigation,
  15. InvestigationUserRole,
  16. Key,
  17. Log,
  18. Profile,
  19. Scan,
  20. Sketch,
  21. )
  22. from flowsint_core.core.types import Role
  23. class ProfileFactory(SQLAlchemyModelFactory):
  24. class Meta:
  25. model = Profile
  26. sqlalchemy_session_persistence = "commit"
  27. id = factory.LazyFunction(uuid4)
  28. email = factory.Sequence(lambda n: f"user{n}@test.com")
  29. hashed_password = factory.LazyFunction(lambda: "hashed_pw")
  30. is_active = True
  31. class InvestigationFactory(SQLAlchemyModelFactory):
  32. class Meta:
  33. model = Investigation
  34. sqlalchemy_session_persistence = "commit"
  35. id = factory.LazyFunction(uuid4)
  36. name = factory.Sequence(lambda n: f"Investigation {n}")
  37. description = "Test description"
  38. owner = factory.SubFactory(ProfileFactory)
  39. owner_id = factory.LazyAttribute(lambda o: o.owner.id)
  40. status = "active"
  41. class InvestigationUserRoleFactory(SQLAlchemyModelFactory):
  42. class Meta:
  43. model = InvestigationUserRole
  44. sqlalchemy_session_persistence = "commit"
  45. id = factory.LazyFunction(uuid4)
  46. user = factory.SubFactory(ProfileFactory)
  47. user_id = factory.LazyAttribute(lambda o: o.user.id)
  48. investigation = factory.SubFactory(InvestigationFactory)
  49. investigation_id = factory.LazyAttribute(lambda o: o.investigation.id)
  50. roles = factory.LazyFunction(lambda: [Role.OWNER])
  51. class SketchFactory(SQLAlchemyModelFactory):
  52. class Meta:
  53. model = Sketch
  54. sqlalchemy_session_persistence = "commit"
  55. id = factory.LazyFunction(uuid4)
  56. title = factory.Sequence(lambda n: f"Sketch {n}")
  57. description = "Test sketch"
  58. owner_id = factory.LazyAttribute(lambda o: o.investigation.owner_id)
  59. investigation = factory.SubFactory(InvestigationFactory)
  60. investigation_id = factory.LazyAttribute(lambda o: o.investigation.id)
  61. status = "active"
  62. class AnalysisFactory(SQLAlchemyModelFactory):
  63. class Meta:
  64. model = Analysis
  65. sqlalchemy_session_persistence = "commit"
  66. id = factory.LazyFunction(uuid4)
  67. title = factory.Sequence(lambda n: f"Analysis {n}")
  68. description = "Test analysis"
  69. content = factory.LazyFunction(lambda: {"data": "test"})
  70. owner_id = factory.LazyAttribute(lambda o: o.investigation.owner_id)
  71. investigation = factory.SubFactory(InvestigationFactory)
  72. investigation_id = factory.LazyAttribute(lambda o: o.investigation.id)
  73. class ChatFactory(SQLAlchemyModelFactory):
  74. class Meta:
  75. model = Chat
  76. sqlalchemy_session_persistence = "commit"
  77. exclude = ["owner"]
  78. id = factory.LazyFunction(uuid4)
  79. title = factory.Sequence(lambda n: f"Chat {n}")
  80. description = "Test chat"
  81. owner = factory.SubFactory(ProfileFactory)
  82. owner_id = factory.LazyAttribute(lambda o: o.owner.id)
  83. investigation = factory.SubFactory(InvestigationFactory)
  84. investigation_id = factory.LazyAttribute(lambda o: o.investigation.id)
  85. class ChatMessageFactory(SQLAlchemyModelFactory):
  86. class Meta:
  87. model = ChatMessage
  88. sqlalchemy_session_persistence = "commit"
  89. id = factory.LazyFunction(uuid4)
  90. content = factory.LazyFunction(lambda: "Test message")
  91. context = None
  92. is_bot = False
  93. chat = factory.SubFactory(ChatFactory)
  94. chat_id = factory.LazyAttribute(lambda o: o.chat.id)
  95. created_at = factory.LazyFunction(lambda: datetime.now(timezone.utc))
  96. class ScanFactory(SQLAlchemyModelFactory):
  97. class Meta:
  98. model = Scan
  99. sqlalchemy_session_persistence = "commit"
  100. id = factory.LazyFunction(uuid4)
  101. sketch = factory.SubFactory(SketchFactory)
  102. sketch_id = factory.LazyAttribute(lambda o: o.sketch.id)
  103. status = EventLevel.PENDING
  104. started_at = factory.LazyFunction(datetime.utcnow)
  105. class LogFactory(SQLAlchemyModelFactory):
  106. class Meta:
  107. model = Log
  108. sqlalchemy_session_persistence = "commit"
  109. id = factory.LazyFunction(uuid4)
  110. content = factory.LazyFunction(lambda: {"message": "test log"})
  111. sketch_id = None
  112. type = EventLevel.INFO
  113. created_at = factory.LazyFunction(lambda: datetime.now(timezone.utc))
  114. class KeyFactory(SQLAlchemyModelFactory):
  115. class Meta:
  116. model = Key
  117. sqlalchemy_session_persistence = "commit"
  118. exclude = ["owner"]
  119. id = factory.LazyFunction(uuid4)
  120. name = factory.Sequence(lambda n: f"key_{n}")
  121. owner = factory.SubFactory(ProfileFactory)
  122. owner_id = factory.LazyAttribute(lambda o: o.owner.id)
  123. ciphertext = b"encrypted_data"
  124. iv = b"123456789012"
  125. salt = b"1234567890123456"
  126. key_version = "V1"
  127. class FlowFactory(SQLAlchemyModelFactory):
  128. class Meta:
  129. model = Flow
  130. sqlalchemy_session_persistence = "commit"
  131. id = factory.LazyFunction(uuid4)
  132. name = factory.Sequence(lambda n: f"Flow {n}")
  133. description = "Test flow"
  134. category = factory.LazyFunction(lambda: ["test"])
  135. flow_schema = factory.LazyFunction(lambda: {"nodes": [], "edges": []})
  136. class CustomTypeFactory(SQLAlchemyModelFactory):
  137. class Meta:
  138. model = CustomType
  139. sqlalchemy_session_persistence = "commit"
  140. id = factory.LazyFunction(uuid4)
  141. name = factory.Sequence(lambda n: f"CustomType{n}")
  142. owner = factory.SubFactory(ProfileFactory)
  143. owner_id = factory.LazyAttribute(lambda o: o.owner.id)
  144. schema = factory.LazyFunction(
  145. lambda: {"type": "object", "properties": {"value": {"type": "string"}}}
  146. )
  147. status = "draft"
  148. description = "Test custom type"
  149. class EnricherTemplateFactory(SQLAlchemyModelFactory):
  150. class Meta:
  151. model = EnricherTemplate
  152. sqlalchemy_session_persistence = "commit"
  153. id = factory.LazyFunction(uuid4)
  154. name = factory.Sequence(lambda n: f"Template{n}")
  155. description = "Test template"
  156. category = "ip"
  157. version = 1.0
  158. content = factory.LazyFunction(
  159. lambda: {"name": "Template", "request": {"url": "https://example.com"}}
  160. )
  161. is_public = False
  162. owner = factory.SubFactory(ProfileFactory)
  163. owner_id = factory.LazyAttribute(lambda o: o.owner.id)