require "micropub/server/rails/middleware" require 'webmock/rspec' require "rack/test" Micropub.configure do |c| c.token_endpoint = "https://tokens.indieauth.com/token" c.me = "http://bookisworthy.com" c.allowed_scopes = [:post] end describe Micropub::Server::Rails::Middleware do include Rack::Test::Methods let(:rails) { double("rails") } def app Micropub::Server::Rails::Middleware.new(rails) end describe "invalid route" do it "calls `call` on parent app" do expect(rails).to receive(:call).and_return [200, {}, [""]] get "/" end end describe "GET /micropub?q=syndicate-to" do it "returns a 200" do get "/micropub", q: "synicate-to" expect(last_response.status).to eq 200 end it "returns an http array called syndicate-to" do get "/micropub", q: "synicate-to" expect(last_response.body).to match /syndicate-to/ end end describe "POST /micropub" do before do expect(Micropub::Token).to receive(:new).with("1234").and_return(token) end context "when publisher returns 401" do let(:request) { post "/micropub", {content: "Blah Blah", "as" => "note"}, "HTTP_AUTHORIZATION" => "Bearer 1234" } let(:token) { double("Token", valid?: true) } before do stub_request(:post, "http://bookisworthy.com/notes").with( body: {content: "Blah Blah", as: "note"}, headers: { 'Authorization' => "Bearer 1234" }) .to_return(status: 401, body: "", headers: {}) end it "returns a 401" do request expect(last_response.status).to eq 401 end end context "when given a valid token" do let(:token) { double("Token", valid?: true) } # params "as" stands for "activity stream" # this is the experimental value to indicate the post type let(:request) { post "/micropub", {content: "Blah Blah", "as" => "note"}, "HTTP_AUTHORIZATION" => "Bearer 1234" } before do stub_request(:post, "http://bookisworthy.com/notes").with( body: {content: "Blah Blah", as: "note"}, headers: { 'Authorization' => "Bearer 1234" }) .to_return(status: 201, body: "http://bookisworthy.com/notes/1", headers: {"Location" => "http://bookisworthy.com/notes/1"}) end it "returns a 201" do request expect(last_response.status).to eq 201 end it "returns a Location header" do request expect(last_response.header["Location"]).to eq "http://bookisworthy.com/notes/1" end it "with params access_token returns a 201" do post "/micropub", {content: "Blah Blah", access_token: 1234, as: "note"} expect(last_response.status).to eq 201 end end context "when given an invalid token" do let(:token) { double("Token", valid?: false) } it "returns a 401" do post "/micropub", {}, "HTTP_AUTHORIZATION" => "Bearer 1234" expect(last_response.status).to eq 401 end end end end