milia 0.3.38 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +94 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +3 -16
- data/README.md +890 -141
- data/Rakefile +1 -55
- data/app/controllers/confirmations_controller.rb +101 -0
- data/app/controllers/passwords_controller.rb +8 -0
- data/app/controllers/registrations_controller.rb +95 -25
- data/app/controllers/sessions_controller.rb +13 -0
- data/app/views/members/new.html.haml +33 -0
- data/doc/gemfile_addition.txt +28 -0
- data/doc/manual_sample.sh +816 -0
- data/doc/sample.sh +229 -0
- data/lib/generators/milia/install_generator.rb +546 -0
- data/lib/generators/milia/temp_generator.rb +93 -0
- data/lib/generators/milia/templates/initializer.rb +51 -0
- data/lib/milia.rb +89 -1
- data/lib/milia/base.rb +29 -4
- data/lib/milia/control.rb +161 -9
- data/lib/milia/invite_member.rb +92 -0
- data/lib/milia/password_generator.rb +171 -0
- data/lib/milia/railtie.rb +4 -1
- data/lib/milia/version.rb +3 -0
- data/milia.gemspec +24 -159
- data/test/.ruby-gemset +1 -0
- data/test/.ruby-version +1 -0
- data/test/Gemfile +81 -0
- data/test/Gemfile.lock +200 -0
- data/test/README.md +83 -0
- data/test/{rails_app/Rakefile → Rakefile} +1 -2
- data/test/app/assets/javascripts/application.js +16 -0
- data/test/app/assets/stylesheets/application.css +13 -0
- data/test/app/controllers/application_controller.rb +13 -0
- data/test/app/controllers/home_controller.rb +10 -0
- data/test/{rails_app/app → app}/helpers/application_helper.rb +0 -0
- data/test/app/models/member.rb +34 -0
- data/test/app/models/post.rb +14 -0
- data/test/{rails_app/app → app}/models/team.rb +3 -2
- data/test/{rails_app/app → app}/models/team_asset.rb +1 -1
- data/test/app/models/tenant.rb +54 -0
- data/test/app/models/user.rb +14 -0
- data/test/app/models/zine.rb +8 -0
- data/test/{rails_app/app → app}/views/home/index.html.erb +0 -0
- data/test/app/views/home/show.html.erb +2 -0
- data/test/app/views/layouts/application.html.erb +14 -0
- data/test/bin/bundle +3 -0
- data/test/bin/rails +4 -0
- data/test/bin/rake +4 -0
- data/test/config/application.rb +36 -0
- data/test/{rails_app/config → config}/boot.rb +0 -2
- data/test/config/database.yml +25 -0
- data/test/config/environment.rb +5 -0
- data/test/config/environments/development.rb +48 -0
- data/test/config/environments/production.rb +95 -0
- data/test/config/environments/test.rb +42 -0
- data/test/{rails_app/config → config}/initializers/backtrace_silencers.rb +0 -0
- data/test/{rails_app/config → config}/initializers/devise.rb +84 -36
- data/test/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/config/initializers/inflections.rb +16 -0
- data/test/config/initializers/milia.rb +51 -0
- data/test/{rails_app/config → config}/initializers/mime_types.rb +0 -0
- data/test/config/initializers/secret_token.rb +12 -0
- data/test/config/initializers/session_store.rb +3 -0
- data/test/{rails_app/config → config}/initializers/wrap_parameters.rb +6 -6
- data/test/config/locales/en.yml +23 -0
- data/test/config/routes.rb +77 -0
- data/test/{rails_app/db/migrate/20111012060818_add_sessions_table.rb → db/migrate/20111012050200_add_sessions_table.rb} +2 -6
- data/test/db/migrate/20111012050340_devise_create_users.rb +48 -0
- data/test/{rails_app/db → db}/migrate/20111012050532_create_tenants.rb +3 -1
- data/test/db/migrate/20111012050600_create_tenants_users_join_table.rb +8 -0
- data/test/db/migrate/20111012050650_create_members.rb +12 -0
- data/test/db/migrate/20111012231923_create_posts.rb +12 -0
- data/test/{rails_app/db → db}/migrate/20111013050657_create_zines.rb +2 -4
- data/test/{rails_app/db → db}/migrate/20111013050753_create_teams.rb +1 -2
- data/test/db/migrate/20111013050837_create_team_assets.rb +11 -0
- data/test/db/schema.rb +126 -0
- data/test/{rails_app/db → db}/seeds.rb +0 -0
- data/test/test/controllers/home_controller_test.rb +133 -0
- data/test/{rails_app/test → test}/ctlr_test_helper.rb +0 -0
- data/test/test/fixtures/members.yml +35 -0
- data/test/test/fixtures/posts.yml +96 -0
- data/test/test/fixtures/team_assets.yml +30 -0
- data/test/test/fixtures/teams.yml +17 -0
- data/test/test/fixtures/tenants.yml +12 -0
- data/test/test/fixtures/tenants_users.yml +15 -0
- data/test/test/fixtures/users.yml +33 -0
- data/test/test/fixtures/zines.yml +25 -0
- data/test/test/models/member_test.rb +75 -0
- data/test/test/models/post_test.rb +66 -0
- data/test/test/models/team_test.rb +49 -0
- data/test/test/models/tenant_test.rb +228 -0
- data/test/test/models/user_test.rb +182 -0
- data/test/test/models/zine_test.rb +40 -0
- data/test/test/test_helper.rb +31 -0
- metadata +199 -154
- data/.rvmrc +0 -1
- data/Gemfile.lock +0 -115
- data/VERSION +0 -1
- data/test/helper.rb +0 -18
- data/test/rails_app/.gitignore +0 -5
- data/test/rails_app/Gemfile +0 -48
- data/test/rails_app/Gemfile.lock +0 -168
- data/test/rails_app/Gemfile.lock.backup +0 -167
- data/test/rails_app/Procfile +0 -1
- data/test/rails_app/README +0 -261
- data/test/rails_app/app/assets/images/rails.png +0 -0
- data/test/rails_app/app/assets/javascripts/application.js +0 -9
- data/test/rails_app/app/assets/javascripts/home.js.coffee +0 -3
- data/test/rails_app/app/assets/stylesheets/application.css +0 -7
- data/test/rails_app/app/assets/stylesheets/home.css.scss +0 -3
- data/test/rails_app/app/controllers/application_controller.rb +0 -50
- data/test/rails_app/app/controllers/home_controller.rb +0 -7
- data/test/rails_app/app/helpers/home_helper.rb +0 -2
- data/test/rails_app/app/mailers/.gitkeep +0 -0
- data/test/rails_app/app/models/.gitkeep +0 -0
- data/test/rails_app/app/models/author.rb +0 -9
- data/test/rails_app/app/models/calendar.rb +0 -6
- data/test/rails_app/app/models/post.rb +0 -15
- data/test/rails_app/app/models/tenant.rb +0 -8
- data/test/rails_app/app/models/user.rb +0 -14
- data/test/rails_app/app/models/zine.rb +0 -6
- data/test/rails_app/app/views/layouts/application.html.erb +0 -12
- data/test/rails_app/config.ru +0 -4
- data/test/rails_app/config/application.rb +0 -56
- data/test/rails_app/config/database.yml +0 -55
- data/test/rails_app/config/environment.rb +0 -5
- data/test/rails_app/config/environments/development.rb +0 -41
- data/test/rails_app/config/environments/production.rb +0 -60
- data/test/rails_app/config/environments/test.rb +0 -56
- data/test/rails_app/config/initializers/inflections.rb +0 -10
- data/test/rails_app/config/initializers/secret_token.rb +0 -7
- data/test/rails_app/config/initializers/session_store.rb +0 -8
- data/test/rails_app/config/locales/devise.en.yml +0 -58
- data/test/rails_app/config/locales/en.yml +0 -5
- data/test/rails_app/config/routes.rb +0 -63
- data/test/rails_app/db/migrate/20111012050340_devise_create_users.rb +0 -39
- data/test/rails_app/db/migrate/20111012050600_create_tenants_users.rb +0 -10
- data/test/rails_app/db/migrate/20111012231923_create_posts.rb +0 -15
- data/test/rails_app/db/migrate/20111013050558_create_calendars.rb +0 -14
- data/test/rails_app/db/migrate/20111013050837_create_team_assets.rb +0 -14
- data/test/rails_app/db/migrate/20111013053403_create_authors.rb +0 -13
- data/test/rails_app/db/schema.rb +0 -133
- data/test/rails_app/lib/assets/.gitkeep +0 -0
- data/test/rails_app/lib/tasks/.gitkeep +0 -0
- data/test/rails_app/log/.gitkeep +0 -0
- data/test/rails_app/public/404.html +0 -26
- data/test/rails_app/public/422.html +0 -26
- data/test/rails_app/public/500.html +0 -26
- data/test/rails_app/public/favicon.ico +0 -0
- data/test/rails_app/script/rails +0 -6
- data/test/rails_app/test/factories/units_factory.rb +0 -84
- data/test/rails_app/test/fixtures/.gitkeep +0 -0
- data/test/rails_app/test/functional/.gitkeep +0 -0
- data/test/rails_app/test/functional/home_controller_test.rb +0 -10
- data/test/rails_app/test/integration/.gitkeep +0 -0
- data/test/rails_app/test/performance/browsing_test.rb +0 -12
- data/test/rails_app/test/test_helper.rb +0 -119
- data/test/rails_app/test/unit/.gitkeep +0 -0
- data/test/rails_app/test/unit/author_test.rb +0 -30
- data/test/rails_app/test/unit/calendar_test.rb +0 -28
- data/test/rails_app/test/unit/helpers/home_helper_test.rb +0 -7
- data/test/rails_app/test/unit/post_test.rb +0 -81
- data/test/rails_app/test/unit/team_test.rb +0 -30
- data/test/rails_app/test/unit/tenant_test.rb +0 -28
- data/test/rails_app/test/unit/user_test.rb +0 -26
- data/test/rails_app/test/unit/zine_test.rb +0 -28
- data/test/rails_app/vendor/assets/stylesheets/.gitkeep +0 -0
- data/test/rails_app/vendor/plugins/.gitkeep +0 -0
- data/test/rails_app/vendor/plugins/rails_log_stdout/init.rb +0 -43
- data/test/test_milia.rb +0 -7
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class PostTest < ActiveSupport::TestCase
|
4
|
+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
5
|
+
|
6
|
+
context "a post" do
|
7
|
+
|
8
|
+
setup do
|
9
|
+
Tenant.set_current_tenant( tenants( :tenant_1 ).id )
|
10
|
+
@zine = Zine.first
|
11
|
+
end
|
12
|
+
|
13
|
+
# validate multi-tenanting structure
|
14
|
+
should have_db_column(:tenant_id)
|
15
|
+
should "match the current tenant" do
|
16
|
+
assert_equal @zine.tenant_id, Thread.current[:tenant_id]
|
17
|
+
end
|
18
|
+
|
19
|
+
# validate the model
|
20
|
+
should belong_to( :member )
|
21
|
+
should belong_to( :zine )
|
22
|
+
should have_one(:team).through(:zine)
|
23
|
+
|
24
|
+
# model-specific tests
|
25
|
+
should "get all posts within tenant" do
|
26
|
+
assert_equal 7, Post.count
|
27
|
+
end
|
28
|
+
|
29
|
+
should "get only member posts in tenant" do
|
30
|
+
Tenant.set_current_tenant( tenants( :tenant_2 ).id )
|
31
|
+
|
32
|
+
x = members(:quentin_2)
|
33
|
+
assert_equal 2, x.posts.size
|
34
|
+
end
|
35
|
+
|
36
|
+
should "see jermaine in two tenants with dif posts" do
|
37
|
+
jermaine = users( :jermaine )
|
38
|
+
Tenant.set_current_tenant( tenants( :tenant_2 ).id )
|
39
|
+
assert_equal 1, jermaine.member.posts.size
|
40
|
+
|
41
|
+
Tenant.set_current_tenant( tenants( :tenant_3 ).id )
|
42
|
+
jermaine.reload
|
43
|
+
assert_equal 6, jermaine.member.posts.size
|
44
|
+
end
|
45
|
+
|
46
|
+
should "get all team posts" do
|
47
|
+
Tenant.set_current_tenant( tenants( :tenant_2 ).id )
|
48
|
+
team = teams( :team_2_b )
|
49
|
+
assert_equal 2, team.posts.size
|
50
|
+
end
|
51
|
+
|
52
|
+
should 'match team in a post' do
|
53
|
+
Tenant.set_current_tenant( tenants( :tenant_2 ).id )
|
54
|
+
assert_equal posts(:post_plum_2_1_b).team, teams(:team_2_b)
|
55
|
+
end # should do
|
56
|
+
|
57
|
+
should 'match a posts zine with tenant' do
|
58
|
+
Tenant.set_current_tenant( tenants( :tenant_2 ).id )
|
59
|
+
assert_equal 2,posts(:post_plum_2_1_b).zine.tenant_id
|
60
|
+
end # should do
|
61
|
+
|
62
|
+
end # context post
|
63
|
+
|
64
|
+
# _____________________________________________________________________________
|
65
|
+
|
66
|
+
end # class test
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TeamTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
context "a team" do
|
6
|
+
|
7
|
+
setup do
|
8
|
+
Tenant.set_current_tenant( tenants( :tenant_2 ).id )
|
9
|
+
@team = Team.first
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
# validate multi-tenanting structure
|
14
|
+
should have_db_column(:tenant_id)
|
15
|
+
should "match the current tenant" do
|
16
|
+
assert_equal @team.tenant_id, Thread.current[:tenant_id]
|
17
|
+
end
|
18
|
+
|
19
|
+
should have_many( :team_assets )
|
20
|
+
should have_many( :team_members ).through( :team_assets )
|
21
|
+
|
22
|
+
should have_many( :zines )
|
23
|
+
should have_many( :posts ).through( :zines )
|
24
|
+
|
25
|
+
# validate the particular associations in the model
|
26
|
+
should 'find team members through teams' do
|
27
|
+
assert_equal 2, teams( :team_2_b ).team_members.count
|
28
|
+
end #should do
|
29
|
+
|
30
|
+
should 'find posts' do
|
31
|
+
assert_equal 3, teams( :team_2_a ).posts.count
|
32
|
+
end #should do
|
33
|
+
|
34
|
+
should 'match a team with tenant' do
|
35
|
+
assert_equal 2,teams( :team_2_b ).tenant_id
|
36
|
+
end # should do
|
37
|
+
|
38
|
+
should 'check team assets' do
|
39
|
+
assert_equal 2, teams( :team_2_a ).team_assets.count
|
40
|
+
end # should do
|
41
|
+
|
42
|
+
should 'check zines' do
|
43
|
+
assert_equal 1, teams( :team_2_b ).zines.count
|
44
|
+
end # should do
|
45
|
+
|
46
|
+
|
47
|
+
end # context team
|
48
|
+
|
49
|
+
end # team
|
@@ -0,0 +1,228 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
# #############################################################################
|
4
|
+
# Note: this tests not only the methods in models/tenant.rb but
|
5
|
+
# also all of the milia-injected methods from base.rb
|
6
|
+
# #############################################################################
|
7
|
+
|
8
|
+
class TenantTest < ActiveSupport::TestCase
|
9
|
+
|
10
|
+
|
11
|
+
context "a tenant" do
|
12
|
+
|
13
|
+
# ------------------------------------------------------------------------
|
14
|
+
setup do
|
15
|
+
@tenant =tenants( :tenant_1 )
|
16
|
+
Tenant.set_current_tenant( @tenant.id )
|
17
|
+
end
|
18
|
+
|
19
|
+
# #############################################################################
|
20
|
+
# #############################################################################
|
21
|
+
# ------------------------------------------------------------------------
|
22
|
+
# validate multi-tenanting structure
|
23
|
+
# ------------------------------------------------------------------------
|
24
|
+
should have_db_column(:tenant_id)
|
25
|
+
should have_db_column(:name)
|
26
|
+
should have_many( :posts )
|
27
|
+
should have_many( :zines )
|
28
|
+
should have_many( :team_assets )
|
29
|
+
should have_many( :teams )
|
30
|
+
should have_many( :members )
|
31
|
+
should have_and_belong_to_many( :users )
|
32
|
+
|
33
|
+
# ------------------------------------------------------------------------
|
34
|
+
# validate tenant creation callbacks, validators
|
35
|
+
# ------------------------------------------------------------------------
|
36
|
+
should 'have a new_signups_not_permitted' do
|
37
|
+
assert Tenant.respond_to? :new_signups_not_permitted?
|
38
|
+
assert !Tenant.new_signups_not_permitted?( {} )
|
39
|
+
end # should do
|
40
|
+
|
41
|
+
should 'create new tenant' do
|
42
|
+
|
43
|
+
assert_difference( 'Tenant.count' ) do
|
44
|
+
# setup new world
|
45
|
+
tenant = Tenant.create_new_tenant(
|
46
|
+
{name: "Mangoland"},
|
47
|
+
{email: "billybob@bob.com"},
|
48
|
+
{coupon: "FreeTrial"}
|
49
|
+
)
|
50
|
+
assert_not_nil tenant
|
51
|
+
assert_kind_of Tenant,tenant
|
52
|
+
assert tenant.errors.empty?
|
53
|
+
assert_equal "Mangoland", tenant.name
|
54
|
+
end
|
55
|
+
|
56
|
+
end # should do
|
57
|
+
|
58
|
+
should 'tenant signup callback' do
|
59
|
+
# setup new world
|
60
|
+
tenant = Tenant.create_new_tenant(
|
61
|
+
{name: "Mangoland"},
|
62
|
+
{email: "billybob@bob.com"},
|
63
|
+
{coupon: "FreeTrial"}
|
64
|
+
)
|
65
|
+
assert tenant.errors.empty?
|
66
|
+
Tenant.set_current_tenant( tenant ) # change world to new tenant
|
67
|
+
|
68
|
+
# setup new user
|
69
|
+
user = User.new(email: "limesublime@example.com")
|
70
|
+
assert user.save_and_invite_member
|
71
|
+
assert user.errors.empty?
|
72
|
+
|
73
|
+
# setup new member
|
74
|
+
member = nil
|
75
|
+
assert_difference( 'Member.count' ) do
|
76
|
+
assert_nothing_raised {
|
77
|
+
member = Tenant.tenant_signup(user, tenant)
|
78
|
+
assert member.errors.empty?
|
79
|
+
}
|
80
|
+
end # new Member DB records created
|
81
|
+
|
82
|
+
assert_equal Member::DEFAULT_ADMIN[:first_name],member.first_name
|
83
|
+
assert_equal Member::DEFAULT_ADMIN[:last_name],member.last_name
|
84
|
+
assert_equal tenant.id, member.tenant_id
|
85
|
+
assert_equal user.member,member
|
86
|
+
|
87
|
+
end # should do
|
88
|
+
|
89
|
+
# #############################################################################
|
90
|
+
# #### acts_as_universal_and_determines_tenant injected methods #############
|
91
|
+
# #############################################################################
|
92
|
+
|
93
|
+
should 'current_tenant_id - non nil' do
|
94
|
+
tid = Tenant.current_tenant_id
|
95
|
+
assert_kind_of Integer,tid
|
96
|
+
assert_equal tenants( :tenant_1 ).id,tid
|
97
|
+
end # should do
|
98
|
+
|
99
|
+
should 'current_tenant - nil' do
|
100
|
+
# force the current_tenant to be nil
|
101
|
+
Thread.current[:tenant_id] = nil
|
102
|
+
|
103
|
+
tenant = Tenant.current_tenant
|
104
|
+
assert_nil tenant
|
105
|
+
|
106
|
+
end # should do
|
107
|
+
|
108
|
+
should 'current_tenant - valid tid' do
|
109
|
+
tenant = Tenant.current_tenant
|
110
|
+
assert_kind_of Tenant,tenant
|
111
|
+
assert_equal tenants( :tenant_1 ),tenant
|
112
|
+
end # should do
|
113
|
+
|
114
|
+
should 'current_tenant - invalid tid' do
|
115
|
+
# force the current_tenant to be nil
|
116
|
+
Thread.current[:tenant_id] = 500
|
117
|
+
|
118
|
+
assert_nothing_raised {
|
119
|
+
assert_nil Tenant.current_tenant
|
120
|
+
}
|
121
|
+
|
122
|
+
end # should do
|
123
|
+
|
124
|
+
should 'set current tenant - tenant obj' do
|
125
|
+
assert_equal tenants( :tenant_1 ).id, Tenant.current_tenant_id
|
126
|
+
Tenant.set_current_tenant( tenants( :tenant_3 ) )
|
127
|
+
assert_equal tenants( :tenant_3 ).id, Tenant.current_tenant_id
|
128
|
+
end # should do
|
129
|
+
|
130
|
+
should 'set current tenant - tenant id' do
|
131
|
+
assert_equal tenants( :tenant_1 ).id, Tenant.current_tenant_id
|
132
|
+
Tenant.set_current_tenant( tenants( :tenant_3 ).id )
|
133
|
+
assert_equal tenants( :tenant_3 ).id, Tenant.current_tenant_id
|
134
|
+
end # should do
|
135
|
+
|
136
|
+
should 'NOT set current tenant - invalid arg' do
|
137
|
+
assert_equal tenants( :tenant_1 ).id, Tenant.current_tenant_id
|
138
|
+
assert_raise(ArgumentError) {
|
139
|
+
Tenant.set_current_tenant( '2' )
|
140
|
+
}
|
141
|
+
assert_equal tenants( :tenant_1 ).id, Tenant.current_tenant_id
|
142
|
+
end # should do
|
143
|
+
|
144
|
+
RESTRICT_SNIPPET = 'posts.tenant_id = 1 AND zines.tenant_id = 1'
|
145
|
+
should 'prepare a restrict tenant snippet' do
|
146
|
+
assert_equal RESTRICT_SNIPPET, Tenant.where_restrict_tenant( Post, Zine )
|
147
|
+
end # should do
|
148
|
+
|
149
|
+
should 'clear tenant.users when tenant destroyed' do
|
150
|
+
target = tenants(:tenant_2)
|
151
|
+
Tenant.set_current_tenant( target )
|
152
|
+
quentin = users(:quentin)
|
153
|
+
assert_equal 2,quentin.tenants.count
|
154
|
+
|
155
|
+
assert_difference( "Tenant.count", -1 ) do
|
156
|
+
target.destroy
|
157
|
+
end
|
158
|
+
|
159
|
+
quentin.reload
|
160
|
+
assert_equal 1,quentin.tenants.count
|
161
|
+
|
162
|
+
end # should do
|
163
|
+
|
164
|
+
|
165
|
+
# #############################################################################
|
166
|
+
# #### acts_as_tenant injected methods #############
|
167
|
+
# #############################################################################
|
168
|
+
|
169
|
+
should "raise exception if tenant is different" do
|
170
|
+
target = members(:quentin_1)
|
171
|
+
# now force tenant to invalid
|
172
|
+
Tenant.set_current_tenant( 0 )
|
173
|
+
|
174
|
+
assert_raise(::Milia::Control::InvalidTenantAccess,
|
175
|
+
"InvalidTenantAccess if tenants dont match"){
|
176
|
+
target.update_attributes( :first_name => "duck walk" )
|
177
|
+
}
|
178
|
+
end # should do
|
179
|
+
|
180
|
+
should 'raise exception if tenanted tid not nil - destroy' do
|
181
|
+
target = members(:quentin_1)
|
182
|
+
assert_no_difference('Member.count') do
|
183
|
+
assert_raise(::Milia::Control::InvalidTenantAccess) {
|
184
|
+
target.tenant_id = 3
|
185
|
+
target.destroy
|
186
|
+
}
|
187
|
+
end # no diff do
|
188
|
+
end # should do
|
189
|
+
|
190
|
+
# #############################################################################
|
191
|
+
# #### acts_as_universal injected methods #############
|
192
|
+
# #############################################################################
|
193
|
+
should 'always force universal tenant id to nil' do
|
194
|
+
# setup new world
|
195
|
+
tenant = Tenant.create_new_tenant(
|
196
|
+
{name: "Mangoland", tenant_id: 1},
|
197
|
+
{email: "billybob@bob.com"},
|
198
|
+
{coupon: "FreeTrial"}
|
199
|
+
)
|
200
|
+
assert tenant.errors.empty?
|
201
|
+
assert_nil tenant.tenant_id
|
202
|
+
end # should do
|
203
|
+
|
204
|
+
|
205
|
+
should 'raise exception if tid not nil - save' do
|
206
|
+
tenant = tenants(:tenant_1)
|
207
|
+
assert_raise(::Milia::Control::InvalidTenantAccess) {
|
208
|
+
tenant.update_attributes( tenant_id: 3, name: 'wild blue2' )
|
209
|
+
}
|
210
|
+
|
211
|
+
end # should do
|
212
|
+
|
213
|
+
should 'raise exception if tid not nil - destroy' do
|
214
|
+
tenant = tenants(:tenant_1)
|
215
|
+
assert_no_difference('Tenant.count') do
|
216
|
+
assert_raise(::Milia::Control::InvalidTenantAccess) {
|
217
|
+
tenant.tenant_id = 3
|
218
|
+
tenant.destroy
|
219
|
+
}
|
220
|
+
end # no diff do
|
221
|
+
end # should do
|
222
|
+
|
223
|
+
# #############################################################################
|
224
|
+
|
225
|
+
end # context
|
226
|
+
|
227
|
+
# #############################################################################
|
228
|
+
end # class test
|
@@ -0,0 +1,182 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
# #############################################################################
|
4
|
+
# Note: this tests not only the methods in models/user.rb but
|
5
|
+
# also all of the milia-injected methods from base.rb for
|
6
|
+
# acts_as_universal_and_determines_account
|
7
|
+
# #############################################################################
|
8
|
+
|
9
|
+
class UserTest < ActiveSupport::TestCase
|
10
|
+
|
11
|
+
context "a user" do
|
12
|
+
|
13
|
+
# ------------------------------------------------------------------------
|
14
|
+
# ------------------------------------------------------------------------
|
15
|
+
setup do
|
16
|
+
Tenant.set_current_tenant( tenants( :tenant_1 ).id )
|
17
|
+
@user = users(:quentin)
|
18
|
+
end
|
19
|
+
|
20
|
+
# ------------------------------------------------------------------------
|
21
|
+
# ------------------------------------------------------------------------
|
22
|
+
should have_one( :member )
|
23
|
+
should have_many( :tenanted_members )
|
24
|
+
should have_and_belong_to_many( :tenants )
|
25
|
+
should_not allow_value("wild blue").for(:email)
|
26
|
+
|
27
|
+
should have_db_column(:tenant_id)
|
28
|
+
should have_db_column(:skip_confirm_change_password).with_options(default: false)
|
29
|
+
|
30
|
+
should have_db_index(:email)
|
31
|
+
should have_db_index(:confirmation_token)
|
32
|
+
should have_db_index(:reset_password_token)
|
33
|
+
|
34
|
+
should "define the current tenant" do
|
35
|
+
assert Thread.current[:tenant_id]
|
36
|
+
end
|
37
|
+
|
38
|
+
should 'have password' do
|
39
|
+
assert !@user.has_no_password?
|
40
|
+
end # should do
|
41
|
+
|
42
|
+
should 'not have password' do
|
43
|
+
assert User.new(email: "billybob@bob.com").has_no_password?
|
44
|
+
end # should do
|
45
|
+
|
46
|
+
should 'attempt set password' do
|
47
|
+
assert users(:jermaine).attempt_set_password(
|
48
|
+
password: 'wild_blue',
|
49
|
+
password_confirmation: 'wild_blue'
|
50
|
+
)
|
51
|
+
end # should do
|
52
|
+
|
53
|
+
should 'check or set password - missing' do
|
54
|
+
user = User.new(email: "billybob@bob.com")
|
55
|
+
assert user.has_no_password?
|
56
|
+
user.check_or_set_password
|
57
|
+
assert !user.has_no_password?
|
58
|
+
assert !user.skip_confirm_change_password?
|
59
|
+
end # should do
|
60
|
+
|
61
|
+
should 'check or set password - present' do
|
62
|
+
user = User.new(
|
63
|
+
email: "billybob@bob.com",
|
64
|
+
password: 'limesublime',
|
65
|
+
password_confirmation: 'limesublime'
|
66
|
+
)
|
67
|
+
assert !user.has_no_password?
|
68
|
+
user.check_or_set_password
|
69
|
+
assert user.skip_confirm_change_password?
|
70
|
+
end # should do
|
71
|
+
|
72
|
+
should 'save and invite member - error no email' do
|
73
|
+
user = User.new(password: "wildblue")
|
74
|
+
assert_nil user.save_and_invite_member
|
75
|
+
assert !user.errors.empty?
|
76
|
+
end # should do
|
77
|
+
|
78
|
+
should 'save and invite member - error duplicate email' do
|
79
|
+
user = User.new(email: "jermaine@example.com")
|
80
|
+
assert_nil user.save_and_invite_member
|
81
|
+
assert !user.errors.empty?
|
82
|
+
end # should do
|
83
|
+
|
84
|
+
should 'save and invite member - success' do
|
85
|
+
user = User.new(email: "limesublime@example.com")
|
86
|
+
assert user.save_and_invite_member
|
87
|
+
assert user.errors.empty?
|
88
|
+
end # should do
|
89
|
+
|
90
|
+
# #############################################################################
|
91
|
+
# #############################################################################
|
92
|
+
|
93
|
+
|
94
|
+
should 'NOT create new user when invalid current tenant - string' do
|
95
|
+
# force the current_tenant to be unexpected object
|
96
|
+
Thread.current[:tenant_id] = 'peanut clusters'
|
97
|
+
|
98
|
+
assert_no_difference("User.count") do
|
99
|
+
assert_raise(::Milia::Control::InvalidTenantAccess,
|
100
|
+
"no existing valid current tenant") {
|
101
|
+
|
102
|
+
# setup new user
|
103
|
+
user = User.new(email: "limesublime@example.com")
|
104
|
+
user.save_and_invite_member
|
105
|
+
}
|
106
|
+
end # no difference
|
107
|
+
|
108
|
+
end # should do
|
109
|
+
|
110
|
+
should 'NOT create new user when invalid current tenant - nil' do
|
111
|
+
# force the current_tenant to be nil
|
112
|
+
Thread.current[:tenant_id] = nil
|
113
|
+
|
114
|
+
assert_no_difference("User.count") do
|
115
|
+
assert_raise(::Milia::Control::InvalidTenantAccess,
|
116
|
+
"no existing valid current tenant") {
|
117
|
+
|
118
|
+
# setup new user
|
119
|
+
user = User.new(email: "limesublime@example.com")
|
120
|
+
user.save_and_invite_member
|
121
|
+
}
|
122
|
+
end # no difference
|
123
|
+
|
124
|
+
end # should do
|
125
|
+
|
126
|
+
should 'NOT create new user when invalid current tenant - zero' do
|
127
|
+
# force the current_tenant to be 0
|
128
|
+
Thread.current[:tenant_id] = 0
|
129
|
+
|
130
|
+
assert_no_difference("User.count") do
|
131
|
+
assert_raise(::Milia::Control::InvalidTenantAccess,
|
132
|
+
"no existing valid current tenant") {
|
133
|
+
|
134
|
+
# setup new user
|
135
|
+
user = User.new(email: "limesublime@example.com")
|
136
|
+
user.save_and_invite_member
|
137
|
+
}
|
138
|
+
end # no difference
|
139
|
+
|
140
|
+
end # should do
|
141
|
+
|
142
|
+
# this validates both the before_create and after_create for users
|
143
|
+
should 'create new user when valid current tenant' do
|
144
|
+
tenant = tenants(:tenant_1)
|
145
|
+
assert_equal 1,tenant.users.count
|
146
|
+
|
147
|
+
assert_difference("User.count") do
|
148
|
+
assert_nothing_raised(::Milia::Control::InvalidTenantAccess,
|
149
|
+
"no existing valid current tenant") {
|
150
|
+
|
151
|
+
# setup new user
|
152
|
+
user = User.new(email: "limesublime@example.com")
|
153
|
+
user.save_and_invite_member
|
154
|
+
}
|
155
|
+
end # no difference
|
156
|
+
|
157
|
+
tenant.reload
|
158
|
+
assert_equal 2,tenant.users.count
|
159
|
+
|
160
|
+
end # should do
|
161
|
+
|
162
|
+
|
163
|
+
should 'destroy a user and clear its tenants habtm' do
|
164
|
+
tenant = tenants(:tenant_2)
|
165
|
+
Tenant.set_current_tenant( tenant )
|
166
|
+
quentin = users(:quentin)
|
167
|
+
assert_equal 3,tenant.users.count
|
168
|
+
quentin.destroy
|
169
|
+
tenant.reload
|
170
|
+
assert_equal 2,tenant.users.count
|
171
|
+
end # should do
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
# ok to create user, member
|
176
|
+
# @user = User.new( user_params )
|
177
|
+
# if @user.save_and_invite_member() && @user.create_member( member_params )
|
178
|
+
|
179
|
+
|
180
|
+
end # context user
|
181
|
+
|
182
|
+
end # class
|