auth_strategist 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e232cd5cbd26ba7c2b716eb360a2f19536aabd9a
4
+ data.tar.gz: ec9865919ffbc0661b69bcd0bfe1fb11e4dab246
5
+ SHA512:
6
+ metadata.gz: 13ecf77a210589567110e931fc1ceb81c161043cd9144ad80692da405c4ead8303e16b104ef8bd179af05e8ae89ad625dbcbb63e3af1a7631f202d07067990a6
7
+ data.tar.gz: fb58cc96b63a54e0b7c8159037b2f40e0c52842723c22fe1a32e9b4e7600e283d96ac7b2f6a53159da4fcaea8501456c636bcf6c816a52005aba35d4a6711487
@@ -0,0 +1,10 @@
1
+ .idea
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,17 @@
1
+ Metrics/AbcSize:
2
+ Exclude:
3
+ - './lib/auth_strategist/strategy_interface.rb'
4
+
5
+ Metrics/MethodLength:
6
+ Max: 15
7
+ Exclude:
8
+ - './lib/auth_strategist/strategy_interface.rb'
9
+
10
+ Metrics/LineLength:
11
+ Max: 100
12
+
13
+ Style/Documentation:
14
+ Enabled: false
15
+
16
+ Style/SignalException:
17
+ EnforcedStyle: only_raise
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in auth_strategist.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Krzysztof Buszewicz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,101 @@
1
+ [gem]: https://github.com/buszu/auth_strategist
2
+ [codeclimate]: https://codeclimate.com/github/buszu/auth_strategist/
3
+
4
+ AuthStrategist
5
+ ======
6
+ [![Code Climate](https://codeclimate.com/github/buszu/auth_strategist/badges/gpa.svg)](https://codeclimate.com/github/buszu/auth_strategist)
7
+
8
+ AuthStrategist is a simple gem to define and use authorization strategies.
9
+
10
+ ## Installation
11
+
12
+ ```ruby
13
+ gem 'auth_strategist'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Generate initializer if using Rails:
21
+
22
+ $ rails g auth_strategist:install
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install auth_strategist
27
+
28
+ ## Configuration
29
+
30
+ ```ruby
31
+ AuthStrategist.configure do |c|
32
+ # Set default strategy components (its attributes).
33
+ # Optional and empty by default.
34
+ c.default_strategy_components = [:application, :ref]
35
+
36
+ # Set strategies directory
37
+ # Required if strategies files are not already loaded
38
+ c.strategies_path = 'lib/auth_strategist/strategies'
39
+
40
+ # Register your strategies
41
+ # Required for each strategy you have defined
42
+ c.strategies do |s|
43
+ # E.g. PasswordStrategy will be available under :password key
44
+ s.password = PasswordStrategy
45
+ end
46
+ end
47
+ ```
48
+
49
+ ## Usage
50
+
51
+ ### Defining a Strategy
52
+ * Define a strategy
53
+ ```ruby
54
+ class PasswordStrategy
55
+ include AuthStrategist::StrategyInterface
56
+
57
+ define_components :user, :password
58
+
59
+ def authorize!
60
+ raise StandardError unless user.password == password
61
+ end
62
+ end
63
+ ```
64
+ * Register it
65
+ ```ruby
66
+ AuthStrategist.strategies do |s|
67
+ s.password = PasswordStrategy
68
+ end
69
+ ```
70
+ or
71
+ ```ruby
72
+ AuthStrategist.configure do |c|
73
+ c.strategies do |s|
74
+ s.password = PasswordStrategy
75
+ end
76
+ end
77
+ ```
78
+ ### Using strategies
79
+ * Using strategy by calling authorization Service Object
80
+ ```ruby
81
+ AuthStrategist::Authorize.call strategy: :password,
82
+ user: current_user,
83
+ password: password
84
+ ```
85
+ * Using strategy with authorize! method
86
+ ```ruby
87
+ class SomethingsController < ApplicationController
88
+ include AuthStrategist::Authorization
89
+
90
+ def show
91
+ authorize! strategy: :password, user: current_user, password: password
92
+ end
93
+ end
94
+ ```
95
+ ## Contributing
96
+
97
+ 1. Fork it ( https://github.com/[my-github-username]/auth_strategist/fork )
98
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
99
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
100
+ 4. Push to the branch (`git push origin my-new-feature`)
101
+ 5. Create a new Pull Request
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+
3
+ $LOAD_PATH.push File.expand_path('../lib', __FILE__)
4
+
5
+ require 'auth_strategist/version'
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = 'auth_strategist'
9
+ s.version = AuthStrategist::VERSION
10
+ s.summary = 'Simple gem to realize API actions authorization with different strategies.'
11
+ s.homepage = 'https://github.com/buszu/auth_strategist'
12
+ s.license = 'MIT'
13
+
14
+ s.authors = ['Krzysztof Buszewicz']
15
+ s.email = ['krzysztof.buszewicz@gmail.com']
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- spec/*`.split("\n")
19
+ s.require_paths = ['lib']
20
+
21
+ s.add_development_dependency 'bundler', '~> 1.9'
22
+ s.add_development_dependency 'factory_girl'
23
+ s.add_development_dependency 'rake', '~> 10.0'
24
+ end
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'auth_strategist'
5
+
6
+ require 'irb'
7
+ IRB.start
@@ -0,0 +1,6 @@
1
+ #!/bin/bash
2
+
3
+ set -euo pipefail
4
+ IFS=$'\n\t'
5
+
6
+ bundle install
@@ -0,0 +1,26 @@
1
+ require 'auth_strategist/version'
2
+ require 'auth_strategist/configuration'
3
+ require 'auth_strategist/configuration/strategies_registry'
4
+ require 'auth_strategist/strategy_interface'
5
+ require 'auth_strategist/authorize'
6
+ require 'auth_strategist/authorization'
7
+
8
+ module AuthStrategist
9
+ class << self
10
+ def configuration
11
+ @configuration ||= Configuration.new
12
+ end
13
+
14
+ def configure
15
+ yield(configuration) if block_given?
16
+ configuration
17
+ end
18
+
19
+ def strategies
20
+ return configuration.strategies unless block_given?
21
+ yield(configuration.strategies)
22
+ end
23
+ end
24
+ end
25
+
26
+ require 'generators/auth_strategist/install_generator' if defined?(Rails)
@@ -0,0 +1,17 @@
1
+ module AuthStrategist
2
+ module Authorization
3
+ def self.included(base)
4
+ base.class_eval do
5
+ def authorize!(options = {})
6
+ authorization_service.call(options)
7
+ end
8
+
9
+ private
10
+
11
+ def authorization_service
12
+ AuthStrategist::Authorize
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,41 @@
1
+ module AuthStrategist
2
+ class Authorize
3
+ attr_reader :strategy
4
+
5
+ def self.call(options = {})
6
+ new(options).call
7
+ end
8
+
9
+ def initialize(options = {})
10
+ @strategy = choose_strategy(options)
11
+ end
12
+
13
+ def call
14
+ strategy.authorize!
15
+ end
16
+
17
+ private
18
+
19
+ def choose_strategy(options = {})
20
+ strategy_name = options.delete(:strategy)
21
+ raise(strategy_name_blank) if strategy_name.nil?
22
+
23
+ strategy_class = strategies[strategy_name]
24
+ raise(strategy_not_found(strategy_name)) if strategy_class.nil?
25
+
26
+ strategy_class.new(options)
27
+ end
28
+
29
+ def strategies
30
+ AuthStrategist.strategies.to_h
31
+ end
32
+
33
+ def strategy_not_found(strategy_name)
34
+ StandardError.new("Strategy :#{strategy_name} was not found.")
35
+ end
36
+
37
+ def strategy_name_blank
38
+ StandardError.new(':strategy option must not be blank.')
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,27 @@
1
+ module AuthStrategist
2
+ class Configuration
3
+ attr_accessor :default_strategy_components, :strategies_path
4
+
5
+ def initialize
6
+ @default_strategy_components = []
7
+ @strategies = strategies_registry_class.new
8
+ end
9
+
10
+ def strategies
11
+ return @strategies unless block_given?
12
+
13
+ load_strategies
14
+ yield(@strategies)
15
+ end
16
+
17
+ private
18
+
19
+ def load_strategies
20
+ Dir["./#{strategies_path}/*.rb"].each { |file| require file } if strategies_path
21
+ end
22
+
23
+ def strategies_registry_class
24
+ self.class::StrategiesRegistry
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,11 @@
1
+ require 'ostruct'
2
+
3
+ module AuthStrategist
4
+ class Configuration
5
+ class StrategiesRegistry < OpenStruct
6
+ def delete(strategy_key)
7
+ delete_field(strategy_key)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,46 @@
1
+ module AuthStrategist
2
+ module StrategyInterface
3
+ def self.included(base)
4
+ base.class_eval do
5
+ class << self
6
+ def define_components(*defined_components)
7
+ defined_components.delete_if { |c| default_components.include?(c) }
8
+ defined_components.uniq!
9
+ attr_accessor(*defined_components)
10
+ @components = defined_components
11
+ end
12
+
13
+ def components
14
+ [*default_components, *@components]
15
+ end
16
+
17
+ def default_components
18
+ AuthStrategist.configuration.default_strategy_components.uniq
19
+ end
20
+ end
21
+
22
+ attr_accessor(*components)
23
+
24
+ def initialize(attributes = {})
25
+ assign_component_values(attributes)
26
+ end
27
+
28
+ def components
29
+ self.class.components
30
+ end
31
+
32
+ def authorize!
33
+ warn("#{self.class}#authorize! was not implemented.")
34
+ end
35
+
36
+ private
37
+
38
+ def assign_component_values(attributes = {})
39
+ components.each do |c|
40
+ instance_variable_set("@#{c}", attributes[c])
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ module AuthStrategist
2
+ VERSION = '0.5.0'
3
+ end
@@ -0,0 +1,12 @@
1
+ require 'rails/generators'
2
+
3
+ module AuthStrategist
4
+ class InstallGenerator < ::Rails::Generators::Base
5
+ source_root File.expand_path('../templates', __FILE__)
6
+ desc 'Installs AuthStrategist.'
7
+
8
+ def install
9
+ template 'initializer.rb', 'config/initializers/auth_strategist.rb'
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ AuthStrategist.configure do |c|
2
+ # Set default strategy components
3
+ c.default_strategy_components = []
4
+
5
+ # Set strategies directory path if they are not autoloaded
6
+ # c.strategies_path = 'lib/auth_strategist/strategies'
7
+
8
+ # Register your strategies
9
+ # c.strategies do |s|
10
+ # # Example strategy - will be available under :password key
11
+ # s.password = PasswordStrategy
12
+ # end
13
+ end
@@ -0,0 +1,97 @@
1
+ require 'spec_helper'
2
+
3
+ describe AuthStrategist do
4
+ it 'has a version number' do
5
+ expect(described_class::VERSION).not_to be(nil)
6
+ end
7
+
8
+ it 'includes Configuration class' do
9
+ expect(described_class::Configuration).to be_kind_of(Class)
10
+ end
11
+
12
+ it 'includes Authorize service object' do
13
+ expect(described_class::Authorize).to be_kind_of(Class)
14
+ end
15
+
16
+ it 'includes StrategyInterface module' do
17
+ expect(described_class::StrategyInterface).to be_kind_of(Module)
18
+ end
19
+
20
+ it 'includes Authorization module' do
21
+ expect(described_class::Authorization).to be_kind_of(Module)
22
+ end
23
+
24
+ it 'has configuration getter' do
25
+ expect(described_class).to respond_to(:configuration)
26
+ end
27
+
28
+ it 'responds to .configure' do
29
+ expect(described_class).to respond_to(:configure)
30
+ end
31
+
32
+ describe '.configure' do
33
+ context 'called with block' do
34
+ let(:default_strategy_components) { [:custom_component] }
35
+ let(:strategies_path) { 'spec/dummy/strategies' }
36
+ let(:test_strategy_class) { NotRegistered }
37
+
38
+ before(:each) do
39
+ described_class.configure do |c|
40
+ c.default_strategy_components = default_strategy_components
41
+ c.strategies_path = strategies_path
42
+ c.strategies do |s|
43
+ s.test = test_strategy_class
44
+ end
45
+ end
46
+ end
47
+
48
+ after(:each) do
49
+ described_class.configuration.strategies.delete(:test)
50
+ end
51
+
52
+ it 'sets up configuration (default_strategy_components)' do
53
+ expect(described_class.configuration.default_strategy_components).to(
54
+ eq(default_strategy_components)
55
+ )
56
+ end
57
+
58
+ it 'sets up configuration (strategies_path)' do
59
+ expect(described_class.configuration.strategies_path).to eq(strategies_path)
60
+ end
61
+
62
+ it 'sets up configuration (strategies)' do
63
+ expect(described_class.configuration.strategies.test).to eq(test_strategy_class)
64
+ end
65
+ end
66
+
67
+ context 'called without block' do
68
+ it 'returns current configuration' do
69
+ expect(described_class.configure).to eq(described_class.configuration)
70
+ end
71
+ end
72
+ end
73
+
74
+ describe '.strategies' do
75
+ context 'called with block' do
76
+ let(:test_strategy_class) { NotRegistered }
77
+
78
+ before(:each) do
79
+ described_class.strategies { |s| s.test = test_strategy_class }
80
+ end
81
+
82
+ after(:each) do
83
+ described_class.strategies.delete(:test)
84
+ end
85
+
86
+ it 'sets up configuration (strategies)' do
87
+ expect(described_class.configuration.strategies.test).to eq(test_strategy_class)
88
+ end
89
+ end
90
+
91
+ context 'called without block' do
92
+ it 'returns registered strategies' do
93
+ expect(described_class.strategies).to eq(described_class.configuration.strategies)
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe AuthStrategist::Authorization do
4
+ let(:dummy_controller) { DummyController.new }
5
+
6
+ context 'included' do
7
+ it 'adds #authorize! method to base' do
8
+ expect(dummy_controller).to respond_to(:authorize!)
9
+ end
10
+
11
+ describe '#authorize!' do
12
+ let(:options) { Hash[:strategy, :dummy_strategy] }
13
+
14
+ it 'calls authorization service with given options' do
15
+ allow(AuthStrategist::Authorize).to receive(:call).with(options).and_return(:ok)
16
+ expect(dummy_controller.authorize!(options)).to eq(:ok)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+
3
+ describe AuthStrategist::Authorize do
4
+ let(:authorize) { FactoryGirl.build(:authorize, dummy_component: :ok) }
5
+ let(:authorize_blank_strategy) { FactoryGirl.build(:authorize_blank_strategy) }
6
+ let(:authorize_missing_strategy) { FactoryGirl.build(:authorize_missing_strategy) }
7
+
8
+ describe '.call' do
9
+ context 'when :strategy key was absent in given options' do
10
+ it 'raises an error' do
11
+ expect { described_class.call }.to raise_error(StandardError)
12
+ end
13
+ end
14
+
15
+ context 'when strategy registered under :strategy key was not found' do
16
+ let(:missing_strategy) { :missing_strategy }
17
+
18
+ it 'raises an error' do
19
+ expect { described_class.call(strategy: missing_strategy) }.to raise_error(StandardError)
20
+ end
21
+ end
22
+
23
+ context 'when strategy was found' do
24
+ let(:options) { Hash[:strategy, :dummy_strategy] }
25
+
26
+ before(:each) do
27
+ allow_any_instance_of(described_class).to receive(:call).and_return(:ok)
28
+ end
29
+
30
+ it 'builds new service object and sends #call to it' do
31
+ expect(described_class.call(options)).to eq(:ok)
32
+ end
33
+ end
34
+ end
35
+
36
+ describe '.new' do
37
+ context 'when :strategy key was absent in given options' do
38
+ it 'raises an error' do
39
+ expect { authorize_blank_strategy }.to raise_error(StandardError)
40
+ end
41
+ end
42
+
43
+ context 'when strategy registered under :strategy key was not found' do
44
+ it 'raises an error' do
45
+ expect { authorize_missing_strategy }.to raise_error(StandardError)
46
+ end
47
+ end
48
+
49
+ context 'when strategy was found' do
50
+ let(:strategy_class) { DummyStrategy }
51
+
52
+ it 'sets strategy up' do
53
+ expect(authorize.strategy).to be_kind_of(strategy_class)
54
+ expect(authorize.strategy.dummy_component).to eq(:ok)
55
+ end
56
+
57
+ it 'returns service object instance' do
58
+ expect(authorize).to be_kind_of(AuthStrategist::Authorize)
59
+ end
60
+ end
61
+ end
62
+
63
+ describe '#call' do
64
+ let(:strategy_class) { DummyStrategy }
65
+
66
+ before(:each) do
67
+ allow_any_instance_of(strategy_class).to receive(:authorize!).and_return(:ok)
68
+ end
69
+
70
+ it 'sends #authorize! to strategy' do
71
+ expect(authorize.call).to eq(:ok)
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe AuthStrategist::Configuration do
4
+ context 'by default for specs environment' do
5
+ let(:default_strategy_components) { [:default_component] }
6
+ let(:strategies_path) { 'spec/dummy/strategies' }
7
+ let(:strategies_registry) do
8
+ described_class::StrategiesRegistry.new(dummy_strategy: DummyStrategy,
9
+ missing_authorize: MissingAuthorize)
10
+ end
11
+
12
+ let(:subject) { AuthStrategist.configuration }
13
+
14
+ its(:default_strategy_components) do
15
+ is_expected.to eq(default_strategy_components)
16
+ end
17
+
18
+ its(:strategies_path) { is_expected.to eq(strategies_path) }
19
+ its(:strategies) { is_expected.to eq(strategies_registry) }
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ class DummyController
2
+ include AuthStrategist::Authorization
3
+ end
@@ -0,0 +1,9 @@
1
+ class DummyStrategy
2
+ include AuthStrategist::StrategyInterface
3
+
4
+ define_components :dummy_component
5
+
6
+ def authorize!
7
+ dummy_component
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ class MissingAuthorize
2
+ include AuthStrategist::StrategyInterface
3
+ end
@@ -0,0 +1,3 @@
1
+ class NotRegistered
2
+ include AuthStrategist::StrategyInterface
3
+ end
@@ -0,0 +1,15 @@
1
+ FactoryGirl.define do
2
+ factory :authorize_blank_strategy, class: AuthStrategist::Authorize do
3
+ initialize_with do
4
+ new(attributes)
5
+ end
6
+ end
7
+
8
+ factory :authorize_missing_strategy, parent: :authorize_blank_strategy do
9
+ strategy :missing_strategy
10
+ end
11
+
12
+ factory :authorize, parent: :authorize_blank_strategy do
13
+ strategy :dummy_strategy
14
+ end
15
+ end
@@ -0,0 +1,23 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'auth_strategist'
4
+
5
+ require 'factory_girl'
6
+ require 'factories/authorize'
7
+
8
+ require 'rspec/its'
9
+
10
+ require 'dummy/controllers/dummy_controller'
11
+
12
+ RSpec.configure do |c|
13
+ c.before(:each) do
14
+ AuthStrategist.configure do |g|
15
+ g.default_strategy_components = [:default_component]
16
+ g.strategies_path = 'spec/dummy/strategies'
17
+ g.strategies do |s|
18
+ s.dummy_strategy = DummyStrategy
19
+ s.missing_authorize = MissingAuthorize
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,166 @@
1
+ require 'spec_helper'
2
+
3
+ describe AuthStrategist::StrategyInterface do
4
+ let(:default_components) { AuthStrategist.configuration.default_strategy_components }
5
+ let(:strategy_class) { MissingAuthorize }
6
+
7
+ context 'included' do
8
+ it 'adds .define_components method to base' do
9
+ expect(strategy_class).to respond_to(:define_components)
10
+ end
11
+
12
+ it 'adds .default_components method to base' do
13
+ expect(strategy_class).to respond_to(:default_components)
14
+ end
15
+
16
+ it 'adds .components method to base' do
17
+ expect(strategy_class).to respond_to(:components)
18
+ end
19
+
20
+ it 'adds constructor (.new method) to base' do
21
+ expect(strategy_class).to respond_to(:new)
22
+ end
23
+
24
+ it 'adds #components method to base' do
25
+ expect(strategy_class.new).to respond_to(:components)
26
+ end
27
+
28
+ it 'adds #authorize! method to base' do
29
+ expect(strategy_class.new).to respond_to(:authorize!)
30
+ end
31
+
32
+ it 'adds getters for default components to base' do
33
+ default_components.each do |c|
34
+ expect(strategy_class.new).to respond_to(c)
35
+ end
36
+ end
37
+
38
+ it 'adds setters for default components to base' do
39
+ default_components.each do |c|
40
+ expect(strategy_class.new).to respond_to("#{c}=")
41
+ end
42
+ end
43
+
44
+ describe '.define_components' do
45
+ before(:each) do
46
+ strategy_class.define_components(*components)
47
+ end
48
+
49
+ context 'when component names are not the same as default' do
50
+ let(:components) { [:user, :password] }
51
+
52
+ it 'adds components getters' do
53
+ components.each { |c| expect(strategy_class.new).to respond_to(c) }
54
+ end
55
+
56
+ it 'adds components setters' do
57
+ components.each { |c| expect(strategy_class.new).to respond_to("#{c}=") }
58
+ end
59
+
60
+ it 'adds components to components list' do
61
+ expect(strategy_class.components & components).to eq(components)
62
+ end
63
+ end
64
+
65
+ context 'when component names are the same as default' do
66
+ let(:components) { default_components }
67
+
68
+ it 'is not being added to components list' do
69
+ expect(strategy_class.components).to eq(strategy_class.components.uniq)
70
+ end
71
+ end
72
+
73
+ context 'when no components provided' do
74
+ let(:components) {}
75
+
76
+ it 'is equal to .default_components' do
77
+ expect(strategy_class.components).to eq(strategy_class.default_components)
78
+ end
79
+ end
80
+ end
81
+
82
+ describe '.default_components' do
83
+ context 'for default specs environment configuration' do
84
+ subject { strategy_class }
85
+
86
+ its(:default_components) do
87
+ is_expected.to eq(default_components)
88
+ end
89
+ end
90
+
91
+ context 'for any configuration' do
92
+ let(:components) { [:application, :requestor] }
93
+
94
+ before(:each) do
95
+ AuthStrategist.configure do |c|
96
+ c.default_strategy_components = components
97
+ end
98
+ end
99
+
100
+ it 'returns components configured as default' do
101
+ expect(strategy_class.default_components).to eq(components)
102
+ end
103
+ end
104
+ end
105
+
106
+ describe '.components' do
107
+ let(:components) { [*default_components, :dup, :dup] }
108
+
109
+ before(:each) do
110
+ strategy_class.define_components(*components)
111
+ end
112
+
113
+ it 'returns default and stratagy specific components without duplicates' do
114
+ expect(strategy_class.components).to eq(components.uniq)
115
+ end
116
+ end
117
+
118
+ describe '.new' do
119
+ let(:options) { Hash[:user, 'john', :password, 'rambo'] }
120
+
121
+ before(:each) do
122
+ strategy_class.define_components(*options.keys)
123
+ options[:missing_component] = :missing_component
124
+ end
125
+
126
+ subject { strategy_class.new(options) }
127
+
128
+ it 'assigns components values properly' do
129
+ expect(subject.user).to eq('john')
130
+ expect(subject.password).to eq('rambo')
131
+ expect { subject.missing_component }.to raise_error
132
+ end
133
+ end
134
+
135
+ describe '#components' do
136
+ subject { strategy_class.new }
137
+
138
+ before(:each) do
139
+ allow(strategy_class).to receive(:components).and_return([:ok])
140
+ end
141
+
142
+ it 'calls .components on strategy class' do
143
+ expect(subject.components).to eq([:ok])
144
+ end
145
+ end
146
+
147
+ describe '#authorize!' do
148
+ subject { strategy_class.new }
149
+
150
+ context 'when not implemented within base' do
151
+ it 'returns nil' do
152
+ expect(subject.authorize!).to be_nil
153
+ end
154
+ end
155
+
156
+ context 'when implemented within base' do
157
+ let(:strategy_class) { DummyStrategy }
158
+ subject { strategy_class.new(dummy_component: :ok) }
159
+
160
+ it 'executes like defined' do
161
+ expect(subject.authorize!).to eq(:ok)
162
+ end
163
+ end
164
+ end
165
+ end
166
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: auth_strategist
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: ruby
6
+ authors:
7
+ - Krzysztof Buszewicz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: factory_girl
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description:
56
+ email:
57
+ - krzysztof.buszewicz@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".rubocop.yml"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - auth_strategist.gemspec
71
+ - bin/console
72
+ - bin/setup
73
+ - lib/auth_strategist.rb
74
+ - lib/auth_strategist/authorization.rb
75
+ - lib/auth_strategist/authorize.rb
76
+ - lib/auth_strategist/configuration.rb
77
+ - lib/auth_strategist/configuration/strategies_registry.rb
78
+ - lib/auth_strategist/strategy_interface.rb
79
+ - lib/auth_strategist/version.rb
80
+ - lib/generators/auth_strategist/install_generator.rb
81
+ - lib/generators/auth_strategist/templates/initializer.rb
82
+ - spec/auth_strategist_spec.rb
83
+ - spec/authorization_spec.rb
84
+ - spec/authorize_spec.rb
85
+ - spec/configuration_spec.rb
86
+ - spec/dummy/controllers/dummy_controller.rb
87
+ - spec/dummy/strategies/dummy_strategy.rb
88
+ - spec/dummy/strategies/missing_authorize.rb
89
+ - spec/dummy/strategies/not_registered.rb
90
+ - spec/factories/authorize.rb
91
+ - spec/spec_helper.rb
92
+ - spec/strategy_interface_spec.rb
93
+ homepage: https://github.com/buszu/auth_strategist
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.4.5
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Simple gem to realize API actions authorization with different strategies.
117
+ test_files:
118
+ - spec/auth_strategist_spec.rb
119
+ - spec/authorization_spec.rb
120
+ - spec/authorize_spec.rb
121
+ - spec/configuration_spec.rb
122
+ - spec/dummy/controllers/dummy_controller.rb
123
+ - spec/dummy/strategies/dummy_strategy.rb
124
+ - spec/dummy/strategies/missing_authorize.rb
125
+ - spec/dummy/strategies/not_registered.rb
126
+ - spec/factories/authorize.rb
127
+ - spec/spec_helper.rb
128
+ - spec/strategy_interface_spec.rb
129
+ has_rdoc: