From 583d567d67d60d7145ac2ca3e8960809576f5362 Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Tue, 19 Jan 2016 01:31:34 +0100 Subject: [PATCH] bump rubocop and pronto-rubocop make some refactorings for new cops --- .rubocop.yml | 15 +- Gemfile | 4 +- Gemfile.lock | 17 +-- .../webfinger_controller.rb | 4 +- lib/diaspora_federation/discovery/h_card.rb | 8 +- .../discovery/host_meta.rb | 2 +- .../discovery/web_finger.rb | 16 +-- .../discovery/xrd_document.rb | 8 +- lib/diaspora_federation/salmon.rb | 2 +- lib/diaspora_federation/salmon/aes.rb | 2 +- .../salmon/magic_envelope.rb | 14 +- lib/diaspora_federation/salmon/slap.rb | 2 +- lib/diaspora_federation/salmon/xml_payload.rb | 2 - lib/diaspora_federation/signing.rb | 5 +- lib/diaspora_federation/test/factories.rb | 4 +- lib/diaspora_federation/version.rb | 2 +- .../h_card_controller_spec.rb | 7 +- .../receive_controller_spec.rb | 24 ++-- .../webfinger_controller_spec.rb | 7 +- .../entities/relayable_retraction_spec.rb | 47 +++--- .../entities/relayable_spec.rb | 135 +++++++++--------- .../entities/signed_retraction_spec.rb | 12 +- .../federation/receiver/private_spec.rb | 17 ++- .../federation/receiver/public_spec.rb | 16 +-- .../federation/sender/hydra_wrapper_spec.rb | 6 +- spec/lib/diaspora_federation/signing_spec.rb | 2 +- .../validators/conversation_validator_spec.rb | 4 +- .../rules/diaspora_id_count_spec.rb | 2 +- spec/support/shared_validator_specs.rb | 2 +- .../initializers/diaspora_federation.rb | 14 +- 30 files changed, 191 insertions(+), 211 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 841fbfe..e4bc060 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,10 +1,12 @@ AllCops: - RunRailsCops: true Exclude: - "bin/**/*" - "test/dummy/bin/**/*" - "test/dummy/db/**/*" +Rails: + Enabled: true + # Commonly used screens these days easily fit more than 80 characters. Metrics/LineLength: Max: 120 @@ -19,6 +21,8 @@ Metrics/MethodLength: # The guiding principle of classes is SRP, SRP can't be accurately measured by LoC Metrics/ClassLength: Max: 1500 +Metrics/ModuleLength: + Max: 1500 # No space makes the method definition shorter and differentiates # from a regular assignment. @@ -144,10 +148,6 @@ Style/DotPosition: # diaspora_federation rules -Rails/TimeZone: - Exclude: - - "spec/**/*" - Metrics/CyclomaticComplexity: Max: 10 Metrics/PerceivedComplexity: @@ -157,8 +157,9 @@ Metrics/AbcSize: Exclude: - "lib/diaspora_federation/test/factories.rb" -Metrics/ModuleLength: - Max: 1500 +Rails/TimeZone: + Exclude: + - "spec/lib/**/*" Style/SingleLineMethods: Exclude: diff --git a/Gemfile b/Gemfile index 934bbd4..d00d45c 100644 --- a/Gemfile +++ b/Gemfile @@ -17,9 +17,9 @@ end group :development do # code style - gem "rubocop", "0.35.1" + gem "rubocop", "0.36.0" gem "pronto", "0.5.3" - gem "pronto-rubocop", "0.5.0", require: false + gem "pronto-rubocop", "0.5.2", require: false # debugging gem "pry" diff --git a/Gemfile.lock b/Gemfile.lock index 364a39e..0a45a7e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -55,8 +55,6 @@ GEM addressable (2.3.8) arel (6.0.3) ast (2.2.0) - astrolabe (1.3.1) - parser (~> 2.2) builder (3.2.2) byebug (8.2.1) codeclimate-test-reporter (0.4.8) @@ -156,9 +154,9 @@ GEM octokit (~> 4.1.0) rugged (~> 0.23.0) thor (~> 0.19.0) - pronto-rubocop (0.5.0) + pronto-rubocop (0.5.2) pronto (~> 0.5.0) - rubocop (~> 0.35.0) + rubocop (~> 0.36.0) pry (0.10.3) coderay (~> 1.1.0) method_source (~> 0.8.1) @@ -223,13 +221,11 @@ GEM rspec-mocks (~> 3.4.0) rspec-support (~> 3.4.0) rspec-support (3.4.1) - rubocop (0.35.1) - astrolabe (~> 1.3) - parser (>= 2.2.3.0, < 3.0) + rubocop (0.36.0) + parser (>= 2.3.0.0, < 3.0) powerpack (~> 0.1) rainbow (>= 1.99.1, < 3.0) ruby-progressbar (~> 1.7) - tins (<= 1.6.0) ruby-progressbar (1.7.5) rugged (0.23.3) safe_yaml (1.0.4) @@ -263,7 +259,6 @@ GEM terminal-table (1.5.2) thor (0.19.1) thread_safe (0.3.5) - tins (1.6.0) typhoeus (1.0.1) ethon (>= 0.8.0) tzinfo (1.2.2) @@ -293,13 +288,13 @@ DEPENDENCIES logging-rails (= 0.5.0) nyan-cat-formatter pronto (= 0.5.3) - pronto-rubocop (= 0.5.0) + pronto-rubocop (= 0.5.2) pry pry-byebug pry-debundle rspec-collection_matchers (~> 1.1.2) rspec-rails (~> 3.4.0) - rubocop (= 0.35.1) + rubocop (= 0.36.0) simplecov (= 0.11.1) simplecov-rcov (= 0.2.3) spring diff --git a/app/controllers/diaspora_federation/webfinger_controller.rb b/app/controllers/diaspora_federation/webfinger_controller.rb index 0c5a8b1..f4b0d8d 100644 --- a/app/controllers/diaspora_federation/webfinger_controller.rb +++ b/app/controllers/diaspora_federation/webfinger_controller.rb @@ -45,14 +45,14 @@ module DiasporaFederation end end - private - # creates the host-meta xml with the configured server_uri and caches it # @return [String] XML string def self.host_meta_xml @host_meta_xml ||= Discovery::HostMeta.from_base_url(DiasporaFederation.server_uri.to_s).to_xml end + private + def find_person_webfinger(query) DiasporaFederation.callbacks.trigger(:fetch_person_for_webfinger, query.strip.downcase.sub("acct:", "")) end diff --git a/lib/diaspora_federation/discovery/h_card.rb b/lib/diaspora_federation/discovery/h_card.rb index 185f8e3..305c3b2 100644 --- a/lib/diaspora_federation/discovery/h_card.rb +++ b/lib/diaspora_federation/discovery/h_card.rb @@ -121,7 +121,7 @@ module DiasporaFederation photo_small: ".entity_photo_small .photo[src]", key: ".key", searchable: ".searchable" - } + }.freeze # Create the HTML string from the current HCard instance # @return [String] HTML string @@ -209,13 +209,13 @@ module DiasporaFederation # # @param container [Nokogiri::XML::Element] parent element for added property HTML # @param name [Symbol] property name - # @param block [Proc] block returning an element - def add_property(container, name, &block) + # @yield [Nokogiri::HTML::Builder] html builder + def add_property(container, name) Nokogiri::HTML::Builder.with(container) do |html| html.dl(class: "entity_#{name}") { html.dt(name.to_s.capitalize) html.dd { - block.call(html) + yield html } } end diff --git a/lib/diaspora_federation/discovery/host_meta.rb b/lib/diaspora_federation/discovery/host_meta.rb index 255c519..ae2f1b9 100644 --- a/lib/diaspora_federation/discovery/host_meta.rb +++ b/lib/diaspora_federation/discovery/host_meta.rb @@ -26,7 +26,7 @@ module DiasporaFederation end # URL fragment to append to the base URL - WEBFINGER_SUFFIX = "/webfinger?q={uri}" + WEBFINGER_SUFFIX = "/webfinger?q={uri}".freeze # Returns the WebFinger URL that was used to build this instance (either from # xml or by giving a base URL). diff --git a/lib/diaspora_federation/discovery/web_finger.rb b/lib/diaspora_federation/discovery/web_finger.rb index 37ea36c..56c96ba 100644 --- a/lib/diaspora_federation/discovery/web_finger.rb +++ b/lib/diaspora_federation/discovery/web_finger.rb @@ -104,35 +104,35 @@ module DiasporaFederation property :public_key # +hcard_url+ link relation - REL_HCARD = "http://microformats.org/profile/hcard" + REL_HCARD = "http://microformats.org/profile/hcard".freeze # +seed_url+ link relation - REL_SEED = "http://joindiaspora.com/seed_location" + REL_SEED = "http://joindiaspora.com/seed_location".freeze # @deprecated This should be a +Property+ or moved to the +hCard+, but +Link+ # is inappropriate according to the specification (will affect older # Diaspora* installations). # +guid+ link relation - REL_GUID = "http://joindiaspora.com/guid" + REL_GUID = "http://joindiaspora.com/guid".freeze # +profile_url+ link relation. # @note This might just as well be an +Alias+ instead of a +Link+. - REL_PROFILE = "http://webfinger.net/rel/profile-page" + REL_PROFILE = "http://webfinger.net/rel/profile-page".freeze # +atom_url+ link relation - REL_ATOM = "http://schemas.google.com/g/2010#updates-from" + REL_ATOM = "http://schemas.google.com/g/2010#updates-from".freeze # +salmon_url+ link relation - REL_SALMON = "salmon" + REL_SALMON = "salmon".freeze # +subscribe_url+ link relation - REL_SUBSCRIBE = "http://ostatus.org/schema/1.0/subscribe" + REL_SUBSCRIBE = "http://ostatus.org/schema/1.0/subscribe".freeze # @deprecated This should be a +Property+ or moved to the +hcard+, but +Link+ # is inappropriate according to the specification (will affect older # Diaspora* installations). # +pubkey+ link relation - REL_PUBKEY = "diaspora-public-key" + REL_PUBKEY = "diaspora-public-key".freeze # Create the XML string from the current WebFinger instance # @return [String] XML string diff --git a/lib/diaspora_federation/discovery/xrd_document.rb b/lib/diaspora_federation/discovery/xrd_document.rb index 57039e9..3f952d7 100644 --- a/lib/diaspora_federation/discovery/xrd_document.rb +++ b/lib/diaspora_federation/discovery/xrd_document.rb @@ -29,13 +29,13 @@ module DiasporaFederation # @see http://docs.oasis-open.org/xri/xrd/v1.0/xrd-1.0.html Extensible Resource Descriptor (XRD) Version 1.0 class XrdDocument # xml namespace url - XMLNS = "http://docs.oasis-open.org/ns/xri/xrd-1.0" + XMLNS = "http://docs.oasis-open.org/ns/xri/xrd-1.0".freeze # +Link+ element attributes - LINK_ATTRS = %i(rel type href template) + LINK_ATTRS = %i(rel type href template).freeze # format string for datetime (+Expires+ element) - DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ" + DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ".freeze # The element contains a time value which specifies the instant at # and after which the document has expired and SHOULD NOT be used. @@ -107,7 +107,7 @@ module DiasporaFederation private - NS = {xrd: XMLNS} + NS = {xrd: XMLNS}.freeze def add_aliases_to(xml) @aliases.each do |a| diff --git a/lib/diaspora_federation/salmon.rb b/lib/diaspora_federation/salmon.rb index 5c68aaf..dd8f288 100644 --- a/lib/diaspora_federation/salmon.rb +++ b/lib/diaspora_federation/salmon.rb @@ -3,7 +3,7 @@ module DiasporaFederation # {http://www.salmon-protocol.org/ Salmon Protocol}. module Salmon # XML namespace url - XMLNS = "https://joindiaspora.com/protocol" + XMLNS = "https://joindiaspora.com/protocol".freeze end end diff --git a/lib/diaspora_federation/salmon/aes.rb b/lib/diaspora_federation/salmon/aes.rb index 2e7fdc4..d33c217 100644 --- a/lib/diaspora_federation/salmon/aes.rb +++ b/lib/diaspora_federation/salmon/aes.rb @@ -3,7 +3,7 @@ module DiasporaFederation # class for AES encryption and decryption class AES # OpenSSL aes cipher definition - CIPHER = "AES-256-CBC" + CIPHER = "AES-256-CBC".freeze # generates a random AES key and initialization vector # @return [Hash] { key: "...", iv: "..." } diff --git a/lib/diaspora_federation/salmon/magic_envelope.rb b/lib/diaspora_federation/salmon/magic_envelope.rb index ad386ac..bfe84ff 100644 --- a/lib/diaspora_federation/salmon/magic_envelope.rb +++ b/lib/diaspora_federation/salmon/magic_envelope.rb @@ -25,19 +25,19 @@ module DiasporaFederation attr_reader :payload # encoding used for the payload data - ENCODING = "base64url" + ENCODING = "base64url".freeze # algorithm used for signing the payload data - ALGORITHM = "RSA-SHA256" + ALGORITHM = "RSA-SHA256".freeze # mime type describing the payload data - DATA_TYPE = "application/xml" + DATA_TYPE = "application/xml".freeze # digest instance used for signing DIGEST = OpenSSL::Digest::SHA256.new # XML namespace url - XMLNS = "http://salmon-protocol.org/ns/magic-env" + XMLNS = "http://salmon-protocol.org/ns/magic-env".freeze # Creates a new instance of MagicEnvelope. # @@ -128,10 +128,7 @@ module DiasporaFederation # # @return [String] the signature def signature - subject = self.class.sig_subject([@payload, - DATA_TYPE, - ENCODING, - ALGORITHM]) + subject = MagicEnvelope.send(:sig_subject, [@payload, DATA_TYPE, ENCODING, ALGORITHM]) @rsa_privkey.sign(DIGEST, subject) end @@ -168,6 +165,7 @@ module DiasporaFederation def self.sig_subject(data_arr) data_arr.map {|i| Base64.urlsafe_encode64(i) }.join(".") end + private_class_method :sig_subject # @param [Nokogiri::XML::Element] magic_env magic envelope XML # @return [Boolean] diff --git a/lib/diaspora_federation/salmon/slap.rb b/lib/diaspora_federation/salmon/slap.rb index d4b6474..aa3ead4 100644 --- a/lib/diaspora_federation/salmon/slap.rb +++ b/lib/diaspora_federation/salmon/slap.rb @@ -34,7 +34,7 @@ module DiasporaFederation attr_accessor :author_id # Namespaces - NS = {d: Salmon::XMLNS, me: MagicEnvelope::XMLNS} + NS = {d: Salmon::XMLNS, me: MagicEnvelope::XMLNS}.freeze # Returns new instance of the Entity that is contained within the XML of # this Slap. diff --git a/lib/diaspora_federation/salmon/xml_payload.rb b/lib/diaspora_federation/salmon/xml_payload.rb index e69a8bf..99b9c55 100644 --- a/lib/diaspora_federation/salmon/xml_payload.rb +++ b/lib/diaspora_federation/salmon/xml_payload.rb @@ -54,8 +54,6 @@ module DiasporaFederation populate_entity(klass, data) end - private - # @param [Nokogiri::XML::Element] element def self.wrap_valid?(element) (element.name == "XML" && !element.at_xpath("post").nil? && diff --git a/lib/diaspora_federation/signing.rb b/lib/diaspora_federation/signing.rb index 1992fa8..8daaa2a 100644 --- a/lib/diaspora_federation/signing.rb +++ b/lib/diaspora_federation/signing.rb @@ -43,14 +43,13 @@ module DiasporaFederation validity end - private - # @param [Hash] hash data to sign # @return [String] signature data string def self.signable_string(hash) hash.map {|name, value| - value.to_s unless name.match(/signature/) + value.to_s unless name =~ /signature/ }.compact.join(";") end + private_class_method :signable_string end end diff --git a/lib/diaspora_federation/test/factories.rb b/lib/diaspora_federation/test/factories.rb index a297e90..7d2d691 100644 --- a/lib/diaspora_federation/test/factories.rb +++ b/lib/diaspora_federation/test/factories.rb @@ -132,7 +132,7 @@ module DiasporaFederation created_at { Time.now.utc } messages [] diaspora_id - participant_ids { 3.times.map { generate(:diaspora_id) }.join(";") } + participant_ids { Array.new(3) { generate(:diaspora_id) }.join(";") } end factory :message_entity, class: DiasporaFederation::Entities::Message, parent: :relayable_entity do @@ -179,7 +179,7 @@ module DiasporaFederation factory :poll_entity, class: DiasporaFederation::Entities::Poll do guid question { "Select an answer" } - poll_answers { 3.times.map { FactoryGirl.build(:poll_answer_entity) } } + poll_answers { Array.new(3) { FactoryGirl.build(:poll_answer_entity) } } end factory :poll_participation_entity, diff --git a/lib/diaspora_federation/version.rb b/lib/diaspora_federation/version.rb index d135b5c..74707f8 100644 --- a/lib/diaspora_federation/version.rb +++ b/lib/diaspora_federation/version.rb @@ -1,4 +1,4 @@ module DiasporaFederation # the gem version - VERSION = "0.0.11" + VERSION = "0.0.11".freeze end diff --git a/spec/controllers/diaspora_federation/h_card_controller_spec.rb b/spec/controllers/diaspora_federation/h_card_controller_spec.rb index e109d8c..83544eb 100644 --- a/spec/controllers/diaspora_federation/h_card_controller_spec.rb +++ b/spec/controllers/diaspora_federation/h_card_controller_spec.rb @@ -25,9 +25,10 @@ module DiasporaFederation end it "calls the fetch_person_for_hcard callback" do - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with(:fetch_person_for_hcard, alice.guid) - .and_call_original + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :fetch_person_for_hcard, alice.guid + ).and_call_original + get :hcard, "guid" => alice.guid end end diff --git a/spec/controllers/diaspora_federation/receive_controller_spec.rb b/spec/controllers/diaspora_federation/receive_controller_spec.rb index e158f58..3131280 100644 --- a/spec/controllers/diaspora_federation/receive_controller_spec.rb +++ b/spec/controllers/diaspora_federation/receive_controller_spec.rb @@ -9,16 +9,14 @@ module DiasporaFederation end it "returns a 202 if queued correctly" do - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with(:queue_public_receive, "") + expect(DiasporaFederation.callbacks).to receive(:trigger).with(:queue_public_receive, "") post :public, xml: "" expect(response.code).to eq("202") end it "unescapes the xml before sending it to the callback" do - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with(:queue_public_receive, "") + expect(DiasporaFederation.callbacks).to receive(:trigger).with(:queue_public_receive, "") post :public, xml: CGI.escape("") end @@ -26,9 +24,9 @@ module DiasporaFederation describe "POST #private" do it "return a 404 if not queued successfully (unknown user guid)" do - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with(:queue_private_receive, "any-guid", "") - .and_return(false) + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :queue_private_receive, "any-guid", "" + ).and_return(false) post :private, guid: "any-guid", xml: "" expect(response.code).to eq("404") @@ -40,18 +38,18 @@ module DiasporaFederation end it "returns a 202 if the callback returned true" do - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with(:queue_private_receive, "any-guid", "") - .and_return(true) + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :queue_private_receive, "any-guid", "" + ).and_return(true) post :private, guid: "any-guid", xml: "" expect(response.code).to eq("202") end it "unescapes the xml before sending it to the callback" do - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with(:queue_private_receive, "any-guid", "") - .and_return(true) + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :queue_private_receive, "any-guid", "" + ).and_return(true) post :private, guid: "any-guid", xml: CGI.escape("") end diff --git a/spec/controllers/diaspora_federation/webfinger_controller_spec.rb b/spec/controllers/diaspora_federation/webfinger_controller_spec.rb index 72888a0..85ff894 100644 --- a/spec/controllers/diaspora_federation/webfinger_controller_spec.rb +++ b/spec/controllers/diaspora_federation/webfinger_controller_spec.rb @@ -59,9 +59,10 @@ module DiasporaFederation end it "calls the fetch_person_for_webfinger callback" do - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with(:fetch_person_for_webfinger, "alice@localhost:3000") - .and_call_original + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :fetch_person_for_webfinger, "alice@localhost:3000" + ).and_call_original + get :legacy_webfinger, "q" => "acct:alice@localhost:3000" end end diff --git a/spec/lib/diaspora_federation/entities/relayable_retraction_spec.rb b/spec/lib/diaspora_federation/entities/relayable_retraction_spec.rb index f980c76..ccde647 100644 --- a/spec/lib/diaspora_federation/entities/relayable_retraction_spec.rb +++ b/spec/lib/diaspora_federation/entities/relayable_retraction_spec.rb @@ -23,16 +23,13 @@ XML let(:hash) { FactoryGirl.attributes_for(:relayable_retraction_entity) } it "updates author signature when it was nil and key was supplied" do - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with( - :fetch_entity_author_id_by_guid, - hash[:target_type], - hash[:target_guid] - ) - .and_return(hash[:diaspora_id]) - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with(:fetch_private_key_by_diaspora_id, hash[:diaspora_id]) - .and_return(author_pkey) + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :fetch_entity_author_id_by_guid, hash[:target_type], hash[:target_guid] + ).and_return(hash[:diaspora_id]) + + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :fetch_private_key_by_diaspora_id, hash[:diaspora_id] + ).and_return(author_pkey) Entities::RelayableRetraction.update_signatures!(hash) @@ -43,16 +40,13 @@ XML end it "updates parent author signature when it was nil, key was supplied and sender is not author of the target" do - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with( - :fetch_entity_author_id_by_guid, - hash[:target_type], - hash[:target_guid] - ) - .and_return(FactoryGirl.generate(:diaspora_id)) - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with(:fetch_private_key_by_diaspora_id, hash[:diaspora_id]) - .and_return(author_pkey) + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :fetch_entity_author_id_by_guid, hash[:target_type], hash[:target_guid] + ).and_return(FactoryGirl.generate(:diaspora_id)) + + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :fetch_private_key_by_diaspora_id, hash[:diaspora_id] + ).and_return(author_pkey) Entities::RelayableRetraction.update_signatures!(hash) @@ -71,12 +65,13 @@ XML end it "doesn't change signatures if keys weren't supplied" do - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with(:fetch_private_key_by_diaspora_id, hash[:diaspora_id]) - .and_return(nil) - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with(:fetch_entity_author_id_by_guid, "Comment", hash[:target_guid]) - .and_return(hash[:diaspora_id]) + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :fetch_private_key_by_diaspora_id, hash[:diaspora_id] + ).and_return(nil) + + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :fetch_entity_author_id_by_guid, "Comment", hash[:target_guid] + ).and_return(hash[:diaspora_id]) Entities::RelayableRetraction.update_signatures!(hash) expect(hash[:target_author_signature]).to eq(nil) diff --git a/spec/lib/diaspora_federation/entities/relayable_spec.rb b/spec/lib/diaspora_federation/entities/relayable_spec.rb index 7175c0d..f6bea69 100644 --- a/spec/lib/diaspora_federation/entities/relayable_spec.rb +++ b/spec/lib/diaspora_federation/entities/relayable_spec.rb @@ -19,25 +19,25 @@ module DiasporaFederation hash[:author_signature] = Signing.sign_with_key(hash, author_pkey) hash[:parent_author_signature] = Signing.sign_with_key(hash, parent_pkey) - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with(:fetch_public_key_by_diaspora_id, hash[:diaspora_id]) - .and_return(author_pkey.public_key) - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with( - :fetch_author_public_key_by_entity_guid, - "Post", - hash[:parent_guid] - ) - .and_return(parent_pkey.public_key) - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with(:entity_author_is_local?, "Post", hash[:parent_guid]) - .and_return(false) + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :fetch_public_key_by_diaspora_id, hash[:diaspora_id] + ).and_return(author_pkey.public_key) + + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :fetch_author_public_key_by_entity_guid, "Post", hash[:parent_guid] + ).and_return(parent_pkey.public_key) + + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :entity_author_is_local?, "Post", hash[:parent_guid] + ).and_return(false) + expect { Entities::Relayable.verify_signatures(hash, SomeRelayable) }.not_to raise_error end it "raises when no public key for author was fetched" do - expect(DiasporaFederation.callbacks).to receive(:trigger).with(:fetch_public_key_by_diaspora_id, anything) - .and_return(nil) + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :fetch_public_key_by_diaspora_id, anything + ).and_return(nil) expect { Entities::Relayable.verify_signatures(hash, SomeRelayable) }.to raise_error( Entities::Relayable::SignatureVerificationFailed @@ -47,9 +47,10 @@ module DiasporaFederation it "raises when bad author signature was passed" do hash[:author_signature] = nil - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with(:fetch_public_key_by_diaspora_id, hash[:diaspora_id]) - .and_return(author_pkey.public_key) + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :fetch_public_key_by_diaspora_id, hash[:diaspora_id] + ).and_return(author_pkey.public_key) + expect { Entities::Relayable.verify_signatures(hash, SomeRelayable) }.to raise_error( Entities::Relayable::SignatureVerificationFailed ) @@ -58,19 +59,18 @@ module DiasporaFederation it "raises when no public key for parent author was fetched" do hash[:author_signature] = Signing.sign_with_key(hash, author_pkey) - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with(:fetch_public_key_by_diaspora_id, hash[:diaspora_id]) - .and_return(author_pkey.public_key) - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with( - :fetch_author_public_key_by_entity_guid, - "Post", - hash[:parent_guid] - ) - .and_return(nil) - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with(:entity_author_is_local?, "Post", hash[:parent_guid]) - .and_return(false) + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :fetch_public_key_by_diaspora_id, hash[:diaspora_id] + ).and_return(author_pkey.public_key) + + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :fetch_author_public_key_by_entity_guid, "Post", hash[:parent_guid] + ).and_return(nil) + + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :entity_author_is_local?, "Post", hash[:parent_guid] + ).and_return(false) + expect { Entities::Relayable.verify_signatures(hash, SomeRelayable) }.to raise_error( Entities::Relayable::SignatureVerificationFailed ) @@ -80,19 +80,18 @@ module DiasporaFederation hash[:author_signature] = Signing.sign_with_key(hash, author_pkey) hash[:parent_author_signature] = nil - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with(:fetch_public_key_by_diaspora_id, hash[:diaspora_id]) - .and_return(author_pkey.public_key) - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with( - :fetch_author_public_key_by_entity_guid, - "Post", - hash[:parent_guid] - ) - .and_return(parent_pkey.public_key) - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with(:entity_author_is_local?, "Post", hash[:parent_guid]) - .and_return(false) + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :fetch_public_key_by_diaspora_id, hash[:diaspora_id] + ).and_return(author_pkey.public_key) + + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :fetch_author_public_key_by_entity_guid, "Post", hash[:parent_guid] + ).and_return(parent_pkey.public_key) + + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :entity_author_is_local?, "Post", hash[:parent_guid] + ).and_return(false) + expect { Entities::Relayable.verify_signatures(hash, SomeRelayable) }.to raise_error( Entities::Relayable::SignatureVerificationFailed ) @@ -102,28 +101,27 @@ module DiasporaFederation hash[:author_signature] = Signing.sign_with_key(hash, author_pkey) hash[:parent_author_signature] = nil - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with(:fetch_public_key_by_diaspora_id, hash[:diaspora_id]) - .and_return(author_pkey.public_key) - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with(:entity_author_is_local?, "Post", hash[:parent_guid]) - .and_return(true) + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :fetch_public_key_by_diaspora_id, hash[:diaspora_id] + ).and_return(author_pkey.public_key) + + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :entity_author_is_local?, "Post", hash[:parent_guid] + ).and_return(true) + expect { Entities::Relayable.verify_signatures(hash, SomeRelayable) }.not_to raise_error end end describe ".update_singatures!" do it "updates signatures when they were nil and keys were supplied" do - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with(:fetch_private_key_by_diaspora_id, hash[:diaspora_id]) - .and_return(author_pkey) - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with( - :fetch_author_private_key_by_entity_guid, - "Post", - hash[:parent_guid] - ) - .and_return(parent_pkey) + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :fetch_private_key_by_diaspora_id, hash[:diaspora_id] + ).and_return(author_pkey) + + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :fetch_author_private_key_by_entity_guid, "Post", hash[:parent_guid] + ).and_return(parent_pkey) Entities::Relayable.update_signatures!(hash, SomeRelayable) expect(Signing.verify_signature(hash, hash[:author_signature], author_pkey)).to be_truthy @@ -140,16 +138,13 @@ module DiasporaFederation end it "doesn't change signatures if keys weren't supplied" do - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with(:fetch_private_key_by_diaspora_id, hash[:diaspora_id]) - .and_return(nil) - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with( - :fetch_author_private_key_by_entity_guid, - "Post", - hash[:parent_guid] - ) - .and_return(nil) + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :fetch_private_key_by_diaspora_id, hash[:diaspora_id] + ).and_return(nil) + + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :fetch_author_private_key_by_entity_guid, "Post", hash[:parent_guid] + ).and_return(nil) Entities::Relayable.update_signatures!(hash, SomeRelayable) expect(hash[:author_signature]).to eq(nil) diff --git a/spec/lib/diaspora_federation/entities/signed_retraction_spec.rb b/spec/lib/diaspora_federation/entities/signed_retraction_spec.rb index f4f6aea..779c49c 100644 --- a/spec/lib/diaspora_federation/entities/signed_retraction_spec.rb +++ b/spec/lib/diaspora_federation/entities/signed_retraction_spec.rb @@ -22,9 +22,9 @@ XML let(:hash) { FactoryGirl.attributes_for(:signed_retraction_entity) } it "updates author signature when it was nil and key was supplied" do - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with(:fetch_private_key_by_diaspora_id, hash[:diaspora_id]) - .and_return(author_pkey) + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :fetch_private_key_by_diaspora_id, hash[:diaspora_id] + ).and_return(author_pkey) signable_hash = hash.select do |key, _| %i(target_guid target_type).include?(key) @@ -44,9 +44,9 @@ XML end it "doesn't change signature if a key wasn't supplied" do - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with(:fetch_private_key_by_diaspora_id, hash[:diaspora_id]) - .and_return(nil) + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :fetch_private_key_by_diaspora_id, hash[:diaspora_id] + ).and_return(nil) Entities::SignedRetraction.update_signatures!(hash) expect(hash[:author_signature]).to eq(nil) diff --git a/spec/lib/diaspora_federation/federation/receiver/private_spec.rb b/spec/lib/diaspora_federation/federation/receiver/private_spec.rb index 3bd1dc3..f376445 100644 --- a/spec/lib/diaspora_federation/federation/receiver/private_spec.rb +++ b/spec/lib/diaspora_federation/federation/receiver/private_spec.rb @@ -5,24 +5,23 @@ module DiasporaFederation let(:recipient_key) { OpenSSL::PKey::RSA.generate(1024) } let(:xml) { DiasporaFederation::Salmon::EncryptedSlap.prepare(sender_id, sender_key, FactoryGirl.build(:request_entity)) - .generate_xml(recipient_key) + .generate_xml(recipient_key) } it "calls save_entity_after_receive if everything is fine" do - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with(:fetch_public_key_by_diaspora_id, sender_id) - .and_return(sender_key) + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :fetch_public_key_by_diaspora_id, sender_id + ).and_return(sender_key) - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with(:save_entity_after_receive, kind_of(Entity)) + expect(DiasporaFederation.callbacks).to receive(:trigger).with(:save_entity_after_receive, kind_of(Entity)) described_class.new(xml, recipient_key).receive! end it "raises when sender public key is not available" do - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with(:fetch_public_key_by_diaspora_id, sender_id) - .and_return(nil) + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :fetch_public_key_by_diaspora_id, sender_id + ).and_return(nil) expect { described_class.new(xml, recipient_key).receive! diff --git a/spec/lib/diaspora_federation/federation/receiver/public_spec.rb b/spec/lib/diaspora_federation/federation/receiver/public_spec.rb index 203645a..b2bad2e 100644 --- a/spec/lib/diaspora_federation/federation/receiver/public_spec.rb +++ b/spec/lib/diaspora_federation/federation/receiver/public_spec.rb @@ -11,19 +11,19 @@ module DiasporaFederation } it "calls save_entity_after_receive if everything is fine" do - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with(:fetch_public_key_by_diaspora_id, sender_id) - .and_return(sender_key) - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with(:save_entity_after_receive, kind_of(Entity)) + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :fetch_public_key_by_diaspora_id, sender_id + ).and_return(sender_key) + + expect(DiasporaFederation.callbacks).to receive(:trigger).with(:save_entity_after_receive, kind_of(Entity)) described_class.new(xml).receive! end it "raises when sender public key is not available" do - expect(DiasporaFederation.callbacks).to receive(:trigger) - .with(:fetch_public_key_by_diaspora_id, sender_id) - .and_return(nil) + expect(DiasporaFederation.callbacks).to receive(:trigger).with( + :fetch_public_key_by_diaspora_id, sender_id + ).and_return(nil) expect { described_class.new(xml).receive! diff --git a/spec/lib/diaspora_federation/federation/sender/hydra_wrapper_spec.rb b/spec/lib/diaspora_federation/federation/sender/hydra_wrapper_spec.rb index 249d5bb..6f39978 100644 --- a/spec/lib/diaspora_federation/federation/sender/hydra_wrapper_spec.rb +++ b/spec/lib/diaspora_federation/federation/sender/hydra_wrapper_spec.rb @@ -16,9 +16,9 @@ module DiasporaFederation describe "#insert_job" do it "queues a request to hydra" do expect(hydra).to receive(:queue).with(kind_of(Typhoeus::Request)) - expect(Typhoeus::Request).to receive(:new) - .with(url, Federation::Sender::HydraWrapper.hydra_opts.merge(body: {xml: xml})) - .and_call_original + expect(Typhoeus::Request).to receive(:new).with( + url, Federation::Sender::HydraWrapper.hydra_opts.merge(body: {xml: xml}) + ).and_call_original hydra_wrapper.insert_job(url, xml) end diff --git a/spec/lib/diaspora_federation/signing_spec.rb b/spec/lib/diaspora_federation/signing_spec.rb index 38ea332..438d62c 100644 --- a/spec/lib/diaspora_federation/signing_spec.rb +++ b/spec/lib/diaspora_federation/signing_spec.rb @@ -37,7 +37,7 @@ RSA describe ".signable_string" do it "forms correct string for a hash" do - expect(Signing.signable_string(hash)).to eq("1;2;3;4") + expect(Signing.send(:signable_string, hash)).to eq("1;2;3;4") end end diff --git a/spec/lib/diaspora_federation/validators/conversation_validator_spec.rb b/spec/lib/diaspora_federation/validators/conversation_validator_spec.rb index 3784902..1b6b603 100644 --- a/spec/lib/diaspora_federation/validators/conversation_validator_spec.rb +++ b/spec/lib/diaspora_federation/validators/conversation_validator_spec.rb @@ -17,8 +17,8 @@ module DiasporaFederation # must not contain more than 20 participant handles it_behaves_like "a property with a value validation/restriction" do let(:property) { :participant_ids } - let(:wrong_values) { [21.times.map { FactoryGirl.generate(:diaspora_id) }.join(";")] } - let(:correct_values) { [20.times.map { FactoryGirl.generate(:diaspora_id) }.join(";")] } + let(:wrong_values) { [Array.new(21) { FactoryGirl.generate(:diaspora_id) }.join(";")] } + let(:correct_values) { [Array.new(20) { FactoryGirl.generate(:diaspora_id) }.join(";")] } end end end diff --git a/spec/lib/diaspora_federation/validators/rules/diaspora_id_count_spec.rb b/spec/lib/diaspora_federation/validators/rules/diaspora_id_count_spec.rb index 28525f2..6f62b16 100644 --- a/spec/lib/diaspora_federation/validators/rules/diaspora_id_count_spec.rb +++ b/spec/lib/diaspora_federation/validators/rules/diaspora_id_count_spec.rb @@ -1,5 +1,5 @@ describe Validation::Rule::DiasporaIdCount do - let(:id_str) { 3.times.map { FactoryGirl.generate(:diaspora_id) }.join(";") } + let(:id_str) { Array.new(3) { FactoryGirl.generate(:diaspora_id) }.join(";") } it "requires a parameter" do validator = Validation::Validator.new({}) diff --git a/spec/support/shared_validator_specs.rb b/spec/support/shared_validator_specs.rb index d075d0f..8812bff 100644 --- a/spec/support/shared_validator_specs.rb +++ b/spec/support/shared_validator_specs.rb @@ -2,7 +2,7 @@ def entity_stub(entity, data={}) OpenStruct.new(FactoryGirl.attributes_for(entity).merge(data)) end -ALPHANUMERIC_RANGE = [*"0".."9", *"A".."Z", *"a".."z"] +ALPHANUMERIC_RANGE = [*"0".."9", *"A".."Z", *"a".."z"].freeze def alphanumeric_string(length) Array.new(length) { ALPHANUMERIC_RANGE.sample }.join diff --git a/test/dummy/config/initializers/diaspora_federation.rb b/test/dummy/config/initializers/diaspora_federation.rb index db16335..eabee9b 100644 --- a/test/dummy/config/initializers/diaspora_federation.rb +++ b/test/dummy/config/initializers/diaspora_federation.rb @@ -1,12 +1,12 @@ require "diaspora_federation/discovery" -if File.file?("/etc/ssl/certs/ca-certificates.crt") - # For Debian, Ubuntu, Archlinux, Gentoo - ca_file = "/etc/ssl/certs/ca-certificates.crt" -else - # For CentOS, Fedora - ca_file = "/etc/pki/tls/certs/ca-bundle.crt" -end +ca_file = if File.file?("/etc/ssl/certs/ca-certificates.crt") + # For Debian, Ubuntu, Archlinux, Gentoo + "/etc/ssl/certs/ca-certificates.crt" + else + # For CentOS, Fedora + "/etc/pki/tls/certs/ca-bundle.crt" + end # configure the federation engine DiasporaFederation.configure do |config|