spec/lib/micropub/server/rails/middleware_spec.rb in micropub-server-rails-0.1.5 vs spec/lib/micropub/server/rails/middleware_spec.rb in micropub-server-rails-0.1.6

- old
+ new

@@ -1,8 +1,12 @@ require "micropub/server/rails/middleware" +require 'webmock/rspec' require "rack/test" +# How do we determine what host to hit? +# Where does the code go which tells the mapping how to notes to work? + Micropub.configure do |c| c.token_endpoint = "https://tokens.indieauth.com/token" c.me = "http://bookisworthy.com" c.allowed_scopes = [:post] end @@ -23,24 +27,53 @@ 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 - post "/micropub", {}, "HTTP_AUTHORIZATION" => "Bearer 1234" + request 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" + 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", {access_token: 1234} + post "/micropub", {content: "Blah Blah", access_token: 1234, as: "note"} expect(last_response.status).to eq 201 end end