auth_manager 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d8faa6b3f0fdf2d60a4a7ecc95080c850d1601cd
4
- data.tar.gz: 39626c404ac227c2af04a9a7af5c435e2f89f58c
3
+ metadata.gz: 8825b7ce1e7d3ac6011ecc4746e3e2e3df7f68e9
4
+ data.tar.gz: e37e2bf6e29e31cdb4d7d4a82c59c07440cdb2fe
5
5
  SHA512:
6
- metadata.gz: 836948b28449525bafa205b4162b5ea7a2b5d8150599c80c0f71c00554f2f37f12fe7494bc19b01371c3e1df58f506f6c2d26eb920b7c040d7432b8e6703307e
7
- data.tar.gz: 7b2e8d23a673dc4bf13e9422dc40225812ef22b107704209de29880e4f629fc7216f5eba0d13986cd17ea608a9d8ec7e55d4308ae8d4b58ad2418125ca019366
6
+ metadata.gz: 1e23db54ef732fd37dfb709ead2eb234d9c0d8cab2cbf13adbfec1d5d89e86a5c9fc734e1326fd3aeaafd50a157dd8f0c28cb906e06c1f257b81293fbc486ca5
7
+ data.tar.gz: f896e2a4db283411384db63be883734e72e41dc6551bf9548307365fd658d4ef3dc60d4c601022bc6ae2f684f6f1ca87f8ba5a5f35581dc51e8cd9a2f3d357f2
data/auth_manager.gemspec CHANGED
@@ -18,12 +18,11 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_development_dependency "bundler", "~> 1.5"
22
- spec.add_development_dependency "rake", "~> 0"
23
- spec.add_development_dependency "redis","~>3.0", ">= 3.0.7"
21
+ spec.add_development_dependency "bundler"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "redis"
24
24
 
25
25
  #(we added) development dependencies
26
26
  spec.required_ruby_version = ">= 2.0"
27
- spec.add_development_dependency "rspec", "~>2.6"
28
-
27
+ spec.add_development_dependency "rspec"
29
28
  end
@@ -1,44 +1,32 @@
1
1
  # coding: utf-8
2
- require 'redis'
3
- require_relative '../base.rb'
2
+ require_relative '../constants'
4
3
 
5
- class AuthManager::Adder < AuthManager::Validation
4
+ class AuthManager::Adder < AuthManager::Constants
6
5
 
7
- def save?(given_user, option={user: true, app: false})
6
+ def self.save?(given_user, option={user: true, app: false})
8
7
  user = option.fetch(:user) || false
9
8
  app = option.fetch(:app) || false
10
- validate_param( given_user, option, user, app )
11
- save(user, given_user) if available_login?(given_user, user)
9
+ response = {}
10
+ #section to allow new validation class to work with a hash
11
+ hash = hash_format_of(given_user, option)
12
+ #end of section
13
+ #hash for base redis_sadd
14
+ validation_mess = AuthManager::Validation.validate_param( hash )
15
+ hash_for_base = { object: hash[:object], list: 'users' } if hash[:option][:user]
16
+ hash_for_base = { object: hash[:object], list: 'applications' } if hash[:option][:app]
17
+ response = AuthManager::Base.redis_sadd(hash_for_base) if (validation_mess[:code] == 200)
18
+ response[:code] == 200 ? true : false
12
19
  end
13
20
 
14
21
  private ###################################
15
22
 
16
- def available_login?(given_user, user)
17
- login_ok = true
18
- liste = user ? USERS : APPS
19
- all = @redis.smembers(liste)
20
- all.each do |member|
21
- member_json = JSON.parse(member)
22
- param_json = JSON.parse(given_user)
23
- if param_json['login'] && login_exist?(member_json['login'], param_json['login'])
24
- login_ok = false
25
- break
26
- end
27
- end
28
- login_ok
29
- end
30
-
31
- def login_exist?(wanted_login, login)
32
- wanted_login == login
33
- end
34
-
35
- def save_this(liste, params)
36
- @redis.sadd(liste, params)
37
- end
38
-
39
- def save(user, params)
40
- liste = user ? USERS : APPS
41
- save_this(liste, params)
23
+ def self.hash_format_of(given_user, option)
24
+ parsed = JSON.parse(given_user)
25
+ object = {}
26
+ object = { login: parsed['login'], password: parsed['password'] } if parsed['login']
27
+ object = { api_key: parsed['api_key'] } if parsed['api_key']
28
+ object[:deleted] = true if parsed['deleted']
29
+ { object: object, option: option }
42
30
  end
43
31
 
44
32
  end
@@ -1,37 +1,63 @@
1
+ require 'redis'
2
+
1
3
  class AuthManager::Base
2
- attr_accessor :redis
3
4
 
4
- USERS = 'users'
5
- APPS = 'applications'
5
+ def self.redis_sadd(hash)
6
+ validation = hash_of_pair(hash)
7
+ added = false
8
+ validation[:code] == 200 ? added = Redis.current.sadd(hash[:list], hash[:object].to_json) : (return validation)
9
+ added ? {code: 200, message: 'Param saved.'} : { code: 403, message: 'Sorry, This object already exists.' }
10
+ end
6
11
 
7
- def initialize
8
- @redis = Redis.current
12
+ def self.redis_smembers(hash)
13
+ validation = hash_of_single(hash)
14
+ found = false
15
+ validation[:code] == 200 ? found = Redis.current.smembers(hash[:list]) : (return validation)
16
+ found.class == Array ? { code: 200, message: found } : { code: 404, message: 'Param not found!' }
9
17
  end
10
18
 
11
- end
19
+ def self.redis_sismember(hash)
20
+ validation = hash_of_pair(hash)
21
+ is_member = false
22
+ validation[:code] == 200 ? is_member = Redis.current.sismember(hash[:list], hash[:object].to_json) : (return validation)
23
+ is_member ? {code: 200, message: 'Param is a member of the given list.'} : { code: 404, message: 'Sorry, param not in the list' }
24
+ end
12
25
 
13
- class AuthManager::Validation < AuthManager::Base
26
+ def self.redis_srem(hash)
27
+ validation = hash_of_pair(hash)
28
+ removed = false
29
+ validation[:code] == 200 ? removed = Redis.current.srem(hash[:list], hash[:object].to_json) : (return validation)
30
+ removed ? {code: 200, message: 'Param removed.'} : { code: 404, message: 'Sorry, param not in the list, thus not removed' }
31
+ end
14
32
 
15
- def validate_param(param, option, user, app)
16
- validate_option(option, user, app)
17
- valid_param = JSON.parse(param)
18
- option[:user] ? user_as_param(valid_param) : app_as_param(valid_param)
33
+ def self.redis_del(hash)
34
+ validation = hash_of_single(hash)
35
+ deleted = 0
36
+ validation[:code] == 200 ? deleted = Redis.current.del(hash[:list]) : (return validation)
37
+ deleted != 0 ? {code: 200, message: 'Param deleted.'} : { code: 404, message: 'Sorry, param list not found' }
19
38
  end
20
39
 
21
- def user_as_param(valid_param)
22
- if (valid_param['login'] && valid_param['login'].empty?) || (valid_param['password'] && valid_param['password'].empty?)
23
- false
40
+ private #######################################
41
+
42
+ def self.hash_of_pair(hash)
43
+ to_return = {code: 400, message: 'Param is not a hash!'}
44
+ if (hash.class == Hash) && (hash.include?(:list) && hash.include?(:object))
45
+ to_return = {code: 200, message: 'Valid hash.'}
46
+ elsif (hash.class == Hash)
47
+ to_return = {code: 400, message:'Hash not well formed!'}
24
48
  end
49
+ to_return
25
50
  end
26
51
 
27
- def app_as_param(valid_param)
28
- if valid_param['api_key'] && valid_param['api_key'].empty?
29
- false
52
+ def self.hash_of_single(hash)
53
+ to_return = {code: 400, message: 'Param is not a hash!'}
54
+ if ((hash.class == Hash) && (hash.include?(:list)))
55
+ to_return = {code: 200, message: 'Valid hash.'}
56
+ elsif (hash.class == Hash)
57
+ to_return = {code: 400, message:'Hash not well formed!'}
30
58
  end
59
+ to_return
31
60
  end
32
61
 
33
- def validate_option(option, user, app)
34
- false unless option.instance_of?(Hash)
35
- false if (user && app)
36
- end
37
62
  end
63
+
@@ -0,0 +1,4 @@
1
+ class AuthManager::Constants
2
+ USERS = 'users'
3
+ APPS = 'applications'
4
+ end
@@ -1,22 +1,14 @@
1
1
  # coding: utf-8
2
- require 'redis'
3
- require_relative '../base'
4
2
 
5
- class AuthManager::Deleter < AuthManager::Base
3
+ class AuthManager::Deleter
6
4
 
7
- def delete?(obj_to_del, option)
8
- new_obj = format_new_obj(obj_to_del)
9
- fake_delete?(obj_to_del, new_obj, option)
5
+ def self.delete?(obj_to_del, option)
6
+ AuthManager::Updater.update(obj_to_del, format_new_obj(obj_to_del), option)
10
7
  end
11
8
 
12
- private
9
+ private ######################
13
10
 
14
- def fake_delete?(obj_to_del, new_obj, option)
15
- updater = AuthManager::Updater.new
16
- updater.update(obj_to_del, new_obj, option)
17
- end
18
-
19
- def format_new_obj(obj_to_del)
11
+ def self.format_new_obj(obj_to_del)
20
12
  json_obj = JSON.parse(obj_to_del)
21
13
  if json_obj['api_key']
22
14
  {api_key: json_obj['api_key'], deleted: true }.to_json
@@ -1,14 +1,28 @@
1
- require 'redis'
2
- require_relative '../base.rb'
1
+ class AuthManager::Finder < AuthManager::Constants
3
2
 
4
- class AuthManager::Finder < AuthManager::Validation
5
-
6
- def find_object?( param, option )
3
+ def self.find_object?( param, option )
7
4
  user = option.fetch(:user) || false
8
5
  app = option.fetch(:app) || false
9
- validate_param(param, option, user, app)
6
+ hash = hash_format_of(param, option)
7
+ AuthManager::Validation.validate_param( hash )
10
8
  liste = user ? USERS : APPS
11
- @redis.sismember( liste, param ) ? true : false
9
+ hash_for_base = { object: hash[:object], list: 'users' } if hash[:option][:user]
10
+ hash_for_base = { object: hash[:object], list: 'applications' } if hash[:option][:app]
11
+ response = {}
12
+ response = exist?(hash_for_base)
13
+ response[:code] == 200 ? true : false
12
14
  end
13
15
 
16
+ private #############################
17
+
18
+ def self.hash_format_of(given_user, option)
19
+ object = JSON.parse(given_user)
20
+ object = { login: object['login'], password: object['password'] } if object['login']
21
+ object = { api_key: object['api_key'] } if object['api_key']
22
+ { object: object, option: option }
23
+ end
24
+
25
+ def self.exist?( hash_for_base )
26
+ AuthManager::Base.redis_sismember( hash_for_base )
27
+ end
14
28
  end
@@ -1,31 +1,36 @@
1
1
  # coding: utf-8
2
- require 'redis'
3
2
  require_relative '../base'
3
+ require_relative '../constants'
4
4
 
5
- class AuthManager::Updater < AuthManager::Base
5
+ class AuthManager::Updater < AuthManager::Constants
6
6
 
7
- def update(old_obj, new_obj, option)
8
- existing_obj?(old_obj, option)
9
- write_new_info?(new_obj, option)
10
- delete_obj?(old_obj, option)
7
+ def self.update(old_obj, new_obj, option)
8
+ updated = false
9
+ if AuthManager::Finder.find_object?(old_obj, option)
10
+ if p AuthManager::Adder.save?(new_obj, option)
11
+ updated = delete_obj?(old_obj, option)
12
+ end
13
+ end
14
+ updated
11
15
  end
12
16
 
13
17
  private ###########################
14
18
 
15
- def existing_obj?(old_obj, option)
16
- finder = AuthManager::Finder.new
17
- finder.find_object?(old_obj, option)
19
+ def self.hash_format_of(given_user, option)
20
+ object = JSON.parse(given_user)
21
+ object = { login: object['login'], password: object['password'] } if object['login']
22
+ object = { api_key: object['api_key'] } if object['api_key']
23
+ { object: object, option: option }
18
24
  end
19
25
 
20
- def delete_obj?(old_obj, option)
26
+ def self.delete_obj?(old_obj, option)
21
27
  user = option.fetch(:user) || false
22
28
  liste = user ? USERS : APPS
23
- @redis.srem(liste, old_obj)
24
- end
25
-
26
- def write_new_info?(new_obj, option)
27
- adder = AuthManager::Adder.new
28
- adder.save?(new_obj, option)
29
+ hash = hash_format_of(old_obj, option)
30
+ hash_for_base = { object: hash[:object], list: 'users' } if hash[:option][:user]
31
+ hash_for_base = { object: hash[:object], list: 'applications' } if hash[:option][:app]
32
+ done = AuthManager::Base.redis_srem(hash_for_base)
33
+ done[:code] == 200 ? true : false
29
34
  end
30
35
 
31
36
  end
@@ -0,0 +1,84 @@
1
+ class AuthManager::Validation
2
+
3
+ def self.validate_param(hash)
4
+ result = validate_format_of(hash)
5
+ result = validate_option(hash[:option]) if result[:code] == 200
6
+ return result if (result[:code] == 400)
7
+ return result = {code: 400, message: 'Bad object and option association'} unless validate_param_association(hash)
8
+ result = user_as_param(hash[:object]) if result[:code] && hash[:option][:user]
9
+ result = app_as_param(hash[:object]) if result[:code] && hash[:option][:app]
10
+ result
11
+ end
12
+
13
+ private ################################
14
+
15
+ def self.validate_param_association(hash)
16
+ good_association = true
17
+ good_association = false if (user_as_param(hash[:object])[:code] == 200) && (hash[:option][:app])
18
+ good_association = false if (app_as_param(hash[:object])[:code] == 200) && (hash[:option][:user])
19
+ good_association
20
+ end
21
+
22
+ def self.user_as_param(object)
23
+ answer = {code: 400, message: 'Object not well formed'}
24
+ answer = {code: 400, message: 'Object is not a hash'} unless
25
+ object.is_a?(Hash)
26
+ answer = {code: 200, message: 'Object is valid'} if (object_has2keys(object) || object_has3keys(object)) && object_contains_string(object)
27
+ answer
28
+ end
29
+
30
+ def self.app_as_param(object)
31
+ answer = {code: 400, message: 'Object not well formed'}
32
+ answer = {code: 400, message: 'Object is not a hash'} unless
33
+ object.is_a?(Hash)
34
+ answer = {code: 200, message: 'Object is valid'} if (object_has1key(object) || object_has2keys(object)) && object_is_string(object)
35
+ answer
36
+ end
37
+
38
+ def self.object_has1key(object)
39
+ object.include?(:api_key) && (object.length == 1)
40
+ end
41
+
42
+ def self.object_is_string(object)
43
+ object[:api_key].is_a?(String) && !object[:api_key].empty?
44
+ end
45
+
46
+ def self.object_has2keys(object)
47
+ ((object.include?(:login) && (object.include?(:password)))|| (object.include?(:api_key) && object.include?(:deleted))) && (object.length == 2)
48
+ end
49
+
50
+ def self.object_has3keys(object)
51
+ (object.include?(:login) && object.include?(:password) && object.include?(:deleted) && (object.length == 3))
52
+ end
53
+
54
+ def self.object_contains_string(object)
55
+ object[:login].is_a?(String) && object[:password].is_a?(String) && !object[:login].empty? && !object[:password].empty?
56
+ end
57
+
58
+ def self.validate_option(option)
59
+ answer = {code: 400, message: 'Option not well formed'}
60
+ answer = {code: 400, message: 'Option is not a hash'} unless
61
+ option.is_a?(Hash)
62
+ answer = {code: 200, message: 'Option is valid'} if option_has2keys(option) && option_keys_are_bools(option) && only_one_option(option)
63
+ answer
64
+ end
65
+
66
+ def self.option_has2keys(option)
67
+ (option && option.include?(:user) && option.include?(:app) && (option.length == 2))
68
+ end
69
+
70
+ def self.option_keys_are_bools(option)
71
+ option && (option[:user].is_a?(TrueClass) || option[:user].is_a?(FalseClass)) && (option[:app].is_a?(TrueClass) || option[:app].is_a?(FalseClass))
72
+ end
73
+
74
+ def self.only_one_option(option)
75
+ (option && option[:user] && ! option[:app]) || (option && !option[:user] && option[:app])
76
+ end
77
+
78
+ def self.validate_format_of(hash)
79
+ answer = {code: 400, message: 'Param not well formed!'}
80
+ answer = {code: 400, message: 'Param not a hash!'} unless hash.is_a?(Hash)
81
+ (answer = {code: 200, message: 'Param is valid.'}) if (hash.is_a?(Hash) && hash.include?(:object) && hash.include?(:option))
82
+ answer
83
+ end
84
+ end
@@ -1,3 +1,3 @@
1
1
  module AuthManager
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
data/lib/auth_manager.rb CHANGED
@@ -3,9 +3,15 @@ require 'auth_manager/adder/adder'
3
3
  require 'auth_manager/finder/finder'
4
4
  require 'auth_manager/updater/updater'
5
5
  require 'auth_manager/deleter/deleter'
6
+ require 'auth_manager/base'
7
+ require 'auth_manager/validation'
8
+ require 'auth_manager/constants'
6
9
 
7
10
  module AuthManager
8
11
  # Your code goes here...
12
+ class Constants
13
+ end
14
+
9
15
  class Base
10
16
  end
11
17
 
data/spec/adder_spec.rb CHANGED
@@ -1,43 +1,42 @@
1
1
  require_relative 'spec_helper'
2
2
 
3
3
  describe AuthManager::Adder do
4
- let(:adder) { AuthManager::Adder.new }
5
4
  let(:user) { { login: 'nano', password: 'password' } }
6
5
  let(:app) { { api_key: 'api_key' } }
7
6
  let(:opt) { {user: false, app: false} }
8
7
 
9
- describe '.initialize' do
10
- context 'When created' do
11
- it { expect( adder.redis).to_not be_nil }
12
- end
13
- end
14
-
15
- describe '#save?' do
8
+ describe '.save?' do
16
9
  #cleaning valid_user and or valid app before new save
17
10
  before(:each) do
18
- adder.redis.del('users', user)
19
- adder.redis.del('applications', app)
11
+ AuthManager::Base.redis_del({ list: 'users' })
12
+ AuthManager::Base.redis_del({ list: 'applications' })
20
13
  end
21
14
  #normal cases
22
15
  context 'When saved a valid user' do
23
16
  it {
24
17
  swap_option_to('user')
25
- expect( adder.save?(user.to_json, opt)).to be_true
18
+ expect( AuthManager::Adder.save?(user.to_json, opt)).to be_true
26
19
  }
27
20
  end
28
21
  context 'When saved a valid app' do
29
22
  it {
30
23
  swap_option_to('app')
31
- expect( adder.save?(app.to_json, opt)).to be_true
24
+ expect( AuthManager::Adder.save?(app.to_json, opt)).to be_true
32
25
  }
33
26
  end
34
27
  context 'When saved with an existing login' do
35
- it {
36
- expect( adder.save?(user.to_json, opt) ).to be_true
37
- expect( adder.save?(user.to_json, opt)).to be_false
38
- user['password'] = 'another password'
39
- expect( adder.save?(user.to_json, opt)).to be_false
40
- }
28
+ it do
29
+ swap_option_to('user')
30
+ expect( AuthManager::Adder.save?(user.to_json, opt) ).to be_true
31
+ expect( AuthManager::Adder.save?(user.to_json, opt)).to be_false
32
+ end
33
+ end
34
+ context 'When saving an existing api_key' do
35
+ it do
36
+ swap_option_to('app')
37
+ expect( AuthManager::Adder.save?(app.to_json, opt) ).to be_true
38
+ expect( AuthManager::Adder.save?(app.to_json, opt) ).to be_false
39
+ end
41
40
  end
42
41
  end
43
42
 
data/spec/base_spec.rb ADDED
@@ -0,0 +1,96 @@
1
+ # coding: utf-8
2
+
3
+ require_relative 'spec_helper'
4
+
5
+ describe AuthManager::Base do
6
+ before(:each)do
7
+ AuthManager::Base.redis_del({list: 'my_list'})
8
+ AuthManager::Base.redis_del({list: ''})
9
+ end
10
+
11
+ describe '.redis_sadd' do
12
+ context 'When given an object different from a hash' do
13
+ it { expect(AuthManager::Base.redis_sadd(nil)[:code]).to eq(400) }
14
+ it { expect(AuthManager::Base.redis_sadd([])[:code]).to eq(400) }
15
+ it { expect(AuthManager::Base.redis_sadd('')[:code]).to eq(400) }
16
+ it { expect(AuthManager::Base.redis_sadd(1524)[:code]).to eq(400) }
17
+ it { expect(AuthManager::Base.redis_sadd(true)[:code]).to eq(400) }
18
+ end
19
+ context 'when given a bad hash' do
20
+ it { expect(AuthManager::Base.redis_sadd({})[:code]).to eq(400) }
21
+ it { expect(AuthManager::Base.redis_sadd({nest: ''})[:code]).to eq(400) }
22
+ it { expect(AuthManager::Base.redis_sadd({t: 12, a: 'fe', aa: true})[:code]).to eq(400) }
23
+ end
24
+ context 'When given a correct hash' do
25
+ it { expect(AuthManager::Base.redis_sadd({list: 'my_list', object: 'my_object'})[:code]).to eq(200) }
26
+ end
27
+ context 'When given another correct hash' do
28
+ it { expect(AuthManager::Base.redis_sadd({list: 'my_list', object: 'my_object'})[:code]).to eq(200) }
29
+ end
30
+ context 'When saving an existing object' do
31
+ it do
32
+ expect(AuthManager::Base.redis_sadd({list: 'my_list', object: 'my_object'})[:code]).to eq(200)
33
+ expect(AuthManager::Base.redis_sadd({list: 'my_list', object: 'my_object'})[:code]).to eq(403)
34
+ end
35
+ end
36
+ context 'When given a random list and object in the hash' do
37
+ it { expect(AuthManager::Base.redis_sadd({list: g_rand_string, object: g_rand_string})[:code]).to eq(200) }
38
+ end
39
+ end
40
+
41
+ describe '.redis_smembers' do
42
+ context 'When given an invalid hash' do
43
+ it { expect(AuthManager::Base.redis_smembers({})[:code]).to eq(400) }
44
+ it { expect(AuthManager::Base.redis_smembers({nest: ''})[:code]).to eq(400) }
45
+ it { expect(AuthManager::Base.redis_smembers({t: 12, a: 'fe', aa: true})[:code]).to eq(400) }
46
+ end
47
+ context 'when given list name in a hash' do
48
+ it { expect(AuthManager::Base.redis_smembers({list: 'my_list'})[:message].class).to eq(Array) }
49
+ end
50
+ context 'When the list is not in the DB, length is 0' do
51
+ it { expect(AuthManager::Base.redis_smembers({list: 'my_list'})[:message].length).to eq(0) }
52
+ end
53
+ context 'When the list has at least one element, lenght > 0' do
54
+ it do
55
+ expect(AuthManager::Base.redis_sadd({ list: 'my_list', object: 'my_object' })).to be_true
56
+ expect(AuthManager::Base.redis_smembers({ list: 'my_list' })[:message].length).to be > 0
57
+ end
58
+ end
59
+ end
60
+
61
+ describe '.redis_sismember' do
62
+ context 'When the object is in the list' do
63
+ it 'expect sismember to return 200' do
64
+ expect( AuthManager::Base.redis_sadd({list: nil, object: nil})[:code] ).to eq(200)
65
+ expect( AuthManager::Base.redis_sismember({list: nil, object: nil})[:code]).to eq(200)
66
+ end
67
+ end
68
+ context 'When object is not in the list' do
69
+ it { expect( AuthManager::Base.redis_sismember({list: '', object: ''})[:code] ).to eq(404) }
70
+ end
71
+ end
72
+
73
+ describe '.redis_srem' do
74
+ context 'When the object is removed' do
75
+ it do
76
+ expect(AuthManager::Base.redis_sadd({list: 'my_list', object: 'my_object'})[:code]).to eq(200)
77
+ expect( AuthManager::Base.redis_srem({list: 'my_list', object: 'my_object'})[:code] ).to eq(200)
78
+ end
79
+ end
80
+ context 'When the object is not in the list' do
81
+ it { expect( AuthManager::Base.redis_srem({list: 'my_list', object: 'my_object'})[:code] ).to eq(404) }
82
+ end
83
+ end
84
+
85
+ describe '.redis_del' do
86
+ context 'When the list is deleted' do
87
+ it do
88
+ expect(AuthManager::Base.redis_sadd({list: 'my_list', object: 'my_object'})[:code]).to eq(200)
89
+ expect( AuthManager::Base.redis_del({list: 'my_list'})[:code] ).to eq(200)
90
+ end
91
+ end
92
+ context 'When the list is not deleted' do
93
+ it { expect( AuthManager::Base.redis_del({list: 'my_list'})[:code] ).to eq(404) }
94
+ end
95
+ end
96
+ end
data/spec/deleter_spec.rb CHANGED
@@ -1,23 +1,22 @@
1
1
  require_relative 'spec_helper'
2
2
 
3
3
  describe AuthManager::Deleter do
4
- let(:deleter) { AuthManager::Deleter.new }
5
4
  let(:user) { {login: 'login', password: 'password'} }
6
5
  let(:app) { {api_key: 'api_key'} }
7
6
  let(:opt) { { user: false, app: true } }
8
7
  describe '#delete' do
9
8
  before(:each) do
10
- deleter.redis.del('users')
11
- deleter.redis.del('applications')
12
- deleter.redis.sadd('users', user.to_json)
13
- deleter.redis.sadd('applications', app.to_json)
9
+ AuthManager::Base.redis_del({ list: 'users' })
10
+ AuthManager::Base.redis_del({ list: 'applications' })
11
+ AuthManager::Base.redis_sadd( { object: user, list: 'users'} )
12
+ AuthManager::Base.redis_sadd({ object: app, list: 'applications' })
14
13
  end
15
14
 
16
15
  context 'When deleted a user' do
17
16
  it {
18
17
  swap_option_to('user')
19
18
  user[:login] = 'login'
20
- expect( deleter.delete?(user.to_json, opt)).to be_true
19
+ expect( AuthManager::Deleter.delete?(user.to_json, opt)).to be_true
21
20
  }
22
21
  end
23
22
 
@@ -25,14 +24,14 @@ describe AuthManager::Deleter do
25
24
  it {
26
25
  swap_option_to('user')
27
26
  user[:login] = 'not a login'
28
- expect( deleter.delete?(user.to_json, opt)).to be_false
27
+ expect( AuthManager::Deleter.delete?(user.to_json, opt)).to be_false
29
28
  }
30
29
  end
31
30
  context 'When deleted an app' do
32
31
  it {
33
32
  swap_option_to('app')
34
33
  app[:api_key] = 'api_key'
35
- expect( deleter.delete?(app.to_json, opt)).to be_true
34
+ expect( AuthManager::Deleter.delete?(app.to_json, opt)).to be_true
36
35
  }
37
36
  end
38
37
 
@@ -40,7 +39,7 @@ describe AuthManager::Deleter do
40
39
  it {
41
40
  swap_option_to('app')
42
41
  app[:api_key] = 'not an api_key'
43
- expect( deleter.delete?(app.to_json, opt)).to be_false
42
+ expect( AuthManager::Deleter.delete?(app.to_json, opt)).to be_false
44
43
  }
45
44
  end
46
45
  end
data/spec/finder_spec.rb CHANGED
@@ -1,42 +1,42 @@
1
+ # coding: utf-8
1
2
  require_relative 'spec_helper'
2
3
 
3
4
  describe AuthManager::Finder do
4
- let(:finder) { AuthManager::Finder.new }
5
5
  let(:user) { { login: 'login', password: 'password' }}
6
6
  let(:app) { { api_key: 'api_key' } }
7
7
  let(:opt) { { user: true, app: false } }
8
8
  describe '#find_object?' do
9
9
  before(:each) do
10
- finder.redis.del('users')
11
- finder.redis.del('applications')
12
- finder.redis.sadd('users', user.to_json)
13
- finder.redis.sadd('applications', app.to_json)
10
+ AuthManager::Base.redis_del({ list: 'users' })
11
+ AuthManager::Base.redis_del({ list: 'applications' })
12
+ AuthManager::Base.redis_sadd( { object: user, list: 'users'} )
13
+ AuthManager::Base.redis_sadd({ object: app, list: 'applications' })
14
14
  end
15
15
  context 'When user is found' do
16
16
  it {
17
17
  swap_option_to('user')
18
- expect( finder.find_object?(user.to_json, opt ) ).to be_true
18
+ expect( AuthManager::Finder.find_object?(user.to_json, opt ) ).to be_true
19
19
  }
20
20
  end
21
21
  context 'When user is not found' do
22
22
  it {
23
23
  swap_option_to('user')
24
24
  user[:login] = 'not a login'
25
- expect( finder.find_object?( user.to_json, opt ) ).to be_false
25
+ expect( AuthManager::Finder.find_object?( user.to_json, opt ) ).to be_false
26
26
  }
27
27
  end
28
28
 
29
29
  context 'When app is found' do
30
30
  it {
31
31
  swap_option_to('app')
32
- expect( finder.find_object?(app.to_json, opt ) ).to be_true
32
+ expect( AuthManager::Finder.find_object?(app.to_json, opt ) ).to be_true
33
33
  }
34
34
  end
35
35
  context 'When app is not found' do
36
36
  it {
37
37
  swap_option_to('app')
38
38
  app[:api_key] = 'not an api_key'
39
- expect( finder.find_object?( app.to_json, opt ) ).to be_false
39
+ expect( AuthManager::Finder.find_object?( app.to_json, opt ) ).to be_false
40
40
  }
41
41
  end
42
42
  end
data/spec/spec_helper.rb CHANGED
@@ -1,9 +1,13 @@
1
1
  require_relative './fixtures/environment'
2
2
 
3
+ def g_rand_string
4
+ a = []
5
+ ('32'..'127').to_a.each { |e| a << e.to_i.chr }
6
+ (0..10).map{ [rand(a.length)] }.join
7
+ end
3
8
 
4
9
  def swap_option_to(list)
5
- case list
6
- when 'user'
10
+ if list == 'user'
7
11
  opt[:user] = true
8
12
  opt[:app] = false
9
13
  else
@@ -13,8 +17,8 @@ def swap_option_to(list)
13
17
  end
14
18
 
15
19
  def new_object_from(object)
16
- case object
17
- when object.include?(:login)
20
+ if object.include?(:login)
21
+ object[:login] = 'new login'
18
22
  object[:password] = 'new password'
19
23
  else
20
24
  object[:api_key] = 'new api_key'
data/spec/updater_spec.rb CHANGED
@@ -2,41 +2,40 @@
2
2
  require_relative 'spec_helper'
3
3
 
4
4
  describe AuthManager::Updater do
5
- let(:updater) { AuthManager::Updater.new }
6
5
  let(:app) { { api_key: 'api_key' } }
7
6
  let(:user) { { login: 'login', password: 'password' }}
8
7
  let(:opt) { { user: true, app: false } }
9
8
  describe '#update' do
10
9
  before(:each) do
11
- updater.redis.del('users')
12
- updater.redis.del('applications')
13
- updater.redis.sadd('users', user.to_json)
14
- updater.redis.sadd('applications', app.to_json)
10
+ AuthManager::Base.redis_del({ list: 'users' })
11
+ AuthManager::Base.redis_del({ list: 'applications' })
12
+ AuthManager::Base.redis_sadd( { object: user, list: 'users'} )
13
+ AuthManager::Base.redis_sadd({ object: app, list: 'applications' })
15
14
  end
16
15
  context 'When user object is updated' do
17
16
  it {
18
17
  swap_option_to('user')
19
- expect(updater.update(user.to_json, new_object_from(user).to_json, opt)).to be_true
18
+ expect(AuthManager::Updater.update(user.to_json, new_object_from(user).to_json, opt)).to be_true
20
19
  }
21
20
  end
22
21
  context 'When app object is updated' do
23
22
  it {
24
23
  swap_option_to('app')
25
- expect(updater.update(app.to_json, new_object_from(app).to_json, opt)).to be_true
24
+ expect(AuthManager::Updater.update(app.to_json, new_object_from(app).to_json, opt)).to be_true
26
25
  }
27
26
  end
28
27
  context 'When user object is not updated' do
29
28
  it {
30
29
  swap_option_to('user')
31
- user[:login] = 'not a login'
32
- expect(updater.update(user.to_json, new_object_from(user).to_json, opt)).to be_false
30
+ user[:login] = 'this is not a login'
31
+ expect(AuthManager::Updater.update(user.to_json, new_object_from(user).to_json, opt)).to be_false
33
32
  }
34
33
  end
35
34
  context 'When app object is not updated' do
36
35
  it {
37
36
  swap_option_to('app')
38
37
  app[:api_key] = 'not an api_key'
39
- expect(updater.update(app.to_json, new_object_from(app).to_json, opt)).to be_false
38
+ expect(AuthManager::Updater.update(app.to_json, new_object_from(app).to_json, opt)).to be_false
40
39
  }
41
40
  end
42
41
  end
@@ -0,0 +1,30 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe AuthManager::Validation do
4
+ describe '.validate_param' do
5
+ context 'When given something different from a hash' do
6
+ it { expect(AuthManager::Validation.validate_param(nil)[:code]).to eq(400) }
7
+ it { expect(AuthManager::Validation.validate_param('')[:code]).to eq(400) }
8
+ it { expect(AuthManager::Validation.validate_param([])[:code]).to eq(400) }
9
+ it { expect(AuthManager::Validation.validate_param(152)[:code]).to eq(400) }
10
+ end
11
+ context 'When given a bad formed hash' do
12
+ it { expect(AuthManager::Validation.validate_param({})[:code]).to eq(400) }
13
+ it { expect(AuthManager::Validation.validate_param({ob: ''})[:code]).to eq(400) }
14
+ it { expect(AuthManager::Validation.validate_param({ob: '', op: ''})[:code]).to eq(400) }
15
+ it { expect(AuthManager::Validation.validate_param({object: '', op: ''})[:code]).to eq(400) }
16
+ it { expect(AuthManager::Validation.validate_param({object: nil, option: nil})[:code]).to eq(400) }
17
+ it { expect(AuthManager::Validation.validate_param({object: {name: nil}, option: {user: nil}})[:code]).to eq(400) }
18
+ it { expect(AuthManager::Validation.validate_param({object: {login: '', password: ''}, option: {user: true, app: true}})[:code]).to eq(400) }
19
+ it { expect(AuthManager::Validation.validate_param({object: {login: '', password: ''}, option: {user: true, app: false}})[:code]).to eq(400) }
20
+ it { expect(AuthManager::Validation.validate_param({object: {api_key: ''}, option: {user: true, app: false}})[:code]).to eq(400) }
21
+ it { expect(AuthManager::Validation.validate_param({object: {api_key: ''}, option: {user: false, app: true}})[:code]).to eq(400) }
22
+ it { expect(AuthManager::Validation.validate_param({object: {login: 'my_login', password: 'my_password'}, option: { user: false, app: true }})[:code]).to eq(400) }
23
+ it { expect(AuthManager::Validation.validate_param({object: {api_key: 'api_key'}, option: { user: true, app: false }})[:code]).to eq(400) }
24
+ end
25
+ context 'When given a well formed hash' do
26
+ it { expect(AuthManager::Validation.validate_param({object: {api_key: 'api_key'}, option: { user: false, app: true }})[:code]).to eq(200) }
27
+ it { expect(AuthManager::Validation.validate_param({object: {login: 'my_login', password: 'my_password'}, option: { user: true, app: false }})[:code]).to eq(200) }
28
+ end
29
+ end
30
+ end
metadata CHANGED
@@ -1,77 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: auth_manager
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - atacraft && fabira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-16 00:00:00.000000000 Z
11
+ date: 2014-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.5'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.5'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: redis
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '3.0'
48
45
  - - ">="
49
46
  - !ruby/object:Gem::Version
50
- version: 3.0.7
47
+ version: '0'
51
48
  type: :development
52
49
  prerelease: false
53
50
  version_requirements: !ruby/object:Gem::Requirement
54
51
  requirements:
55
- - - "~>"
56
- - !ruby/object:Gem::Version
57
- version: '3.0'
58
52
  - - ">="
59
53
  - !ruby/object:Gem::Version
60
- version: 3.0.7
54
+ version: '0'
61
55
  - !ruby/object:Gem::Dependency
62
56
  name: rspec
63
57
  requirement: !ruby/object:Gem::Requirement
64
58
  requirements:
65
- - - "~>"
59
+ - - ">="
66
60
  - !ruby/object:Gem::Version
67
- version: '2.6'
61
+ version: '0'
68
62
  type: :development
69
63
  prerelease: false
70
64
  version_requirements: !ruby/object:Gem::Requirement
71
65
  requirements:
72
- - - "~>"
66
+ - - ">="
73
67
  - !ruby/object:Gem::Version
74
- version: '2.6'
68
+ version: '0'
75
69
  description: '"Authenticate developpers and applications."'
76
70
  email:
77
71
  - atacraft@gmail.com, fabira90@gmail.com
@@ -91,17 +85,21 @@ files:
91
85
  - lib/auth_manager.rb
92
86
  - lib/auth_manager/adder/adder.rb
93
87
  - lib/auth_manager/base.rb
88
+ - lib/auth_manager/constants.rb
94
89
  - lib/auth_manager/deleter/deleter.rb
95
90
  - lib/auth_manager/finder/finder.rb
96
91
  - lib/auth_manager/updater/updater.rb
92
+ - lib/auth_manager/validation.rb
97
93
  - lib/auth_manager/version.rb
98
94
  - spec/adder_spec.rb
95
+ - spec/base_spec.rb
99
96
  - spec/deleter_spec.rb
100
97
  - spec/feed/populate.rb
101
98
  - spec/finder_spec.rb
102
99
  - spec/fixtures/environment.rb
103
100
  - spec/spec_helper.rb
104
101
  - spec/updater_spec.rb
102
+ - spec/validation_spec.rb
105
103
  homepage: ''
106
104
  licenses:
107
105
  - MIT
@@ -128,9 +126,11 @@ specification_version: 4
128
126
  summary: '"Module auth_manager"'
129
127
  test_files:
130
128
  - spec/adder_spec.rb
129
+ - spec/base_spec.rb
131
130
  - spec/deleter_spec.rb
132
131
  - spec/feed/populate.rb
133
132
  - spec/finder_spec.rb
134
133
  - spec/fixtures/environment.rb
135
134
  - spec/spec_helper.rb
136
135
  - spec/updater_spec.rb
136
+ - spec/validation_spec.rb