channel.rb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. FactoryBot.define do
  3. factory :channel do
  4. area { 'Email::Dummy' }
  5. group { Group.find(1) }
  6. active { true }
  7. options { nil }
  8. preferences { nil }
  9. updated_by_id { 1 }
  10. created_by_id { 1 }
  11. factory :email_notification_channel do
  12. area { 'Email::Notification' }
  13. transient do
  14. outbound { {} }
  15. end
  16. options do
  17. {
  18. outbound: outbound,
  19. }
  20. end
  21. trait :sendmail do
  22. outbound do
  23. {
  24. 'adapter' => 'sendmail',
  25. }
  26. end
  27. end
  28. trait :smtp do
  29. transient do
  30. outbound_port { 465 }
  31. end
  32. outbound do
  33. {
  34. 'adapter' => 'smtp',
  35. 'options' => {
  36. 'host' => 'smtp.example.com',
  37. 'port' => outbound_port,
  38. 'ssl' => true,
  39. 'ssl_verify' => true,
  40. 'user' => 'user@example.com',
  41. 'authentication' => 'plain',
  42. }
  43. }
  44. end
  45. end
  46. end
  47. factory :email_channel do
  48. area { 'Email::Account' }
  49. options do
  50. {
  51. inbound: inbound,
  52. outbound: outbound,
  53. }
  54. end
  55. transient do
  56. inbound do
  57. {
  58. 'adapter' => 'imap',
  59. 'options' => {
  60. 'auth_type' => 'plain',
  61. 'host' => 'imap.example.com',
  62. 'ssl' => 'ssl',
  63. 'ssl_verify' => true,
  64. 'user' => mail_server_user,
  65. 'folder' => '',
  66. 'keep_on_server' => false,
  67. }
  68. }
  69. end
  70. outbound do
  71. {
  72. adapter: 'sendmail'
  73. }
  74. end
  75. sequence(:mail_server_user) { |n| "user#{n}@example.com" }
  76. end
  77. trait :sendmail do
  78. outbound do
  79. {
  80. 'adapter' => 'sendmail',
  81. }
  82. end
  83. end
  84. trait :smtp do
  85. transient do
  86. outbound_port { 465 }
  87. end
  88. outbound do
  89. {
  90. 'adapter' => 'smtp',
  91. 'options' => {
  92. 'host' => 'smtp.example.com',
  93. 'port' => outbound_port,
  94. 'ssl' => true,
  95. 'ssl_verify' => true,
  96. 'user' => mail_server_user,
  97. 'authentication' => 'plain',
  98. }
  99. }
  100. end
  101. end
  102. trait :imap do
  103. inbound do
  104. {
  105. 'adapter' => 'imap',
  106. 'options' => {
  107. 'auth_type' => 'plain',
  108. 'host' => 'imap.example.com',
  109. 'ssl' => 'ssl',
  110. 'ssl_verify' => true,
  111. 'user' => mail_server_user,
  112. 'folder' => '',
  113. 'keep_on_server' => false,
  114. }
  115. }
  116. end
  117. end
  118. trait :pop3 do
  119. inbound do
  120. {
  121. 'adapter' => 'pop3',
  122. 'options' => {
  123. 'auth_type' => 'plain',
  124. 'host' => 'pop3.example.com',
  125. 'ssl' => 'ssl',
  126. 'ssl_verify' => true,
  127. 'user' => mail_server_user,
  128. 'folder' => '',
  129. 'keep_on_server' => false,
  130. }
  131. }
  132. end
  133. end
  134. end
  135. factory :twitter_channel do
  136. area { 'Twitter::Account' }
  137. options do
  138. {
  139. adapter: 'twitter',
  140. user: {
  141. id: oauth_token&.split('-')&.first,
  142. screen_name: 'APITesting001',
  143. name: 'Test API Account',
  144. },
  145. auth: {
  146. external_credential_id: external_credential.id,
  147. oauth_token: oauth_token,
  148. oauth_token_secret: oauth_token_secret,
  149. consumer_key: consumer_key,
  150. consumer_secret: consumer_secret,
  151. },
  152. sync: {
  153. webhook_id: '',
  154. mentions: {
  155. group_id: Group.first.id
  156. },
  157. direct_messages: {
  158. group_id: Group.first.id
  159. },
  160. search: [
  161. {
  162. term: search_term,
  163. group_id: Group.first.id
  164. },
  165. ],
  166. },
  167. subscribed_to_webhook_id: external_credential.credentials[:webhook_id],
  168. }.deep_merge(custom_options)
  169. end
  170. transient do
  171. custom_options { {} }
  172. external_credential { association :twitter_credential }
  173. oauth_token { external_credential.credentials[:oauth_token] }
  174. oauth_token_secret { external_credential.credentials[:oauth_token_secret] }
  175. consumer_key { external_credential.credentials[:consumer_key] }
  176. consumer_secret { external_credential.credentials[:consumer_secret] }
  177. search_term { 'zammad' }
  178. end
  179. trait :legacy do
  180. transient do
  181. custom_options { { sync: { import_older_tweets: true } } }
  182. end
  183. end
  184. trait :invalid do
  185. transient do
  186. external_credential { association :twitter_credential, :invalid }
  187. end
  188. end
  189. end
  190. factory :facebook_channel do
  191. area { 'Facebook::Account' }
  192. options do
  193. {
  194. adapter: 'facebook',
  195. user: {
  196. id: ENV['FACEBOOK_ADMIN_USER_ID'],
  197. name: "#{ENV['FACEBOOK_ADMIN_FIRSTNAME']} #{ENV['FACEBOOK_ADMIN_LASTNAME']}",
  198. },
  199. auth: {
  200. access_token: ENV['FACEBOOK_ADMIN_ACCESS_TOKEN'],
  201. },
  202. sync: {
  203. pages: {
  204. ENV['FACEBOOK_PAGE_1_ID'] => {
  205. group_id: Group.first.id,
  206. }
  207. }
  208. },
  209. pages: [
  210. {
  211. id: ENV['FACEBOOK_PAGE_1_ID'],
  212. name: ENV['FACEBOOK_PAGE_1_NAME'],
  213. access_token: ENV['FACEBOOK_PAGE_1_ACCCESS_TOKEN'],
  214. },
  215. {
  216. id: ENV['FACEBOOK_PAGE_2_ID'],
  217. name: ENV['FACEBOOK_PAGE_2_NAME'],
  218. access_token: ENV['FACEBOOK_PAGE_2_ACCCESS_TOKEN'],
  219. }
  220. ],
  221. }
  222. end
  223. end
  224. factory :google_channel do
  225. transient do
  226. gmail_user { ENV['GMAIL_USER'] }
  227. end
  228. area { 'Google::Account' }
  229. options do
  230. {
  231. 'inbound' => {
  232. 'adapter' => 'imap',
  233. 'options' => {
  234. 'auth_type' => 'XOAUTH2',
  235. 'host' => 'imap.gmail.com',
  236. 'ssl' => 'ssl',
  237. 'user' => gmail_user,
  238. 'folder' => '',
  239. 'keep_on_server' => false,
  240. }
  241. },
  242. 'outbound' => {
  243. 'adapter' => 'smtp',
  244. 'options' => {
  245. 'host' => 'smtp.gmail.com',
  246. 'port' => 465,
  247. 'ssl' => true,
  248. 'user' => gmail_user,
  249. 'authentication' => 'xoauth2',
  250. }
  251. },
  252. 'auth' => {
  253. 'type' => 'XOAUTH2',
  254. 'provider' => 'google',
  255. 'access_token' => 'xxx',
  256. 'expires_in' => 3599,
  257. 'refresh_token' => ENV['GMAIL_REFRESH_TOKEN'],
  258. 'scope' => 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://mail.google.com/ openid',
  259. 'token_type' => 'Bearer',
  260. 'id_token' => 'xxx',
  261. 'created_at' => 30.days.ago,
  262. 'client_id' => ENV['GMAIL_CLIENT_ID'],
  263. 'client_secret' => ENV['GMAIL_CLIENT_SECRET'],
  264. }
  265. }
  266. end
  267. end
  268. factory :microsoft365_channel do
  269. area { 'Microsoft365::Account' }
  270. options do
  271. {
  272. 'inbound' => {
  273. 'adapter' => 'imap',
  274. 'options' => {
  275. 'auth_type' => 'XOAUTH2',
  276. 'host' => 'outlook.office365.com',
  277. 'ssl' => 'ssl',
  278. 'user' => ENV['MICROSOFT365_USER'],
  279. 'folder' => '',
  280. 'keep_on_server' => false,
  281. }
  282. },
  283. 'outbound' => {
  284. 'adapter' => 'smtp',
  285. 'options' => {
  286. 'host' => 'smtp.office365.com',
  287. 'port' => 587,
  288. 'user' => ENV['MICROSOFT365_USER'],
  289. 'authentication' => 'xoauth2',
  290. }
  291. },
  292. 'auth' => {
  293. 'type' => 'XOAUTH2',
  294. 'provider' => 'microsoft365',
  295. 'access_token' => 'xxx',
  296. 'expires_in' => 3599,
  297. 'refresh_token' => ENV['MICROSOFT365_REFRESH_TOKEN'],
  298. 'scope' => 'https://outlook.office.com/IMAP.AccessAsUser.All https://outlook.office.com/SMTP.Send offline_access openid profile email',
  299. 'token_type' => 'Bearer',
  300. 'id_token' => 'xxx',
  301. 'created_at' => 30.days.ago,
  302. 'client_id' => ENV['MICROSOFT365_CLIENT_ID'],
  303. 'client_secret' => ENV['MICROSOFT365_CLIENT_SECRET'],
  304. 'client_tenant' => ENV['MICROSOFT365_CLIENT_TENANT'],
  305. }
  306. }
  307. end
  308. end
  309. factory :sms_message_bird_channel do
  310. area { 'Sms::Account' }
  311. status_in { 'ok' }
  312. status_out { 'ok' }
  313. options do
  314. {
  315. adapter: 'sms/message_bird',
  316. webhook: "http://localhost:3000/api/v1/sms_webhook/#{webhook_token}",
  317. sender: '+490123456789',
  318. token: external_credential.credentials['token'],
  319. webhook_token: webhook_token,
  320. }.deep_merge(custom_options)
  321. end
  322. transient do
  323. custom_options { {} }
  324. external_credential { association :sms_message_bird_credential }
  325. webhook_token { Faker::Crypto.md5 }
  326. end
  327. end
  328. factory :telegram_channel do
  329. area { 'Telegram::Bot' }
  330. options do
  331. {
  332. bot: {
  333. id: bid,
  334. username: "#{Faker::Internet.username}bot",
  335. first_name: Faker::Name.unique.first_name,
  336. last_name: Faker::Name.unique.last_name,
  337. },
  338. callback_token: callback_token,
  339. callback_url: "http://localhost:3000/api/v1/channels_telegram_webhook/#{callback_token}?bid=#{bid}",
  340. api_token: "#{bid}:#{external_credential.credentials['api_token']}",
  341. welcome: Faker::Lorem.unique.sentence,
  342. goodbye: Faker::Lorem.unique.sentence,
  343. }.deep_merge(custom_options)
  344. end
  345. transient do
  346. custom_options { {} }
  347. external_credential { association :telegram_credential }
  348. bid { Faker::Number.unique.number(digits: 10) }
  349. callback_token { Faker::Alphanumeric.alphanumeric(number: 14) }
  350. end
  351. end
  352. factory :whatsapp_channel do
  353. area { 'WhatsApp::Business' }
  354. options do
  355. {
  356. adapter: 'whatsapp',
  357. business_id:,
  358. access_token:,
  359. app_secret:,
  360. phone_number_id:,
  361. welcome:,
  362. name:,
  363. phone_number:,
  364. reminder_active:,
  365. callback_url_uuid:,
  366. verify_token:,
  367. }
  368. end
  369. transient do
  370. business_id { Faker::Number.unique.number(digits: 15) }
  371. access_token { Faker::Omniauth.unique.facebook[:credentials][:token] }
  372. app_secret { Faker::Crypto.unique.md5 }
  373. phone_number_id { Faker::Number.unique.number(digits: 15) }
  374. welcome { Faker::Lorem.unique.sentence }
  375. name { Faker::Company.name }
  376. phone_number { Faker::PhoneNumber.unique.cell_phone_with_country_code }
  377. reminder_active { true }
  378. callback_url_uuid { SecureRandom.uuid }
  379. verify_token { SecureRandom.urlsafe_base64(12) }
  380. end
  381. end
  382. end
  383. end