user_spec.rb 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Selector::Sql, 'user' do
  4. describe 'user.role_ids' do
  5. let(:user_1) { create(:agent) }
  6. let(:user_2) { create(:admin) }
  7. let(:user_3) { create(:customer) }
  8. let(:role_1) { create(:role, name: 'Unused') }
  9. before do
  10. role_1
  11. user_1 && user_2 && user_3
  12. end
  13. it 'does find agents and admins', :aggregate_failures do
  14. condition = {
  15. operator: 'AND',
  16. conditions: [
  17. {
  18. name: 'user.id',
  19. operator: 'is',
  20. value: [user_1.id.to_s, user_2.id.to_s, user_3.id.to_s],
  21. },
  22. {
  23. name: 'user.role_ids',
  24. operator: 'is',
  25. value: Role.where(name: %w[Agent Admin]).pluck(:id).map(&:to_s),
  26. },
  27. ]
  28. }
  29. count, = User.selectors(condition)
  30. expect(count).to eq(2)
  31. end
  32. it 'does find non agents', :aggregate_failures do
  33. condition = {
  34. operator: 'AND',
  35. conditions: [
  36. {
  37. name: 'user.id',
  38. operator: 'is',
  39. value: [user_1.id.to_s, user_2.id.to_s, user_3.id.to_s],
  40. },
  41. {
  42. name: 'user.role_ids',
  43. operator: 'is not',
  44. value: Role.where(name: %w[Agent Admin]).pluck(:id).map(&:to_s),
  45. },
  46. ]
  47. }
  48. count, = User.selectors(condition)
  49. expect(count).to eq(1)
  50. end
  51. end
  52. describe 'ticket_customer.tickets_last_contact_at' do
  53. let(:user_1) { create(:user) }
  54. let(:ticket_1) { create(:ticket, last_contact_at: Time.zone.now) }
  55. before do
  56. Ticket.destroy_all
  57. user_1
  58. ticket_1
  59. end
  60. it 'does find users by last contact', :aggregate_failures do
  61. condition = {
  62. operator: 'OR',
  63. conditions: [
  64. {
  65. name: 'ticket_customer.last_contact_at',
  66. operator: 'today',
  67. },
  68. ]
  69. }
  70. count, = User.selectors(condition)
  71. expect(count).to eq(1)
  72. end
  73. end
  74. describe 'ticket_customer.last_contact_agent_at' do
  75. let(:user_1) { create(:user) }
  76. let(:ticket_1) { create(:ticket, last_contact_agent_at: Time.zone.now) }
  77. before do
  78. Ticket.destroy_all
  79. user_1
  80. ticket_1
  81. end
  82. it 'does find users by last contact', :aggregate_failures do
  83. condition = {
  84. operator: 'OR',
  85. conditions: [
  86. {
  87. name: 'ticket_customer.last_contact_agent_at',
  88. operator: 'today',
  89. },
  90. ]
  91. }
  92. count, = User.selectors(condition)
  93. expect(count).to eq(1)
  94. end
  95. end
  96. describe 'ticket_customer.last_contact_customer_at' do
  97. let(:user_1) { create(:user) }
  98. let(:ticket_1) { create(:ticket, last_contact_customer_at: Time.zone.now) }
  99. before do
  100. Ticket.destroy_all
  101. user_1
  102. ticket_1
  103. end
  104. it 'does find users by last contact', :aggregate_failures do
  105. condition = {
  106. operator: 'OR',
  107. conditions: [
  108. {
  109. name: 'ticket_customer.last_contact_customer_at',
  110. operator: 'today',
  111. },
  112. ]
  113. }
  114. count, = User.selectors(condition)
  115. expect(count).to eq(1)
  116. end
  117. end
  118. describe 'ticket_customer.updated_at' do
  119. let(:user_1) { create(:user) }
  120. let(:ticket_1) { create(:ticket, updated_at: Time.zone.now) }
  121. before do
  122. Ticket.destroy_all
  123. user_1
  124. ticket_1
  125. end
  126. it 'does find users by last contact', :aggregate_failures do
  127. condition = {
  128. operator: 'OR',
  129. conditions: [
  130. {
  131. name: 'ticket_customer.updated_at',
  132. operator: 'today',
  133. },
  134. ]
  135. }
  136. count, = User.selectors(condition)
  137. expect(count).to eq(1)
  138. end
  139. end
  140. describe 'ticket_customer.existing' do
  141. let(:user_1) { create(:customer) }
  142. let(:user_2) { create(:customer) }
  143. let(:user_3) { create(:customer) }
  144. let(:ticket_1) { create(:ticket, customer: user_1) }
  145. before do
  146. user_1 && user_2 && user_3
  147. ticket_1
  148. end
  149. it 'does find users by customer tickets existing', :aggregate_failures do
  150. condition = {
  151. operator: 'AND',
  152. conditions: [
  153. {
  154. name: 'user.id',
  155. operator: 'is',
  156. value: [user_1.id.to_s, user_2.id.to_s, user_3.id.to_s],
  157. },
  158. {
  159. name: 'ticket_customer.existing',
  160. operator: 'is',
  161. value: 'true',
  162. },
  163. ]
  164. }
  165. count, = User.selectors(condition)
  166. expect(count).to eq(1)
  167. end
  168. it 'does find users by without customer tickets existing', :aggregate_failures do
  169. condition = {
  170. operator: 'AND',
  171. conditions: [
  172. {
  173. name: 'user.id',
  174. operator: 'is',
  175. value: [user_1.id.to_s, user_2.id.to_s, user_3.id.to_s],
  176. },
  177. {
  178. name: 'ticket_customer.existing',
  179. operator: 'is',
  180. value: 'false',
  181. },
  182. ]
  183. }
  184. count, = User.selectors(condition)
  185. expect(count).to eq(2)
  186. end
  187. it 'does not find user 1', :aggregate_failures do
  188. condition = {
  189. operator: 'AND',
  190. conditions: [
  191. {
  192. name: 'user.id',
  193. operator: 'is',
  194. value: '1',
  195. },
  196. {
  197. name: 'ticket_customer.existing',
  198. operator: 'is',
  199. value: 'false',
  200. },
  201. ]
  202. }
  203. count, = User.selectors(condition)
  204. expect(count).to eq(0)
  205. end
  206. end
  207. describe 'ticket_customer.open_existing' do
  208. let(:user_1) { create(:customer) }
  209. let(:user_2) { create(:customer) }
  210. let(:user_3) { create(:customer) }
  211. let(:ticket_1) { create(:ticket, customer: user_1) }
  212. let(:ticket_2) { create(:ticket, customer: user_2, state: Ticket::State.find_by(name: 'closed')) }
  213. before do
  214. user_1 && user_2 && user_3
  215. ticket_1 && ticket_2
  216. end
  217. it 'does find users by customer tickets existing', :aggregate_failures do
  218. condition = {
  219. operator: 'AND',
  220. conditions: [
  221. {
  222. name: 'user.id',
  223. operator: 'is',
  224. value: [user_1.id.to_s, user_2.id.to_s, user_3.id.to_s],
  225. },
  226. {
  227. name: 'ticket_customer.open_existing',
  228. operator: 'is',
  229. value: 'true',
  230. },
  231. ]
  232. }
  233. count, = User.selectors(condition)
  234. expect(count).to eq(1)
  235. end
  236. it 'does find users by without customer tickets existing', :aggregate_failures do
  237. condition = {
  238. operator: 'AND',
  239. conditions: [
  240. {
  241. name: 'user.id',
  242. operator: 'is',
  243. value: [user_1.id.to_s, user_2.id.to_s, user_3.id.to_s],
  244. },
  245. {
  246. name: 'ticket_customer.open_existing',
  247. operator: 'is',
  248. value: 'false',
  249. },
  250. ]
  251. }
  252. count, = User.selectors(condition)
  253. expect(count).to eq(2)
  254. end
  255. end
  256. describe 'ticket_owner.existing' do
  257. let(:user_1) { create(:agent) }
  258. let(:user_2) { create(:agent) }
  259. let(:user_3) { create(:agent) }
  260. let(:ticket_1) { create(:ticket, owner: user_1) }
  261. before do
  262. user_1 && user_2 && user_3
  263. ticket_1
  264. end
  265. it 'does find users by owner tickets existing', :aggregate_failures do
  266. condition = {
  267. operator: 'AND',
  268. conditions: [
  269. {
  270. name: 'user.id',
  271. operator: 'is',
  272. value: [user_1.id.to_s, user_2.id.to_s, user_3.id.to_s],
  273. },
  274. {
  275. name: 'ticket_owner.existing',
  276. operator: 'is',
  277. value: 'true',
  278. },
  279. ]
  280. }
  281. count, = User.selectors(condition)
  282. expect(count).to eq(1)
  283. end
  284. it 'does find users by without owner tickets existing', :aggregate_failures do
  285. condition = {
  286. operator: 'AND',
  287. conditions: [
  288. {
  289. name: 'user.id',
  290. operator: 'is',
  291. value: [user_1.id.to_s, user_2.id.to_s, user_3.id.to_s],
  292. },
  293. {
  294. name: 'ticket_owner.existing',
  295. operator: 'is',
  296. value: 'false',
  297. },
  298. ]
  299. }
  300. count, = User.selectors(condition)
  301. expect(count).to eq(2)
  302. end
  303. end
  304. describe 'ticket_owner.open_existing' do
  305. let(:user_1) { create(:agent) }
  306. let(:user_2) { create(:agent) }
  307. let(:user_3) { create(:agent) }
  308. let(:ticket_1) { create(:ticket, owner: user_1) }
  309. let(:ticket_2) { create(:ticket, owner: user_2, state: Ticket::State.find_by(name: 'closed')) }
  310. before do
  311. user_1 && user_2 && user_3
  312. ticket_1 && ticket_2
  313. end
  314. it 'does find users by owner tickets existing', :aggregate_failures do
  315. condition = {
  316. operator: 'AND',
  317. conditions: [
  318. {
  319. name: 'user.id',
  320. operator: 'is',
  321. value: [user_1.id.to_s, user_2.id.to_s, user_3.id.to_s],
  322. },
  323. {
  324. name: 'ticket_owner.open_existing',
  325. operator: 'is',
  326. value: 'true',
  327. },
  328. ]
  329. }
  330. count, = User.selectors(condition)
  331. expect(count).to eq(1)
  332. end
  333. it 'does find users by without owner tickets existing', :aggregate_failures do
  334. condition = {
  335. operator: 'AND',
  336. conditions: [
  337. {
  338. name: 'user.id',
  339. operator: 'is',
  340. value: [user_1.id.to_s, user_2.id.to_s, user_3.id.to_s],
  341. },
  342. {
  343. name: 'ticket_owner.open_existing',
  344. operator: 'is',
  345. value: 'false',
  346. },
  347. ]
  348. }
  349. count, = User.selectors(condition)
  350. expect(count).to eq(2)
  351. end
  352. end
  353. end