diff --git a/lib/cassandra-cql/database.rb b/lib/cassandra-cql/database.rb index e93dc20..37f20d1 100644 --- a/lib/cassandra-cql/database.rb +++ b/lib/cassandra-cql/database.rb @@ -42,6 +42,7 @@ def connect! obj = self @connection.add_callback(:post_connect) do execute("USE #{@keyspace}") + @connection.login(@auth_request) if @auth_request end end @@ -110,5 +111,15 @@ def schema # TODO: This should be replaced with a CQL call that doesn't exist yet Schema.new(@connection.describe_keyspace(@keyspace)) end + + def login!(username, password) + request = CassandraCQL::Thrift::AuthenticationRequest.new + request.credentials = {'username' => username, 'password' => password} + ret = @connection.login(request) + # To avoid a double login on the initial connect, we set + # @auth_request after the first successful login. + @auth_request = request + ret + end end end diff --git a/spec/database_spec.rb b/spec/database_spec.rb index beae02e..95e2ad9 100644 --- a/spec/database_spec.rb +++ b/spec/database_spec.rb @@ -13,4 +13,13 @@ end end + describe "login!" do + it "should call login! on connection" do + creds = { 'username' => 'myuser', 'password' => 'mypass' } + @connection.connection.should_receive(:login) do |auth| + auth.credentials.should eq(creds) + end + @connection.login!(creds['username'], creds['password']) + end + end end \ No newline at end of file