1+ require 'test_helper'
2+
3+ class PermissionServiceTest < ActiveSupport ::TestCase
4+
5+ def setup
6+ # Use existing fixture users
7+ @free_user = users ( :one )
8+ @premium_user = users ( :two )
9+
10+ # Set billing plan IDs
11+ @free_user . update ( selected_billing_plan_id : 1 ) # Free plan
12+ @premium_user . update ( selected_billing_plan_id : 4 ) # Premium plan
13+
14+ # Create test content using existing fixtures
15+ @test_character = characters ( :one )
16+ @other_character = characters ( :two )
17+
18+ # Ensure proper associations
19+ @test_character . update ( user_id : @free_user . id , privacy : 'public' )
20+ @other_character . update ( user_id : @premium_user . id , privacy : 'private' )
21+ end
22+
23+ # Test user_owns_content?
24+ test "user_owns_content returns true for owned content" do
25+ assert PermissionService . user_owns_content? ( user : @free_user , content : @test_character )
26+ assert PermissionService . user_owns_content? ( user : @premium_user , content : @other_character )
27+ end
28+
29+ test "user_owns_content returns false for non-owned content" do
30+ refute PermissionService . user_owns_content? ( user : @free_user , content : @other_character )
31+ refute PermissionService . user_owns_content? ( user : @premium_user , content : @test_character )
32+ end
33+
34+ test "user_owns_content handles nil user safely" do
35+ refute PermissionService . user_owns_content? ( user : nil , content : @test_character )
36+ end
37+
38+ test "user_owns_content handles nil content by raising error" do
39+ # The method expects content to respond to .user, so nil content should raise NoMethodError
40+ assert_raises ( NoMethodError ) do
41+ PermissionService . user_owns_content? ( user : @free_user , content : nil )
42+ end
43+ end
44+
45+ test "user_owns_content handles content without user_id" do
46+ # Create a stub content object without user_id
47+ content_without_user = OpenStruct . new ( user : nil )
48+ content_without_user . define_singleton_method ( :try ) { |method | nil if method == :user_id }
49+
50+ refute PermissionService . user_owns_content? ( user : @free_user , content : content_without_user )
51+ end
52+
53+ # Test user_can_contribute_to_universe?
54+ test "user_can_contribute_to_universe handles nil user" do
55+ # Create a mock universe for testing
56+ universe = OpenStruct . new ( id : 1 )
57+ refute PermissionService . user_can_contribute_to_universe? ( user : nil , universe : universe )
58+ end
59+
60+ test "user_can_contribute_to_universe returns false without contributable universes" do
61+ # Create a mock universe
62+ universe = OpenStruct . new ( id : 999 ) # ID that won't match any associations
63+ refute PermissionService . user_can_contribute_to_universe? ( user : @free_user , universe : universe )
64+ end
65+
66+ # Test content_is_public?
67+ test "content_is_public returns true for public content" do
68+ @test_character . update ( privacy : 'public' )
69+ assert PermissionService . content_is_public? ( content : @test_character )
70+ end
71+
72+ test "content_is_public returns false for private content" do
73+ @other_character . update ( privacy : 'private' )
74+ refute PermissionService . content_is_public? ( content : @other_character )
75+ end
76+
77+ test "content_is_public handles content without privacy method" do
78+ content_without_privacy = OpenStruct . new
79+ content_without_privacy . define_singleton_method ( :respond_to? ) { |method | false if method == :privacy }
80+
81+ refute PermissionService . content_is_public? ( content : content_without_privacy )
82+ end
83+
84+ # Test user_can_contribute_to_containing_universe?
85+ test "user_can_contribute_to_containing_universe handles nil user" do
86+ refute PermissionService . user_can_contribute_to_containing_universe? ( user : nil , content : @test_character )
87+ end
88+
89+ test "user_can_contribute_to_containing_universe handles content without universe_id" do
90+ content_without_universe_id = OpenStruct . new ( universe_id : nil )
91+ refute PermissionService . user_can_contribute_to_containing_universe? ( user : @free_user , content : content_without_universe_id )
92+ end
93+
94+ test "user_can_contribute_to_containing_universe returns true for attribute-related content" do
95+ # Mock attribute content classes
96+ stub_const ( 'AttributeCategory' , Class . new )
97+ stub_const ( 'AttributeField' , Class . new )
98+ stub_const ( 'Attribute' , Class . new )
99+
100+ attribute_category = OpenStruct . new ( class : AttributeCategory )
101+ attribute_field = OpenStruct . new ( class : AttributeField )
102+ attribute = OpenStruct . new ( class : Attribute )
103+
104+ assert PermissionService . user_can_contribute_to_containing_universe? ( user : @free_user , content : attribute_category )
105+ assert PermissionService . user_can_contribute_to_containing_universe? ( user : @free_user , content : attribute_field )
106+ assert PermissionService . user_can_contribute_to_containing_universe? ( user : @free_user , content : attribute )
107+ end
108+
109+ # Test content_has_no_containing_universe?
110+ test "content_has_no_containing_universe returns true when universe is nil" do
111+ content_without_universe = OpenStruct . new ( universe : nil )
112+ assert PermissionService . content_has_no_containing_universe? ( content : content_without_universe )
113+ end
114+
115+ test "content_has_no_containing_universe returns false when universe is present" do
116+ content_with_universe = OpenStruct . new ( universe : "some_universe" )
117+ refute PermissionService . content_has_no_containing_universe? ( content : content_with_universe )
118+ end
119+
120+ # Test user_is_on_premium_plan?
121+ test "user_is_on_premium_plan returns true for premium users" do
122+ assert PermissionService . user_is_on_premium_plan? ( user : @premium_user )
123+ end
124+
125+ test "user_is_on_premium_plan returns false for free users" do
126+ refute PermissionService . user_is_on_premium_plan? ( user : @free_user )
127+ end
128+
129+ # Test billing_plan_allows_core_content?
130+ test "billing_plan_allows_core_content returns true for all users with active plans" do
131+ assert PermissionService . billing_plan_allows_core_content? ( user : @free_user )
132+ assert PermissionService . billing_plan_allows_core_content? ( user : @premium_user )
133+ end
134+
135+ test "billing_plan_allows_core_content returns true for users without active billing plans" do
136+ user_without_plan = User . create! (
137+ email : 'noplan@example.com' ,
138+ password : 'password123' ,
139+ selected_billing_plan_id : nil
140+ )
141+ assert PermissionService . billing_plan_allows_core_content? ( user : user_without_plan )
142+ end
143+
144+ # Test billing_plan_allows_extended_content?
145+ test "billing_plan_allows_extended_content returns true for premium users" do
146+ assert PermissionService . billing_plan_allows_extended_content? ( user : @premium_user )
147+ end
148+
149+ test "billing_plan_allows_extended_content returns false for free users" do
150+ refute PermissionService . billing_plan_allows_extended_content? ( user : @free_user )
151+ end
152+
153+ # Test user_can_collaborate_in_universe_that_allows_extended_content?
154+ test "user_can_collaborate_in_universe_that_allows_extended_content returns false when no premium collaborations" do
155+ refute PermissionService . user_can_collaborate_in_universe_that_allows_extended_content? ( user : @free_user )
156+ end
157+
158+ # Test billing_plan_allows_collective_content?
159+ test "billing_plan_allows_collective_content returns true for premium users" do
160+ assert PermissionService . billing_plan_allows_collective_content? ( user : @premium_user )
161+ end
162+
163+ test "billing_plan_allows_collective_content returns false for free users" do
164+ refute PermissionService . billing_plan_allows_collective_content? ( user : @free_user )
165+ end
166+
167+ # Test user_can_collaborate_in_universe_that_allows_collective_content?
168+ test "user_can_collaborate_in_universe_that_allows_collective_content returns false when no premium collaborations" do
169+ refute PermissionService . user_can_collaborate_in_universe_that_allows_collective_content? ( user : @free_user )
170+ end
171+
172+ # Test user_has_active_promotion_for_this_content_type
173+ test "user_has_active_promotion_for_this_content_type returns false without promotion" do
174+ refute PermissionService . user_has_active_promotion_for_this_content_type ( user : @free_user , content_type : 'Creature' )
175+ end
176+
177+ # Integration tests
178+ test "premium user permissions work correctly" do
179+ assert PermissionService . user_is_on_premium_plan? ( user : @premium_user )
180+ assert PermissionService . billing_plan_allows_core_content? ( user : @premium_user )
181+ assert PermissionService . billing_plan_allows_extended_content? ( user : @premium_user )
182+ assert PermissionService . billing_plan_allows_collective_content? ( user : @premium_user )
183+ end
184+
185+ test "free user permissions work correctly" do
186+ refute PermissionService . user_is_on_premium_plan? ( user : @free_user )
187+ assert PermissionService . billing_plan_allows_core_content? ( user : @free_user )
188+ refute PermissionService . billing_plan_allows_extended_content? ( user : @free_user )
189+ refute PermissionService . billing_plan_allows_collective_content? ( user : @free_user )
190+ end
191+
192+ test "content ownership works correctly" do
193+ # Free user owns their own content
194+ assert PermissionService . user_owns_content? ( user : @free_user , content : @test_character )
195+
196+ # Free user doesn't own premium user's content
197+ refute PermissionService . user_owns_content? ( user : @free_user , content : @other_character )
198+
199+ # Premium user owns their own content
200+ assert PermissionService . user_owns_content? ( user : @premium_user , content : @other_character )
201+
202+ # Premium user doesn't own free user's content
203+ refute PermissionService . user_owns_content? ( user : @premium_user , content : @test_character )
204+ end
205+
206+ test "content privacy detection works correctly" do
207+ # Set up public content
208+ @test_character . update ( privacy : 'public' )
209+ assert PermissionService . content_is_public? ( content : @test_character )
210+
211+ # Set up private content
212+ @other_character . update ( privacy : 'private' )
213+ refute PermissionService . content_is_public? ( content : @other_character )
214+ end
215+
216+ test "different billing plan types work correctly" do
217+ # Test beta user (plan ID 2)
218+ beta_user = User . create! (
219+ email : 'beta@example.com' ,
220+ password : 'password123' ,
221+ selected_billing_plan_id : 2
222+ )
223+
224+ assert PermissionService . user_is_on_premium_plan? ( user : beta_user )
225+ assert PermissionService . billing_plan_allows_extended_content? ( user : beta_user )
226+ assert PermissionService . billing_plan_allows_collective_content? ( user : beta_user )
227+ end
228+
229+ private
230+
231+ def stub_const ( const_name , const_value )
232+ # Simple constant stubbing for testing
233+ Object . const_set ( const_name , const_value ) unless Object . const_defined? ( const_name )
234+ end
235+ end
0 commit comments