NameL3ss

let_it_be person

Jul 26th, 2023
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.14 KB | None | 0 0
  1. require "rails_helper"
  2.  
  3. RSpec.describe Organization::Api::V1::PersonsController, type: :request do
  4. let_it_be(:customer, reload: true) { create(:customer) }
  5. let_it_be(:privilege, reload: true) { create(:privilege, customer: customer) }
  6. let_it_be(:user, reload: true) { create(:user, customer: customer) }
  7. let_it_be(:customer_two, reload: true) { create(:customer) }
  8. let_it_be(:privilege_two, reload: true) { create(:privilege, access_level: 0, customer: customer_two) }
  9. let_it_be(:user_two, reload: true) { create(:user, customer: customer_two) }
  10. let_it_be(:company, reload: true) { create(:company, customer: customer, user: user) }
  11. let_it_be(:company_two, reload: true) { create(:company, customer: customer, user: user) }
  12. let_it_be(:person, reload: true) { create(:person, customer: customer, user: user) }
  13. let_it_be(:person_two, reload: true) { create(:person, customer: customer, user: user) }
  14. let_it_be(:person_three, reload: true) { create(:person, customer: customer, user: user) }
  15. let_it_be(:person_four, reload: true) { create(:person, customer: customer, user: user_two) }
  16. let_it_be(:entity_positions_lrep, reload: true) do
  17. create(:entity_position,
  18. entity: company,
  19. person: person,
  20. position: company.positions.find_by_name("legal_representative"))
  21. end
  22. let_it_be(:company_person, reload: true) { create(:company_person, company: company, person: person) }
  23. let_it_be(:entity_positions_contact, reload: true) do
  24. create(:entity_position,
  25. entity: company_two,
  26. person: person,
  27. position: company_two.positions.find_by_name("contact"))
  28. end
  29. let_it_be(:company_person_two, reload: true) { create(:company_person, company: company_two, person: person) }
  30. let_it_be(:company_person_three, reload: true) { create(:company_person, company: company_two, person: person_two) }
  31.  
  32. describe "GET /index" do
  33. context "unauthorized" do
  34. it "should redirect if privilege doesnt exists" do
  35. privilege.destroy
  36. customer.reload
  37. login_portal(user)
  38. get organization_api_v1_persons_path, as: :json
  39. expect(response.body.include?("redirected")).to eq true
  40. expect(response).to have_http_status(:unauthorized)
  41. end
  42.  
  43. it "should return forbidden" do
  44. login_portal(user_two)
  45. get organization_api_v1_persons_path, as: :json
  46. expect(response).to have_http_status(:forbidden)
  47. expect(JSON.parse(response.body)["errors"].first["status"]).to eq 403
  48. end
  49. end
  50.  
  51. context "data and response" do
  52. before do
  53. login_portal(user)
  54. get organization_api_v1_persons_path, as: :json
  55. end
  56.  
  57. it "should be ok" do
  58. expect(response).to have_http_status(:ok)
  59. end
  60.  
  61. it "returns with data" do
  62. person_total_companies = JSON.parse(response.body)["data"].map{|el| [el["id"], el.dig("relationships", "companies", "data").try(:size) || 0] }
  63. person_total_companies.each do |key, value|
  64. expect(customer.persons.find_by_id(key.to_i).companies.distinct.size).to eq value
  65. end
  66. end
  67.  
  68. it "response JSON structure is correct" do
  69. json_response = JSON.parse(response.body).deep_symbolize_keys
  70. expect(json_response).to match_response_schema("organization", "persons")
  71. end
  72. end
  73.  
  74. context "sort attributes" do
  75. it "should return default order by asc first name" do
  76. person.update(first_name: "person01")
  77. person_two.update(first_name: "person02")
  78. person_three.update(first_name: "person03")
  79. person_four.update(first_name: "person04")
  80. login_portal(user)
  81. get organization_api_v1_persons_path, params: { sort_by: "first_name" }, as: :json
  82. expect(first_id_response(response)).to eq person.id
  83. end
  84.  
  85. it "should return order by desc first name" do
  86. person.update(first_name: "person04")
  87. person_two.update(first_name: "person03")
  88. person_three.update(first_name: "person02")
  89. person_four.update(first_name: "person01")
  90. login_portal(user)
  91. get organization_api_v1_persons_path, params: { sort_by: "-first_name" }, as: :json
  92. expect(first_id_response(response)).to eq person.id
  93. end
  94.  
  95. it "should return order by asc last name" do
  96. person.update(last_name: "person01")
  97. person_two.update(last_name: "person02")
  98. person_three.update(last_name: "person03")
  99. person_four.update(last_name: "person04")
  100. login_portal(user)
  101. get organization_api_v1_persons_path, params: { sort_by: "last_name" }, as: :json
  102. expect(first_id_response(response)).to eq person.id
  103. end
  104.  
  105. it "should return order by desc last name" do
  106. person.update(last_name: "person04")
  107. person_two.update(last_name: "person03")
  108. person_three.update(last_name: "person02")
  109. person_four.update(last_name: "person01")
  110. login_portal(user)
  111. get organization_api_v1_persons_path, params: { sort_by: "-last_name" }, as: :json
  112. expect(first_id_response(response)).to eq person.id
  113. end
  114.  
  115. it "should return order by asc email" do
  116. person.update(email: "[email protected]")
  117. person_two.update(email: "[email protected]")
  118. person_three.update(email: "[email protected]")
  119. person_four.update(email: "[email protected]")
  120. login_portal(user)
  121. get organization_api_v1_persons_path, params: { sort_by: "email" }, as: :json
  122. expect(first_id_response(response)).to eq person.id
  123. end
  124.  
  125. it "should return order by desc email" do
  126. person.update(email: "[email protected]")
  127. person_two.update(email: "[email protected]")
  128. person_three.update(email: "[email protected]")
  129. person_four.update(email: "[email protected]")
  130. login_portal(user)
  131. get organization_api_v1_persons_path, params: { sort_by: "-email" }, as: :json
  132. expect(first_id_response(response)).to eq person.id
  133. end
  134.  
  135. it "should return order by desc updated at" do
  136. person.update(updated_at: Time.current + 1.minute)
  137. person_two.update(updated_at: Time.current + 2.minute)
  138. person_three.update(updated_at: Time.current + 3.minute)
  139. person_four.update(updated_at: Time.current + 4.minute)
  140. login_portal(user)
  141. get organization_api_v1_persons_path, params: { sort_by: "updated_at" }, as: :json
  142. expect(first_id_response(response)).to eq person.id
  143. end
  144.  
  145. it "should return order by desc updated at" do
  146. person.update(updated_at: Time.current + 4.minute)
  147. person_two.update(updated_at: Time.current + 3.minute)
  148. person_three.update(updated_at: Time.current + 2.minute)
  149. person_four.update(updated_at: Time.current + 1.minute)
  150. login_portal(user)
  151. get organization_api_v1_persons_path, params: { sort_by: "-updated_at" }, as: :json
  152. expect(first_id_response(response)).to eq person.id
  153. end
  154. end
  155.  
  156. context "search attributes" do
  157. before do
  158. user.update(first_name: "user01", last_name: "lastname01")
  159. user_two.update(first_name: "user02", last_name: "lastname02")
  160. person.update(first_name: "pluton")
  161. person_two.update(first_name: "urano")
  162. person_three.update(first_name: "saturno")
  163. person_four.update(first_name: "venus")
  164. person.update(last_name: "thor")
  165. person_two.update(last_name: "hades")
  166. person_three.update(last_name: "poseidon")
  167. person_four.update(last_name: "ares")
  168. person.update(email: "[email protected]")
  169. person_two.update(email: "[email protected]")
  170. person_three.update(email: "[email protected]")
  171. person_four.update(email: "[email protected]")
  172. end
  173.  
  174. it "should return search by first name records" do
  175. login_portal(user)
  176. get organization_api_v1_persons_path, params: { first_name: { value: "urano" } }, as: :json
  177. expect(total_elements(response)).to eq 1
  178. expect(first_id_response(response)).to eq person_two.id
  179. end
  180.  
  181. it "should return search by last name records" do
  182. login_portal(user)
  183. get organization_api_v1_persons_path, params: { last_name: { value: "poseidon" } }, as: :json
  184. expect(total_elements(response)).to eq 1
  185. expect(first_id_response(response)).to eq person_three.id
  186. end
  187.  
  188. it "should return search by email records" do
  189. login_portal(user)
  190. get organization_api_v1_persons_path, params: { email: { value: "ares" } }, as: :json
  191. expect(total_elements(response)).to eq 1
  192. expect(first_id_response(response)).to eq person_four.id
  193. end
  194.  
  195. it "should return search for multiples attributes" do
  196. login_portal(user)
  197. get organization_api_v1_persons_path, params: { first_name: { value: "pluton" }, email: { value: "webdoxclm.com" } }, as: :json
  198. expect(total_elements(response)).to eq 1
  199. expect(first_id_response(response)).to eq person.id
  200. end
  201. end
  202. end
  203.  
  204. describe "POST /export" do
  205. context "ok" do
  206. it "authorized user" do
  207. person.update(first_name: "pluton")
  208. person_two.update(first_name: "urano")
  209. person_three.update(first_name: "saturno")
  210. person_four.update(first_name: "venus")
  211. person.update(last_name: "thor")
  212. person_two.update(last_name: "hades")
  213. person_three.update(last_name: "poseidon")
  214. person_four.update(last_name: "ares")
  215. person.update(email: "[email protected]")
  216. person_two.update(email: "[email protected]")
  217. person_three.update(email: "[email protected]")
  218. person_four.update(email: "[email protected]")
  219. group = Group.find_or_create_by(name: "Admin", customer: user.customer)
  220. group.push_roles Role.all
  221. user.groups << group
  222. user.groups.reload
  223. login_portal(user)
  224. post export_organization_api_v1_persons_path, params: { user_first_name: { value: "user02" } }, as: :json
  225. expect(response).to have_http_status(:ok)
  226. end
  227. end
  228.  
  229. context "fail" do
  230. it "unauthorized user" do
  231. login_portal(user_two)
  232. post export_organization_api_v1_persons_path, as: :json
  233. expect(response).to have_http_status(:forbidden)
  234. end
  235. end
  236. end
  237.  
  238. describe "POST /create" do
  239. context "when the user is unauthorized" do
  240. it "return forbidden error" do
  241. login_portal(user_two)
  242. post organization_api_v1_persons_path, params: { person: "" }, as: :json
  243. expect(response).to have_http_status(:forbidden)
  244. end
  245. end
  246.  
  247. context "when the user is authorized" do
  248. let_it_be(:group, reload: true) { Group.find_or_create_by(name: "Admin", customer: user.customer) }
  249. let!(:persons_count) { customer.persons.count }
  250. let(:general_params) { { first_name: Faker::Name.first_name, last_name: Faker::Name.last_name, email: Faker::Internet.email} }
  251. before do
  252. group.push_roles Role.all
  253. user.groups << group
  254. user.customer.organization_permission.update(access_level: :public_edit)
  255. end
  256.  
  257. context "BASIC INFORMATION OF CONTACT (required *)" do
  258. context "only with sent first_name, last_name and a valid email" do
  259. it "returns OK and add a person" do
  260. login_portal(user)
  261. post organization_api_v1_persons_path, params: { person: general_params}, as: :json
  262.  
  263. expect(response).to have_http_status(:ok)
  264. expect(customer.persons.count).to eq(persons_count + 1)
  265. end
  266. end
  267.  
  268. context "if not sent a first_name or first_name or a email" do
  269. it "returns first_name error can't be blank" do
  270. login_portal(user)
  271. post organization_api_v1_persons_path, params: {
  272. person: {first_name: "", last_name: Faker::Name.last_name, email: Faker::Internet.email}}, as: :json
  273.  
  274. error = JSON.parse(response.body)["errors"].first
  275. expect(response).to have_http_status(:unprocessable_entity)
  276. expect(error["source"].to_s).to include('first_name')
  277. expect(error["title"]).to eq(I18n.t("errors.messages.blank"))
  278. expect(customer.persons.count).to eq(persons_count)
  279. end
  280.  
  281. it "returns last_name error can't be blank" do
  282. login_portal(user)
  283. post organization_api_v1_persons_path, params: {
  284. person: {first_name: Faker::Name.first_name, last_name: "", email: Faker::Internet.email}}, as: :json
  285.  
  286. error = JSON.parse(response.body)["errors"].first
  287. expect(response).to have_http_status(:unprocessable_entity)
  288. expect(error["source"].to_s).to include('last_name')
  289. expect(error["title"]).to eq(I18n.t("errors.messages.blank"))
  290. expect(customer.persons.count).to eq(persons_count)
  291. end
  292.  
  293. it "returns email error can't be blank" do
  294. login_portal(user)
  295. post organization_api_v1_persons_path, params: {
  296. person: {first_name: Faker::Name.first_name, last_name: Faker::Name.last_name, email: ""}}, as: :json
  297.  
  298. error = JSON.parse(response.body)["errors"].first
  299. expect(response).to have_http_status(:unprocessable_entity)
  300. expect(error["source"].to_s).to include('email')
  301. expect(error["title"]).to eq(I18n.t("errors.messages.blank"))
  302. expect(customer.persons.count).to eq(persons_count)
  303. end
  304. end
  305.  
  306. context "if sent an invalid email" do
  307. it "returns email error is not valid" do
  308. login_portal(user)
  309. post organization_api_v1_persons_path, params: {
  310. person: {first_name: Faker::Name.first_name, last_name: Faker::Name.last_name, email: "test"}}, as: :json
  311.  
  312. error = JSON.parse(response.body)["errors"].first
  313. expect(response).to have_http_status(:unprocessable_entity)
  314. expect(error["source"].to_s).to include('email')
  315. expect(error["title"]).to eq(I18n.t("errors.messages.invalid"))
  316. expect(customer.persons.count).to eq(persons_count)
  317. end
  318. end
  319. end
  320.  
  321. context "INFORMATION OF COMPANIES (NOT required)" do
  322. pending "under construction"
  323. end
  324.  
  325. context "DOCUMENT OF IDENTIFICATIONS (NOT required)" do
  326. context "if sent a general info OK and a identification with country_code, country_name, identification_type and identification_number valid" do
  327. let(:params_with_identifications) {
  328. general_params.merge(identifications_attributes: [
  329. { country_code: "cl", country_name: "chile", identification_type: :rut, identification_number: "1111111111"}])
  330. }
  331.  
  332. it "returns ok and add a person" do
  333. login_portal(user)
  334. post organization_api_v1_persons_path, params: { person: params_with_identifications }, as: :json
  335.  
  336. expect(response).to have_http_status(:ok)
  337. expect(customer.persons.count).to eq(persons_count + 1)
  338. expect(customer.persons.last.identifications.count).to eq(1)
  339. end
  340. end
  341.  
  342. context "if sent 2 identifications" do
  343. let(:params_with_identifications) {
  344. general_params.merge(identifications_attributes: [
  345. { country_code: "cl", country_name: "chile", identification_type: :rut, identification_number: "1111111111"},
  346. { country_code: "cl", country_name: "chile", identification_type: :passport, identification_number: "1111111111"}
  347. ])
  348. }
  349.  
  350. it "returns ok and add a person with 2 identifications" do
  351. login_portal(user)
  352. post organization_api_v1_persons_path, params: { person: params_with_identifications }, as: :json
  353.  
  354. expect(response).to have_http_status(:ok)
  355. expect(customer.persons.count).to eq(persons_count + 1)
  356. expect(customer.persons.last.identifications.count).to eq(2)
  357. end
  358. end
  359.  
  360. context "if sent 3 identifications or more" do
  361. let(:params_with_identifications) {
  362. general_params.merge(identifications_attributes: [
  363. { country_code: "cl", country_name: "chile", identification_type: :rut, identification_number: "1111111111"},
  364. { country_code: "cl", country_name: "chile", identification_type: :passport, identification_number: "1111111111"},
  365. { country_code: "cl", country_name: "chile", identification_type: :passport, identification_number: "2222222222"}
  366. ])
  367. }
  368.  
  369. it "returns identifications error can't be more than 2 identifications to one person" do
  370. login_portal(user)
  371. post organization_api_v1_persons_path, params: { person: params_with_identifications }, as: :json
  372.  
  373. error = JSON.parse(response.body)["errors"].first
  374. expect(response).to have_http_status(:unprocessable_entity)
  375.  
  376. expect(error["source"].to_s).to include('identifications')
  377. expect(error["title"]).to eq(I18n.t("activerecord.errors.models.organization/person.attributes.identifications.too_many_identifications"))
  378. expect(customer.persons.count).to eq(persons_count)
  379. end
  380. end
  381.  
  382. context "if NOT sent a country_code, country_name, identification_type or a identification_number" do
  383. it "returns country_code error can't be blank" do
  384. login_portal(user)
  385. post organization_api_v1_persons_path, params: { person: general_params.merge(
  386. identifications_attributes: [{ country_code: "", country_name: "chile", identification_type: :rut, identification_number: "1111111111"}]
  387. )}, as: :json
  388.  
  389. error = JSON.parse(response.body)["errors"].first
  390. expect(response).to have_http_status(:unprocessable_entity)
  391. expect(error["source"].to_s).to include('country_code')
  392. expect(error["title"]).to eq(I18n.t("errors.messages.blank"))
  393. expect(customer.persons.count).to eq(persons_count)
  394. end
  395.  
  396. it "returns country_name error can't be blank" do
  397. login_portal(user)
  398. post organization_api_v1_persons_path, params: { person: general_params.merge(
  399. identifications_attributes: [{ country_code: "cl", country_name: "", identification_type: :rut, identification_number: "1111111111"}]
  400. )}, as: :json
  401.  
  402. error = JSON.parse(response.body)["errors"].first
  403. expect(response).to have_http_status(:unprocessable_entity)
  404. expect(error["source"].to_s).to include('country_name')
  405. expect(error["title"]).to eq(I18n.t("errors.messages.blank"))
  406. expect(customer.persons.count).to eq(persons_count)
  407. end
  408.  
  409. it "returns identification_type error can't be blank" do
  410. login_portal(user)
  411. post organization_api_v1_persons_path, params: { person: general_params.merge(
  412. identifications_attributes: [{ country_code: "cl", country_name: "chile", identification_type: "", identification_number: "1111111111"}]
  413. )}, as: :json
  414.  
  415. error = JSON.parse(response.body)["errors"].first
  416. expect(response).to have_http_status(:unprocessable_entity)
  417. expect(error["source"].to_s).to include('identification_type')
  418. expect(error["title"]).to eq(I18n.t("errors.messages.blank"))
  419. expect(customer.persons.count).to eq(persons_count)
  420. end
  421.  
  422. it "returns identification_number error can't be blank" do
  423. login_portal(user)
  424. post organization_api_v1_persons_path, params: { person: general_params.merge(
  425. identifications_attributes: [{ country_code: "cl", country_name: "chile", identification_type: :rut, identification_number: ""}]
  426. )}, as: :json
  427.  
  428. error = JSON.parse(response.body)["errors"].first
  429. expect(response).to have_http_status(:unprocessable_entity)
  430. expect(error["source"].to_s).to include('identification_number')
  431. expect(error["title"]).to eq(I18n.t("errors.messages.blank"))
  432. expect(customer.persons.count).to eq(persons_count)
  433. end
  434. end
  435.  
  436. context "if sent a identification_number with length more than 100" do
  437. let(:max_length_of_identification_number) { Organization::Identification::MAX_IDENTIFICATION_NUMBER_LENGTH }
  438. it "returns identification_number error can't be too long" do
  439. login_portal(user)
  440. post organization_api_v1_persons_path, params: { person: general_params.merge(
  441. identifications_attributes: [{ country_code: "cl", country_name: "chile", identification_type: :rut, identification_number: (SecureRandom.hex*4)}]
  442. )}, as: :json
  443.  
  444. error = JSON.parse(response.body)["errors"].first
  445. expect(response).to have_http_status(:unprocessable_entity)
  446. expect(error["source"].to_s).to include('identification_number')
  447. expect(error["title"]).to eq(I18n.t("errors.messages.too_long", count: max_length_of_identification_number))
  448. expect(customer.persons.count).to eq(persons_count)
  449. end
  450. end
  451. end
  452.  
  453. context "CONTACT INFORMATION (NOT required)" do
  454. context "Phone" do
  455. context "if sent a general info OK and a phone number" do
  456. let(:phone) { "+56911111111" }
  457. let(:params_with_phone) { general_params.merge(phone: phone) }
  458.  
  459. it "returns ok and add a person with phone number" do
  460. login_portal(user)
  461. post organization_api_v1_persons_path, params: { person: params_with_phone }, as: :json
  462.  
  463. expect(response).to have_http_status(:ok)
  464. expect(customer.persons.count).to eq(persons_count + 1)
  465. expect("+#{customer.persons.last.phone}").to eq(phone)
  466. end
  467. end
  468.  
  469. context "if sent a phone number empty" do
  470. let(:params_with_phone) { general_params.merge(phone: "") }
  471.  
  472. it "returns ok because it is not required" do
  473. login_portal(user)
  474. post organization_api_v1_persons_path, params: { person: params_with_phone }, as: :json
  475.  
  476. expect(response).to have_http_status(:ok)
  477. expect(customer.persons.count).to eq(persons_count + 1)
  478. expect(customer.persons.last.phone).to eq("")
  479. end
  480. end
  481.  
  482. context "if sent a invalid phone number" do
  483. let(:params_with_phone) { general_params.merge(phone: "081341") }
  484.  
  485. it "returns phone error is a invalid number" do
  486. login_portal(user)
  487. post organization_api_v1_persons_path, params: { person: params_with_phone }, as: :json
  488.  
  489. error = JSON.parse(response.body)["errors"].first
  490. expect(response).to have_http_status(:unprocessable_entity)
  491. expect(error["source"].to_s).to include('phone')
  492. expect(error["title"]).to eq(I18n.t("errors.messages.improbable_phone"))
  493. expect(customer.persons.count).to eq(persons_count)
  494. end
  495. end
  496. end
  497.  
  498. context "Addresses" do
  499. context "if sent a general info OK and a Address info with country_name, country_code, name, district, city region and postal_code valid" do
  500. let(:params_with_addresses) {
  501. general_params.merge(addresses_attributes: [
  502. { country_name: "chile", country_code: "cl", name: "Address 1", district: "District 1", city: "City 1", region: "Region 1", postal_code: 8_320_000, default: false }
  503. ])
  504. }
  505.  
  506. it "returns ok and add a person" do
  507. login_portal(user)
  508. post organization_api_v1_persons_path, params: { person: params_with_addresses }, as: :json
  509.  
  510. expect(response).to have_http_status(:ok)
  511. expect(customer.persons.count).to eq(persons_count + 1)
  512. expect(customer.persons.last.addresses.count).to eq(1)
  513. end
  514. end
  515.  
  516. context "if sent a address info with country_name, country_code, name, district, city region and postal_code empty" do
  517. let(:params_with_addresses) {
  518. general_params.merge(addresses_attributes: [
  519. { country_name: "", country_code: "", name: "", district: "", city: "", region: "", postal_code: "", default: false }
  520. ])
  521. }
  522.  
  523. it "returns error There must be at least one non-empty field because" do
  524. login_portal(user)
  525. post organization_api_v1_persons_path, params: { person: params_with_addresses }, as: :json
  526.  
  527. error = JSON.parse(response.body)["errors"].first
  528. expect(response).to have_http_status(:unprocessable_entity)
  529. expect(error["source"].to_s).to include('addresses')
  530. expect(error["title"]).to eq(I18n.t("activerecord.errors.models.organization/address.attributes.base.at_least_one_field_present"))
  531. expect(customer.persons.count).to eq(persons_count)
  532. end
  533. end
  534.  
  535. context "if sent a general info OK and 2 Address info valid" do
  536. let(:params_with_addresses) {
  537. general_params.merge(addresses_attributes: [
  538. { country_name: "chile", country_code: "cl", name: "Address 1", district: "District 1", city: "City 2", region: "Region 1", postal_code: 8_320_000, default: true },
  539. { name: "Address 2", default: false }
  540. ])
  541. }
  542.  
  543. it "returns ok and add a person with 2 addresses" do
  544. login_portal(user)
  545. post organization_api_v1_persons_path, params: { person: params_with_addresses }, as: :json
  546.  
  547. expect(response).to have_http_status(:ok)
  548. expect(customer.persons.count).to eq(persons_count + 1)
  549. expect(customer.persons.last.addresses.count).to eq(2)
  550. end
  551. end
  552. end
  553. end
  554. end
  555. end
  556. end
  557.  
Add Comment
Please, Sign In to add comment