Skip to content

Commit 8e329a5

Browse files
committed
add pin/unpin image tests
1 parent de2feb0 commit 8e329a5

File tree

4 files changed

+780
-0
lines changed

4 files changed

+780
-0
lines changed
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
require 'test_helper'
2+
3+
class ContentControllerPinTest < ActionDispatch::IntegrationTest
4+
include Devise::Test::IntegrationHelpers
5+
6+
setup do
7+
@user = users(:one)
8+
@character = characters(:one)
9+
@image_upload = image_uploads(:regular)
10+
@pinned_image = image_uploads(:pinned)
11+
12+
# Create a test basil commission
13+
@basil_commission = BasilCommission.create!(
14+
user: @user,
15+
entity: @character,
16+
entity_type: 'Character',
17+
prompt: 'Test prompt',
18+
job_id: 'test_job_123',
19+
pinned: false
20+
)
21+
22+
sign_in @user
23+
end
24+
25+
test "should pin an unpinned image upload" do
26+
assert_not @image_upload.pinned, "Image should start unpinned"
27+
28+
post toggle_image_pin_path,
29+
params: { image_id: @image_upload.id, image_type: 'image_upload' },
30+
headers: { 'Accept' => 'application/json' }
31+
32+
assert_response :success
33+
34+
json_response = JSON.parse(response.body)
35+
assert_equal @image_upload.id, json_response['id']
36+
assert_equal 'image_upload', json_response['type']
37+
assert json_response['pinned'], "Response should indicate image is now pinned"
38+
39+
@image_upload.reload
40+
assert @image_upload.pinned, "Image should be pinned in database"
41+
end
42+
43+
test "should unpin a pinned image upload" do
44+
assert @pinned_image.pinned, "Image should start pinned"
45+
46+
post toggle_image_pin_path,
47+
params: { image_id: @pinned_image.id, image_type: 'image_upload' },
48+
headers: { 'Accept' => 'application/json' }
49+
50+
assert_response :success
51+
52+
json_response = JSON.parse(response.body)
53+
assert_equal @pinned_image.id, json_response['id']
54+
assert_equal 'image_upload', json_response['type']
55+
assert_not json_response['pinned'], "Response should indicate image is now unpinned"
56+
57+
@pinned_image.reload
58+
assert_not @pinned_image.pinned, "Image should be unpinned in database"
59+
end
60+
61+
test "should pin a basil commission" do
62+
assert_not @basil_commission.pinned, "Basil commission should start unpinned"
63+
64+
post toggle_image_pin_path,
65+
params: { image_id: @basil_commission.id, image_type: 'basil_commission' },
66+
headers: { 'Accept' => 'application/json' }
67+
68+
assert_response :success
69+
70+
json_response = JSON.parse(response.body)
71+
assert_equal @basil_commission.id, json_response['id']
72+
assert_equal 'basil_commission', json_response['type']
73+
assert json_response['pinned'], "Response should indicate basil commission is now pinned"
74+
75+
@basil_commission.reload
76+
assert @basil_commission.pinned, "Basil commission should be pinned in database"
77+
end
78+
79+
test "should unpin previously pinned basil commission" do
80+
@basil_commission.update!(pinned: true)
81+
82+
post toggle_image_pin_path,
83+
params: { image_id: @basil_commission.id, image_type: 'basil_commission' },
84+
headers: { 'Accept' => 'application/json' }
85+
86+
assert_response :success
87+
88+
json_response = JSON.parse(response.body)
89+
assert_not json_response['pinned'], "Response should indicate basil commission is now unpinned"
90+
91+
@basil_commission.reload
92+
assert_not @basil_commission.pinned, "Basil commission should be unpinned in database"
93+
end
94+
95+
test "pinning an image should unpin other images for same content" do
96+
# Ensure we have a pinned image and an unpinned image for the same character
97+
@pinned_image.update!(pinned: true)
98+
@image_upload.update!(pinned: false)
99+
100+
# Pin the unpinned image
101+
post toggle_image_pin_path,
102+
params: { image_id: @image_upload.id, image_type: 'image_upload' },
103+
headers: { 'Accept' => 'application/json' }
104+
105+
assert_response :success
106+
107+
# Check that the previously pinned image is now unpinned
108+
@pinned_image.reload
109+
@image_upload.reload
110+
111+
assert_not @pinned_image.pinned, "Previously pinned image should be unpinned"
112+
assert @image_upload.pinned, "Newly pinned image should be pinned"
113+
end
114+
115+
test "pinning an image should unpin basil commissions for same content" do
116+
@basil_commission.update!(pinned: true)
117+
118+
post toggle_image_pin_path,
119+
params: { image_id: @image_upload.id, image_type: 'image_upload' },
120+
headers: { 'Accept' => 'application/json' }
121+
122+
assert_response :success
123+
124+
@basil_commission.reload
125+
@image_upload.reload
126+
127+
assert_not @basil_commission.pinned, "Basil commission should be unpinned"
128+
assert @image_upload.pinned, "Image upload should be pinned"
129+
end
130+
131+
test "pinning a basil commission should unpin image uploads for same content" do
132+
@pinned_image.update!(pinned: true)
133+
134+
post toggle_image_pin_path,
135+
params: { image_id: @basil_commission.id, image_type: 'basil_commission' },
136+
headers: { 'Accept' => 'application/json' }
137+
138+
assert_response :success
139+
140+
@pinned_image.reload
141+
@basil_commission.reload
142+
143+
assert_not @pinned_image.pinned, "Image upload should be unpinned"
144+
assert @basil_commission.pinned, "Basil commission should be pinned"
145+
end
146+
147+
test "should return error for non-existent image" do
148+
post toggle_image_pin_path,
149+
params: { image_id: 99999, image_type: 'image_upload' },
150+
headers: { 'Accept' => 'application/json' }
151+
152+
assert_response :not_found
153+
154+
json_response = JSON.parse(response.body)
155+
assert_equal 'Image not found', json_response['error']
156+
end
157+
158+
test "should return error for invalid image type" do
159+
post toggle_image_pin_path,
160+
params: { image_id: @image_upload.id, image_type: 'invalid_type' },
161+
headers: { 'Accept' => 'application/json' }
162+
163+
assert_response :bad_request
164+
165+
json_response = JSON.parse(response.body)
166+
assert_equal 'Invalid image type', json_response['error']
167+
end
168+
169+
test "should return unauthorized for other user's content" do
170+
other_user = users(:two)
171+
other_character = characters(:two)
172+
other_image = ImageUpload.create!(
173+
user: other_user,
174+
content_type: 'Character',
175+
content_id: other_character.id,
176+
privacy: 'public'
177+
)
178+
179+
post toggle_image_pin_path,
180+
params: { image_id: other_image.id, image_type: 'image_upload' },
181+
headers: { 'Accept' => 'application/json' }
182+
183+
assert_response :forbidden
184+
185+
json_response = JSON.parse(response.body)
186+
assert_equal 'Unauthorized', json_response['error']
187+
end
188+
189+
test "should require authentication" do
190+
sign_out @user
191+
192+
post toggle_image_pin_path,
193+
params: { image_id: @image_upload.id, image_type: 'image_upload' },
194+
headers: { 'Accept' => 'application/json' }
195+
196+
assert_response :unauthorized
197+
end
198+
199+
# Using Devise::Test::IntegrationHelpers for sign_in/sign_out
200+
end

0 commit comments

Comments
 (0)