require "micropub/server/rails/middleware" 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 "POST /micropub" do before do expect(Micropub::Token).to receive(:new).with("1234").and_return(token) end context "when given a valid token" do let(:token) { double("Token", valid?: true) } it "returns a 201" do post "/micropub", {}, "HTTP_AUTHORIZATION" => "Bearer 1234" expect(last_response.status).to eq 201 end it "returns a Location header" do post "/micropub", {}, "HTTP_AUTHORIZATION" => "Bearer 1234" expect(last_response.header["Location"]).to eq "http://bookisworthy.com/posts/1" end it "with params access_token returns a 201" do post "/micropub", {access_token: 1234} 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