-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from pearkes/pm_custom_faraday
Custom faraday parameters and verify method added
- Loading branch information
Showing
6 changed files
with
104 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module Tugboat | ||
class AuthenticationMiddleware < Faraday::Middleware | ||
extend Forwardable | ||
def_delegators :'Faraday::Utils', :parse_query, :build_query | ||
RED = "\e[31m" | ||
CLEAR = "\e[0m" | ||
|
||
def initialize(app, client_id, api_key) | ||
@client_id = client_id | ||
@api_key = api_key | ||
|
||
super(app) | ||
end | ||
|
||
def call(env) | ||
params = { 'client_id' => @client_id, 'api_key' => @api_key }.update query_params(env[:url]) | ||
|
||
env[:url].query = build_query params | ||
|
||
begin | ||
@app.call(env) | ||
rescue Faraday::Error::ClientError => e | ||
puts "#{RED} Authentication with DigitalOcean failed (#{e})" | ||
puts "#{CLEAR} Check your API keys and run `tugboat authorize` to re-enter them if needed" | ||
exit 1 | ||
end | ||
end | ||
|
||
def query_params(url) | ||
if url.query.nil? or url.query.empty? | ||
{} | ||
else | ||
parse_query url.query | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require 'spec_helper' | ||
|
||
describe Tugboat::CLI do | ||
include_context "spec" | ||
|
||
describe "verify" do | ||
it "returns confirmation text when verify passes" do | ||
stub_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}"). | ||
to_return(:status => 200) | ||
@cli.verify | ||
expect($stdout.string).to eq "Authentication with DigitalOcean was successful.\n" | ||
expect(a_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}")).to have_been_made | ||
end | ||
|
||
it "returns error string when verify fails" do | ||
stub_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}"). | ||
to_return(:status => 401, :body => '{"status":"ERROR", "message":"Access Denied"}') | ||
expect { @cli.resize("verify") }.to raise_error(SystemExit) | ||
expect($stdout.string).to eq "Droplet fuzzy name provided. Finding droplet ID...\e[31m Authentication with DigitalOcean failed (the server responded with status 401)\n\e[0m Check your API keys and run `tugboat authorize` to re-enter them if needed\n" | ||
expect(a_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}")).to have_been_made | ||
end | ||
|
||
end | ||
|
||
end | ||
|