helioth 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ module Helioth
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,116 @@
1
+ require 'spec_helper'
2
+
3
+ describe Helioth::ControllerAdditions do
4
+
5
+ before(:each) do
6
+ stub_const("DSL", Helioth::Dsl.load("spec/fixtures/valid_dsl.rb"))
7
+ end
8
+
9
+ describe ".load_and_authorize_for" do
10
+ it "should be an ApplicationController method" do
11
+ expect(ApplicationController.public_methods.include?(:load_and_authorize_for)).to be(true)
12
+ end
13
+ end
14
+
15
+ describe "#access_to?" do
16
+ it "should return a boolean" do
17
+ expect_any_instance_of(User).to receive(:helioth_role?).and_return(:beta)
18
+ expect_any_instance_of(ActionController::Base).to receive(:current_user).and_return(User.new)
19
+
20
+ expect_any_instance_of(Instance).to receive(:helioth_role?).and_return(:beta)
21
+ expect_any_instance_of(ActionController::Base).to receive(:current_instance).and_return(Instance.new)
22
+
23
+ boolean = ActionController::Base.new.access_to?(:no_name)
24
+ expect( !!boolean ).to be(boolean)
25
+ end
26
+
27
+ it "should return false when locale_access is false" do
28
+ expect_any_instance_of(ActionController::Base).to receive(:locale_access_to?).and_return(false)
29
+ expect( ActionController::Base.new.access_to?(:no_name) ).to be(false)
30
+ end
31
+
32
+ describe "when locale_access is true" do
33
+ before(:each) do
34
+ expect_any_instance_of(ActionController::Base).to receive(:locale_access_to?).and_return(true)
35
+ end
36
+
37
+ it "should return true if user_access is true" do
38
+ expect_any_instance_of(ActionController::Base).to receive(:user_access_to?).and_return(true)
39
+ expect( ActionController::Base.new.access_to?(:no_name) ).to be(true)
40
+ end
41
+
42
+ describe "and user_access is false" do
43
+ before(:each) do
44
+ expect_any_instance_of(ActionController::Base).to receive(:user_access_to?).and_return(false)
45
+ end
46
+
47
+ it "should return true if instance_access is true" do
48
+ expect_any_instance_of(ActionController::Base).to receive(:instance_access_to?).and_return(true)
49
+ expect( ActionController::Base.new.access_to?(:no_name) ).to be(true)
50
+ end
51
+
52
+ it "should return false if instance_access is false" do
53
+ expect_any_instance_of(ActionController::Base).to receive(:instance_access_to?).and_return(false)
54
+ expect( ActionController::Base.new.access_to?(:no_name) ).to be(false)
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ describe "#locale_access_to?" do
61
+ it "should return a boolean" do
62
+ boolean = ActionController::Base.new.locale_access_to?(:no_name)
63
+ expect( !!boolean ).to be(boolean)
64
+ end
65
+
66
+ it "should thrown an error when no argument is used" do
67
+ expect{ ActionController::Base.new.locale_access_to? }.to raise_error(ArgumentError)
68
+ end
69
+
70
+ it "should accept a list of arguments" do
71
+ expect{ ActionController::Base.new.locale_access_to?(:no_name, :index, :search) }.to_not raise_error
72
+ end
73
+ end
74
+
75
+ describe "#user_access_to?" do
76
+ it "should return a boolean" do
77
+ expect_any_instance_of(User).to receive(:helioth_role?).and_return(:beta)
78
+ expect_any_instance_of(ActionController::Base).to receive(:current_user).and_return(User.new)
79
+
80
+ boolean = ActionController::Base.new.user_access_to?(:no_name)
81
+ expect( !!boolean ).to be(boolean)
82
+ end
83
+
84
+ it "should thrown an error when no argument is used" do
85
+ expect{ ActionController::Base.new.user_access_to? }.to raise_error(ArgumentError)
86
+ end
87
+
88
+ it "should accept a list of arguments" do
89
+ expect_any_instance_of(User).to receive(:helioth_role?).and_return(:beta)
90
+ expect_any_instance_of(ActionController::Base).to receive(:current_user).and_return(User.new)
91
+ expect{ ActionController::Base.new.user_access_to?(:no_name, :index, :search) }.to_not raise_error
92
+ end
93
+ end
94
+
95
+ describe "#instance_access_to?" do
96
+ it "should return a boolean" do
97
+ expect_any_instance_of(Instance).to receive(:helioth_role?).and_return(:beta)
98
+ expect_any_instance_of(ActionController::Base).to receive(:current_instance).and_return(Instance.new)
99
+
100
+ boolean = ActionController::Base.new.instance_access_to?(:no_name)
101
+ expect( !!boolean ).to be(boolean)
102
+ end
103
+
104
+ it "should thrown an error when no argument is used" do
105
+ expect{ ActionController::Base.new.instance_access_to? }.to raise_error(ArgumentError)
106
+ end
107
+
108
+ it "should accept a list of arguments" do
109
+ expect_any_instance_of(Instance).to receive(:helioth_role?).and_return(:beta)
110
+ expect_any_instance_of(ActionController::Base).to receive(:current_instance).and_return(Instance.new)
111
+
112
+ expect{ ActionController::Base.new.instance_access_to?(:no_name, :index, :search) }.to_not raise_error
113
+ end
114
+ end
115
+
116
+ end
data/spec/dsl_spec.rb ADDED
@@ -0,0 +1,142 @@
1
+ require 'spec_helper'
2
+
3
+ describe Helioth::Dsl do
4
+
5
+ before(:all) do
6
+ @dsl = Helioth::Dsl.load("spec/fixtures/valid_dsl.rb")
7
+ end
8
+
9
+ describe "#features" do
10
+ it "should retrieve all features" do
11
+ expect(@dsl.features.size).to be == 3
12
+ end
13
+ end
14
+
15
+ describe "#feature" do
16
+ it "should retrieve the feature :tutoring" do
17
+ expect(@dsl.feature(:tutoring)).to be_an_instance_of(Helioth::Feature)
18
+ expect(@dsl.feature(:tutoring).name).to be == :tutoring
19
+ end
20
+ end
21
+
22
+ describe "#action" do
23
+ it "should find the action :search for :tutoring" do
24
+ expect(@dsl.action(:tutoring, :search)).to be_an_instance_of(Helioth::Action)
25
+ expect(@dsl.action(:tutoring, :search).name).to be == :search
26
+ end
27
+
28
+ it "should not find the action :search for :no_name" do
29
+ expect(@dsl.action(:no_name, :search)).to be == nil
30
+ end
31
+ end
32
+
33
+ describe "#authorized_for_locale?" do
34
+ describe "when available locales locales is :en" do
35
+ it "should be true for feature :tutoring and locale :en" do
36
+ expect(@dsl.authorized_for_locale?(:tutoring, [], :en)).to be(true)
37
+ end
38
+
39
+ it "should be false for feature :tutoring and locale :fr" do
40
+ expect(@dsl.authorized_for_locale?(:tutoring, [], :fr)).to be(false)
41
+ end
42
+
43
+ it "should be true for feature :tutoring, action :search and locale :fr" do
44
+ expect(@dsl.authorized_for_locale?(:tutoring, [:search, :send], :fr)).to be(true)
45
+ end
46
+
47
+ it "should be false for feature :tutoring, action :search and locale :en" do
48
+ expect(@dsl.authorized_for_locale?(:tutoring, [:search, :send], :en)).to be(false)
49
+ end
50
+
51
+ it "should be true for feature :social_learning and locale :fr" do
52
+ expect(@dsl.authorized_for_locale?(:social_learning, [], :fr)).to be(true)
53
+ end
54
+
55
+ it "should be true for feature :social_learning and locale :en" do
56
+ expect(@dsl.authorized_for_locale?(:social_learning, [], :en)).to be(true)
57
+ end
58
+ end
59
+
60
+ describe "when available locales locales are [:fr, :en]" do
61
+ before(:all) do
62
+ I18n.available_locales = [:en, :fr]
63
+ @dsl2 = Helioth::Dsl.load("spec/fixtures/valid_dsl.rb")
64
+ end
65
+
66
+ it "should be true for feature :tutoring and locale :fr" do
67
+ expect(@dsl2.authorized_for_locale?(:tutoring, [], :fr)).to be(true)
68
+ end
69
+
70
+ it "should be true for feature :tutoring, action :search and locale :fr" do
71
+ expect(@dsl2.authorized_for_locale?(:tutoring, [:search, :send], :fr)).to be(true)
72
+ end
73
+
74
+ it "should be false for feature :tutoring, action :search and locale :en" do
75
+ expect(@dsl2.authorized_for_locale?(:tutoring, [:search, :send], :en)).to be(false)
76
+ end
77
+ end
78
+ end
79
+
80
+ describe "#authorized_for_user?" do
81
+ it "should be false for all user and feature :no_name" do
82
+ @dsl.roles.user.each{|role|
83
+ expect(@dsl.authorized_for_user?(:no_name, [], role)).to be(false)
84
+ }
85
+ end
86
+
87
+ it "should be true for user :beta and feature :tutoring" do
88
+ expect(@dsl.authorized_for_user?(:tutoring, [], :beta)).to be(true)
89
+ end
90
+
91
+ it "should be false for user not :beta and feature :tutoring" do
92
+ @dsl.roles.user.reject!{|e| e==:beta }.each{|role|
93
+ expect(@dsl.authorized_for_user?(:tutoring, [], role)).to be(false)
94
+ }
95
+ end
96
+
97
+ it "should be false for user :standard, feature :tutoring and action :search" do
98
+ expect(@dsl.authorized_for_user?(:tutoring, :search, :standard)).to be(false)
99
+ end
100
+
101
+ it "should be true for user :standard, feature :tutoring and action :index" do
102
+ expect(@dsl.authorized_for_user?(:tutoring, :index, :standard)).to be(true)
103
+ end
104
+ end
105
+
106
+ describe "#authorized_for_instance?" do
107
+ it "should be false for all instance and feature :no_name" do
108
+ @dsl.roles.instance.each{|role|
109
+ expect(@dsl.authorized_for_instance?(:no_name, [], role)).to be(false)
110
+ }
111
+ end
112
+
113
+ it "should be true for instance :beta and feature :tutoring" do
114
+ expect(@dsl.authorized_for_instance?(:tutoring, [], :beta)).to be(true)
115
+ end
116
+
117
+ it "should be true for instance :standard and feature :tutoring" do
118
+ expect(@dsl.authorized_for_instance?(:tutoring, [], :standard)).to be(true)
119
+ end
120
+
121
+ it "should be false for instance :critical and feature :tutoring" do
122
+ expect(@dsl.authorized_for_instance?(:tutoring, [], :critical)).to be(false)
123
+ end
124
+
125
+ it "should be true for instance :critical, feature :tutoring and action :index" do
126
+ expect(@dsl.authorized_for_instance?(:tutoring, :index, :critical)).to be(true)
127
+ end
128
+
129
+ it "should be true for instance :beta and feature :social_learning" do
130
+ expect(@dsl.authorized_for_instance?(:social_learning, [], :beta)).to be(true)
131
+ end
132
+
133
+ it "should be false for instance :standard and feature :social_learning" do
134
+ expect(@dsl.authorized_for_instance?(:social_learning, [], :standard)).to be(false)
135
+ end
136
+
137
+ it "should be false for instance :critical and feature :social_learning" do
138
+ expect(@dsl.authorized_for_instance?(:social_learning, [], :critical)).to be(false)
139
+ end
140
+ end
141
+
142
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ describe Helioth::Feature do
4
+
5
+ before(:all) do
6
+ @feature = Helioth::Feature.new :no_name do
7
+ status :disabled
8
+ locales :en, :fr, :br
9
+ actions :index do
10
+ status :beta
11
+ end
12
+ end
13
+ end
14
+
15
+ describe "#status" do
16
+ it "should find correct :status" do
17
+ expect(@feature.status).to be == :disabled
18
+ end
19
+ end
20
+
21
+ describe "#locales" do
22
+ it "should find correct :locales" do
23
+ expect(@feature.locales).to be == [:en, :fr, :br]
24
+ end
25
+
26
+ it 'should find correct :locales when not defined' do
27
+ @feature = Helioth::Feature.new :test do
28
+ status :disabled
29
+ end
30
+
31
+ expect(@feature.locales).to be == I18n.available_locales
32
+ end
33
+ end
34
+
35
+ describe "#actions" do
36
+ it "should find correct :actions" do
37
+ expect(@feature.actions.size).to be == 1
38
+ expect(@feature.actions.first).to be_an_instance_of(Helioth::Action)
39
+ expect(@feature.actions.first.name).to be == :index
40
+ end
41
+ end
42
+
43
+ describe "#action" do
44
+ it "should find specific related action" do
45
+ expect(@feature.action(:index)).to be_an_instance_of(Helioth::Action)
46
+ expect(@feature.action(:index).name).to be == :index
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe Helioth::Features do
4
+
5
+ before(:all) do
6
+ @features = Helioth::Features.new do
7
+ feature :no_name do
8
+ status :disabled
9
+ end
10
+
11
+ feature :no_name2 do
12
+ status :beta
13
+ end
14
+ end
15
+ end
16
+
17
+ describe "#feature" do
18
+ it "should raise an ArgumentError error if no parameters passed" do
19
+ expect{@features.feature}.to raise_error(ArgumentError)
20
+ end
21
+ end
22
+
23
+ describe "#list" do
24
+ it "should be an Array" do
25
+ expect(@features.list).to be_an_instance_of(Array)
26
+ end
27
+
28
+ it "should contain 2 elements" do
29
+ expect(@features.list.size).to be == 2
30
+ end
31
+
32
+ it "should contain instances of Helioth::Feature" do
33
+ expect(@features.list.first).to be_an_instance_of(Helioth::Feature)
34
+ end
35
+ end
36
+
37
+ end
@@ -0,0 +1,3 @@
1
+ invalid do
2
+ invalid
3
+ end
@@ -0,0 +1,48 @@
1
+ roles do
2
+ user :beta, :standard
3
+ instance :beta, :standard, :critical
4
+ feature :disabled, :beta, :pre_release, :production
5
+ end
6
+
7
+ relations do
8
+ feature :disabled
9
+
10
+ feature :beta do
11
+ instance :beta
12
+ user :beta
13
+ end
14
+
15
+ feature :pre_release do
16
+ instance :beta, :standard
17
+ user :beta
18
+ end
19
+
20
+ feature :production do
21
+ instance :beta, :standard, :critical
22
+ user :beta, :standard
23
+ end
24
+ end
25
+
26
+ features do
27
+ feature :no_name do
28
+ status :disabled
29
+ end
30
+
31
+ feature :tutoring do
32
+ status :pre_release
33
+
34
+ actions :search, :send do
35
+ status :pre_release
36
+ locales :fr
37
+ end
38
+
39
+ actions :index do
40
+ status :production
41
+ end
42
+ end
43
+
44
+ feature :social_learning do
45
+ status :beta
46
+ locales :fr, :en
47
+ end
48
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe Helioth do
4
+ context "DSL" do
5
+ it 'should throw an error' do
6
+ expect { Helioth::DSL }.to raise_error
7
+ end
8
+ end
9
+
10
+ context ".dsl" do
11
+ it 'should throw an error with an invalid DSL file' do
12
+ expect { Helioth.dsl("spec/fixtures/invalid_dsl.rb") }.to raise_error
13
+ end
14
+
15
+ it 'should load the DSL with a valid DSL' do
16
+ expect { Helioth.dsl("spec/fixtures/valid_dsl.rb") }.to_not raise_error
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,146 @@
1
+ require 'spec_helper'
2
+
3
+ describe Helioth::ModelAdditions do
4
+
5
+ before(:all) do
6
+ Helioth::Dsl.load("spec/fixtures/valid_dsl.rb")
7
+ end
8
+
9
+ describe "with standard column :role" do
10
+ before(:all) do
11
+ class User < ActiveRecord::Base
12
+ belongs_to :instance
13
+ accepts_nested_attributes_for :instance
14
+ has_helioth_role :user, column: :role
15
+ end
16
+
17
+ class Instance < ActiveRecord::Base
18
+ has_helioth_role :instance, column: :role
19
+ end
20
+ end
21
+
22
+ describe "#validations" do
23
+ it "should validate role based on DSL defintion" do
24
+ Helioth::DSL.roles.user.each{|role|
25
+ expect(User.create(role: role.to_s)).to be_valid
26
+ }
27
+
28
+ Helioth::DSL.roles.instance.each{|role|
29
+ expect(Instance.create(role: role.to_s)).to be_valid
30
+ }
31
+ end
32
+
33
+ it "shouldn't validate role based on DSL definition" do
34
+ expect(User.create(role: "none")).to be_invalid
35
+ expect(Instance.create(role: "none")).to be_invalid
36
+ end
37
+ end
38
+
39
+ describe "#role?" do
40
+ it "should return user role as a Symbol" do
41
+ expect(User.new(role: "beta").role?).to be(:beta)
42
+ expect(User.new(role: "beta").role?).to be_an_instance_of(Symbol)
43
+
44
+ expect(Instance.new(role: "production").role?).to be(:production)
45
+ expect(Instance.new(role: "production").role?).to be_an_instance_of(Symbol)
46
+ end
47
+ end
48
+
49
+ describe "#helioth_role?" do
50
+ it "should return user role as a Symbol" do
51
+ expect(User.new(role: "beta").helioth_role?).to be(:beta)
52
+ expect(User.new(role: "beta").helioth_role?).to be_an_instance_of(Symbol)
53
+
54
+ expect(Instance.new(role: "production").helioth_role?).to be(:production)
55
+ expect(Instance.new(role: "production").helioth_role?).to be_an_instance_of(Symbol)
56
+ end
57
+ end
58
+
59
+ describe "#is_role?" do
60
+ it "should return true if user role match argument" do
61
+ expect(User.new(role: "beta").is_role?(:beta)).to be(true)
62
+ expect(User.new(role: "beta").is_role?("beta")).to be(true)
63
+
64
+ expect(Instance.new(role: "production").is_role?(:production)).to be(true)
65
+ expect(Instance.new(role: "production").is_role?("production")).to be(true)
66
+ end
67
+
68
+ it "should return false if user role match argument" do
69
+ expect(User.new(role: "beta").is_role?(:none)).to be(false)
70
+ expect(User.new(role: "beta").is_role?("none")).to be(false)
71
+
72
+ expect(Instance.new(role: "production").is_role?(:none)).to be(false)
73
+ expect(Instance.new(role: "production").is_role?("none")).to be(false)
74
+ end
75
+ end
76
+ end
77
+
78
+ describe "with non standard column :status as role" do
79
+ before(:all) do
80
+ class User2 < ActiveRecord::Base
81
+ belongs_to :instance2
82
+ accepts_nested_attributes_for :instance2
83
+ has_helioth_role :user, column: :status
84
+ end
85
+
86
+ class Instance2 < ActiveRecord::Base
87
+ has_helioth_role :instance, column: :status
88
+ end
89
+ end
90
+
91
+ describe "#validations" do
92
+ it "should validate role based on DSL defintion" do
93
+ Helioth::DSL.roles.user.each{|role|
94
+ expect(User2.create(status: role.to_s)).to be_valid
95
+ }
96
+
97
+ Helioth::DSL.roles.instance.each{|role|
98
+ expect(Instance2.create(status: role.to_s)).to be_valid
99
+ }
100
+ end
101
+
102
+ it "shouldn't validate role based on DSL definition" do
103
+ expect(User2.create(status: "none")).to be_invalid
104
+ expect(Instance2.create(status: "none")).to be_invalid
105
+ end
106
+ end
107
+
108
+ describe "#role?" do
109
+ it "should return user role as a Symbol" do
110
+ expect(User2.new(status: "beta").status?).to be(:beta)
111
+ expect(User2.new(status: "beta").status?).to be_an_instance_of(Symbol)
112
+
113
+ expect(Instance2.new(status: "production").status?).to be(:production)
114
+ expect(Instance2.new(status: "production").status?).to be_an_instance_of(Symbol)
115
+ end
116
+ end
117
+
118
+ describe "#helioth_role?" do
119
+ it "should return user role as a Symbol" do
120
+ expect(User2.new(status: "beta").helioth_role?).to be(:beta)
121
+ expect(User2.new(status: "beta").helioth_role?).to be_an_instance_of(Symbol)
122
+
123
+ expect(Instance2.new(status: "production").helioth_role?).to be(:production)
124
+ expect(Instance2.new(status: "production").helioth_role?).to be_an_instance_of(Symbol)
125
+ end
126
+ end
127
+
128
+ describe "#is_role?" do
129
+ it "should return true if user role match argument" do
130
+ expect(User2.new(status: "beta").is_status?(:beta)).to be(true)
131
+ expect(User2.new(status: "beta").is_status?("beta")).to be(true)
132
+
133
+ expect(Instance2.new(status: "production").is_status?(:production)).to be(true)
134
+ expect(Instance2.new(status: "production").is_status?("production")).to be(true)
135
+ end
136
+
137
+ it "should return false if user role match argument" do
138
+ expect(User2.new(status: "beta").is_status?(:none)).to be(false)
139
+ expect(User2.new(status: "beta").is_status?("none")).to be(false)
140
+
141
+ expect(Instance2.new(status: "production").is_status?(:none)).to be(false)
142
+ expect(Instance2.new(status: "production").is_status?("none")).to be(false)
143
+ end
144
+ end
145
+ end
146
+ end