From b3b4e87443c309b1c6d1ca24ab11914c8f3fe5ef Mon Sep 17 00:00:00 2001 From: Alejandro Rueda Date: Wed, 12 Jun 2024 14:12:10 +0200 Subject: [PATCH 1/9] Compatibility with CivciCRM API v4 --- lib/decidim/civicrm/api.rb | 45 +++++++++++++- lib/decidim/civicrm/api/base/base_query.rb | 13 ---- lib/decidim/civicrm/api/base/find_query.rb | 23 ------- lib/decidim/civicrm/api/base/list_query.rb | 24 -------- lib/decidim/civicrm/api/base/request.rb | 53 ---------------- lib/decidim/civicrm/api/base/v3/base_query.rb | 30 ++++++++++ lib/decidim/civicrm/api/base/v3/find_query.rb | 25 ++++++++ lib/decidim/civicrm/api/base/v3/list_query.rb | 26 ++++++++ lib/decidim/civicrm/api/base/v3/request.rb | 54 +++++++++++++++++ lib/decidim/civicrm/api/base/v4/base_query.rb | 22 +++++++ lib/decidim/civicrm/api/base/v4/find_query.rb | 19 ++++++ lib/decidim/civicrm/api/base/v4/list_query.rb | 47 +++++++++++++++ lib/decidim/civicrm/api/base/v4/request.rb | 60 +++++++++++++++++++ lib/decidim/civicrm/api/find_contact.rb | 42 +++---------- lib/decidim/civicrm/api/find_event.rb | 33 +++------- lib/decidim/civicrm/api/find_group.rb | 35 +++-------- lib/decidim/civicrm/api/find_participant.rb | 33 +++------- lib/decidim/civicrm/api/find_user.rb | 51 +++------------- .../civicrm/api/list_contact_groups.rb | 29 +++------ .../civicrm/api/list_contact_memberships.rb | 29 +++------ .../civicrm/api/list_contacts_in_group.rb | 32 +++------- lib/decidim/civicrm/api/list_groups.rb | 29 +++------ .../civicrm/api/list_membership_types.rb | 31 +++------- .../civicrm/api/participants_in_event.rb | 32 +++------- lib/decidim/civicrm/api/request.rb | 23 +++++++ lib/decidim/civicrm/api/v3/find_contact.rb | 46 ++++++++++++++ lib/decidim/civicrm/api/v3/find_event.rb | 37 ++++++++++++ lib/decidim/civicrm/api/v3/find_group.rb | 39 ++++++++++++ .../civicrm/api/v3/find_participant.rb | 37 ++++++++++++ lib/decidim/civicrm/api/v3/find_user.rb | 55 +++++++++++++++++ .../civicrm/api/v3/list_contact_groups.rb | 33 ++++++++++ .../api/v3/list_contact_memberships.rb | 33 ++++++++++ .../civicrm/api/v3/list_contacts_in_group.rb | 36 +++++++++++ lib/decidim/civicrm/api/v3/list_groups.rb | 33 ++++++++++ .../civicrm/api/v3/list_membership_types.rb | 35 +++++++++++ .../civicrm/api/v3/participants_in_event.rb | 36 +++++++++++ lib/decidim/civicrm/api/v4/find_contact.rb | 41 +++++++++++++ lib/decidim/civicrm/api/v4/find_event.rb | 40 +++++++++++++ lib/decidim/civicrm/api/v4/find_group.rb | 38 ++++++++++++ .../civicrm/api/v4/find_participant.rb | 43 +++++++++++++ lib/decidim/civicrm/api/v4/find_user.rb | 45 ++++++++++++++ .../civicrm/api/v4/list_contact_groups.rb | 31 ++++++++++ .../api/v4/list_contact_memberships.rb | 31 ++++++++++ .../civicrm/api/v4/list_contacts_in_group.rb | 34 +++++++++++ lib/decidim/civicrm/api/v4/list_groups.rb | 30 ++++++++++ .../civicrm/api/v4/list_membership_types.rb | 33 ++++++++++ .../civicrm/api/v4/participants_in_event.rb | 40 +++++++++++++ .../templates/development_civicrm_config.rb | 3 +- 48 files changed, 1275 insertions(+), 394 deletions(-) delete mode 100644 lib/decidim/civicrm/api/base/find_query.rb delete mode 100644 lib/decidim/civicrm/api/base/list_query.rb delete mode 100644 lib/decidim/civicrm/api/base/request.rb create mode 100644 lib/decidim/civicrm/api/base/v3/base_query.rb create mode 100644 lib/decidim/civicrm/api/base/v3/find_query.rb create mode 100644 lib/decidim/civicrm/api/base/v3/list_query.rb create mode 100644 lib/decidim/civicrm/api/base/v3/request.rb create mode 100644 lib/decidim/civicrm/api/base/v4/base_query.rb create mode 100644 lib/decidim/civicrm/api/base/v4/find_query.rb create mode 100644 lib/decidim/civicrm/api/base/v4/list_query.rb create mode 100644 lib/decidim/civicrm/api/base/v4/request.rb create mode 100644 lib/decidim/civicrm/api/request.rb create mode 100644 lib/decidim/civicrm/api/v3/find_contact.rb create mode 100644 lib/decidim/civicrm/api/v3/find_event.rb create mode 100644 lib/decidim/civicrm/api/v3/find_group.rb create mode 100644 lib/decidim/civicrm/api/v3/find_participant.rb create mode 100644 lib/decidim/civicrm/api/v3/find_user.rb create mode 100644 lib/decidim/civicrm/api/v3/list_contact_groups.rb create mode 100644 lib/decidim/civicrm/api/v3/list_contact_memberships.rb create mode 100644 lib/decidim/civicrm/api/v3/list_contacts_in_group.rb create mode 100644 lib/decidim/civicrm/api/v3/list_groups.rb create mode 100644 lib/decidim/civicrm/api/v3/list_membership_types.rb create mode 100644 lib/decidim/civicrm/api/v3/participants_in_event.rb create mode 100644 lib/decidim/civicrm/api/v4/find_contact.rb create mode 100644 lib/decidim/civicrm/api/v4/find_event.rb create mode 100644 lib/decidim/civicrm/api/v4/find_group.rb create mode 100644 lib/decidim/civicrm/api/v4/find_participant.rb create mode 100644 lib/decidim/civicrm/api/v4/find_user.rb create mode 100644 lib/decidim/civicrm/api/v4/list_contact_groups.rb create mode 100644 lib/decidim/civicrm/api/v4/list_contact_memberships.rb create mode 100644 lib/decidim/civicrm/api/v4/list_contacts_in_group.rb create mode 100644 lib/decidim/civicrm/api/v4/list_groups.rb create mode 100644 lib/decidim/civicrm/api/v4/list_membership_types.rb create mode 100644 lib/decidim/civicrm/api/v4/participants_in_event.rb diff --git a/lib/decidim/civicrm/api.rb b/lib/decidim/civicrm/api.rb index 8965ebe..934a6e1 100644 --- a/lib/decidim/civicrm/api.rb +++ b/lib/decidim/civicrm/api.rb @@ -1,20 +1,48 @@ # frozen_string_literal: true require_relative "api/base/base_query" -require_relative "api/base/find_query" -require_relative "api/base/list_query" -require_relative "api/base/request" +require_relative "api/base/v3/base_query" +require_relative "api/base/v4/base_query" +require_relative "api/base/v3/find_query" +require_relative "api/base/v4/find_query" +require_relative "api/base/v3/list_query" +require_relative "api/base/v4/list_query" +require_relative "api/request" +require_relative "api/base/v3/request" +require_relative "api/base/v4/request" require_relative "api/find_contact" +require_relative "api/v3/find_contact" +require_relative "api/v4/find_contact" require_relative "api/find_group" +require_relative "api/v3/find_group" +require_relative "api/v4/find_group" require_relative "api/find_user" +require_relative "api/v3/find_user" +require_relative "api/v4/find_user" require_relative "api/find_event" +require_relative "api/v3/find_event" +require_relative "api/v4/find_event" require_relative "api/find_participant" +require_relative "api/v3/find_participant" +require_relative "api/v4/find_participant" require_relative "api/participants_in_event" +require_relative "api/v3/participants_in_event" +require_relative "api/v4/participants_in_event" require_relative "api/list_contact_groups" +require_relative "api/v3/list_contact_groups" +require_relative "api/v4/list_contact_groups" require_relative "api/list_contacts_in_group" +require_relative "api/v3/list_contacts_in_group" +require_relative "api/v4/list_contacts_in_group" require_relative "api/list_contact_memberships" +require_relative "api/v3/list_contact_memberships" +require_relative "api/v4/list_contact_memberships" require_relative "api/list_groups" +require_relative "api/v3/list_groups" +require_relative "api/v4/list_groups" require_relative "api/list_membership_types" +require_relative "api/v3/list_membership_types" +require_relative "api/v4/list_membership_types" module Decidim module Civicrm @@ -34,6 +62,17 @@ def self.credentials def self.url config[:url] end + + def self.version + config[:api_version] + end + + def self.available_versions + @available_versions ||= { + v3: "V3", + v4: "V4" + } + end end end end diff --git a/lib/decidim/civicrm/api/base/base_query.rb b/lib/decidim/civicrm/api/base/base_query.rb index a23d136..6dacaa9 100644 --- a/lib/decidim/civicrm/api/base/base_query.rb +++ b/lib/decidim/civicrm/api/base/base_query.rb @@ -19,12 +19,6 @@ def response protected - def json_params(params) - params.merge( - sequential: 1 - ).to_json - end - def parsed_response raise NotImplementedError end @@ -32,13 +26,6 @@ def parsed_response def default_query raise NotImplementedError end - - def store_result - return unless success? - - @result = parsed_response - @result = @result.deep_symbolize_keys if @result.is_a? Hash - end end end end diff --git a/lib/decidim/civicrm/api/base/find_query.rb b/lib/decidim/civicrm/api/base/find_query.rb deleted file mode 100644 index 82df083..0000000 --- a/lib/decidim/civicrm/api/base/find_query.rb +++ /dev/null @@ -1,23 +0,0 @@ -# frozen_string_literal: true - -module Decidim - module Civicrm - module Api - module Base - class FindQuery < BaseQuery - protected - - def json_params(params) - params.deep_merge( - sequential: 1 - ).to_json - end - - def parsed_response - self.class.parse_item(response["values"].first) - end - end - end - end - end -end diff --git a/lib/decidim/civicrm/api/base/list_query.rb b/lib/decidim/civicrm/api/base/list_query.rb deleted file mode 100644 index 8cbbbc1..0000000 --- a/lib/decidim/civicrm/api/base/list_query.rb +++ /dev/null @@ -1,24 +0,0 @@ -# frozen_string_literal: true - -module Decidim - module Civicrm - module Api - module Base - class ListQuery < BaseQuery - protected - - def json_params(params) - params.deep_merge( - sequential: 1, - options: { limit: 0 } - ).to_json - end - - def parsed_response - response["values"].map { |item| self.class.parse_item(item) } - end - end - end - end - end -end diff --git a/lib/decidim/civicrm/api/base/request.rb b/lib/decidim/civicrm/api/base/request.rb deleted file mode 100644 index 76f7d69..0000000 --- a/lib/decidim/civicrm/api/base/request.rb +++ /dev/null @@ -1,53 +0,0 @@ -# frozen_string_literal: true - -module Decidim - module Civicrm - module Api - module Base - class Request - def initialize(verify_ssl: true) - @verify_ssl = verify_ssl - end - - attr_accessor :response - - def self.get(params, verify_ssl: true) - instance = Request.new(verify_ssl: verify_ssl) - response = instance.connection.get Decidim::Civicrm::Api.url do |request| - request.params = instance.base_params.merge(params) - - # puts [request.path, URI.encode_www_form(request.params.sort)].join("/?") # DEBUG, to obtain the correct URL for stub_request - end - - raise Decidim::Civicrm::Error, response.reason_phrase unless response.success? - - instance.response = JSON.parse(response.body).to_h - instance - end - - def self.post(params, verify_ssl: true) - instance = Request.new(verify_ssl: verify_ssl) - response = instance.connection.post Decidim::Civicrm::Api.url do |request| - request.params = instance.base_params.merge(params) - end - - raise Decidim::Civicrm::Error, response.reason_phrase unless response.success? - - instance.response = JSON.parse(response.body).to_h - instance - end - - def connection - @connection ||= Faraday.new(ssl: { verify: @verify_ssl }) - end - - def base_params - Decidim::Civicrm::Api.credentials.merge( - action: "Get" - ) - end - end - end - end - end -end diff --git a/lib/decidim/civicrm/api/base/v3/base_query.rb b/lib/decidim/civicrm/api/base/v3/base_query.rb new file mode 100644 index 0000000..c12bdce --- /dev/null +++ b/lib/decidim/civicrm/api/base/v3/base_query.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +module Decidim + module Civicrm + module Api + module Base + module V3 + class BaseQuery < Base::BaseQuery + attr_reader :result, :request + + protected + + def json_params(params) + params.merge( + sequential: 1 + ).to_json + end + + def store_result + return unless success? + + @result = parsed_response + @result = @result.deep_symbolize_keys if @result.is_a? Hash + end + end + end + end + end + end +end diff --git a/lib/decidim/civicrm/api/base/v3/find_query.rb b/lib/decidim/civicrm/api/base/v3/find_query.rb new file mode 100644 index 0000000..be93e6b --- /dev/null +++ b/lib/decidim/civicrm/api/base/v3/find_query.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +module Decidim + module Civicrm + module Api + module Base + module V3 + class FindQuery < BaseQuery + protected + + def json_params(params) + params.deep_merge( + sequential: 1 + ).to_json + end + + def parsed_response + self.class.parse_item(response["values"].first) + end + end + end + end + end + end +end diff --git a/lib/decidim/civicrm/api/base/v3/list_query.rb b/lib/decidim/civicrm/api/base/v3/list_query.rb new file mode 100644 index 0000000..3ae68a9 --- /dev/null +++ b/lib/decidim/civicrm/api/base/v3/list_query.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +module Decidim + module Civicrm + module Api + module Base + module V3 + class ListQuery < BaseQuery + protected + + def json_params(params) + params.deep_merge( + sequential: 1, + options: { limit: 0 } + ).to_json + end + + def parsed_response + response["values"].map { |item| self.class.parse_item(item) } + end + end + end + end + end + end +end diff --git a/lib/decidim/civicrm/api/base/v3/request.rb b/lib/decidim/civicrm/api/base/v3/request.rb new file mode 100644 index 0000000..cc34d45 --- /dev/null +++ b/lib/decidim/civicrm/api/base/v3/request.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +module Decidim + module Civicrm + module Api + module Base + module V3 + class Request + def initialize(verify_ssl: true) + @verify_ssl = verify_ssl + end + + attr_accessor :response + + def self.get(params, verify_ssl: true) + instance = Request.new(verify_ssl: verify_ssl) + response = instance.connection.get Decidim::Civicrm::Api.url do |request| + request.params = instance.base_params.merge(params) + # puts [request.path, URI.encode_www_form(request.params.sort)].join("/?") # DEBUG, to obtain the correct URL for stub_request + end + + raise Decidim::Civicrm::Error, response.reason_phrase unless response.success? + + instance.response = JSON.parse(response.body).to_h + instance + end + + def self.post(params, verify_ssl: true) + instance = Request.new(verify_ssl: verify_ssl) + response = instance.connection.post Decidim::Civicrm::Api.url do |request| + request.params = instance.base_params.merge(params) + end + + raise Decidim::Civicrm::Error, response.reason_phrase unless response.success? + + instance.response = JSON.parse(response.body).to_h + instance + end + + def connection + @connection ||= Faraday.new(ssl: { verify: @verify_ssl }) + end + + def base_params + Decidim::Civicrm::Api.credentials.merge( + action: "Get" + ) + end + end + end + end + end + end +end diff --git a/lib/decidim/civicrm/api/base/v4/base_query.rb b/lib/decidim/civicrm/api/base/v4/base_query.rb new file mode 100644 index 0000000..56a2ca4 --- /dev/null +++ b/lib/decidim/civicrm/api/base/v4/base_query.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module Decidim + module Civicrm + module Api + module Base + module V4 + class BaseQuery < Base::BaseQuery + attr_reader :result, :request + + def store_result + return unless success? + + @result = parsed_response + @result[:values] = @result[:values].deep_symbolize_keys if @result[:values].is_a? Hash + end + end + end + end + end + end +end diff --git a/lib/decidim/civicrm/api/base/v4/find_query.rb b/lib/decidim/civicrm/api/base/v4/find_query.rb new file mode 100644 index 0000000..de8495c --- /dev/null +++ b/lib/decidim/civicrm/api/base/v4/find_query.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module Decidim + module Civicrm + module Api + module Base + module V4 + class FindQuery < BaseQuery + protected + + def parsed_response + self.class.parse_item(response["values"].first) + end + end + end + end + end + end +end diff --git a/lib/decidim/civicrm/api/base/v4/list_query.rb b/lib/decidim/civicrm/api/base/v4/list_query.rb new file mode 100644 index 0000000..1727a48 --- /dev/null +++ b/lib/decidim/civicrm/api/base/v4/list_query.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +module Decidim + module Civicrm + module Api + module Base + module V4 + class ListQuery < BaseQuery + protected + + def initialize(id = nil, query = nil) + results = [] + offset = 0 + @id = id + @request = request(offset, query) + + store_result + results << @result[:values] + offset += self.class.records_by_page + while offset < @result[:count_matched] + @request = request(offset, query) + + store_result + results << @result[:values] + offset += self.class.records_by_page + end + @result = results.flatten + end + + def parsed_response + { + count_matched: response["countMatched"], + values: response["values"].map { |item| self.class.parse_item(item) } + } + end + + class << self + def records_by_page + 200 + end + end + end + end + end + end + end +end diff --git a/lib/decidim/civicrm/api/base/v4/request.rb b/lib/decidim/civicrm/api/base/v4/request.rb new file mode 100644 index 0000000..996f84a --- /dev/null +++ b/lib/decidim/civicrm/api/base/v4/request.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +module Decidim + module Civicrm + module Api + module Base + module V4 + class Request + def initialize(verify_ssl: true) + @verify_ssl = verify_ssl + end + + attr_accessor :response + + def self.get(entity, params, action, verify_ssl: true) + instance = Request.new(verify_ssl: verify_ssl) + url = "#{Decidim::Civicrm::Api.url}/#{entity}/#{action}" + response = instance.connection.get url do |request| + request.params = { + "_authx" => auth_token, + "params" => params.to_json + } + end + raise Decidim::Civicrm::Error, response.reason_phrase unless response.success? + + instance.response = JSON.parse(response.body).to_h + instance + end + + def self.post(entity, params, action, verify_ssl: true) + instance = Request.new(verify_ssl: verify_ssl) + url = "#{Decidim::Civicrm::Api.url}/#{entity}/#{action}" + + response = instance.connection.post url do |request| + request.params = if action == "get" + { "params" => params.to_json } + else + params + end + request.headers["X-Civi-Auth"] = auth_token + end + raise Decidim::Civicrm::Error, response.reason_phrase unless response.success? + + instance.response = JSON.parse(response.body).to_h + instance + end + + def connection + @connection ||= Faraday.new(ssl: { verify: @verify_ssl }) + end + + def self.auth_token + "Bearer #{Decidim::Civicrm::Api.credentials[:api_key]}" + end + end + end + end + end + end +end diff --git a/lib/decidim/civicrm/api/find_contact.rb b/lib/decidim/civicrm/api/find_contact.rb index 3f45c15..e731794 100644 --- a/lib/decidim/civicrm/api/find_contact.rb +++ b/lib/decidim/civicrm/api/find_contact.rb @@ -3,40 +3,16 @@ module Decidim module Civicrm module Api - class FindContact < Base::FindQuery - def initialize(id, query = nil) - @request = Base::Request.get( - { - entity: "Contact", - contact_id: id, - json: json_params(query || default_query) - } - ) - - store_result - end - - def default_query - { - return: "display_name", - "api.Membership.get" => { - return: "membership_type_id" - } - } - end - - def self.parse_item(item) - contact = { - id: item["id"].to_i, - display_name: item["display_name"] - } + class FindContact + attr_reader :result - memberships = item["api.Membership.get"]["values"] - - { - contact: contact, - memberships: memberships.map { |m| ListContactMemberships.parse_item(m) } - } + def initialize(id, query = nil) + @result = case Decidim::Civicrm::Api.version + when Decidim::Civicrm::Api.available_versions[:v3] + Decidim::Civicrm::Api::V3::FindContact.new(id, query).result + when Decidim::Civicrm::Api.available_versions[:v4] + Decidim::Civicrm::Api::V4::FindContact.new(id, query).result + end end end end diff --git a/lib/decidim/civicrm/api/find_event.rb b/lib/decidim/civicrm/api/find_event.rb index 42eddf6..d04d100 100644 --- a/lib/decidim/civicrm/api/find_event.rb +++ b/lib/decidim/civicrm/api/find_event.rb @@ -3,31 +3,16 @@ module Decidim module Civicrm module Api - class FindEvent < Base::FindQuery - def initialize(id, query = nil) - @request = Base::Request.get( - { - entity: "Event", - id: id, - json: json_params(query || default_query) - } - ) - - store_result - end + class FindEvent + attr_reader :result - def default_query - { - return: "event_id,title,event_title,event_type,start_date,evet_start_date,end_date,event_end_date" - } - end - - private - - def parsed_response - { - event: response["values"].first - } + def initialize(id, query = nil) + @result = case Decidim::Civicrm::Api.version + when Decidim::Civicrm::Api.available_versions[:v3] + Decidim::Civicrm::Api::V3::FindEvent.new(id, query).result + when Decidim::Civicrm::Api.available_versions[:v4] + Decidim::Civicrm::Api::V4::FindEvent.new(id, query).result + end end end end diff --git a/lib/decidim/civicrm/api/find_group.rb b/lib/decidim/civicrm/api/find_group.rb index f768113..ce12371 100644 --- a/lib/decidim/civicrm/api/find_group.rb +++ b/lib/decidim/civicrm/api/find_group.rb @@ -3,33 +3,16 @@ module Decidim module Civicrm module Api - class FindGroup < Base::FindQuery - def initialize(id, query = nil) - @request = Base::Request.get( - { - entity: "Group", - group_id: id, - json: json_params(query || default_query) - } - ) - - store_result - end + class FindGroup + attr_reader :result - def default_query - { - return: "group_id,name,title,description,group_type" - } - end - - def self.parse_item(item) - { - id: item["id"].to_i, - name: item["name"], - title: item["title"], - description: item["description"], - group_type: item["group_type"].respond_to?(:map) ? item["group_type"].map(&:to_i) : [item["group_type"].to_i] - } + def initialize(id, query = nil) + @result = case Decidim::Civicrm::Api.version + when Decidim::Civicrm::Api.available_versions[:v3] + Decidim::Civicrm::Api::V3::FindGroup.new(id, query).result + when Decidim::Civicrm::Api.available_versions[:v4] + Decidim::Civicrm::Api::V4::FindGroup.new(id, query).result + end end end end diff --git a/lib/decidim/civicrm/api/find_participant.rb b/lib/decidim/civicrm/api/find_participant.rb index 2dc1af5..c84dc84 100644 --- a/lib/decidim/civicrm/api/find_participant.rb +++ b/lib/decidim/civicrm/api/find_participant.rb @@ -3,31 +3,16 @@ module Decidim module Civicrm module Api - class FindParticipant < Base::FindQuery - def initialize(id, query = nil) - @request = Base::Request.get( - { - entity: "Participant", - id: id, - json: json_params(query || default_query) - } - ) - - store_result - end + class FindParticipant + attr_reader :result - def default_query - { - return: "contact_id,display_name,participant_status,id,participant_fee_amount,participant_fee_level,participant_fee_currency" - } - end - - private - - def parsed_response - { - participant: response["values"].first - } + def initialize(id, query = nil) + @result = case Decidim::Civicrm::Api.version + when Decidim::Civicrm::Api.available_versions[:v3] + Decidim::Civicrm::Api::V3::FindParticipant.new(id, query).result + when Decidim::Civicrm::Api.available_versions[:v4] + Decidim::Civicrm::Api::V4::FindParticipant.new(id, query).result + end end end end diff --git a/lib/decidim/civicrm/api/find_user.rb b/lib/decidim/civicrm/api/find_user.rb index 95b01f4..45796e1 100644 --- a/lib/decidim/civicrm/api/find_user.rb +++ b/lib/decidim/civicrm/api/find_user.rb @@ -3,49 +3,16 @@ module Decidim module Civicrm module Api - class FindUser < Base::FindQuery - def initialize(id, query = nil) - @request = Base::Request.get( - { - entity: "User", - id: id, - json: json_params(query || default_query) - } - ) - - store_result - end + class FindUser + attr_reader :result - def default_query - { - "api.Contact.get" => { - return: "display_name" - }, - "api.Membership.get": { - contact_id: "$value.contact_id", - return: "membership_type_id" - } - } - end - - def self.parse_item(item) - user = item - contact = user.delete("api.Contact.get")["values"].first - memberships = user.delete("api.Membership.get")["values"] - - { - user: { - id: user["id"].to_i, - name: user["name"], - email: user["email"], - contact_id: user["contact_id"].to_i - }, - contact: { - id: user["contact_id"].to_i, - display_name: contact["display_name"] - }, - memberships: memberships.map { |m| ListContactMemberships.parse_item(m) } - } + def initialize(id, query = nil) + @result = case Decidim::Civicrm::Api.version + when Decidim::Civicrm::Api.available_versions[:v3] + Decidim::Civicrm::Api::V3::FindUser.new(id, query).result + when Decidim::Civicrm::Api.available_versions[:v4] + Decidim::Civicrm::Api::V4::FindUser.new(id, query).result + end end end end diff --git a/lib/decidim/civicrm/api/list_contact_groups.rb b/lib/decidim/civicrm/api/list_contact_groups.rb index 7a828d0..051f868 100644 --- a/lib/decidim/civicrm/api/list_contact_groups.rb +++ b/lib/decidim/civicrm/api/list_contact_groups.rb @@ -3,27 +3,16 @@ module Decidim module Civicrm module Api - class ListContactGroups < Base::ListQuery - def initialize(id, query = nil) - @request = Base::Request.get( - { - entity: "GroupContact", - contact_id: id, - json: json_params(query || default_query) - } - ) - - store_result - end + class ListContactGroups + attr_reader :result - def default_query - { - return: "group_id" - } - end - - def self.parse_item(item) - item["group_id"].to_i + def initialize(id, query = nil) + @result = case Decidim::Civicrm::Api.version + when Decidim::Civicrm::Api.available_versions[:v3] + Decidim::Civicrm::Api::V3::ListContactGroups.new(id, query).result + when Decidim::Civicrm::Api.available_versions[:v4] + Decidim::Civicrm::Api::V4::ListContactGroups.new(id, query).result + end end end end diff --git a/lib/decidim/civicrm/api/list_contact_memberships.rb b/lib/decidim/civicrm/api/list_contact_memberships.rb index 72104f9..bdd4e0f 100644 --- a/lib/decidim/civicrm/api/list_contact_memberships.rb +++ b/lib/decidim/civicrm/api/list_contact_memberships.rb @@ -3,27 +3,16 @@ module Decidim module Civicrm module Api - class ListContactMemberships < Base::ListQuery - def initialize(id, query = nil) - @request = Base::Request.get( - { - entity: "Membership", - contact_id: id, - json: json_params(query || default_query) - } - ) - - store_result - end + class ListContactMemberships + attr_reader :result - def default_query - { - return: "membership_type_id" - } - end - - def self.parse_item(item) - item["membership_type_id"].to_i + def initialize(id, query = nil) + @result = case Decidim::Civicrm::Api.version + when Decidim::Civicrm::Api.available_versions[:v3] + Decidim::Civicrm::Api::V3::ListContactMemberships.new(id, query).result + when Decidim::Civicrm::Api.available_versions[:v4] + Decidim::Civicrm::Api::V4::ListContactMemberships.new(id, query).result + end end end end diff --git a/lib/decidim/civicrm/api/list_contacts_in_group.rb b/lib/decidim/civicrm/api/list_contacts_in_group.rb index 20f1210..0785f03 100644 --- a/lib/decidim/civicrm/api/list_contacts_in_group.rb +++ b/lib/decidim/civicrm/api/list_contacts_in_group.rb @@ -3,30 +3,16 @@ module Decidim module Civicrm module Api - class ListContactsInGroup < Base::ListQuery - def initialize(id, query = nil) - @request = Base::Request.get( - { - entity: "Contact", - group: id, - json: json_params(query || default_query) - } - ) - - store_result - end + class ListContactsInGroup + attr_reader :result - def default_query - { - return: "contact_id,display_name" - } - end - - def self.parse_item(item) - { - contact_id: item["contact_id"].to_i, - display_name: item["display_name"] - } + def initialize(id, query = nil) + @result = case Decidim::Civicrm::Api.version + when Decidim::Civicrm::Api.available_versions[:v3] + Decidim::Civicrm::Api::V3::ListContactsInGroup.new(id, query).result + when Decidim::Civicrm::Api.available_versions[:v4] + Decidim::Civicrm::Api::V4::ListContactsInGroup.new(id, query).result + end end end end diff --git a/lib/decidim/civicrm/api/list_groups.rb b/lib/decidim/civicrm/api/list_groups.rb index fba9f90..ae48a39 100644 --- a/lib/decidim/civicrm/api/list_groups.rb +++ b/lib/decidim/civicrm/api/list_groups.rb @@ -3,27 +3,16 @@ module Decidim module Civicrm module Api - class ListGroups < Base::ListQuery - def initialize(query = nil) - @request = Base::Request.get( - { - entity: "Group", - is_active: 1, - json: json_params(query || default_query) - } - ) - - store_result - end + class ListGroups + attr_reader :result - def default_query - { - return: "group_id,name,title,description,group_type" - } - end - - def self.parse_item(item) - FindGroup.parse_item(item) + def initialize(query = nil) + @result = case Decidim::Civicrm::Api.version + when Decidim::Civicrm::Api.available_versions[:v3] + Decidim::Civicrm::Api::V3::ListGroups.new(query).result + when Decidim::Civicrm::Api.available_versions[:v4] + Decidim::Civicrm::Api::V4::ListGroups.new(query).result + end end end end diff --git a/lib/decidim/civicrm/api/list_membership_types.rb b/lib/decidim/civicrm/api/list_membership_types.rb index a90b127..b76c9ed 100644 --- a/lib/decidim/civicrm/api/list_membership_types.rb +++ b/lib/decidim/civicrm/api/list_membership_types.rb @@ -3,29 +3,16 @@ module Decidim module Civicrm module Api - class ListMembershipTypes < Base::ListQuery - def initialize(query = nil) - @request = Base::Request.get( - { - entity: "MembershipType", - json: json_params(query || default_query) - } - ) - store_result - end + class ListMembershipTypes + attr_reader :result - def default_query - { - options: { limit: 0 }, - return: "name" - } - end - - def self.parse_item(item) - { - id: item["id"].to_i, - name: item["name"] - } + def initialize(query = nil) + @result = case Decidim::Civicrm::Api.version + when Decidim::Civicrm::Api.available_versions[:v3] + Decidim::Civicrm::Api::V3::ListMembershipTypes.new(query).result + when Decidim::Civicrm::Api.available_versions[:v4] + Decidim::Civicrm::Api::V4::ListMembershipTypes.new(query).result + end end end end diff --git a/lib/decidim/civicrm/api/participants_in_event.rb b/lib/decidim/civicrm/api/participants_in_event.rb index 9884f6c..107c229 100644 --- a/lib/decidim/civicrm/api/participants_in_event.rb +++ b/lib/decidim/civicrm/api/participants_in_event.rb @@ -3,30 +3,16 @@ module Decidim module Civicrm module Api - class ParticipantsInEvent < Base::ListQuery - def initialize(id, query = nil) - @request = Base::Request.get( - { - entity: "Participant", - event_id: id, - json: json_params(query || default_query) - } - ) - - store_result - end + class ParticipantsInEvent + attr_reader :result - def default_query - { - options: { limit: 0 }, - return: "contact_id,display_name,participant_status,id,participant_fee_amount,participant_fee_level,participant_fee_currency" - } - end - - private - - def parsed_response - response["values"] + def initialize(id, query = nil) + @result = case Decidim::Civicrm::Api.version + when Decidim::Civicrm::Api.available_versions[:v3] + Decidim::Civicrm::Api::V3::ParticipantsInEvent.new(id, query).result + when Decidim::Civicrm::Api.available_versions[:v4] + Decidim::Civicrm::Api::V4::ParticipantsInEvent.new(id, query).result + end end end end diff --git a/lib/decidim/civicrm/api/request.rb b/lib/decidim/civicrm/api/request.rb new file mode 100644 index 0000000..4fed043 --- /dev/null +++ b/lib/decidim/civicrm/api/request.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +module Decidim + module Civicrm + module Api + class Request + def self.post(data) + case Decidim::Civicrm::Api.version + when Decidim::Civicrm::Api.available_versions[:v3] + Decidim::Civicrm::Api::Base::V3::Request.post(data, verify_ssl: true) + when Decidim::Civicrm::Api.available_versions[:v4] + entity = data[:entity] + action = data[:action] + data.delete(:entity) + data.delete(:action) + data.delete(:json) + Decidim::Civicrm::Api::Base::V4::Request.post(entity, data, action, verify_ssl: true) + end + end + end + end + end +end diff --git a/lib/decidim/civicrm/api/v3/find_contact.rb b/lib/decidim/civicrm/api/v3/find_contact.rb new file mode 100644 index 0000000..713f26c --- /dev/null +++ b/lib/decidim/civicrm/api/v3/find_contact.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +module Decidim + module Civicrm + module Api + module V3 + class FindContact < Base::V3::FindQuery + def initialize(id, query = nil) + @request = Base::V3::Request.get( + { + entity: "Contact", + contact_id: id, + json: json_params(query || default_query) + } + ) + + store_result + end + + def default_query + { + return: "display_name", + "api.Membership.get" => { + return: "membership_type_id" + } + } + end + + def self.parse_item(item) + contact = { + id: item["id"].to_i, + display_name: item["display_name"] + } + + memberships = item["api.Membership.get"]["values"] + + { + contact: contact, + memberships: memberships.map { |m| ListContactMemberships.parse_item(m) } + } + end + end + end + end + end +end diff --git a/lib/decidim/civicrm/api/v3/find_event.rb b/lib/decidim/civicrm/api/v3/find_event.rb new file mode 100644 index 0000000..107139a --- /dev/null +++ b/lib/decidim/civicrm/api/v3/find_event.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +module Decidim + module Civicrm + module Api + module V3 + class FindEvent < Base::V3::FindQuery + def initialize(id, query = nil) + @request = Base::V3::Request.get( + { + entity: "Event", + id: id, + json: json_params(query || default_query) + } + ) + + store_result + end + + def default_query + { + return: "event_id,title,event_title,event_type,start_date,event_start_date,end_date,event_end_date" + } + end + + private + + def parsed_response + { + event: response["values"].first + } + end + end + end + end + end +end diff --git a/lib/decidim/civicrm/api/v3/find_group.rb b/lib/decidim/civicrm/api/v3/find_group.rb new file mode 100644 index 0000000..dae356b --- /dev/null +++ b/lib/decidim/civicrm/api/v3/find_group.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +module Decidim + module Civicrm + module Api + module V3 + class FindGroup < Base::V3::FindQuery + def initialize(id, query = nil) + @request = Base::V3::Request.get( + { + entity: "Group", + group_id: id, + json: json_params(query || default_query) + } + ) + + store_result + end + + def default_query + { + return: "group_id,name,title,description,group_type" + } + end + + def self.parse_item(item) + { + id: item["id"].to_i, + name: item["name"], + title: item["title"], + description: item["description"], + group_type: item["group_type"].respond_to?(:map) ? item["group_type"].map(&:to_i) : [item["group_type"].to_i] + } + end + end + end + end + end +end diff --git a/lib/decidim/civicrm/api/v3/find_participant.rb b/lib/decidim/civicrm/api/v3/find_participant.rb new file mode 100644 index 0000000..929f23c --- /dev/null +++ b/lib/decidim/civicrm/api/v3/find_participant.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +module Decidim + module Civicrm + module Api + module V3 + class FindParticipant < Base::V3::FindQuery + def initialize(id, query = nil) + @request = Base::V3::Request.get( + { + entity: "Participant", + id: id, + json: json_params(query || default_query) + } + ) + + store_result + end + + def default_query + { + return: "contact_id,display_name,participant_status,id,participant_fee_amount,participant_fee_level,participant_fee_currency" + } + end + + private + + def parsed_response + { + participant: response["values"].first + } + end + end + end + end + end +end diff --git a/lib/decidim/civicrm/api/v3/find_user.rb b/lib/decidim/civicrm/api/v3/find_user.rb new file mode 100644 index 0000000..9f8b57f --- /dev/null +++ b/lib/decidim/civicrm/api/v3/find_user.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +module Decidim + module Civicrm + module Api + module V3 + class FindUser < Base::V3::FindQuery + def initialize(id, query = nil) + @request = Base::V3::Request.get( + { + entity: "User", + id: id, + json: json_params(query || default_query) + } + ) + + store_result + end + + def default_query + { + "api.Contact.get" => { + return: "display_name" + }, + "api.Membership.get": { + contact_id: "$value.contact_id", + return: "membership_type_id" + } + } + end + + def self.parse_item(item) + user = item + contact = user.delete("api.Contact.get")["values"].first + memberships = user.delete("api.Membership.get")["values"] + + { + user: { + id: user["id"].to_i, + name: user["name"], + email: user["email"], + contact_id: user["contact_id"].to_i + }, + contact: { + id: user["contact_id"].to_i, + display_name: contact["display_name"] + }, + memberships: memberships.map { |m| ListContactMemberships.parse_item(m) } + } + end + end + end + end + end +end diff --git a/lib/decidim/civicrm/api/v3/list_contact_groups.rb b/lib/decidim/civicrm/api/v3/list_contact_groups.rb new file mode 100644 index 0000000..0ddac20 --- /dev/null +++ b/lib/decidim/civicrm/api/v3/list_contact_groups.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +module Decidim + module Civicrm + module Api + module V3 + class ListContactGroups < Base::V3::ListQuery + def initialize(id, query = nil) + @request = Base::V3::Request.get( + { + entity: "GroupContact", + contact_id: id, + json: json_params(query || default_query) + } + ) + + store_result + end + + def default_query + { + select: "group_id" + } + end + + def self.parse_item(item) + item["group_id"].to_i + end + end + end + end + end +end diff --git a/lib/decidim/civicrm/api/v3/list_contact_memberships.rb b/lib/decidim/civicrm/api/v3/list_contact_memberships.rb new file mode 100644 index 0000000..644362b --- /dev/null +++ b/lib/decidim/civicrm/api/v3/list_contact_memberships.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +module Decidim + module Civicrm + module Api + module V3 + class ListContactMemberships < Base::V3::ListQuery + def initialize(id, query = nil) + @request = Base::V3::Request.get( + { + entity: "Membership", + contact_id: id, + json: json_params(query || default_query) + } + ) + + store_result + end + + def default_query + { + return: "membership_type_id" + } + end + + def self.parse_item(item) + item["membership_type_id"].to_i + end + end + end + end + end +end diff --git a/lib/decidim/civicrm/api/v3/list_contacts_in_group.rb b/lib/decidim/civicrm/api/v3/list_contacts_in_group.rb new file mode 100644 index 0000000..09b4baa --- /dev/null +++ b/lib/decidim/civicrm/api/v3/list_contacts_in_group.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +module Decidim + module Civicrm + module Api + module V3 + class ListContactsInGroup < Base::V3::ListQuery + def initialize(id, query = nil) + @request = Base::V3::Request.get( + { + entity: "Contact", + group: id, + json: json_params(query || default_query) + } + ) + + store_result + end + + def default_query + { + return: "contact_id,display_name" + } + end + + def self.parse_item(item) + { + contact_id: item["contact_id"].to_i, + display_name: item["display_name"] + } + end + end + end + end + end +end diff --git a/lib/decidim/civicrm/api/v3/list_groups.rb b/lib/decidim/civicrm/api/v3/list_groups.rb new file mode 100644 index 0000000..fc0411a --- /dev/null +++ b/lib/decidim/civicrm/api/v3/list_groups.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +module Decidim + module Civicrm + module Api + module V3 + class ListGroups < Base::V3::ListQuery + def initialize(query = nil) + @request = Base::V3::Request.get( + { + entity: "Group", + is_active: 1, # esto sigue existiendo? + json: json_params(query || default_query) + } + ) + + store_result + end + + def default_query + { + return: "group_id,name,title,description,group_type" + } + end + + def self.parse_item(item) + FindGroup.parse_item(item) + end + end + end + end + end +end diff --git a/lib/decidim/civicrm/api/v3/list_membership_types.rb b/lib/decidim/civicrm/api/v3/list_membership_types.rb new file mode 100644 index 0000000..be4444d --- /dev/null +++ b/lib/decidim/civicrm/api/v3/list_membership_types.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +module Decidim + module Civicrm + module Api + module V3 + class ListMembershipTypes < Base::V3::ListQuery + def initialize(query = nil) + @request = Base::V3::Request.get( + { + entity: "MembershipType", + json: json_params(query || default_query) + } + ) + store_result + end + + def default_query + { + options: { limit: 0 }, + return: "name" + } + end + + def self.parse_item(item) + { + id: item["id"].to_i, + name: item["name"] + } + end + end + end + end + end +end diff --git a/lib/decidim/civicrm/api/v3/participants_in_event.rb b/lib/decidim/civicrm/api/v3/participants_in_event.rb new file mode 100644 index 0000000..e783cad --- /dev/null +++ b/lib/decidim/civicrm/api/v3/participants_in_event.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +module Decidim + module Civicrm + module Api + module V3 + class ParticipantsInEvent < Base::V3::ListQuery + def initialize(id, query = nil) + @request = Base::V3::Request.get( + { + entity: "Participant", + event_id: id, + json: json_params(query || default_query) + } + ) + + store_result + end + + def default_query + { + options: { limit: 0 }, + return: "contact_id,display_name,participant_status,id,participant_fee_amount,participant_fee_level,participant_fee_currency" + } + end + + private + + def parsed_response + response["values"] + end + end + end + end + end +end diff --git a/lib/decidim/civicrm/api/v4/find_contact.rb b/lib/decidim/civicrm/api/v4/find_contact.rb new file mode 100644 index 0000000..2c362a9 --- /dev/null +++ b/lib/decidim/civicrm/api/v4/find_contact.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +module Decidim + module Civicrm + module Api + module V4 + class FindContact < Base::V4::FindQuery + def initialize(id, query = nil) + @request = Base::V4::Request.post( + "Contact", + query || default_query(id), + "get" + ) + + store_result + end + + def default_query(id) + { + select: %w(id membership.membership_type_id display_name), + join: [["Membership AS membership", "LEFT"]], + where: [["id", "=", id]] + } + end + + def self.parse_item(item) + contact = { + id: item["id"], + display_name: item["display_name"] + } + + { + contact: contact, + memberships: Array(item["membership.membership_type_id"]) + } + end + end + end + end + end +end diff --git a/lib/decidim/civicrm/api/v4/find_event.rb b/lib/decidim/civicrm/api/v4/find_event.rb new file mode 100644 index 0000000..2436fbd --- /dev/null +++ b/lib/decidim/civicrm/api/v4/find_event.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +module Decidim + module Civicrm + module Api + module V4 + class FindEvent < Base::V4::FindQuery + def initialize(id, query = nil) + @request = Base::V4::Request.post( + "Event", + query || default_query(id), + "get" + ) + + store_result + end + + def default_query(id) + { + select: %w(event_id title event_title event_type start_date event_start_date end_date event_end_date), + where: [["id", "=", id]] + } + end + + private + + def parsed_response + response_hash = response["values"].first + response_hash["event_title"] = response_hash["title"] + response_hash["event_start_date"] = response_hash["start_date"] + response_hash["event_end_date"] = response_hash["end_date"] + { + event: response_hash + } + end + end + end + end + end +end diff --git a/lib/decidim/civicrm/api/v4/find_group.rb b/lib/decidim/civicrm/api/v4/find_group.rb new file mode 100644 index 0000000..5331105 --- /dev/null +++ b/lib/decidim/civicrm/api/v4/find_group.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +module Decidim + module Civicrm + module Api + module V4 + class FindGroup < Base::V4::FindQuery + def initialize(id, query = nil) + @request = Base::V4::Request.post( + "Group", + query || default_query(id), + "get" + ) + + store_result + end + + def default_query(id) + { + select: %w(group_id name title description group_type), + where: [["id", "=", id]] + } + end + + def self.parse_item(item) + { + id: item["id"].to_i, + name: item["name"], + title: item["title"], + description: item["description"], + group_type: item["group_type"].respond_to?(:map) ? item["group_type"].map(&:to_i) : [item["group_type"].to_i] + } + end + end + end + end + end +end diff --git a/lib/decidim/civicrm/api/v4/find_participant.rb b/lib/decidim/civicrm/api/v4/find_participant.rb new file mode 100644 index 0000000..e09c255 --- /dev/null +++ b/lib/decidim/civicrm/api/v4/find_participant.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +module Decidim + module Civicrm + module Api + module V4 + class FindParticipant < Base::V4::FindQuery + def initialize(id, query = nil) + @request = Base::V4::Request.post( + "Participant", + query || default_query(id), + "get" + ) + + store_result + end + + def default_query(id) + { + select: %w(contact_id contact_id.display_name status_id:label id fee_amount fee_level fee_currency), + where: [["id", "=", id]] + } + end + + private + + def parsed_response + response_hash = response["values"].first + response_hash["display_name"] = response_hash.delete("contact_id.display_name") + response_hash["participant_status"] = response_hash.delete("status_id:label") + response_hash["participant_fee_level"] = response_hash.delete("fee_level") + response_hash["participant_fee_amount"] = response_hash.delete("fee_amount") + response_hash["participant_fee_currency"] = response_hash.delete("fee_currency") + response_hash["participant_id"] = response_hash["id"] + { + participant: response_hash + } + end + end + end + end + end +end diff --git a/lib/decidim/civicrm/api/v4/find_user.rb b/lib/decidim/civicrm/api/v4/find_user.rb new file mode 100644 index 0000000..88607c7 --- /dev/null +++ b/lib/decidim/civicrm/api/v4/find_user.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +module Decidim + module Civicrm + module Api + module V4 + class FindUser < Base::V4::FindQuery + def initialize(id, query = nil) + @request = Base::V4::Request.post( + "Contact", + query || default_query(id), + "get" + ) + + store_result + end + + def default_query(id) + { + select: %w(uf_match.uf_id uf_match.contact_id display_name email_primary.email membership.membership_type_id:label), + join: [["UFMatch AS uf_match", "LEFT"], ["Membership AS membership", "LEFT"]], + where: [["uf_match.uf_id", "=", id]] + } + end + + def self.parse_item(item) + { + user: { + id: item["id"], + name: item["name"], + email: item["email_primary.email"], + contact_id: item["uf_match.contact_id"] + }, + contact: { + id: item["uf_match.contact_id"], + display_name: item["email_primary.email"] + }, + memberships: Array(item["membership.membership_type_id:label"]) + } + end + end + end + end + end +end diff --git a/lib/decidim/civicrm/api/v4/list_contact_groups.rb b/lib/decidim/civicrm/api/v4/list_contact_groups.rb new file mode 100644 index 0000000..fb8de8d --- /dev/null +++ b/lib/decidim/civicrm/api/v4/list_contact_groups.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +module Decidim + module Civicrm + module Api + module V4 + class ListContactGroups < Base::V4::ListQuery + def request(offset, query = nil) + Base::V4::Request.post( + "GroupContact", + query || default_query(offset), + "get" + ) + end + + def default_query(offset) + { + select: %w(row_count group_id), + where: [["contact_id", "=", @id]], + offset: offset + } + end + + def self.parse_item(item) + item["group_id"].to_i + end + end + end + end + end +end diff --git a/lib/decidim/civicrm/api/v4/list_contact_memberships.rb b/lib/decidim/civicrm/api/v4/list_contact_memberships.rb new file mode 100644 index 0000000..600fa19 --- /dev/null +++ b/lib/decidim/civicrm/api/v4/list_contact_memberships.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +module Decidim + module Civicrm + module Api + module V4 + class ListContactMemberships < Base::V4::ListQuery + def request(offset, query = nil) + Base::V4::Request.post( + "Membership", + query || default_query(offset), + "get" + ) + end + + def default_query(offset) + { + select: %w(row_count membership_type_id), + where: [["contact_id", "=", @id]], + offset: offset + } + end + + def self.parse_item(item) + item["membership_type_id"].to_i + end + end + end + end + end +end diff --git a/lib/decidim/civicrm/api/v4/list_contacts_in_group.rb b/lib/decidim/civicrm/api/v4/list_contacts_in_group.rb new file mode 100644 index 0000000..914185c --- /dev/null +++ b/lib/decidim/civicrm/api/v4/list_contacts_in_group.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +module Decidim + module Civicrm + module Api + module V4 + class ListContactsInGroup < Base::V4::ListQuery + def request(offset, query = nil) + Base::V4::Request.post( + "Contact", + query || default_query(offset), + "get" + ) + end + + def default_query(offset) + { + select: %w(row_count contact_id display_name), + offset: offset, + where: [["groups", "IN", @id]] + } + end + + def self.parse_item(item) + { + contact_id: item["id"], + display_name: item["display_name"] + } + end + end + end + end + end +end diff --git a/lib/decidim/civicrm/api/v4/list_groups.rb b/lib/decidim/civicrm/api/v4/list_groups.rb new file mode 100644 index 0000000..9d15955 --- /dev/null +++ b/lib/decidim/civicrm/api/v4/list_groups.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +module Decidim + module Civicrm + module Api + module V4 + class ListGroups < Base::V4::ListQuery + def request(offset, query = nil) + Base::V4::Request.post( + "Group", + query || default_query(offset), + "get" + ) + end + + def default_query(offset) + { + select: %w(row_count group_id name title description group_type), + offset: offset + } + end + + def self.parse_item(item) + FindGroup.parse_item(item) + end + end + end + end + end +end diff --git a/lib/decidim/civicrm/api/v4/list_membership_types.rb b/lib/decidim/civicrm/api/v4/list_membership_types.rb new file mode 100644 index 0000000..97a6a6e --- /dev/null +++ b/lib/decidim/civicrm/api/v4/list_membership_types.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +module Decidim + module Civicrm + module Api + module V4 + class ListMembershipTypes < Base::V4::ListQuery + def request(offset, query = nil) + Base::V4::Request.post( + "MembershipType", + query || default_query(offset), + "get" + ) + end + + def default_query(offset) + { + select: %w(row_count name), + offset: offset + } + end + + def self.parse_item(item) + { + id: item["id"].to_i, + name: item["name"] + } + end + end + end + end + end +end diff --git a/lib/decidim/civicrm/api/v4/participants_in_event.rb b/lib/decidim/civicrm/api/v4/participants_in_event.rb new file mode 100644 index 0000000..a32cb72 --- /dev/null +++ b/lib/decidim/civicrm/api/v4/participants_in_event.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +module Decidim + module Civicrm + module Api + module V4 + class ParticipantsInEvent < Base::V4::ListQuery + def request(offset, query = nil) + Base::V4::Request.post( + "Participant", + query || default_query(offset), + "get" + ) + end + + def default_query(offset) + { + select: %w(row_count id contact_id contact_id.display_name status_id:name fee_amount fee_level fee_currency), + offset: offset, + where: [["event_id", "=", @id]] + } + end + + def self.parse_item(item) + { + contact_id: item["contact_id"].to_s, + display_name: item["contact_id.display_name"], + participant_id: item["id"].to_s, + participant_fee_level: item["fee_level"], + participant_fee_amount: item["fee_amount"], + participant_fee_currency: item["fee_currency"], + participant_status: item["status_id:name"], + id: item["id"].to_s + } + end + end + end + end + end +end diff --git a/lib/generators/decidim/civicrm/templates/development_civicrm_config.rb b/lib/generators/decidim/civicrm/templates/development_civicrm_config.rb index 4c615dc..f289f18 100644 --- a/lib/generators/decidim/civicrm/templates/development_civicrm_config.rb +++ b/lib/generators/decidim/civicrm/templates/development_civicrm_config.rb @@ -6,7 +6,8 @@ config.api = { key: ENV.fetch("CIVICRM_VERIFICATION_API_KEY", nil), secret: ENV.fetch("CIVICRM_VERIFICATION_SECRET", nil), - url: ENV.fetch("CIVICRM_VERIFICATION_URL", nil) + url: ENV.fetch("CIVICRM_VERIFICATION_URL", nil), + api_version: ENV.fetch("API_VERSION", nil) } # Configure omniauth secrets From d2da4f005f35000d2897bc91ffc3f0939ddff2d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Bol=C3=ADvar?= Date: Wed, 12 Jun 2024 16:06:39 +0200 Subject: [PATCH 2/9] Change API version variable --- lib/decidim/civicrm/api.rb | 2 +- .../decidim/civicrm/templates/development_civicrm_config.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/decidim/civicrm/api.rb b/lib/decidim/civicrm/api.rb index 934a6e1..9192580 100644 --- a/lib/decidim/civicrm/api.rb +++ b/lib/decidim/civicrm/api.rb @@ -64,7 +64,7 @@ def self.url end def self.version - config[:api_version] + config[:version] end def self.available_versions diff --git a/lib/generators/decidim/civicrm/templates/development_civicrm_config.rb b/lib/generators/decidim/civicrm/templates/development_civicrm_config.rb index f289f18..fad1146 100644 --- a/lib/generators/decidim/civicrm/templates/development_civicrm_config.rb +++ b/lib/generators/decidim/civicrm/templates/development_civicrm_config.rb @@ -7,7 +7,7 @@ key: ENV.fetch("CIVICRM_VERIFICATION_API_KEY", nil), secret: ENV.fetch("CIVICRM_VERIFICATION_SECRET", nil), url: ENV.fetch("CIVICRM_VERIFICATION_URL", nil), - api_version: ENV.fetch("API_VERSION", nil) + version: ENV.fetch("CIVICRM_API_VERSION", nil) } # Configure omniauth secrets From 0271763cde5861b03951ce550f316a9e915ae771 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Bol=C3=ADvar?= Date: Tue, 18 Jun 2024 13:26:08 +0200 Subject: [PATCH 3/9] Show display name instead of email in contact displaoy name --- lib/decidim/civicrm/api/v4/find_user.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/decidim/civicrm/api/v4/find_user.rb b/lib/decidim/civicrm/api/v4/find_user.rb index 88607c7..a2a0536 100644 --- a/lib/decidim/civicrm/api/v4/find_user.rb +++ b/lib/decidim/civicrm/api/v4/find_user.rb @@ -33,7 +33,7 @@ def self.parse_item(item) }, contact: { id: item["uf_match.contact_id"], - display_name: item["email_primary.email"] + display_name: item["display_name"] }, memberships: Array(item["membership.membership_type_id:label"]) } From 39cc10b9412863048fc444e2d35b8527f1c65a4f Mon Sep 17 00:00:00 2001 From: Alejandro Rueda Date: Tue, 25 Jun 2024 12:36:39 +0200 Subject: [PATCH 4/9] Added name and nickname parsers --- lib/omniauth/strategies/civicrm.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/omniauth/strategies/civicrm.rb b/lib/omniauth/strategies/civicrm.rb index 734791a..1362e67 100644 --- a/lib/omniauth/strategies/civicrm.rb +++ b/lib/omniauth/strategies/civicrm.rb @@ -20,8 +20,8 @@ class Civicrm < OmniAuth::Strategies::OAuth2 info do { - name: extra[:contact][:display_name], - nickname: raw_info["preferred_username"], + name: parsed_name(extra[:contact][:display_name]), + nickname: parsed_nickname(raw_info["preferred_username"]), email: raw_info["email"], image: raw_info["picture"] } @@ -55,6 +55,14 @@ def raw_info def civicrm_info @civicrm_info ||= ::Decidim::Civicrm::Api::FindUser.new(uid).result end + + def parsed_name(expr) + expr.gsub(/[^\w\-]/, "") + end + + def parsed_nickname(expr) + expr.gsub(/[<>?%&$\^*#@()\[\]=+:;., "{}\\|]/, "") + end end end end From ab5c91e06244ce4d395ddb82554cb674e9edece2 Mon Sep 17 00:00:00 2001 From: Alejandro Rueda Date: Fri, 5 Jul 2024 12:19:48 +0200 Subject: [PATCH 5/9] Added tests for API v4 --- lib/decidim/civicrm/api/v4/find_event.rb | 8 +- .../civicrm/api/v4/find_participant.rb | 14 ++-- .../civicrm/test/{ => v3}/shared_contexts.rb | 6 +- .../civicrm/test/v4/shared_contexts.rb | 41 ++++++++++ .../files/{ => v3}/empty_response.json | 0 .../files/{ => v3}/error_response.json | 0 .../files/{ => v3}/event_valid_response.json | 0 .../{ => v3}/find_contact_valid_response.json | 0 .../{ => v3}/find_group_valid_response.json | 0 .../{ => v3}/find_user_valid_response.json | 0 .../list_contact_groups_valid_response.json | 0 ...st_contact_memberships_valid_response.json | 0 ...list_contacts_in_group_valid_response.json | 0 .../{ => v3}/list_groups_valid_response.json | 0 .../list_membership_types_valid_response.json | 0 .../{ => v3}/participant_valid_response.json | 0 spec/fixtures/files/v4/empty_response.json | 9 ++ spec/fixtures/files/v4/error_response.json | 3 + .../files/v4/event_valid_response.json | 19 +++++ .../files/v4/find_contact_valid_response.json | 15 ++++ .../files/v4/find_group_valid_response.json | 19 +++++ .../files/v4/find_user_valid_response.json | 18 ++++ .../list_contact_groups_valid_response.json | 23 ++++++ ...st_contact_memberships_valid_response.json | 19 +++++ ...list_contacts_in_group_valid_response.json | 23 ++++++ .../files/v4/list_groups_valid_response.json | 28 +++++++ .../list_membership_types_valid_response.json | 27 ++++++ .../files/v4/participant_valid_response.json | 19 +++++ .../admin/{ => v3}/event_meeting_form_spec.rb | 4 +- .../forms/admin/v4/event_meeting_form_spec.rb | 82 +++++++++++++++++++ .../{ => v3}/civicrm_groups_spec.rb | 4 +- .../{ => v3}/civicrm_membership_types_spec.rb | 4 +- .../verifications/{ => v3}/civicrm_spec.rb | 4 +- .../verifications/v4/civicrm_groups_spec.rb | 27 ++++++ .../v4/civicrm_membership_types_spec.rb | 27 ++++++ spec/forms/verifications/v4/civicrm_spec.rb | 27 ++++++ .../civicrm/api/{ => v3}/base/request_spec.rb | 4 +- .../civicrm/api/{ => v3}/find_contact_spec.rb | 4 +- .../civicrm/api/{ => v3}/find_group_spec.rb | 4 +- .../civicrm/api/{ => v3}/find_user_spec.rb | 4 +- .../api/{ => v3}/list_contact_groups_spec.rb | 4 +- .../{ => v3}/list_contact_memberships_spec.rb | 4 +- .../{ => v3}/list_contacts_in_group_spec.rb | 4 +- .../civicrm/api/{ => v3}/list_groups_spec.rb | 4 +- .../{ => v3}/list_membership_types_spec.rb | 4 +- spec/lib/civicrm/api/v4/base/request_spec.rb | 64 +++++++++++++++ spec/lib/civicrm/api/v4/find_contact_spec.rb | 26 ++++++ spec/lib/civicrm/api/v4/find_group_spec.rb | 25 ++++++ spec/lib/civicrm/api/v4/find_user_spec.rb | 29 +++++++ .../api/v4/list_contact_groups_spec.rb | 18 ++++ .../api/v4/list_contact_memberships_spec.rb | 18 ++++ .../api/v4/list_contacts_in_group_spec.rb | 27 ++++++ spec/lib/civicrm/api/v4/list_groups_spec.rb | 30 +++++++ .../api/v4/list_membership_types_spec.rb | 27 ++++++ .../civicrm_automatic_verification_spec.rb | 0 .../v4/civicrm_automatic_verification_spec.rb | 51 ++++++++++++ .../{ => v3}/event_meeting_parser_spec.rb | 4 +- .../event_registration_parser_spec.rb | 4 +- .../v4/event_meeting_parser_spec.rb | 71 ++++++++++++++++ .../v4/event_registration_parser_spec.rb | 78 ++++++++++++++++++ 60 files changed, 938 insertions(+), 40 deletions(-) rename lib/decidim/civicrm/test/{ => v3}/shared_contexts.rb (76%) create mode 100644 lib/decidim/civicrm/test/v4/shared_contexts.rb rename spec/fixtures/files/{ => v3}/empty_response.json (100%) rename spec/fixtures/files/{ => v3}/error_response.json (100%) rename spec/fixtures/files/{ => v3}/event_valid_response.json (100%) rename spec/fixtures/files/{ => v3}/find_contact_valid_response.json (100%) rename spec/fixtures/files/{ => v3}/find_group_valid_response.json (100%) rename spec/fixtures/files/{ => v3}/find_user_valid_response.json (100%) rename spec/fixtures/files/{ => v3}/list_contact_groups_valid_response.json (100%) rename spec/fixtures/files/{ => v3}/list_contact_memberships_valid_response.json (100%) rename spec/fixtures/files/{ => v3}/list_contacts_in_group_valid_response.json (100%) rename spec/fixtures/files/{ => v3}/list_groups_valid_response.json (100%) rename spec/fixtures/files/{ => v3}/list_membership_types_valid_response.json (100%) rename spec/fixtures/files/{ => v3}/participant_valid_response.json (100%) create mode 100644 spec/fixtures/files/v4/empty_response.json create mode 100644 spec/fixtures/files/v4/error_response.json create mode 100644 spec/fixtures/files/v4/event_valid_response.json create mode 100644 spec/fixtures/files/v4/find_contact_valid_response.json create mode 100644 spec/fixtures/files/v4/find_group_valid_response.json create mode 100644 spec/fixtures/files/v4/find_user_valid_response.json create mode 100644 spec/fixtures/files/v4/list_contact_groups_valid_response.json create mode 100644 spec/fixtures/files/v4/list_contact_memberships_valid_response.json create mode 100644 spec/fixtures/files/v4/list_contacts_in_group_valid_response.json create mode 100644 spec/fixtures/files/v4/list_groups_valid_response.json create mode 100644 spec/fixtures/files/v4/list_membership_types_valid_response.json create mode 100644 spec/fixtures/files/v4/participant_valid_response.json rename spec/forms/admin/{ => v3}/event_meeting_form_spec.rb (92%) create mode 100644 spec/forms/admin/v4/event_meeting_form_spec.rb rename spec/forms/verifications/{ => v3}/civicrm_groups_spec.rb (77%) rename spec/forms/verifications/{ => v3}/civicrm_membership_types_spec.rb (77%) rename spec/forms/verifications/{ => v3}/civicrm_spec.rb (76%) create mode 100644 spec/forms/verifications/v4/civicrm_groups_spec.rb create mode 100644 spec/forms/verifications/v4/civicrm_membership_types_spec.rb create mode 100644 spec/forms/verifications/v4/civicrm_spec.rb rename spec/lib/civicrm/api/{ => v3}/base/request_spec.rb (93%) rename spec/lib/civicrm/api/{ => v3}/find_contact_spec.rb (85%) rename spec/lib/civicrm/api/{ => v3}/find_group_spec.rb (84%) rename spec/lib/civicrm/api/{ => v3}/find_user_spec.rb (90%) rename spec/lib/civicrm/api/{ => v3}/list_contact_groups_spec.rb (68%) rename spec/lib/civicrm/api/{ => v3}/list_contact_memberships_spec.rb (69%) rename spec/lib/civicrm/api/{ => v3}/list_contacts_in_group_spec.rb (80%) rename spec/lib/civicrm/api/{ => v3}/list_groups_spec.rb (83%) rename spec/lib/civicrm/api/{ => v3}/list_membership_types_spec.rb (79%) create mode 100644 spec/lib/civicrm/api/v4/base/request_spec.rb create mode 100644 spec/lib/civicrm/api/v4/find_contact_spec.rb create mode 100644 spec/lib/civicrm/api/v4/find_group_spec.rb create mode 100644 spec/lib/civicrm/api/v4/find_user_spec.rb create mode 100644 spec/lib/civicrm/api/v4/list_contact_groups_spec.rb create mode 100644 spec/lib/civicrm/api/v4/list_contact_memberships_spec.rb create mode 100644 spec/lib/civicrm/api/v4/list_contacts_in_group_spec.rb create mode 100644 spec/lib/civicrm/api/v4/list_groups_spec.rb create mode 100644 spec/lib/civicrm/api/v4/list_membership_types_spec.rb rename spec/lib/civicrm/{ => v3}/civicrm_automatic_verification_spec.rb (100%) create mode 100644 spec/lib/civicrm/v4/civicrm_automatic_verification_spec.rb rename spec/lib/event_parsers/{ => v3}/event_meeting_parser_spec.rb (92%) rename spec/lib/event_parsers/{ => v3}/event_registration_parser_spec.rb (93%) create mode 100644 spec/lib/event_parsers/v4/event_meeting_parser_spec.rb create mode 100644 spec/lib/event_parsers/v4/event_registration_parser_spec.rb diff --git a/lib/decidim/civicrm/api/v4/find_event.rb b/lib/decidim/civicrm/api/v4/find_event.rb index 2436fbd..30e3824 100644 --- a/lib/decidim/civicrm/api/v4/find_event.rb +++ b/lib/decidim/civicrm/api/v4/find_event.rb @@ -26,9 +26,11 @@ def default_query(id) def parsed_response response_hash = response["values"].first - response_hash["event_title"] = response_hash["title"] - response_hash["event_start_date"] = response_hash["start_date"] - response_hash["event_end_date"] = response_hash["end_date"] + if response_hash + response_hash["event_title"] = response_hash["title"] + response_hash["event_start_date"] = response_hash["start_date"] + response_hash["event_end_date"] = response_hash["end_date"] + end { event: response_hash } diff --git a/lib/decidim/civicrm/api/v4/find_participant.rb b/lib/decidim/civicrm/api/v4/find_participant.rb index e09c255..c067408 100644 --- a/lib/decidim/civicrm/api/v4/find_participant.rb +++ b/lib/decidim/civicrm/api/v4/find_participant.rb @@ -26,12 +26,14 @@ def default_query(id) def parsed_response response_hash = response["values"].first - response_hash["display_name"] = response_hash.delete("contact_id.display_name") - response_hash["participant_status"] = response_hash.delete("status_id:label") - response_hash["participant_fee_level"] = response_hash.delete("fee_level") - response_hash["participant_fee_amount"] = response_hash.delete("fee_amount") - response_hash["participant_fee_currency"] = response_hash.delete("fee_currency") - response_hash["participant_id"] = response_hash["id"] + if response_hash + response_hash["display_name"] = response_hash.delete("contact_id.display_name") + response_hash["participant_status"] = response_hash.delete("status_id:label") + response_hash["participant_fee_level"] = response_hash.delete("fee_level") + response_hash["participant_fee_amount"] = response_hash.delete("fee_amount") + response_hash["participant_fee_currency"] = response_hash.delete("fee_currency") + response_hash["participant_id"] = response_hash["id"] + end { participant: response_hash } diff --git a/lib/decidim/civicrm/test/shared_contexts.rb b/lib/decidim/civicrm/test/v3/shared_contexts.rb similarity index 76% rename from lib/decidim/civicrm/test/shared_contexts.rb rename to lib/decidim/civicrm/test/v3/shared_contexts.rb index ebe35c0..cfb8dd4 100644 --- a/lib/decidim/civicrm/test/shared_contexts.rb +++ b/lib/decidim/civicrm/test/v3/shared_contexts.rb @@ -2,8 +2,8 @@ shared_context "with stubs example api" do let(:url) { "https://api.example.org/" } - let(:http_method) { :get } let(:http_status) { 200 } + let(:http_method) { :any } let(:data) do { "values" => [], @@ -17,6 +17,7 @@ end before do + Decidim::Civicrm::Api.config[:version] = Decidim::Civicrm::Api.available_versions[:v3] allow(Decidim::Civicrm::Api).to receive(:url).and_return(url) stub_request(http_method, /api\.example\.org/) .to_return(status: http_status, body: data.to_json, headers: {}) @@ -24,6 +25,9 @@ end shared_examples "returns mapped array ids" do |property| + before do + Decidim::Civicrm::Api.config[:version] = Decidim::Civicrm::Api.available_versions[:v3] + end it "returns an Array with the result" do expect(subject.result).to be_a Array expect(subject.result).to eq(data["values"].map { |v| v[property].to_i }) diff --git a/lib/decidim/civicrm/test/v4/shared_contexts.rb b/lib/decidim/civicrm/test/v4/shared_contexts.rb new file mode 100644 index 0000000..54d13d5 --- /dev/null +++ b/lib/decidim/civicrm/test/v4/shared_contexts.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +shared_context "with stubs example api #{Decidim::Civicrm::Api.available_versions[:v4]}" do + let(:url) { "https://api.example.org/" } + let(:http_status) { 200 } + let(:http_method) { :any } + let(:data) do + { + "values" => [], + "entity" => "", + "action" => "", + "version" => 4, + "count" => 0, + "countFetched" => 0, + "countMatched" => 0 + } + end + let(:params) do + {} + end + + let(:entity) { "" } + let(:action) { "" } + + before do + Decidim::Civicrm::Api.config[:version] = Decidim::Civicrm::Api.available_versions[:v4] + allow(Decidim::Civicrm::Api).to receive(:url).and_return(url) + stub_request(http_method, /api\.example\.org/) + .to_return(status: http_status, body: data.to_json, headers: {}) + end +end + +shared_examples "returns mapped array ids #{Decidim::Civicrm::Api.available_versions[:v4]}" do |property| + before do + Decidim::Civicrm::Api.config[:version] = Decidim::Civicrm::Api.available_versions[:v4] + end + it "returns an Array with the result" do + expect(subject.result).to be_a Array + expect(subject.result).to eq(data["values"].map { |v| v[property].to_i }) + end +end diff --git a/spec/fixtures/files/empty_response.json b/spec/fixtures/files/v3/empty_response.json similarity index 100% rename from spec/fixtures/files/empty_response.json rename to spec/fixtures/files/v3/empty_response.json diff --git a/spec/fixtures/files/error_response.json b/spec/fixtures/files/v3/error_response.json similarity index 100% rename from spec/fixtures/files/error_response.json rename to spec/fixtures/files/v3/error_response.json diff --git a/spec/fixtures/files/event_valid_response.json b/spec/fixtures/files/v3/event_valid_response.json similarity index 100% rename from spec/fixtures/files/event_valid_response.json rename to spec/fixtures/files/v3/event_valid_response.json diff --git a/spec/fixtures/files/find_contact_valid_response.json b/spec/fixtures/files/v3/find_contact_valid_response.json similarity index 100% rename from spec/fixtures/files/find_contact_valid_response.json rename to spec/fixtures/files/v3/find_contact_valid_response.json diff --git a/spec/fixtures/files/find_group_valid_response.json b/spec/fixtures/files/v3/find_group_valid_response.json similarity index 100% rename from spec/fixtures/files/find_group_valid_response.json rename to spec/fixtures/files/v3/find_group_valid_response.json diff --git a/spec/fixtures/files/find_user_valid_response.json b/spec/fixtures/files/v3/find_user_valid_response.json similarity index 100% rename from spec/fixtures/files/find_user_valid_response.json rename to spec/fixtures/files/v3/find_user_valid_response.json diff --git a/spec/fixtures/files/list_contact_groups_valid_response.json b/spec/fixtures/files/v3/list_contact_groups_valid_response.json similarity index 100% rename from spec/fixtures/files/list_contact_groups_valid_response.json rename to spec/fixtures/files/v3/list_contact_groups_valid_response.json diff --git a/spec/fixtures/files/list_contact_memberships_valid_response.json b/spec/fixtures/files/v3/list_contact_memberships_valid_response.json similarity index 100% rename from spec/fixtures/files/list_contact_memberships_valid_response.json rename to spec/fixtures/files/v3/list_contact_memberships_valid_response.json diff --git a/spec/fixtures/files/list_contacts_in_group_valid_response.json b/spec/fixtures/files/v3/list_contacts_in_group_valid_response.json similarity index 100% rename from spec/fixtures/files/list_contacts_in_group_valid_response.json rename to spec/fixtures/files/v3/list_contacts_in_group_valid_response.json diff --git a/spec/fixtures/files/list_groups_valid_response.json b/spec/fixtures/files/v3/list_groups_valid_response.json similarity index 100% rename from spec/fixtures/files/list_groups_valid_response.json rename to spec/fixtures/files/v3/list_groups_valid_response.json diff --git a/spec/fixtures/files/list_membership_types_valid_response.json b/spec/fixtures/files/v3/list_membership_types_valid_response.json similarity index 100% rename from spec/fixtures/files/list_membership_types_valid_response.json rename to spec/fixtures/files/v3/list_membership_types_valid_response.json diff --git a/spec/fixtures/files/participant_valid_response.json b/spec/fixtures/files/v3/participant_valid_response.json similarity index 100% rename from spec/fixtures/files/participant_valid_response.json rename to spec/fixtures/files/v3/participant_valid_response.json diff --git a/spec/fixtures/files/v4/empty_response.json b/spec/fixtures/files/v4/empty_response.json new file mode 100644 index 0000000..4d82fec --- /dev/null +++ b/spec/fixtures/files/v4/empty_response.json @@ -0,0 +1,9 @@ +{ + "values" : [], + "entity" : "", + "action" : "", + "version" : 4, + "count" : 0, + "countFetched" : 0, + "countMatched" : 0 +} \ No newline at end of file diff --git a/spec/fixtures/files/v4/error_response.json b/spec/fixtures/files/v4/error_response.json new file mode 100644 index 0000000..dd39d13 --- /dev/null +++ b/spec/fixtures/files/v4/error_response.json @@ -0,0 +1,3 @@ +{ + "is_error": 1 +} \ No newline at end of file diff --git a/spec/fixtures/files/v4/event_valid_response.json b/spec/fixtures/files/v4/event_valid_response.json new file mode 100644 index 0000000..ce91d6b --- /dev/null +++ b/spec/fixtures/files/v4/event_valid_response.json @@ -0,0 +1,19 @@ +{ + "values": [ + { + "id": 2, + "title": "Quaerat accusamus ducimus odit quibusdam.: Una trobada de prova", + "summary": null, + "description": null, + "event_type_id:label": "Quibusdam", + "start_date": "2020-12-03 18:00:00", + "end_date": "2020-12-03 19:30:00" + } + ], + "entity": "Event", + "action": "get", + "debug": null, + "version": 4, + "count": 1, + "countFetched": 1 +} \ No newline at end of file diff --git a/spec/fixtures/files/v4/find_contact_valid_response.json b/spec/fixtures/files/v4/find_contact_valid_response.json new file mode 100644 index 0000000..a52328d --- /dev/null +++ b/spec/fixtures/files/v4/find_contact_valid_response.json @@ -0,0 +1,15 @@ +{ + "values": [ + { + "id": 30, + "membership.membership_type_id": 3, + "display_name": "Roberto Abela Serra" + } + ], + "entity": "Contact", + "action": "get", + "debug": null, + "version": 4, + "count": 2, + "countFetched": 2 +} \ No newline at end of file diff --git a/spec/fixtures/files/v4/find_group_valid_response.json b/spec/fixtures/files/v4/find_group_valid_response.json new file mode 100644 index 0000000..a24f18c --- /dev/null +++ b/spec/fixtures/files/v4/find_group_valid_response.json @@ -0,0 +1,19 @@ +{ + "values": [ + { + "id": 1, + "name": "Administrators", + "title": "Administradors", + "description": "Als contactes d'aquest grup se'ls-hi assigna el rol administrador.", + "group_type": [ + "1" + ] + } + ], + "entity": "Group", + "action": "get", + "debug": null, + "version": 4, + "count": 1, + "countFetched": 1 +} \ No newline at end of file diff --git a/spec/fixtures/files/v4/find_user_valid_response.json b/spec/fixtures/files/v4/find_user_valid_response.json new file mode 100644 index 0000000..7d55952 --- /dev/null +++ b/spec/fixtures/files/v4/find_user_valid_response.json @@ -0,0 +1,18 @@ +{ + "values": [ + { + "id": 42122, + "uf_match.uf_id": 5, + "uf_match.contact_id": 42122, + "display_name": "Test User", + "email_primary.email": "testuser001@email.com", + "membership.membership_type_id:label": null + } + ], + "entity": "Contact", + "action": "get", + "debug": null, + "version": 4, + "count": 1, + "countFetched": 1 +} \ No newline at end of file diff --git a/spec/fixtures/files/v4/list_contact_groups_valid_response.json b/spec/fixtures/files/v4/list_contact_groups_valid_response.json new file mode 100644 index 0000000..c90c3c6 --- /dev/null +++ b/spec/fixtures/files/v4/list_contact_groups_valid_response.json @@ -0,0 +1,23 @@ +{ + "values": [ + { + "id": 83427, + "group_id": 32 + }, + { + "id": 4692, + "group_id": 83 + }, + { + "id": 28668, + "group_id": 220 + } + ], + "entity": "GroupContact", + "action": "get", + "debug": null, + "version": 4, + "count": 6, + "countFetched": 6, + "countMatched": 6 +} \ No newline at end of file diff --git a/spec/fixtures/files/v4/list_contact_memberships_valid_response.json b/spec/fixtures/files/v4/list_contact_memberships_valid_response.json new file mode 100644 index 0000000..7ba893c --- /dev/null +++ b/spec/fixtures/files/v4/list_contact_memberships_valid_response.json @@ -0,0 +1,19 @@ +{ + "values": [ + { + "id": 420, + "membership_type_id": 3 + }, + { + "id": 42745, + "membership_type_id": 4 + } + ], + "entity": "Membership", + "action": "get", + "debug": null, + "version": 4, + "count": 2, + "countFetched": 2, + "countMatched": 2 +} \ No newline at end of file diff --git a/spec/fixtures/files/v4/list_contacts_in_group_valid_response.json b/spec/fixtures/files/v4/list_contacts_in_group_valid_response.json new file mode 100644 index 0000000..a25751b --- /dev/null +++ b/spec/fixtures/files/v4/list_contacts_in_group_valid_response.json @@ -0,0 +1,23 @@ +{ + "values": [ + { + "id": 17, + "display_name": "User 1" + }, + { + "id": 18, + "display_name": "User 2" + }, + { + "id": 23, + "display_name": "User 3" + } + ], + "entity": "Contact", + "action": "get", + "debug": null, + "version": 4, + "count": 3, + "countFetched": 3, + "countMatched": 3 +} \ No newline at end of file diff --git a/spec/fixtures/files/v4/list_groups_valid_response.json b/spec/fixtures/files/v4/list_groups_valid_response.json new file mode 100644 index 0000000..fbcef8d --- /dev/null +++ b/spec/fixtures/files/v4/list_groups_valid_response.json @@ -0,0 +1,28 @@ +{ + "values": [ + { + "id": "1", + "name": "Administrators", + "title": "Administrators", + "description": "The users in this group are assigned admin privileges.", + "group_type": [ + "1" + ] + }, + { + "id": "2", + "name": "Another_Group", + "title": "Another Group", + "description": "...", + "group_type": [ + "2" + ] + } + ], + "entity": "Group", + "action": "get", + "debug": null, + "count": 2, + "countFetched": 2, + "countMatched": 2 +} \ No newline at end of file diff --git a/spec/fixtures/files/v4/list_membership_types_valid_response.json b/spec/fixtures/files/v4/list_membership_types_valid_response.json new file mode 100644 index 0000000..bfb2720 --- /dev/null +++ b/spec/fixtures/files/v4/list_membership_types_valid_response.json @@ -0,0 +1,27 @@ +{ + "values": [ + { + "id": 1, + "name": "Info Puntual" + }, + { + "id": 2, + "name": "Interessat" + }, + { + "id": 3, + "name": "Inscrit" + }, + { + "id": 4, + "name": "Rebut domiciliat" + } + ], + "entity": "MembershipType", + "action": "get", + "debug": null, + "version": 4, + "count": 4, + "countFetched": 4, + "countMatched": 4 +} \ No newline at end of file diff --git a/spec/fixtures/files/v4/participant_valid_response.json b/spec/fixtures/files/v4/participant_valid_response.json new file mode 100644 index 0000000..2bc64a6 --- /dev/null +++ b/spec/fixtures/files/v4/participant_valid_response.json @@ -0,0 +1,19 @@ +{ + "values": [ + { + "id": 12, + "contact_id": 15070, + "contact_id.display_name": "Test User", + "status_id:label": "Inscrit", + "fee_amount": null, + "fee_level": null, + "fee_currency": null + } + ], + "entity": "Participant", + "action": "get", + "debug": null, + "version": 4, + "count": 1, + "countFetched": 1 +} \ No newline at end of file diff --git a/spec/forms/admin/event_meeting_form_spec.rb b/spec/forms/admin/v3/event_meeting_form_spec.rb similarity index 92% rename from spec/forms/admin/event_meeting_form_spec.rb rename to spec/forms/admin/v3/event_meeting_form_spec.rb index a780e3a..044ab12 100644 --- a/spec/forms/admin/event_meeting_form_spec.rb +++ b/spec/forms/admin/v3/event_meeting_form_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require "spec_helper" -require "decidim/civicrm/test/shared_contexts" +require "decidim/civicrm/test/v3/shared_contexts" module Decidim::Civicrm module Admin @@ -10,7 +10,7 @@ module Admin include_context "with stubs example api" - let(:data) { JSON.parse(file_fixture("event_valid_response.json").read) } + let(:data) { JSON.parse(file_fixture("v3/event_valid_response.json").read) } let(:attributes) do { diff --git a/spec/forms/admin/v4/event_meeting_form_spec.rb b/spec/forms/admin/v4/event_meeting_form_spec.rb new file mode 100644 index 0000000..a81fd8c --- /dev/null +++ b/spec/forms/admin/v4/event_meeting_form_spec.rb @@ -0,0 +1,82 @@ +# frozen_string_literal: true + +require "spec_helper" +require "decidim/civicrm/test/v4/shared_contexts" + +module Decidim::Civicrm + module Admin + describe EventMeetingForm do + subject { described_class.from_params(attributes) } + + include_context "with stubs example api #{Decidim::Civicrm::Api.available_versions[:v4]}" + + let(:data) { JSON.parse(file_fixture("v4/event_valid_response.json").read) } + + let(:attributes) do + { + "event_meeting" => { + "decidim_meeting_id" => decidim_meeting_id, + "civicrm_event_id" => event_id, + "redirect_url" => redirect_url, + "redirect_active" => active + } + } + end + let(:meeting) { create :meeting } + let(:decidim_meeting_id) { meeting.id } + let(:redirect_url) { ::Faker::Internet.url } + let(:active) { true } + let(:event_id) { 123 } + + context "when everything is OK" do + it { is_expected.to be_valid } + end + + context "when no meeting" do + let(:decidim_meeting_id) { nil } + + it { is_expected.to be_invalid } + end + + context "when no url" do + let(:redirect_url) { nil } + + it { is_expected.to be_valid } + + context "and no civicrm_event_id" do + let(:event_id) { nil } + + it { is_expected.to be_invalid } + end + end + + context "when no event_id" do + let(:event_id) { nil } + + it { is_expected.to be_valid } + + context "and no url" do + let(:redirect_url) { nil } + + it { is_expected.to be_invalid } + end + end + + context "when civicrm event does not exist" do + let(:data) do + { + "values" => [], + "entity" => "Event", + "action" => "get", + "version" => 4, + "count" => 0, + "countFetched" => 0, + "countMatched" => 0 + } + end + + it { is_expected.to be_invalid } + end + end + end +end diff --git a/spec/forms/verifications/civicrm_groups_spec.rb b/spec/forms/verifications/v3/civicrm_groups_spec.rb similarity index 77% rename from spec/forms/verifications/civicrm_groups_spec.rb rename to spec/forms/verifications/v3/civicrm_groups_spec.rb index 0a5b520..c62cf7f 100644 --- a/spec/forms/verifications/civicrm_groups_spec.rb +++ b/spec/forms/verifications/v3/civicrm_groups_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require "spec_helper" -require "decidim/civicrm/test/shared_contexts" +require "decidim/civicrm/test/v3/shared_contexts" module Decidim::Civicrm module Verifications @@ -10,7 +10,7 @@ module Verifications include_context "with stubs example api" - let(:data) { JSON.parse(file_fixture("find_user_valid_response.json").read) } + let(:data) { JSON.parse(file_fixture("v3/find_user_valid_response.json").read) } let(:attributes) do { diff --git a/spec/forms/verifications/civicrm_membership_types_spec.rb b/spec/forms/verifications/v3/civicrm_membership_types_spec.rb similarity index 77% rename from spec/forms/verifications/civicrm_membership_types_spec.rb rename to spec/forms/verifications/v3/civicrm_membership_types_spec.rb index ae3d32d..512aae0 100644 --- a/spec/forms/verifications/civicrm_membership_types_spec.rb +++ b/spec/forms/verifications/v3/civicrm_membership_types_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require "spec_helper" -require "decidim/civicrm/test/shared_contexts" +require "decidim/civicrm/test/v3/shared_contexts" module Decidim::Civicrm module Verifications @@ -10,7 +10,7 @@ module Verifications include_context "with stubs example api" - let(:data) { JSON.parse(file_fixture("find_user_valid_response.json").read) } + let(:data) { JSON.parse(file_fixture("v3/find_user_valid_response.json").read) } let(:attributes) do { diff --git a/spec/forms/verifications/civicrm_spec.rb b/spec/forms/verifications/v3/civicrm_spec.rb similarity index 76% rename from spec/forms/verifications/civicrm_spec.rb rename to spec/forms/verifications/v3/civicrm_spec.rb index eedca37..dc603c2 100644 --- a/spec/forms/verifications/civicrm_spec.rb +++ b/spec/forms/verifications/v3/civicrm_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require "spec_helper" -require "decidim/civicrm/test/shared_contexts" +require "decidim/civicrm/test/v3/shared_contexts" module Decidim::Civicrm module Verifications @@ -10,7 +10,7 @@ module Verifications include_context "with stubs example api" - let(:data) { JSON.parse(file_fixture("find_user_valid_response.json").read) } + let(:data) { JSON.parse(file_fixture("v3/find_user_valid_response.json").read) } let(:attributes) do { diff --git a/spec/forms/verifications/v4/civicrm_groups_spec.rb b/spec/forms/verifications/v4/civicrm_groups_spec.rb new file mode 100644 index 0000000..f400d56 --- /dev/null +++ b/spec/forms/verifications/v4/civicrm_groups_spec.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require "spec_helper" +require "decidim/civicrm/test/v4/shared_contexts" + +module Decidim::Civicrm + module Verifications + describe CivicrmGroups do + subject { described_class.from_params(attributes) } + + include_context "with stubs example api #{Decidim::Civicrm::Api.available_versions[:v4]}" + + let(:data) { JSON.parse(file_fixture("v4/find_user_valid_response.json").read) } + + let(:attributes) do + { + "user" => user + } + end + let(:user) { create :user } + + context "when everything is OK" do + it { is_expected.to be_valid } + end + end + end +end diff --git a/spec/forms/verifications/v4/civicrm_membership_types_spec.rb b/spec/forms/verifications/v4/civicrm_membership_types_spec.rb new file mode 100644 index 0000000..b63632d --- /dev/null +++ b/spec/forms/verifications/v4/civicrm_membership_types_spec.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require "spec_helper" +require "decidim/civicrm/test/v4/shared_contexts" + +module Decidim::Civicrm + module Verifications + describe CivicrmMembershipTypes do + subject { described_class.from_params(attributes) } + + include_context "with stubs example api #{Decidim::Civicrm::Api.available_versions[:v4]}" + + let(:data) { JSON.parse(file_fixture("v4/find_user_valid_response.json").read) } + + let(:attributes) do + { + "user" => user + } + end + let(:user) { create :user } + + context "when everything is OK" do + it { is_expected.to be_valid } + end + end + end +end diff --git a/spec/forms/verifications/v4/civicrm_spec.rb b/spec/forms/verifications/v4/civicrm_spec.rb new file mode 100644 index 0000000..419ade2 --- /dev/null +++ b/spec/forms/verifications/v4/civicrm_spec.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require "spec_helper" +require "decidim/civicrm/test/v4/shared_contexts" + +module Decidim::Civicrm + module Verifications + describe Civicrm do + subject { described_class.from_params(attributes) } + + include_context "with stubs example api #{Decidim::Civicrm::Api.available_versions[:v4]}" + + let(:data) { JSON.parse(file_fixture("v4/find_user_valid_response.json").read) } + + let(:attributes) do + { + "user" => user + } + end + let(:user) { create :user } + + context "when everything is OK" do + it { is_expected.to be_valid } + end + end + end +end diff --git a/spec/lib/civicrm/api/base/request_spec.rb b/spec/lib/civicrm/api/v3/base/request_spec.rb similarity index 93% rename from spec/lib/civicrm/api/base/request_spec.rb rename to spec/lib/civicrm/api/v3/base/request_spec.rb index 2a802af..3b3c387 100644 --- a/spec/lib/civicrm/api/base/request_spec.rb +++ b/spec/lib/civicrm/api/v3/base/request_spec.rb @@ -1,10 +1,10 @@ # frozen_string_literal: true require "spec_helper" -require "decidim/civicrm/test/shared_contexts" +require "decidim/civicrm/test/v3/shared_contexts" module Decidim - describe Civicrm::Api::Base::Request, type: :class do + describe Civicrm::Api::Base::V3::Request, type: :class do subject { described_class } include_context "with stubs example api" diff --git a/spec/lib/civicrm/api/find_contact_spec.rb b/spec/lib/civicrm/api/v3/find_contact_spec.rb similarity index 85% rename from spec/lib/civicrm/api/find_contact_spec.rb rename to spec/lib/civicrm/api/v3/find_contact_spec.rb index b6ed30e..ba13ccb 100644 --- a/spec/lib/civicrm/api/find_contact_spec.rb +++ b/spec/lib/civicrm/api/v3/find_contact_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require "spec_helper" -require "decidim/civicrm/test/shared_contexts" +require "decidim/civicrm/test/v3/shared_contexts" module Decidim describe Civicrm::Api::FindContact, type: :class do @@ -9,7 +9,7 @@ module Decidim include_context "with stubs example api" - let(:data) { JSON.parse(file_fixture("find_contact_valid_response.json").read) } + let(:data) { JSON.parse(file_fixture("v3/find_contact_valid_response.json").read) } describe "#result" do it "returns a mapped object" do diff --git a/spec/lib/civicrm/api/find_group_spec.rb b/spec/lib/civicrm/api/v3/find_group_spec.rb similarity index 84% rename from spec/lib/civicrm/api/find_group_spec.rb rename to spec/lib/civicrm/api/v3/find_group_spec.rb index a8b0dac..76bf014 100644 --- a/spec/lib/civicrm/api/find_group_spec.rb +++ b/spec/lib/civicrm/api/v3/find_group_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require "spec_helper" -require "decidim/civicrm/test/shared_contexts" +require "decidim/civicrm/test/v3/shared_contexts" module Decidim describe Civicrm::Api::FindGroup, type: :class do @@ -9,7 +9,7 @@ module Decidim include_context "with stubs example api" - let(:data) { JSON.parse(file_fixture("find_group_valid_response.json").read) } + let(:data) { JSON.parse(file_fixture("v3/find_group_valid_response.json").read) } describe "#result" do it "returns a mapped object" do diff --git a/spec/lib/civicrm/api/find_user_spec.rb b/spec/lib/civicrm/api/v3/find_user_spec.rb similarity index 90% rename from spec/lib/civicrm/api/find_user_spec.rb rename to spec/lib/civicrm/api/v3/find_user_spec.rb index c275ab7..d4842bb 100644 --- a/spec/lib/civicrm/api/find_user_spec.rb +++ b/spec/lib/civicrm/api/v3/find_user_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require "spec_helper" -require "decidim/civicrm/test/shared_contexts" +require "decidim/civicrm/test/v3/shared_contexts" module Decidim describe Civicrm::Api::FindUser, type: :class do @@ -9,7 +9,7 @@ module Decidim include_context "with stubs example api" - let(:data) { JSON.parse(file_fixture("find_user_valid_response.json").read) } + let(:data) { JSON.parse(file_fixture("v3/find_user_valid_response.json").read) } describe "#result" do it "returns a mapped object" do diff --git a/spec/lib/civicrm/api/list_contact_groups_spec.rb b/spec/lib/civicrm/api/v3/list_contact_groups_spec.rb similarity index 68% rename from spec/lib/civicrm/api/list_contact_groups_spec.rb rename to spec/lib/civicrm/api/v3/list_contact_groups_spec.rb index f7c319a..51189e3 100644 --- a/spec/lib/civicrm/api/list_contact_groups_spec.rb +++ b/spec/lib/civicrm/api/v3/list_contact_groups_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require "spec_helper" -require "decidim/civicrm/test/shared_contexts" +require "decidim/civicrm/test/v3/shared_contexts" module Decidim describe Civicrm::Api::ListContactGroups, type: :class do @@ -9,7 +9,7 @@ module Decidim include_context "with stubs example api" - let(:data) { JSON.parse(file_fixture("list_contact_groups_valid_response.json").read) } + let(:data) { JSON.parse(file_fixture("v3/list_contact_groups_valid_response.json").read) } describe "#result" do it_behaves_like "returns mapped array ids", "group_id" diff --git a/spec/lib/civicrm/api/list_contact_memberships_spec.rb b/spec/lib/civicrm/api/v3/list_contact_memberships_spec.rb similarity index 69% rename from spec/lib/civicrm/api/list_contact_memberships_spec.rb rename to spec/lib/civicrm/api/v3/list_contact_memberships_spec.rb index e741bb7..102419a 100644 --- a/spec/lib/civicrm/api/list_contact_memberships_spec.rb +++ b/spec/lib/civicrm/api/v3/list_contact_memberships_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require "spec_helper" -require "decidim/civicrm/test/shared_contexts" +require "decidim/civicrm/test/v3/shared_contexts" module Decidim describe Civicrm::Api::ListContactMemberships, type: :class do @@ -9,7 +9,7 @@ module Decidim include_context "with stubs example api" - let(:data) { JSON.parse(file_fixture("list_contact_memberships_valid_response.json").read) } + let(:data) { JSON.parse(file_fixture("v3/list_contact_memberships_valid_response.json").read) } describe "#result" do it_behaves_like "returns mapped array ids", "membership_type_id" diff --git a/spec/lib/civicrm/api/list_contacts_in_group_spec.rb b/spec/lib/civicrm/api/v3/list_contacts_in_group_spec.rb similarity index 80% rename from spec/lib/civicrm/api/list_contacts_in_group_spec.rb rename to spec/lib/civicrm/api/v3/list_contacts_in_group_spec.rb index c17390b..4f09aad 100644 --- a/spec/lib/civicrm/api/list_contacts_in_group_spec.rb +++ b/spec/lib/civicrm/api/v3/list_contacts_in_group_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require "spec_helper" -require "decidim/civicrm/test/shared_contexts" +require "decidim/civicrm/test/v3/shared_contexts" module Decidim describe Civicrm::Api::ListContactsInGroup, type: :class do @@ -9,7 +9,7 @@ module Decidim include_context "with stubs example api" - let(:data) { JSON.parse(file_fixture("list_contacts_in_group_valid_response.json").read) } + let(:data) { JSON.parse(file_fixture("v3/list_contacts_in_group_valid_response.json").read) } describe "#result" do it "returns array of objects" do diff --git a/spec/lib/civicrm/api/list_groups_spec.rb b/spec/lib/civicrm/api/v3/list_groups_spec.rb similarity index 83% rename from spec/lib/civicrm/api/list_groups_spec.rb rename to spec/lib/civicrm/api/v3/list_groups_spec.rb index 4eb27e0..fd3e943 100644 --- a/spec/lib/civicrm/api/list_groups_spec.rb +++ b/spec/lib/civicrm/api/v3/list_groups_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require "spec_helper" -require "decidim/civicrm/test/shared_contexts" +require "decidim/civicrm/test/v3/shared_contexts" module Decidim describe Civicrm::Api::ListGroups, type: :class do @@ -9,7 +9,7 @@ module Decidim include_context "with stubs example api" - let(:data) { JSON.parse(file_fixture("list_groups_valid_response.json").read) } + let(:data) { JSON.parse(file_fixture("v3/list_groups_valid_response.json").read) } describe "#result" do it "returns array of objects" do diff --git a/spec/lib/civicrm/api/list_membership_types_spec.rb b/spec/lib/civicrm/api/v3/list_membership_types_spec.rb similarity index 79% rename from spec/lib/civicrm/api/list_membership_types_spec.rb rename to spec/lib/civicrm/api/v3/list_membership_types_spec.rb index 340c8c6..cdee941 100644 --- a/spec/lib/civicrm/api/list_membership_types_spec.rb +++ b/spec/lib/civicrm/api/v3/list_membership_types_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require "spec_helper" -require "decidim/civicrm/test/shared_contexts" +require "decidim/civicrm/test/v3/shared_contexts" module Decidim describe Civicrm::Api::ListMembershipTypes, type: :class do @@ -9,7 +9,7 @@ module Decidim include_context "with stubs example api" - let(:data) { JSON.parse(file_fixture("list_membership_types_valid_response.json").read) } + let(:data) { JSON.parse(file_fixture("v3/list_membership_types_valid_response.json").read) } describe "#result" do it "returns array of objects" do diff --git a/spec/lib/civicrm/api/v4/base/request_spec.rb b/spec/lib/civicrm/api/v4/base/request_spec.rb new file mode 100644 index 0000000..9265dc6 --- /dev/null +++ b/spec/lib/civicrm/api/v4/base/request_spec.rb @@ -0,0 +1,64 @@ +# frozen_string_literal: true + +require "spec_helper" +require "decidim/civicrm/test/v4/shared_contexts" + +module Decidim + describe Civicrm::Api::Base::V4::Request, type: :class do + subject { described_class } + + include_context "with stubs example api #{Decidim::Civicrm::Api.available_versions[:v4]}" + + describe "#get petition" do + it "returns a connection instance" do + expect(subject.get(entity, params, action).connection).to be_a(Faraday::Connection) + end + + it "returns a response parsed into a ruby Hash" do + expect(subject.get(entity, params, action).response).to be_a(Hash) + expect(subject.get(entity, params, action).response).to eq(data) + end + + context "when is not succesful" do + let(:http_status) { 500 } + let(:data) do + { + "is_error" => 1, + "version" => 3 + } + end + + it "throws error" do + expect { subject.get(entity, params, action) }.to raise_error Decidim::Civicrm::Error + end + end + end + + describe "#post petition" do + let(:http_method) { :post } + + it "returns a connection instance" do + expect(subject.post(entity, params, action).connection).to be_a(Faraday::Connection) + end + + it "returns a response parsed into a ruby Hash" do + expect(subject.post(entity, params, action).response).to be_a(Hash) + expect(subject.post(entity, params, action).response).to eq(data) + end + + context "when is not succesful" do + let(:http_status) { 500 } + let(:data) do + { + "is_error" => 1, + "version" => 3 + } + end + + it "throws error" do + expect { subject.post(entity, params, action) }.to raise_error Decidim::Civicrm::Error + end + end + end + end +end diff --git a/spec/lib/civicrm/api/v4/find_contact_spec.rb b/spec/lib/civicrm/api/v4/find_contact_spec.rb new file mode 100644 index 0000000..dbe5af7 --- /dev/null +++ b/spec/lib/civicrm/api/v4/find_contact_spec.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +require "spec_helper" +require "decidim/civicrm/test/v4/shared_contexts" + +module Decidim + describe Civicrm::Api::FindContact, type: :class do + subject { described_class.new(42) } + + include_context "with stubs example api #{Decidim::Civicrm::Api.available_versions[:v4]}" + + let(:data) { JSON.parse(file_fixture("v4/find_contact_valid_response.json").read) } + + describe "#result" do + it "returns a mapped object" do + expect(subject.result).to be_a Hash + expect(subject.result[:contact]).to be_a Hash + expect(subject.result[:memberships]).to be_a Array + expect(subject.result[:contact]).to be_a Hash + expect(subject.result[:contact][:id]).to eq(data["values"].first["id"].to_i) + expect(subject.result[:contact][:display_name]).to eq(data["values"].first["display_name"]) + expect(subject.result[:memberships].first).to eq(data["values"].first["membership.membership_type_id"]) + end + end + end +end diff --git a/spec/lib/civicrm/api/v4/find_group_spec.rb b/spec/lib/civicrm/api/v4/find_group_spec.rb new file mode 100644 index 0000000..c2ea766 --- /dev/null +++ b/spec/lib/civicrm/api/v4/find_group_spec.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require "spec_helper" +require "decidim/civicrm/test/v4/shared_contexts" + +module Decidim + describe Civicrm::Api::FindGroup, type: :class do + subject { described_class.new(1) } + + include_context "with stubs example api #{Decidim::Civicrm::Api.available_versions[:v4]}" + + let(:data) { JSON.parse(file_fixture("v4/find_group_valid_response.json").read) } + + describe "#result" do + it "returns a mapped object" do + expect(subject.result).to be_a Hash + expect(subject.result[:group_type]).to eq(data["values"].first["group_type"].map(&:to_i)) + expect(subject.result[:title]).to eq(data["values"].first["title"]) + expect(subject.result[:id]).to eq(data["values"].first["id"].to_i) + expect(subject.result[:name]).to eq(data["values"].first["name"]) + expect(subject.result[:description]).to eq(data["values"].first["description"]) + end + end + end +end diff --git a/spec/lib/civicrm/api/v4/find_user_spec.rb b/spec/lib/civicrm/api/v4/find_user_spec.rb new file mode 100644 index 0000000..7157c3b --- /dev/null +++ b/spec/lib/civicrm/api/v4/find_user_spec.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +require "spec_helper" +require "decidim/civicrm/test/v4/shared_contexts" + +module Decidim + describe Civicrm::Api::FindUser, type: :class do + subject { described_class.new(42) } + + include_context "with stubs example api #{Decidim::Civicrm::Api.available_versions[:v4]}" + + let(:data) { JSON.parse(file_fixture("v4/find_user_valid_response.json").read) } + + describe "#result" do + it "returns a mapped object" do + expect(subject.result).to be_a Hash + expect(subject.result[:user]).to be_a Hash + expect(subject.result[:memberships]).to be_a Array + expect(subject.result[:contact]).to be_a Hash + expect(subject.result[:user][:id]).to eq(data["values"].first["id"].to_i) + expect(subject.result[:user][:name]).to eq(data["values"].first["name"]) + expect(subject.result[:user][:email]).to eq(data["values"].first["email_primary.email"]) + expect(subject.result[:user][:contact_id]).to eq(data["values"].first["id"].to_i) + expect(subject.result[:contact][:id]).to eq(data["values"].first["id"].to_i) + expect(subject.result[:contact][:display_name]).to eq(data["values"].first["display_name"]) + end + end + end +end diff --git a/spec/lib/civicrm/api/v4/list_contact_groups_spec.rb b/spec/lib/civicrm/api/v4/list_contact_groups_spec.rb new file mode 100644 index 0000000..611b391 --- /dev/null +++ b/spec/lib/civicrm/api/v4/list_contact_groups_spec.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +require "spec_helper" +require "decidim/civicrm/test/v4/shared_contexts" + +module Decidim + describe Civicrm::Api::ListContactGroups, type: :class do + subject { described_class.new(1) } + + include_context "with stubs example api #{Decidim::Civicrm::Api.available_versions[:v4]}" + + let(:data) { JSON.parse(file_fixture("v4/list_contact_groups_valid_response.json").read) } + + describe "#result" do + it_behaves_like "returns mapped array ids #{Decidim::Civicrm::Api.available_versions[:v4]}", "group_id" + end + end +end diff --git a/spec/lib/civicrm/api/v4/list_contact_memberships_spec.rb b/spec/lib/civicrm/api/v4/list_contact_memberships_spec.rb new file mode 100644 index 0000000..1e4bf24 --- /dev/null +++ b/spec/lib/civicrm/api/v4/list_contact_memberships_spec.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +require "spec_helper" +require "decidim/civicrm/test/v4/shared_contexts" + +module Decidim + describe Civicrm::Api::ListContactMemberships, type: :class do + subject { described_class.new(1) } + + include_context "with stubs example api #{Decidim::Civicrm::Api.available_versions[:v4]}" + + let(:data) { JSON.parse(file_fixture("v4/list_contact_memberships_valid_response.json").read) } + + describe "#result" do + it_behaves_like "returns mapped array ids #{Decidim::Civicrm::Api.available_versions[:v4]}", "membership_type_id" + end + end +end diff --git a/spec/lib/civicrm/api/v4/list_contacts_in_group_spec.rb b/spec/lib/civicrm/api/v4/list_contacts_in_group_spec.rb new file mode 100644 index 0000000..38a4f0c --- /dev/null +++ b/spec/lib/civicrm/api/v4/list_contacts_in_group_spec.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require "spec_helper" +require "decidim/civicrm/test/v4/shared_contexts" + +module Decidim + describe Civicrm::Api::ListContactsInGroup, type: :class do + subject { described_class.new(1) } + + include_context "with stubs example api #{Decidim::Civicrm::Api.available_versions[:v4]}" + + let(:data) { JSON.parse(file_fixture("v4/list_contacts_in_group_valid_response.json").read) } + + describe "#result" do + it "returns array of objects" do + expect(subject.result).to be_a Array + data["values"].each do |member| + member = { + contact_id: member["id"].to_i, + display_name: member["display_name"] + } + expect(subject.result).to include(member) + end + end + end + end +end diff --git a/spec/lib/civicrm/api/v4/list_groups_spec.rb b/spec/lib/civicrm/api/v4/list_groups_spec.rb new file mode 100644 index 0000000..fbfe18f --- /dev/null +++ b/spec/lib/civicrm/api/v4/list_groups_spec.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +require "spec_helper" +require "decidim/civicrm/test/v4/shared_contexts" + +module Decidim + describe Civicrm::Api::ListGroups, type: :class do + subject { described_class.new } + + include_context "with stubs example api #{Decidim::Civicrm::Api.available_versions[:v4]}" + + let(:data) { JSON.parse(file_fixture("v4/list_groups_valid_response.json").read) } + + describe "#result" do + it "returns array of objects" do + expect(subject.result).to be_a Array + data["values"].each do |group| + group = { + id: group["id"].to_i, + name: group["name"], + title: group["title"], + description: group["description"], + group_type: group["group_type"].map(&:to_i) + } + expect(subject.result).to include(group) + end + end + end + end +end diff --git a/spec/lib/civicrm/api/v4/list_membership_types_spec.rb b/spec/lib/civicrm/api/v4/list_membership_types_spec.rb new file mode 100644 index 0000000..9b72f62 --- /dev/null +++ b/spec/lib/civicrm/api/v4/list_membership_types_spec.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require "spec_helper" +require "decidim/civicrm/test/v4/shared_contexts" + +module Decidim + describe Civicrm::Api::ListMembershipTypes, type: :class do + subject { described_class.new } + + include_context "with stubs example api #{Decidim::Civicrm::Api.available_versions[:v4]}" + + let(:data) { JSON.parse(file_fixture("v4/list_membership_types_valid_response.json").read) } + + describe "#result" do + it "returns array of objects" do + expect(subject.result).to be_a Array + data["values"].each do |group| + group = { + id: group["id"].to_i, + name: group["name"] + } + expect(subject.result).to include(group) + end + end + end + end +end diff --git a/spec/lib/civicrm/civicrm_automatic_verification_spec.rb b/spec/lib/civicrm/v3/civicrm_automatic_verification_spec.rb similarity index 100% rename from spec/lib/civicrm/civicrm_automatic_verification_spec.rb rename to spec/lib/civicrm/v3/civicrm_automatic_verification_spec.rb diff --git a/spec/lib/civicrm/v4/civicrm_automatic_verification_spec.rb b/spec/lib/civicrm/v4/civicrm_automatic_verification_spec.rb new file mode 100644 index 0000000..db476d1 --- /dev/null +++ b/spec/lib/civicrm/v4/civicrm_automatic_verification_spec.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +require "spec_helper" + +# rubocop:disable RSpec/DescribeClass +describe "Automatic verification after oauth sign up" do + context "when a user is registered with omniauth" do + let!(:user) { create(:user) } + + it "runs the OmniauthContactSyncJob" do + expect do + ActiveSupport::Notifications.publish( + "decidim.user.omniauth_registration", + user_id: user.id, + identity_id: 1234, + provider: "civicrm", + uid: "aaa", + email: user.email, + name: "Civicrm User", + nickname: "civicrm_user", + avatar_url: "http://www.example.com/foo.jpg", + raw_data: {} + ) + end.to have_enqueued_job(Decidim::Civicrm::OmniauthContactSyncJob) + end + end + + context "when a contact is updated" do + let!(:contact) { create(:civicrm_contact) } + + it "runs the AutoVerificationJob" do + expect do + ActiveSupport::Notifications.publish( + "decidim.civicrm.contact.updated", + contact_id: contact.id + ) + end.to have_enqueued_job(Decidim::Civicrm::AutoVerificationJob) + end + + it "runs the JoinContactToParticipatorySpacesJob" do + expect do + ActiveSupport::Notifications.publish( + "decidim.civicrm.contact.updated", + contact_id: contact.id + ) + end.to have_enqueued_job(Decidim::Civicrm::JoinContactToParticipatorySpacesJob) + end + end +end + +# rubocop:enable RSpec/DescribeClass diff --git a/spec/lib/event_parsers/event_meeting_parser_spec.rb b/spec/lib/event_parsers/v3/event_meeting_parser_spec.rb similarity index 92% rename from spec/lib/event_parsers/event_meeting_parser_spec.rb rename to spec/lib/event_parsers/v3/event_meeting_parser_spec.rb index 0b9429f..2fc54b8 100644 --- a/spec/lib/event_parsers/event_meeting_parser_spec.rb +++ b/spec/lib/event_parsers/v3/event_meeting_parser_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require "spec_helper" -require "decidim/civicrm/test/shared_contexts" +require "decidim/civicrm/test/v3/shared_contexts" module Decidim::Civicrm describe EventParsers::EventMeetingParser, type: :class do @@ -9,7 +9,7 @@ module Decidim::Civicrm include_context "with stubs example api" - let(:data) { JSON.parse(file_fixture("event_valid_response.json").read) } + let(:data) { JSON.parse(file_fixture("v3/event_valid_response.json").read) } let(:meeting) { create :meeting } let(:json) do diff --git a/spec/lib/event_parsers/event_registration_parser_spec.rb b/spec/lib/event_parsers/v3/event_registration_parser_spec.rb similarity index 93% rename from spec/lib/event_parsers/event_registration_parser_spec.rb rename to spec/lib/event_parsers/v3/event_registration_parser_spec.rb index 722d8fa..37bc283 100644 --- a/spec/lib/event_parsers/event_registration_parser_spec.rb +++ b/spec/lib/event_parsers/v3/event_registration_parser_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require "spec_helper" -require "decidim/civicrm/test/shared_contexts" +require "decidim/civicrm/test/v3/shared_contexts" module Decidim::Civicrm describe EventParsers::EventRegistrationParser, type: :class do @@ -9,7 +9,7 @@ module Decidim::Civicrm include_context "with stubs example api" - let(:data) { JSON.parse(file_fixture("participant_valid_response.json").read) } + let(:data) { JSON.parse(file_fixture("v3/participant_valid_response.json").read) } let(:registration) { create :registration } let(:organization) { meeting.organization } diff --git a/spec/lib/event_parsers/v4/event_meeting_parser_spec.rb b/spec/lib/event_parsers/v4/event_meeting_parser_spec.rb new file mode 100644 index 0000000..7462b79 --- /dev/null +++ b/spec/lib/event_parsers/v4/event_meeting_parser_spec.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +require "spec_helper" +require "decidim/civicrm/test/v4/shared_contexts" + +module Decidim::Civicrm + describe EventParsers::EventMeetingParser, type: :class do + subject { described_class.new(meeting) } + + include_context "with stubs example api #{Decidim::Civicrm::Api.available_versions[:v4]}" + + let(:data) { JSON.parse(file_fixture("v4/event_valid_response.json").read) } + + let(:meeting) { create :meeting } + let(:json) do + { + start_date: meeting.start_time.strftime("%Y%m%d"), + end_date: meeting.end_time.strftime("%Y%m%d"), + title: "#{meeting.participatory_space.title["ca"]}: #{meeting.title["ca"]}", + template_id: template_id + } + end + let(:template_id) { 666 } + let(:attributes) do + { template_id: template_id } + end + let(:parser_data) do + { + entity: "Event", + action: "create", + json: 1 + } + end + let(:result) do + { + "id" => "123" + } + end + + before do + subject.result = result + allow(Decidim::Civicrm).to receive(:auto_sync_meetings_event_attributes).and_return(attributes) + end + + it "is valid" do + expect(subject.valid?).to be(true) + end + + it "returns data" do + expect(subject.json).to eq(json) + expect(subject.data).to eq(parser_data.merge(json)) + end + + it "saves data" do + expect { subject.save! }.to change(Decidim::Civicrm::EventMeeting, :count).by(1) + end + + context "when no result" do + let(:result) do + { + "id" => "" + } + end + + it "don't save data" do + expect { subject.save! }.to raise_error ActiveRecord::RecordInvalid + expect(Decidim::Civicrm::EventMeeting.count).to eq(0) + end + end + end +end diff --git a/spec/lib/event_parsers/v4/event_registration_parser_spec.rb b/spec/lib/event_parsers/v4/event_registration_parser_spec.rb new file mode 100644 index 0000000..4b8de11 --- /dev/null +++ b/spec/lib/event_parsers/v4/event_registration_parser_spec.rb @@ -0,0 +1,78 @@ +# frozen_string_literal: true + +require "spec_helper" +require "decidim/civicrm/test/v4/shared_contexts" + +module Decidim::Civicrm + describe EventParsers::EventRegistrationParser, type: :class do + subject { described_class.new(registration) } + + include_context "with stubs example api #{Decidim::Civicrm::Api.available_versions[:v4]}" + + let(:data) { JSON.parse(file_fixture("v4/participant_valid_response.json").read) } + + let(:registration) { create :registration } + let(:organization) { meeting.organization } + let(:meeting) { registration.meeting } + let!(:authorization) { create :authorization, name: "civicrm", user: registration.user, metadata: { contact_id: contact_id } } + let!(:event_meeting) { create :civicrm_event_meeting, organization: organization, meeting: meeting, civicrm_event_id: event_id } + let(:contact_id) { 451 } + let(:event_id) { 2345 } + let(:json) do + { + event_id: event_id, + contact_id: contact_id + } + end + let(:parser_data) do + { + entity: "Participant", + action: "create", + json: 1 + } + end + let(:result) do + { + "id" => "123" + } + end + + before do + subject.result = result + end + + it "is valid" do + expect(subject.valid?).to be(true) + end + + it "returns data" do + expect(subject.json).to eq(json) + expect(subject.data).to eq(parser_data.merge(json)) + end + + it "saves data" do + expect { subject.save! }.to change(Decidim::Civicrm::EventRegistration, :count).by(1) + end + + context "when no result" do + let(:result) do + { + "id" => "" + } + end + + it "don't save data" do + expect { subject.save! }.to raise_error ActiveRecord::RecordInvalid + expect(Decidim::Civicrm::EventRegistration.count).to eq(0) + end + end + + context "when no contact_id" do + let(:contact_id) { nil } + + it "is invalid" do + expect(subject.valid?).to be(false) + end + end + end +end From 85c669fb0e80da038561e610cb3bd62a8de37c9f Mon Sep 17 00:00:00 2001 From: Alejandro Rueda Date: Fri, 5 Jul 2024 12:34:59 +0200 Subject: [PATCH 6/9] Pinning chrome version to v119 --- .github/workflows/test.yml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 94362bd..7faf289 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -39,6 +39,22 @@ jobs: ruby-version: ${{ env.RUBY_VERSION }} bundler-cache: true + - uses: nanasess/setup-chromedriver@v2 + with: + chromedriver-version: 119.0.6045.105 + + - name: List Chrome + shell: "bash" + run: apt list --installed | grep chrome + + - name: Remove Chrome + shell: "bash" + run: sudo apt remove google-chrome-stable + + - uses: browser-actions/setup-chrome@v1 + with: + chrome-version: 119.0.6045.105 + - name: Setup Database run: bundle exec rake test_app From 136273dc51f2e552af78071d13167d58caf394e3 Mon Sep 17 00:00:00 2001 From: Alejandro Rueda Date: Wed, 17 Jul 2024 12:25:50 +0200 Subject: [PATCH 7/9] Improved parsers methods and requires ordered --- lib/decidim/civicrm/api.rb | 54 +++++++++++++++--------------- lib/omniauth/strategies/civicrm.rb | 12 +++---- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/lib/decidim/civicrm/api.rb b/lib/decidim/civicrm/api.rb index 9192580..7df0a5e 100644 --- a/lib/decidim/civicrm/api.rb +++ b/lib/decidim/civicrm/api.rb @@ -2,47 +2,47 @@ require_relative "api/base/base_query" require_relative "api/base/v3/base_query" -require_relative "api/base/v4/base_query" require_relative "api/base/v3/find_query" -require_relative "api/base/v4/find_query" require_relative "api/base/v3/list_query" -require_relative "api/base/v4/list_query" -require_relative "api/request" require_relative "api/base/v3/request" +require_relative "api/base/v4/base_query" +require_relative "api/base/v4/find_query" +require_relative "api/base/v4/list_query" require_relative "api/base/v4/request" require_relative "api/find_contact" -require_relative "api/v3/find_contact" -require_relative "api/v4/find_contact" +require_relative "api/find_event" require_relative "api/find_group" -require_relative "api/v3/find_group" -require_relative "api/v4/find_group" +require_relative "api/find_participant" require_relative "api/find_user" -require_relative "api/v3/find_user" -require_relative "api/v4/find_user" -require_relative "api/find_event" +require_relative "api/list_contact_groups" +require_relative "api/list_contact_memberships" +require_relative "api/list_contacts_in_group" +require_relative "api/list_groups" +require_relative "api/list_membership_types" +require_relative "api/participants_in_event" +require_relative "api/request" +require_relative "api/v3/find_contact" require_relative "api/v3/find_event" -require_relative "api/v4/find_event" -require_relative "api/find_participant" +require_relative "api/v3/find_group" require_relative "api/v3/find_participant" -require_relative "api/v4/find_participant" -require_relative "api/participants_in_event" -require_relative "api/v3/participants_in_event" -require_relative "api/v4/participants_in_event" -require_relative "api/list_contact_groups" +require_relative "api/v3/find_user" require_relative "api/v3/list_contact_groups" -require_relative "api/v4/list_contact_groups" -require_relative "api/list_contacts_in_group" -require_relative "api/v3/list_contacts_in_group" -require_relative "api/v4/list_contacts_in_group" -require_relative "api/list_contact_memberships" require_relative "api/v3/list_contact_memberships" -require_relative "api/v4/list_contact_memberships" -require_relative "api/list_groups" +require_relative "api/v3/list_contacts_in_group" require_relative "api/v3/list_groups" -require_relative "api/v4/list_groups" -require_relative "api/list_membership_types" require_relative "api/v3/list_membership_types" +require_relative "api/v3/participants_in_event" +require_relative "api/v4/find_contact" +require_relative "api/v4/find_event" +require_relative "api/v4/find_group" +require_relative "api/v4/find_participant" +require_relative "api/v4/find_user" +require_relative "api/v4/list_contact_groups" +require_relative "api/v4/list_contact_memberships" +require_relative "api/v4/list_contacts_in_group" +require_relative "api/v4/list_groups" require_relative "api/v4/list_membership_types" +require_relative "api/v4/participants_in_event" module Decidim module Civicrm diff --git a/lib/omniauth/strategies/civicrm.rb b/lib/omniauth/strategies/civicrm.rb index 1362e67..37ac3ff 100644 --- a/lib/omniauth/strategies/civicrm.rb +++ b/lib/omniauth/strategies/civicrm.rb @@ -20,8 +20,8 @@ class Civicrm < OmniAuth::Strategies::OAuth2 info do { - name: parsed_name(extra[:contact][:display_name]), - nickname: parsed_nickname(raw_info["preferred_username"]), + name: parsed_name, + nickname: parsed_nickname, email: raw_info["email"], image: raw_info["picture"] } @@ -56,12 +56,12 @@ def civicrm_info @civicrm_info ||= ::Decidim::Civicrm::Api::FindUser.new(uid).result end - def parsed_name(expr) - expr.gsub(/[^\w\-]/, "") + def parsed_name + extra[:contact][:display_name].gsub(/[<>?%&^*#@()\[\]=+:;"{}\\|]/, "") end - def parsed_nickname(expr) - expr.gsub(/[<>?%&$\^*#@()\[\]=+:;., "{}\\|]/, "") + def parsed_nickname + Decidim::UserBaseEntity.nicknamize(raw_info["preferred_username"]) end end end From 501de42951a7aa81ad69e9c8490ea896c9f929e8 Mon Sep 17 00:00:00 2001 From: Alejandro Rueda Date: Wed, 17 Jul 2024 12:26:54 +0200 Subject: [PATCH 8/9] Added test for oauth response parsers --- .rubocop.yml | 4 ++++ spec/omni_auth/strategies/civicrm_spec.rb | 27 +++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 spec/omni_auth/strategies/civicrm_spec.rb diff --git a/.rubocop.yml b/.rubocop.yml index b2dc6b1..748cda2 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1 +1,5 @@ inherit_from: https://raw.githubusercontent.com/decidim/decidim/release/0.27-stable/.rubocop.yml + +RSpec/AnyInstance: + Exclude: + - 'spec/omni_auth/strategies/civicrm_spec.rb' \ No newline at end of file diff --git a/spec/omni_auth/strategies/civicrm_spec.rb b/spec/omni_auth/strategies/civicrm_spec.rb new file mode 100644 index 0000000..bb52c15 --- /dev/null +++ b/spec/omni_auth/strategies/civicrm_spec.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe OmniAuth::Strategies::Civicrm do + subject { described_class.new(nil, {}) } + + let(:extra) { { contact: { display_name: "John+Doe" } } } + let(:raw_info) { { "preferred_username" => "john.doe" } } + + before do + allow_any_instance_of(described_class).to receive(:extra).and_return(extra) + allow_any_instance_of(described_class).to receive(:raw_info).and_return(raw_info) + end + + context "when the name is invalid" do + it "is sanitized" do + expect(subject.parsed_name).to eq("JohnDoe") + end + end + + context "when the nickname is invalid" do + it "is sanitized" do + expect(subject.parsed_nickname).to eq("john_doe") + end + end +end From 1281f7cc535db8c43c5ab2454ab3d9c98080a696 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Bol=C3=ADvar?= Date: Mon, 29 Jul 2024 17:16:15 +0200 Subject: [PATCH 9/9] Fix namespace --- lib/omniauth/strategies/civicrm.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/omniauth/strategies/civicrm.rb b/lib/omniauth/strategies/civicrm.rb index 37ac3ff..9ee8935 100644 --- a/lib/omniauth/strategies/civicrm.rb +++ b/lib/omniauth/strategies/civicrm.rb @@ -61,7 +61,7 @@ def parsed_name end def parsed_nickname - Decidim::UserBaseEntity.nicknamize(raw_info["preferred_username"]) + ::Decidim::UserBaseEntity.nicknamize(raw_info["preferred_username"]) end end end