Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Faraday authentication bug #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/mediumite/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def token

def agent
@agent ||= Sawyer::Agent.new("https://api.medium.com/v1") do |http|
http.request :authorization, "Bearer", token
http.headers[:content_type] = "application/json"
http.authorization "Bearer", token
end
end

Expand Down
3 changes: 2 additions & 1 deletion mediumite.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency "sawyer"
spec.add_dependency "sawyer", "~> 0.9.2"

spec.add_development_dependency "bundler", "~> 1.11"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec"
spec.add_development_dependency "pry"
spec.add_development_dependency "webmock", "~> 3.17.1"
end
66 changes: 62 additions & 4 deletions spec/mediumite/client_spec.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,76 @@
require "spec_helper"

describe Mediumite::Client do
let(:client) { Mediumite::Client.new(token: "access-token") }

describe "instantiation" do
it "masks token on inspect" do
client = Mediumite::Client.new(token: "abcdefghijklmnopqrstuvwxyz")
inspected = client.inspect
expect(inspected).not_to include("abcdefghijklmnopqrstuvwxyz")
expect(inspected).not_to include("access-token")
end
end

describe "#token" do
it "returns given token" do
client = Mediumite::Client.new(token: "token")
expect(client.token).to eq "token"
expect(client.token).to eq "access-token"
end
end

describe "#create_post" do
it "sends the expected params to Medium blog" do
post = Mediumite::Post.new(
title: "Test post title",
body: "# Test post body",
publication_id: "publication-id"
)
stub = stubbed_post_create

response = client.create_post(post)

expect(stub).to have_been_requested
end
end

private

def stubbed_post_create(title: "Test post title",
body: "# Test post body",
publication_id: "publication-id",
access_token: "access-token")
response_headers = { "Content-Type" => "application/json" }

stub_request(:post, "https://api.medium.com/v1/publications/#{publication_id}/posts").
with(
body: {
title: title,
contentFormat: "markdown",
content: body,
canonicalUrl: nil,
tags: nil,
publishStatus: "draft"
}.to_json,
headers: {
"Accept" => "*/*",
"Accept-Encoding" => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3",
"Authorization" => "Bearer #{access_token}",
"Content-Type" => "application/json"
}
).
to_return(status: 200, body: <<~JSON, headers: response_headers)
{
"data": {
"id": "e6f36a",
"title": "Test post title",
"authorId": "#{Random.hash}",
"tags": [],
"url": "https://medium.com/@mediumuser/test-post-title-e6f36a",
"canonicalUrl": "http://example.com/posts/test-post-title",
"publishStatus": "public",
"publishedAt": #{Time.now.to_i},
"license": "all-rights-reserved",
"licenseUrl": "https://medium.com/policy/9db0094a1e0f"
}
}
JSON
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "mediumite"
require 'webmock/rspec'

RSpec.configure do |config|
end