Unverified Commit 7934c1e9 authored by Benjamin Neff's avatar Benjamin Neff Committed by Dennis Schubert
Browse files

Add NodeInfo 2.0 support

parent c2eb53e8
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -26,8 +26,7 @@ class NodeInfoPresenter

  def add_static_data(doc)
    doc.software.name = "diaspora"
    doc.protocols.inbound << "diaspora"
    doc.protocols.outbound << "diaspora"
    doc.protocols.protocols << "diaspora"
  end

  def add_user_counts(doc)
+24 −6
Original line number Diff line number Diff line
@@ -2,7 +2,7 @@ require "pathname"
require "json-schema"

module NodeInfo
  VERSIONS = %w(1.0)
  VERSIONS = %w(1.0 2.0).freeze
  SCHEMAS = {}
  private_constant :VERSIONS, :SCHEMAS

@@ -21,17 +21,21 @@ module NodeInfo
      end
    end

    Protocols = Struct.new(:inbound, :outbound) do
      def initialize(inbound=[], outbound=[])
        super(inbound, outbound)
    Protocols = Struct.new(:protocols) do
      def initialize(protocols=[])
        super(protocols)
      end

      def version_10_hash
        {
          "inbound"  => inbound,
          "outbound" => outbound
          "inbound"  => protocols,
          "outbound" => protocols
        }
      end

      def version_20_array
        protocols
      end
    end

    Services = Struct.new(:inbound, :outbound) do
@@ -90,6 +94,8 @@ module NodeInfo
      case version
      when "1.0"
        version_10_hash
      when "2.0"
        version_20_hash
      end
    end

@@ -124,6 +130,18 @@ module NodeInfo
      )
    end

    def version_20_hash
      deep_compact(
        "version"           => "2.0",
        "software"          => software.version_10_hash,
        "protocols"         => protocols.version_20_array,
        "services"          => services.version_10_hash,
        "openRegistrations" => open_registrations,
        "usage"             => usage.version_10_hash,
        "metadata"          => metadata
      )
    end

    def deep_compact(hash)
      hash.tap do |hash|
        hash.reject! {|_, value|
+19 −13
Original line number Diff line number Diff line
@@ -15,6 +15,9 @@ describe NodeInfoController do
      expect(jrd).to include "links" => [{
        "rel"  => "http://nodeinfo.diaspora.software/ns/schema/1.0",
        "href" => node_info_url("1.0")
      }, {
        "rel"  => "http://nodeinfo.diaspora.software/ns/schema/2.0",
        "href" => node_info_url("2.0")
      }]
    end
  end
@@ -28,24 +31,27 @@ describe NodeInfoController do
      end
    end

    context "version 1.0" do
    %w(1.0 2.0).each do |version|
      context "version #{version}" do
        it "responds to JSON" do
        get :document, version: "1.0", format: :json
          get :document, version: version, format: :json

          expect(response).to be_success
        end

        it "calls NodeInfoPresenter" do
        expect(NodeInfoPresenter).to receive(:new).with("1.0")
          expect(NodeInfoPresenter).to receive(:new).with(version)
            .and_return(double(as_json: {}, content_type: "application/json"))

        get :document, version: "1.0", format: :json
          get :document, version: version, format: :json
        end

        it "notes the schema in the content type" do
        get :document, version: "1.0", format: :json
          get :document, version: version, format: :json

        expect(response.content_type).to eq "application/json; profile=http://nodeinfo.diaspora.software/ns/schema/1.0#"
          expect(response.content_type)
            .to eq("application/json; profile=http://nodeinfo.diaspora.software/ns/schema/#{version}#")
        end
      end
    end
  end
+1 −2
Original line number Diff line number Diff line
@@ -116,8 +116,7 @@ describe ConnectionTester do
      ni_document = NodeInfo.build do |doc|
        doc.version = "1.0"
        doc.open_registrations = true
        doc.protocols.inbound << "diaspora"
        doc.protocols.outbound << "diaspora"
        doc.protocols.protocols << "diaspora"
        doc.software.name = "diaspora"
        doc.software.version = "a.b.c.d"
      end
+25 −0
Original line number Diff line number Diff line
@@ -128,5 +128,30 @@ describe NodeInfoPresenter do
        expect(hash).to include "metadata" => include("xmppChat" => true)
      end
    end

    context "version 2.0" do
      it "provides generic pod data in json" do
        expect(NodeInfoPresenter.new("2.0").as_json.as_json).to eq(
          "version"           => "2.0",
          "software"          => {
            "name"    => "diaspora",
            "version" => AppConfig.version_string
          },
          "protocols"         => ["diaspora"],
          "services"          => {
            "inbound"  => [],
            "outbound" => AppConfig.configured_services.map(&:to_s)
          },
          "openRegistrations" => AppConfig.settings.enable_registrations?,
          "usage"             => {
            "users" => {}
          },
          "metadata"          => {
            "nodeName" => AppConfig.settings.pod_name,
            "xmppChat" => AppConfig.chat.enabled?
          }
        )
      end
    end
  end
end
Loading