Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require "rails_helper"
- RSpec.describe Organization::Api::V1::PersonsController, type: :request do
- let_it_be(:customer, reload: true) { create(:customer) }
- let_it_be(:privilege, reload: true) { create(:privilege, customer: customer) }
- let_it_be(:user, reload: true) { create(:user, customer: customer) }
- let_it_be(:customer_two, reload: true) { create(:customer) }
- let_it_be(:privilege_two, reload: true) { create(:privilege, access_level: 0, customer: customer_two) }
- let_it_be(:user_two, reload: true) { create(:user, customer: customer_two) }
- let_it_be(:company, reload: true) { create(:company, customer: customer, user: user) }
- let_it_be(:company_two, reload: true) { create(:company, customer: customer, user: user) }
- let_it_be(:person, reload: true) { create(:person, customer: customer, user: user) }
- let_it_be(:person_two, reload: true) { create(:person, customer: customer, user: user) }
- let_it_be(:person_three, reload: true) { create(:person, customer: customer, user: user) }
- let_it_be(:person_four, reload: true) { create(:person, customer: customer, user: user_two) }
- let_it_be(:entity_positions_lrep, reload: true) do
- create(:entity_position,
- entity: company,
- person: person,
- position: company.positions.find_by_name("legal_representative"))
- end
- let_it_be(:company_person, reload: true) { create(:company_person, company: company, person: person) }
- let_it_be(:entity_positions_contact, reload: true) do
- create(:entity_position,
- entity: company_two,
- person: person,
- position: company_two.positions.find_by_name("contact"))
- end
- let_it_be(:company_person_two, reload: true) { create(:company_person, company: company_two, person: person) }
- let_it_be(:company_person_three, reload: true) { create(:company_person, company: company_two, person: person_two) }
- describe "GET /index" do
- context "unauthorized" do
- it "should redirect if privilege doesnt exists" do
- privilege.destroy
- customer.reload
- login_portal(user)
- get organization_api_v1_persons_path, as: :json
- expect(response.body.include?("redirected")).to eq true
- expect(response).to have_http_status(:unauthorized)
- end
- it "should return forbidden" do
- login_portal(user_two)
- get organization_api_v1_persons_path, as: :json
- expect(response).to have_http_status(:forbidden)
- expect(JSON.parse(response.body)["errors"].first["status"]).to eq 403
- end
- end
- context "data and response" do
- before do
- login_portal(user)
- get organization_api_v1_persons_path, as: :json
- end
- it "should be ok" do
- expect(response).to have_http_status(:ok)
- end
- it "returns with data" do
- person_total_companies = JSON.parse(response.body)["data"].map{|el| [el["id"], el.dig("relationships", "companies", "data").try(:size) || 0] }
- person_total_companies.each do |key, value|
- expect(customer.persons.find_by_id(key.to_i).companies.distinct.size).to eq value
- end
- end
- it "response JSON structure is correct" do
- json_response = JSON.parse(response.body).deep_symbolize_keys
- expect(json_response).to match_response_schema("organization", "persons")
- end
- end
- context "sort attributes" do
- it "should return default order by asc first name" do
- person.update(first_name: "person01")
- person_two.update(first_name: "person02")
- person_three.update(first_name: "person03")
- person_four.update(first_name: "person04")
- login_portal(user)
- get organization_api_v1_persons_path, params: { sort_by: "first_name" }, as: :json
- expect(first_id_response(response)).to eq person.id
- end
- it "should return order by desc first name" do
- person.update(first_name: "person04")
- person_two.update(first_name: "person03")
- person_three.update(first_name: "person02")
- person_four.update(first_name: "person01")
- login_portal(user)
- get organization_api_v1_persons_path, params: { sort_by: "-first_name" }, as: :json
- expect(first_id_response(response)).to eq person.id
- end
- it "should return order by asc last name" do
- person.update(last_name: "person01")
- person_two.update(last_name: "person02")
- person_three.update(last_name: "person03")
- person_four.update(last_name: "person04")
- login_portal(user)
- get organization_api_v1_persons_path, params: { sort_by: "last_name" }, as: :json
- expect(first_id_response(response)).to eq person.id
- end
- it "should return order by desc last name" do
- person.update(last_name: "person04")
- person_two.update(last_name: "person03")
- person_three.update(last_name: "person02")
- person_four.update(last_name: "person01")
- login_portal(user)
- get organization_api_v1_persons_path, params: { sort_by: "-last_name" }, as: :json
- expect(first_id_response(response)).to eq person.id
- end
- it "should return order by asc email" do
- person.update(email: "[email protected]")
- person_two.update(email: "[email protected]")
- person_three.update(email: "[email protected]")
- person_four.update(email: "[email protected]")
- login_portal(user)
- get organization_api_v1_persons_path, params: { sort_by: "email" }, as: :json
- expect(first_id_response(response)).to eq person.id
- end
- it "should return order by desc email" do
- person.update(email: "[email protected]")
- person_two.update(email: "[email protected]")
- person_three.update(email: "[email protected]")
- person_four.update(email: "[email protected]")
- login_portal(user)
- get organization_api_v1_persons_path, params: { sort_by: "-email" }, as: :json
- expect(first_id_response(response)).to eq person.id
- end
- it "should return order by desc updated at" do
- person.update(updated_at: Time.current + 1.minute)
- person_two.update(updated_at: Time.current + 2.minute)
- person_three.update(updated_at: Time.current + 3.minute)
- person_four.update(updated_at: Time.current + 4.minute)
- login_portal(user)
- get organization_api_v1_persons_path, params: { sort_by: "updated_at" }, as: :json
- expect(first_id_response(response)).to eq person.id
- end
- it "should return order by desc updated at" do
- person.update(updated_at: Time.current + 4.minute)
- person_two.update(updated_at: Time.current + 3.minute)
- person_three.update(updated_at: Time.current + 2.minute)
- person_four.update(updated_at: Time.current + 1.minute)
- login_portal(user)
- get organization_api_v1_persons_path, params: { sort_by: "-updated_at" }, as: :json
- expect(first_id_response(response)).to eq person.id
- end
- end
- context "search attributes" do
- before do
- user.update(first_name: "user01", last_name: "lastname01")
- user_two.update(first_name: "user02", last_name: "lastname02")
- person.update(first_name: "pluton")
- person_two.update(first_name: "urano")
- person_three.update(first_name: "saturno")
- person_four.update(first_name: "venus")
- person.update(last_name: "thor")
- person_two.update(last_name: "hades")
- person_three.update(last_name: "poseidon")
- person_four.update(last_name: "ares")
- person.update(email: "[email protected]")
- person_two.update(email: "[email protected]")
- person_three.update(email: "[email protected]")
- person_four.update(email: "[email protected]")
- end
- it "should return search by first name records" do
- login_portal(user)
- get organization_api_v1_persons_path, params: { first_name: { value: "urano" } }, as: :json
- expect(total_elements(response)).to eq 1
- expect(first_id_response(response)).to eq person_two.id
- end
- it "should return search by last name records" do
- login_portal(user)
- get organization_api_v1_persons_path, params: { last_name: { value: "poseidon" } }, as: :json
- expect(total_elements(response)).to eq 1
- expect(first_id_response(response)).to eq person_three.id
- end
- it "should return search by email records" do
- login_portal(user)
- get organization_api_v1_persons_path, params: { email: { value: "ares" } }, as: :json
- expect(total_elements(response)).to eq 1
- expect(first_id_response(response)).to eq person_four.id
- end
- it "should return search for multiples attributes" do
- login_portal(user)
- get organization_api_v1_persons_path, params: { first_name: { value: "pluton" }, email: { value: "webdoxclm.com" } }, as: :json
- expect(total_elements(response)).to eq 1
- expect(first_id_response(response)).to eq person.id
- end
- end
- end
- describe "POST /export" do
- context "ok" do
- it "authorized user" do
- person.update(first_name: "pluton")
- person_two.update(first_name: "urano")
- person_three.update(first_name: "saturno")
- person_four.update(first_name: "venus")
- person.update(last_name: "thor")
- person_two.update(last_name: "hades")
- person_three.update(last_name: "poseidon")
- person_four.update(last_name: "ares")
- person.update(email: "[email protected]")
- person_two.update(email: "[email protected]")
- person_three.update(email: "[email protected]")
- person_four.update(email: "[email protected]")
- group = Group.find_or_create_by(name: "Admin", customer: user.customer)
- group.push_roles Role.all
- user.groups << group
- user.groups.reload
- login_portal(user)
- post export_organization_api_v1_persons_path, params: { user_first_name: { value: "user02" } }, as: :json
- expect(response).to have_http_status(:ok)
- end
- end
- context "fail" do
- it "unauthorized user" do
- login_portal(user_two)
- post export_organization_api_v1_persons_path, as: :json
- expect(response).to have_http_status(:forbidden)
- end
- end
- end
- describe "POST /create" do
- context "when the user is unauthorized" do
- it "return forbidden error" do
- login_portal(user_two)
- post organization_api_v1_persons_path, params: { person: "" }, as: :json
- expect(response).to have_http_status(:forbidden)
- end
- end
- context "when the user is authorized" do
- let_it_be(:group, reload: true) { Group.find_or_create_by(name: "Admin", customer: user.customer) }
- let!(:persons_count) { customer.persons.count }
- let(:general_params) { { first_name: Faker::Name.first_name, last_name: Faker::Name.last_name, email: Faker::Internet.email} }
- before do
- group.push_roles Role.all
- user.groups << group
- user.customer.organization_permission.update(access_level: :public_edit)
- end
- context "BASIC INFORMATION OF CONTACT (required *)" do
- context "only with sent first_name, last_name and a valid email" do
- it "returns OK and add a person" do
- login_portal(user)
- post organization_api_v1_persons_path, params: { person: general_params}, as: :json
- expect(response).to have_http_status(:ok)
- expect(customer.persons.count).to eq(persons_count + 1)
- end
- end
- context "if not sent a first_name or first_name or a email" do
- it "returns first_name error can't be blank" do
- login_portal(user)
- post organization_api_v1_persons_path, params: {
- person: {first_name: "", last_name: Faker::Name.last_name, email: Faker::Internet.email}}, as: :json
- error = JSON.parse(response.body)["errors"].first
- expect(response).to have_http_status(:unprocessable_entity)
- expect(error["source"].to_s).to include('first_name')
- expect(error["title"]).to eq(I18n.t("errors.messages.blank"))
- expect(customer.persons.count).to eq(persons_count)
- end
- it "returns last_name error can't be blank" do
- login_portal(user)
- post organization_api_v1_persons_path, params: {
- person: {first_name: Faker::Name.first_name, last_name: "", email: Faker::Internet.email}}, as: :json
- error = JSON.parse(response.body)["errors"].first
- expect(response).to have_http_status(:unprocessable_entity)
- expect(error["source"].to_s).to include('last_name')
- expect(error["title"]).to eq(I18n.t("errors.messages.blank"))
- expect(customer.persons.count).to eq(persons_count)
- end
- it "returns email error can't be blank" do
- login_portal(user)
- post organization_api_v1_persons_path, params: {
- person: {first_name: Faker::Name.first_name, last_name: Faker::Name.last_name, email: ""}}, as: :json
- error = JSON.parse(response.body)["errors"].first
- expect(response).to have_http_status(:unprocessable_entity)
- expect(error["source"].to_s).to include('email')
- expect(error["title"]).to eq(I18n.t("errors.messages.blank"))
- expect(customer.persons.count).to eq(persons_count)
- end
- end
- context "if sent an invalid email" do
- it "returns email error is not valid" do
- login_portal(user)
- post organization_api_v1_persons_path, params: {
- person: {first_name: Faker::Name.first_name, last_name: Faker::Name.last_name, email: "test"}}, as: :json
- error = JSON.parse(response.body)["errors"].first
- expect(response).to have_http_status(:unprocessable_entity)
- expect(error["source"].to_s).to include('email')
- expect(error["title"]).to eq(I18n.t("errors.messages.invalid"))
- expect(customer.persons.count).to eq(persons_count)
- end
- end
- end
- context "INFORMATION OF COMPANIES (NOT required)" do
- pending "under construction"
- end
- context "DOCUMENT OF IDENTIFICATIONS (NOT required)" do
- context "if sent a general info OK and a identification with country_code, country_name, identification_type and identification_number valid" do
- let(:params_with_identifications) {
- general_params.merge(identifications_attributes: [
- { country_code: "cl", country_name: "chile", identification_type: :rut, identification_number: "1111111111"}])
- }
- it "returns ok and add a person" do
- login_portal(user)
- post organization_api_v1_persons_path, params: { person: params_with_identifications }, as: :json
- expect(response).to have_http_status(:ok)
- expect(customer.persons.count).to eq(persons_count + 1)
- expect(customer.persons.last.identifications.count).to eq(1)
- end
- end
- context "if sent 2 identifications" do
- let(:params_with_identifications) {
- general_params.merge(identifications_attributes: [
- { country_code: "cl", country_name: "chile", identification_type: :rut, identification_number: "1111111111"},
- { country_code: "cl", country_name: "chile", identification_type: :passport, identification_number: "1111111111"}
- ])
- }
- it "returns ok and add a person with 2 identifications" do
- login_portal(user)
- post organization_api_v1_persons_path, params: { person: params_with_identifications }, as: :json
- expect(response).to have_http_status(:ok)
- expect(customer.persons.count).to eq(persons_count + 1)
- expect(customer.persons.last.identifications.count).to eq(2)
- end
- end
- context "if sent 3 identifications or more" do
- let(:params_with_identifications) {
- general_params.merge(identifications_attributes: [
- { country_code: "cl", country_name: "chile", identification_type: :rut, identification_number: "1111111111"},
- { country_code: "cl", country_name: "chile", identification_type: :passport, identification_number: "1111111111"},
- { country_code: "cl", country_name: "chile", identification_type: :passport, identification_number: "2222222222"}
- ])
- }
- it "returns identifications error can't be more than 2 identifications to one person" do
- login_portal(user)
- post organization_api_v1_persons_path, params: { person: params_with_identifications }, as: :json
- error = JSON.parse(response.body)["errors"].first
- expect(response).to have_http_status(:unprocessable_entity)
- expect(error["source"].to_s).to include('identifications')
- expect(error["title"]).to eq(I18n.t("activerecord.errors.models.organization/person.attributes.identifications.too_many_identifications"))
- expect(customer.persons.count).to eq(persons_count)
- end
- end
- context "if NOT sent a country_code, country_name, identification_type or a identification_number" do
- it "returns country_code error can't be blank" do
- login_portal(user)
- post organization_api_v1_persons_path, params: { person: general_params.merge(
- identifications_attributes: [{ country_code: "", country_name: "chile", identification_type: :rut, identification_number: "1111111111"}]
- )}, as: :json
- error = JSON.parse(response.body)["errors"].first
- expect(response).to have_http_status(:unprocessable_entity)
- expect(error["source"].to_s).to include('country_code')
- expect(error["title"]).to eq(I18n.t("errors.messages.blank"))
- expect(customer.persons.count).to eq(persons_count)
- end
- it "returns country_name error can't be blank" do
- login_portal(user)
- post organization_api_v1_persons_path, params: { person: general_params.merge(
- identifications_attributes: [{ country_code: "cl", country_name: "", identification_type: :rut, identification_number: "1111111111"}]
- )}, as: :json
- error = JSON.parse(response.body)["errors"].first
- expect(response).to have_http_status(:unprocessable_entity)
- expect(error["source"].to_s).to include('country_name')
- expect(error["title"]).to eq(I18n.t("errors.messages.blank"))
- expect(customer.persons.count).to eq(persons_count)
- end
- it "returns identification_type error can't be blank" do
- login_portal(user)
- post organization_api_v1_persons_path, params: { person: general_params.merge(
- identifications_attributes: [{ country_code: "cl", country_name: "chile", identification_type: "", identification_number: "1111111111"}]
- )}, as: :json
- error = JSON.parse(response.body)["errors"].first
- expect(response).to have_http_status(:unprocessable_entity)
- expect(error["source"].to_s).to include('identification_type')
- expect(error["title"]).to eq(I18n.t("errors.messages.blank"))
- expect(customer.persons.count).to eq(persons_count)
- end
- it "returns identification_number error can't be blank" do
- login_portal(user)
- post organization_api_v1_persons_path, params: { person: general_params.merge(
- identifications_attributes: [{ country_code: "cl", country_name: "chile", identification_type: :rut, identification_number: ""}]
- )}, as: :json
- error = JSON.parse(response.body)["errors"].first
- expect(response).to have_http_status(:unprocessable_entity)
- expect(error["source"].to_s).to include('identification_number')
- expect(error["title"]).to eq(I18n.t("errors.messages.blank"))
- expect(customer.persons.count).to eq(persons_count)
- end
- end
- context "if sent a identification_number with length more than 100" do
- let(:max_length_of_identification_number) { Organization::Identification::MAX_IDENTIFICATION_NUMBER_LENGTH }
- it "returns identification_number error can't be too long" do
- login_portal(user)
- post organization_api_v1_persons_path, params: { person: general_params.merge(
- identifications_attributes: [{ country_code: "cl", country_name: "chile", identification_type: :rut, identification_number: (SecureRandom.hex*4)}]
- )}, as: :json
- error = JSON.parse(response.body)["errors"].first
- expect(response).to have_http_status(:unprocessable_entity)
- expect(error["source"].to_s).to include('identification_number')
- expect(error["title"]).to eq(I18n.t("errors.messages.too_long", count: max_length_of_identification_number))
- expect(customer.persons.count).to eq(persons_count)
- end
- end
- end
- context "CONTACT INFORMATION (NOT required)" do
- context "Phone" do
- context "if sent a general info OK and a phone number" do
- let(:phone) { "+56911111111" }
- let(:params_with_phone) { general_params.merge(phone: phone) }
- it "returns ok and add a person with phone number" do
- login_portal(user)
- post organization_api_v1_persons_path, params: { person: params_with_phone }, as: :json
- expect(response).to have_http_status(:ok)
- expect(customer.persons.count).to eq(persons_count + 1)
- expect("+#{customer.persons.last.phone}").to eq(phone)
- end
- end
- context "if sent a phone number empty" do
- let(:params_with_phone) { general_params.merge(phone: "") }
- it "returns ok because it is not required" do
- login_portal(user)
- post organization_api_v1_persons_path, params: { person: params_with_phone }, as: :json
- expect(response).to have_http_status(:ok)
- expect(customer.persons.count).to eq(persons_count + 1)
- expect(customer.persons.last.phone).to eq("")
- end
- end
- context "if sent a invalid phone number" do
- let(:params_with_phone) { general_params.merge(phone: "081341") }
- it "returns phone error is a invalid number" do
- login_portal(user)
- post organization_api_v1_persons_path, params: { person: params_with_phone }, as: :json
- error = JSON.parse(response.body)["errors"].first
- expect(response).to have_http_status(:unprocessable_entity)
- expect(error["source"].to_s).to include('phone')
- expect(error["title"]).to eq(I18n.t("errors.messages.improbable_phone"))
- expect(customer.persons.count).to eq(persons_count)
- end
- end
- end
- context "Addresses" do
- 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
- let(:params_with_addresses) {
- general_params.merge(addresses_attributes: [
- { 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 }
- ])
- }
- it "returns ok and add a person" do
- login_portal(user)
- post organization_api_v1_persons_path, params: { person: params_with_addresses }, as: :json
- expect(response).to have_http_status(:ok)
- expect(customer.persons.count).to eq(persons_count + 1)
- expect(customer.persons.last.addresses.count).to eq(1)
- end
- end
- context "if sent a address info with country_name, country_code, name, district, city region and postal_code empty" do
- let(:params_with_addresses) {
- general_params.merge(addresses_attributes: [
- { country_name: "", country_code: "", name: "", district: "", city: "", region: "", postal_code: "", default: false }
- ])
- }
- it "returns error There must be at least one non-empty field because" do
- login_portal(user)
- post organization_api_v1_persons_path, params: { person: params_with_addresses }, as: :json
- error = JSON.parse(response.body)["errors"].first
- expect(response).to have_http_status(:unprocessable_entity)
- expect(error["source"].to_s).to include('addresses')
- expect(error["title"]).to eq(I18n.t("activerecord.errors.models.organization/address.attributes.base.at_least_one_field_present"))
- expect(customer.persons.count).to eq(persons_count)
- end
- end
- context "if sent a general info OK and 2 Address info valid" do
- let(:params_with_addresses) {
- general_params.merge(addresses_attributes: [
- { 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 },
- { name: "Address 2", default: false }
- ])
- }
- it "returns ok and add a person with 2 addresses" do
- login_portal(user)
- post organization_api_v1_persons_path, params: { person: params_with_addresses }, as: :json
- expect(response).to have_http_status(:ok)
- expect(customer.persons.count).to eq(persons_count + 1)
- expect(customer.persons.last.addresses.count).to eq(2)
- end
- end
- end
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment