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

Add support for Cassandra and Cequel. Resolves #315 #318

Open
wants to merge 2 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
9 changes: 8 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
simple_token_authentication (1.15.1)
simple_token_authentication (1.15.2)
actionmailer (>= 3.2.6, < 6)
actionpack (>= 3.2.6, < 6)
devise (>= 3.2, < 6)
Expand Down Expand Up @@ -50,6 +50,11 @@ GEM
bcrypt (3.1.11)
bson (4.2.1)
builder (3.2.3)
cassandra-driver (3.2.0)
ione (~> 1.2)
cequel (3.0.0)
activemodel (>= 4.0)
cassandra-driver (~> 3.0)
coderay (1.1.1)
concurrent-ruby (1.0.4)
devise (4.2.0)
Expand All @@ -68,6 +73,7 @@ GEM
sparkr (>= 0.2.0)
term-ansicolor
yard (~> 0.8.7.5)
ione (1.2.4)
loofah (2.0.3)
nokogiri (>= 1.5.9)
mail (2.6.4)
Expand Down Expand Up @@ -139,6 +145,7 @@ PLATFORMS
DEPENDENCIES
activerecord (>= 3.2.6, < 6)
appraisal (~> 2.0)
cequel (>= 1.0, < 4)
inch (~> 0.4)
mongoid (>= 3.1.0, < 7)
rspec (~> 3.0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def generate_authentication_token(token_generator)
end

def token_suitable?(token)
self.class.where(authentication_token: token).count == 0
self.class.find_by_authentication_token(token).nil?
end

def token_generator
Expand Down
14 changes: 14 additions & 0 deletions lib/simple_token_authentication/adapters/cequel_adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'cequel'
require 'simple_token_authentication/adapter'

module SimpleTokenAuthentication
module Adapters
class CequelAdapter
extend SimpleTokenAuthentication::Adapter

def self.base_class
::Cequel::Record
end
end
end
end
3 changes: 2 additions & 1 deletion lib/simple_token_authentication/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ module Configuration
@@identifiers = {}
@@sign_in_token = false
@@controller_adapters = ['rails', 'rails_api', 'rails_metal']
@@model_adapters = ['active_record', 'mongoid']
@@model_adapters = ['active_record', 'cequel', 'mongoid']
@@adapters_dependencies = { 'active_record' => 'ActiveRecord::Base',
'cequel' => 'Cequel::Record',
'mongoid' => 'Mongoid::Document',
'rails' => 'ActionController::Base',
'rails_api' => 'ActionController::API',
Expand Down
2 changes: 1 addition & 1 deletion lib/simple_token_authentication/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module SimpleTokenAuthentication
VERSION = "1.15.1".freeze
VERSION = "1.15.2".freeze
end
1 change: 1 addition & 0 deletions simple_token_authentication.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "rspec", "~> 3.0"
s.add_development_dependency "inch", "~> 0.4"
s.add_development_dependency "activerecord", ">= 3.2.6", "< 6"
s.add_development_dependency "cequel", ">= 1.0", "< 4"
s.add_development_dependency 'mongoid', '>= 3.1.0', '< 7'
s.add_development_dependency "appraisal", "~> 2.0"
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'spec_helper'
require 'simple_token_authentication/adapters/cequel_adapter'

describe 'SimpleTokenAuthentication::Adapters::CequelAdapter' do

before(:each) do
stub_const('Cequel', Module.new)
stub_const('Cequel::Record', double())

@subject = SimpleTokenAuthentication::Adapters::CequelAdapter
end

it_behaves_like 'an adapter'

describe '.base_class' do

it 'is Cequel::Record', private: true do
expect(@subject.base_class).to eq Cequel::Record
end
end
end
5 changes: 3 additions & 2 deletions spec/lib/simple_token_authentication/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@

it_behaves_like 'a configuration option', 'model_adapters'

it "defauts to ['active_record', 'mongoid']", private: true do
expect(@subject.model_adapters).to eq ['active_record', 'mongoid']
it "defauts to ['active_record', 'cequel', 'mongoid']", private: true do
expect(@subject.model_adapters).to eq ['active_record', 'cequel', 'mongoid']
end
end

Expand All @@ -40,6 +40,7 @@

it 'lists the supported adapters dependencies by default', private: true do
expect(@subject.adapters_dependencies['active_record']).to eq 'ActiveRecord::Base'
expect(@subject.adapters_dependencies['cequel']).to eq 'Cequel::Record'
expect(@subject.adapters_dependencies['mongoid']).to eq 'Mongoid::Document'
expect(@subject.adapters_dependencies['rails']).to eq 'ActionController::Base'
expect(@subject.adapters_dependencies['rails_api']).to eq 'ActionController::API'
Expand Down
39 changes: 39 additions & 0 deletions spec/lib/simple_token_authentication_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,44 @@ class SimpleTokenAuthentication::DummyModel < ActiveRecord::Base; end
end
end

context 'when Cequel is available' do

before(:each) do
stub_const('Cequel', Module.new)
stub_const('Cequel::Record', Class.new)

# define a dummy Cequel adapter
dummy_cequel_adapter = double()
allow(dummy_cequel_adapter).to receive(:base_class).and_return(Cequel::Record)
stub_const('SimpleTokenAuthentication::Adapters::DummyCequelAdapter',
dummy_cequel_adapter)
end

describe '#ensure_models_can_act_as_token_authenticatables' do

before(:each) do
class SimpleTokenAuthentication::DummyModel < Cequel::Record; end
@dummy_model = SimpleTokenAuthentication::DummyModel

expect(@dummy_model.new).to be_instance_of SimpleTokenAuthentication::DummyModel
expect(@dummy_model.new).to be_kind_of Cequel::Record
end

after(:each) do
SimpleTokenAuthentication.send(:remove_const, :DummyModel)
end

it 'allows any kind of Cequel::Record to act as token authenticatable', private: true do
expect(@dummy_model).not_to respond_to :acts_as_token_authenticatable

subject.ensure_models_can_act_as_token_authenticatables [
SimpleTokenAuthentication::Adapters::DummyCequelAdapter]

expect(@dummy_model).to respond_to :acts_as_token_authenticatable
end
end
end

context 'when Mongoid is available' do

before(:each) do
Expand Down Expand Up @@ -98,6 +136,7 @@ class SimpleTokenAuthentication::DummyModel < Mongoid::Document; end
it 'raises NoAdapterAvailableError', private: true do
allow(subject).to receive(:require).and_return(true)
hide_const('ActiveRecord')
hide_const('Cequel')
hide_const('Mongoid')

expect do
Expand Down