require 'rails_helper'

RSpec.describe "WebFinger", type: :request do
  describe "remoteStorage link relation" do
    context "user exists" do
      before do
        create :user, cn: 'tony', ou: 'kosmos.org'
      end

      context "remoteStorage enabled globally" do
        it "includes the remoteStorage link for the user" do
          get "/.well-known/webfinger?resource=acct%3Atony%40kosmos.org"
          expect(response).to have_http_status(:ok)

          res = JSON.parse(response.body)
          rs_link = res["links"].find {|l| l["rel"] == "http://tools.ietf.org/id/draft-dejong-remotestorage"}

          expect(rs_link["href"]).to eql("https://storage.kosmos.org/tony")

          oauth_url = rs_link["properties"]["http://tools.ietf.org/html/rfc6749#section-4.2"]
          expect(oauth_url).to eql("http://www.example.com/rs/oauth/tony")
        end
      end

      context "remoteStorage not available" do
        before do
          Setting.remotestorage_enabled = false
        end

        it "does not include the remoteStorage link" do
          get "/.well-known/webfinger?resource=acct%3Atony%40kosmos.org"
          expect(response).to have_http_status(:ok)

          res = JSON.parse(response.body)
          rs_link = res["links"].find {|l| l["rel"] == "http://tools.ietf.org/id/draft-dejong-remotestorage"}

          expect(rs_link).to be_nil
        end
      end
    end

    context "user does not exist" do
      it "does return a 404 status" do
        get "/.well-known/webfinger?resource=acct%3Ajane.doe%40kosmos.org"
        expect(response).to have_http_status(:not_found)
      end
    end
  end
end
