Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
Make logout use a connection and not node
Browse files Browse the repository at this point in the history
Authenticable methods now run on a connection not on a node, so logout must write
directly to the socket
  • Loading branch information
arthurnn committed Nov 13, 2013
1 parent 6f211ac commit 413c858
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 30 deletions.
8 changes: 6 additions & 2 deletions lib/moped/authenticatable.rb
Expand Up @@ -61,7 +61,7 @@ def credentials
def login(database, username, password)
getnonce = Protocol::Command.new(database, getnonce: 1)
self.write([getnonce])
reply = self.receive_replies([getnonce]).first#.first.documents.first
reply = self.receive_replies([getnonce]).first
if getnonce.failure?(reply)
return
end
Expand All @@ -87,7 +87,11 @@ def login(database, username, password)
# @since 2.0.0
def logout(database)
command = Protocol::Command.new(database, logout: 1)
Operation::Read.new(command).execute(self)
self.write([command])
reply = self.receive_replies([command]).first
if command.failure?(reply)
return
end
credentials.delete(database)
end
end
Expand Down
69 changes: 43 additions & 26 deletions spec/moped/cluster_spec.rb
Expand Up @@ -419,46 +419,63 @@

describe Moped::Cluster, "authentication", mongohq: :auth do

let(:session) do
Support::MongoHQ.auth_session(false)
end
shared_examples_for "authenticable session" do

describe "logging in with valid credentials" do
context "when logging in with valid credentials" do

it "logs in and processes commands" do
session.login(*Support::MongoHQ.auth_credentials)
session.command(ping: 1).should eq("ok" => 1)
it "logs in and processes commands" do
session.login(*Support::MongoHQ.auth_credentials)
session.command(ping: 1).should eq("ok" => 1)
end
end
end

describe "logging in with invalid credentials" do
context "when logging in with invalid credentials" do

it "raises an AuthenticationFailure exception" do
session.login "invalid-user", "invalid-password"
it "raises an AuthenticationFailure exception" do
session.login "invalid-user", "invalid-password"

lambda do
session.command(ping: 1)
end.should raise_exception(Moped::Errors::AuthenticationFailure)
lambda do
session.command(ping: 1)
end.should raise_exception(Moped::Errors::AuthenticationFailure)
end
end

context "when logging in with valid credentials and then logging out" do

before do
session.login(*Support::MongoHQ.auth_credentials)
session.command(ping: 1).should eq("ok" => 1)
end

it "logs out" do
lambda do
session.command dbStats: 1
end.should_not raise_exception

session.logout

lambda do
session.command dbStats: 1
end.should raise_exception(Moped::Errors::OperationFailure)
end
end
end

describe "logging in with valid credentials and then logging out" do
context "when there are multiple connections on the pool" do

before do
session.login(*Support::MongoHQ.auth_credentials)
session.command(ping: 1).should eq("ok" => 1)
let(:session) do
Support::MongoHQ.auth_session(false)
end

it "logs out" do
lambda do
session.command dbStats: 1
end.should_not raise_exception
it_behaves_like "authenticable session"
end

session.logout
context "when there is one connections on the pool" do

lambda do
session.command dbStats: 1
end.should raise_exception(Moped::Errors::OperationFailure)
let(:session) do
Support::MongoHQ.auth_session(false, pool_size: 1)
end

it_behaves_like "authenticable session"
end
end
5 changes: 3 additions & 2 deletions spec/support/mongohq.rb
Expand Up @@ -40,8 +40,9 @@ def auth_database
ENV["MONGOHQ_SINGLE_NAME"]
end

def auth_session(auth = true)
session = Moped::Session.new auth_seeds, database: auth_database
def auth_session(auth = true, options = {})
options.merge!(database: auth_database)
session = Moped::Session.new(auth_seeds, options)
session.login(*auth_credentials) if auth
session
end
Expand Down

0 comments on commit 413c858

Please sign in to comment.