Skip to content

Commit

Permalink
Merge pull request #33 from taylorfinnell/add_connections
Browse files Browse the repository at this point in the history
Add connections
  • Loading branch information
benschwarz committed May 30, 2015
2 parents 718a9d4 + f383868 commit 2c4c33a
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 9 deletions.
3 changes: 3 additions & 0 deletions lib/auth0/api/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require "auth0/api/v2/blacklists"
require "auth0/api/v2/jobs"
require "auth0/api/v2/stats"
require "auth0/api/v2/connections"

module Auth0
module Api
# https://auth0.com/docs/apiv2
Expand All @@ -12,6 +14,7 @@ module V2
include Auth0::Api::V2::Blacklists
include Auth0::Api::V2::Jobs
include Auth0::Api::V2::Stats
include Auth0::Api::V2::Connections
end
end
end
45 changes: 45 additions & 0 deletions lib/auth0/api/v2/connections.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module Auth0
module Api
module V2
module Connections
def connections(strategy: nil, fields: nil, include_fields: true)
request_params = {
strategy: strategy,
fields: fields,
include_fields: include_fields
}
path = "/api/v2/connections"
get(path, request_params)
end
alias :get_connections :connections

def create_connection(body)
path = "/api/v2/connections"
request_params = body
post(path, request_params )
end

def connection(connection_id, fields: nil, include_fields: true)
path = "/api/v2/connections/" + connection_id.to_s
request_params = {
fields: fields,
include_fields: include_fields
}
get(path, request_params)
end

def delete_connection(connection_id)
raise Auth0::MissingConnectionId, "you must specify a connection id" if connection_id.to_s.empty?
path = "/api/v2/connections/#{connection_id}"
delete(path)
end

def update_connection(connection_id, body)
raise Auth0::MissingConnectionId, "you must specify a connection id" if connection_id.to_s.empty?
path = "/api/v2/connections/" + connection_id.to_s
patch(path, body)
end
end
end
end
end
2 changes: 2 additions & 0 deletions lib/auth0/exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Auth0::ServerError < Auth0::Exception; end
class Auth0::BadRequest < Auth0::Exception; end
# exception for unset user_id, this might cause removal of all users, or other unexpected bahaviour
class Auth0::MissingUserId < Auth0::Exception; end
# exception for an unset connection_id
class Auth0::MissingConnectionId < Auth0::Exception; end
# Api v2 access denied
class Auth0::AccessDenied < Auth0::Exception; end
# Invalid parameter passed, e.g. empty where ID is required
Expand Down
71 changes: 71 additions & 0 deletions spec/integration/lib/auth0/api/v2/api_connections_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
require "spec_helper"
describe Auth0::Api::V2::Connections do

let(:client) { Auth0Client.new(v2_creds) }

let(:name) { SecureRandom.uuid[0..25] }
let(:strategy) { 'google-oauth2' }
let(:options) { {} }
let(:enabled_clients) { [] }

let!(:connection) { client.create_connection({
name: name,
strategy: strategy,
options: options,
enabled_clients: enabled_clients,
})}

describe '.connections' do

let(:connections) { client.connections() }

it { expect(connections.size).to be > 0 }
it { expect(connections.find {|con| con["name"] == name}).to_not be_nil }

context "#filters" do
it { expect(client.connections(strategy: strategy).size).to be > 0 }
it { expect(client.connections(strategy: strategy, fields: [:name].join(',')).first).to include("name") }
it { expect(client.connections(strategy: strategy, fields: [:name].join(','), include_fields: false).first).to_not include("name") }
end

end

describe '.connection' do

let(:subject) { client.connection(connection["id"]) }

it { should include("name" => connection['name']) }

context "#filters" do
it { expect(client.connection(connection["id"], fields: [:name, :id].join(','))).to include("id", "name") }
it { expect(client.connection(connection["id"], fields: [:name, :id].join(','), include_fields: false)).to_not include("id", "name") }
end

end

describe '.create_connection' do

let(:subject) { connection }

it { should include("id", "name") }
it { should include(
"name" => connection['name'],
)}

end

describe '.delete_connection' do

it { expect { client.delete_connection connection["id"] }.to_not raise_error }

it { expect { client.delete_connection "" }.to raise_error(Auth0::MissingConnectionId) }

end


describe '.update_connection' do
new_name = SecureRandom.uuid[0..25]
it { expect(client.update_connection(connection["id"], {"name" => new_name})).to include("name" => new_name) }
end

end
70 changes: 70 additions & 0 deletions spec/lib/auth0/api/v2/connections_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
require "spec_helper"
describe Auth0::Api::V2::Connections do
before :all do
dummy_instance = DummyClass.new
dummy_instance.extend(Auth0::Api::V2::Connections)
@instance = dummy_instance
end

context ".connections" do
it {expect(@instance).to respond_to(:connections)}
it {expect(@instance).to respond_to(:get_connections)}

it "is expected to call /api/v2/connections" do
expect(@instance).to receive(:get).with("/api/v2/connections", {
strategy: nil,
fields: nil,
include_fields: true
})

expect{@instance.connections}.not_to raise_error
end
end

context '.create_connection' do
it {expect(@instance).to respond_to(:connection)}
it {expect(@instance).to respond_to(:create_connection)}

it "is expected to call /api/v2/connections" do
body = double
expect(@instance).to receive(:post).with("/api/v2/connections", body)

expect{@instance.create_connection(body)}.not_to raise_error
end
end

context ".connection" do
it {expect(@instance).to respond_to(:connection)}
it "is expected to call get request to /api/v2/connection/CONNECTION_ID" do
expect(@instance).to receive(:get).with("/api/v2/connections/CONNECTION_ID", {fields: nil, include_fields: true})
expect{@instance.connection("CONNECTION_ID")}.not_to raise_error
end
end

context ".delete_connection" do
it {expect(@instance).to respond_to(:delete_connection)}
it "is expected to call delete to /api/v2/connections/connectionId" do
expect(@instance).to receive(:delete).with("/api/v2/connections/connectionId")
@instance.delete_connection("connectionId")
end

it "is expected not to call delete to /api/v2/connections if connection_id is blank" do
expect(@instance).not_to receive(:delete)
expect{@instance.delete_connection("")}.to raise_exception(Auth0::MissingConnectionId)
end
end

context ".update_connection" do
it {expect(@instance).to respond_to(:update_connection)}
it "is expected to call patch to /api/v2/connections/connectionId" do
body = double
expect(@instance).to receive(:patch).with("/api/v2/connections/connectionId", body)
@instance.update_connection("connectionId", body)
end

it "is expected not to call delete to /api/v2/connections if connection_id is blank" do
expect(@instance).not_to receive(:patch)
expect{@instance.delete_connection("")}.to raise_exception(Auth0::MissingConnectionId)
end
end
end
18 changes: 9 additions & 9 deletions spec/support/credentials.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module Credentials
def v1_creds
{client_id: ENV["CLIENT_ID"], client_secret: ENV["CLIENT_SECRET"], domain: ENV["DOMAIN"]}
end
def v1_creds
{client_id: ENV["CLIENT_ID"], client_secret: ENV["CLIENT_SECRET"], domain: ENV["DOMAIN"]}
end

def v1_global_creds
{client_id: ENV["GLOBAL_CLIENT_ID"], client_secret: ENV["GLOBAL_CLIENT_SECRET"], domain: ENV["DOMAIN"]}
end
def v1_global_creds
{client_id: ENV["GLOBAL_CLIENT_ID"], client_secret: ENV["GLOBAL_CLIENT_SECRET"], domain: ENV["DOMAIN"]}
end

def v2_creds
{token: ENV["MASTER_JWT"], api_version: 2, domain: ENV["DOMAIN"]}
end
def v2_creds
{token: ENV["MASTER_JWT"], api_version: 2, domain: ENV["DOMAIN"]}
end
end

0 comments on commit 2c4c33a

Please sign in to comment.