Skip to content

Commit 34677ee

Browse files
committed
check in generated tests
1 parent 5729602 commit 34677ee

File tree

7 files changed

+520
-0
lines changed

7 files changed

+520
-0
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
require 'test_helper'
2+
3+
class Api::V1::GalleryImagesControllerTest < ActionDispatch::IntegrationTest
4+
include Devise::Test::IntegrationHelpers
5+
6+
setup do
7+
# Create a user for testing
8+
@user = users(:one)
9+
10+
# Create a character owned by this user
11+
@character = characters(:one)
12+
@character.update(user: @user)
13+
14+
# Create some test images
15+
@image1 = ImageUpload.create!(
16+
user: @user,
17+
content_type: "Character",
18+
content_id: @character.id,
19+
privacy: "public",
20+
position: 2
21+
)
22+
23+
@image2 = ImageUpload.create!(
24+
user: @user,
25+
content_type: "Character",
26+
content_id: @character.id,
27+
privacy: "public",
28+
position: 1
29+
)
30+
31+
# Create a test basil commission
32+
@commission = BasilCommission.create!(
33+
user: @user,
34+
entity_type: "Character",
35+
entity_id: @character.id,
36+
position: 3
37+
)
38+
39+
# Sign in as the user
40+
sign_in @user
41+
end
42+
43+
test "should update image positions" do
44+
# Prepare the sorting data
45+
sort_data = {
46+
images: [
47+
{ id: @image1.id, type: 'image_upload', position: 3 },
48+
{ id: @commission.id, type: 'basil_commission', position: 1 },
49+
{ id: @image2.id, type: 'image_upload', position: 2 }
50+
],
51+
content_type: "Character",
52+
content_id: @character.id
53+
}
54+
55+
# Send the request
56+
post api_v1_gallery_images_sort_path, params: sort_data, as: :json
57+
58+
# Check response
59+
assert_response :success
60+
assert_equal true, JSON.parse(response.body)["success"]
61+
62+
# Reload the records to check positions
63+
@image1.reload
64+
@image2.reload
65+
@commission.reload
66+
67+
assert_equal 3, @image1.position
68+
assert_equal 2, @image2.position
69+
assert_equal 1, @commission.position
70+
end
71+
72+
test "should not update positions for unauthorized user" do
73+
# Create another user
74+
@other_user = User.create!(
75+
email: 'other@example.com',
76+
password: 'password',
77+
password_confirmation: 'password'
78+
)
79+
80+
# Sign in as the other user
81+
sign_in @other_user
82+
83+
# Prepare the sorting data
84+
sort_data = {
85+
images: [
86+
{ id: @image1.id, type: 'image_upload', position: 3 },
87+
{ id: @commission.id, type: 'basil_commission', position: 1 },
88+
{ id: @image2.id, type: 'image_upload', position: 2 }
89+
],
90+
content_type: "Character",
91+
content_id: @character.id
92+
}
93+
94+
# Send the request
95+
post api_v1_gallery_images_sort_path, params: sort_data, as: :json
96+
97+
# Check response - should be unauthorized
98+
assert_response :unauthorized
99+
100+
# Positions should remain unchanged
101+
@image1.reload
102+
@image2.reload
103+
@commission.reload
104+
105+
assert_equal 2, @image1.position
106+
assert_equal 1, @image2.position
107+
assert_equal 3, @commission.position
108+
end
109+
110+
test "should handle invalid image ids" do
111+
# Prepare sorting data with a non-existent image
112+
sort_data = {
113+
images: [
114+
{ id: 9999, type: 'image_upload', position: 1 },
115+
{ id: @image1.id, type: 'image_upload', position: 2 }
116+
],
117+
content_type: "Character",
118+
content_id: @character.id
119+
}
120+
121+
# Send the request
122+
post api_v1_gallery_images_sort_path, params: sort_data, as: :json
123+
124+
# Check response - should be unprocessable entity
125+
assert_response :unprocessable_entity
126+
127+
# Positions should remain unchanged
128+
@image1.reload
129+
@image2.reload
130+
131+
assert_equal 2, @image1.position
132+
assert_equal 1, @image2.position
133+
end
134+
135+
test "should handle invalid image types" do
136+
# Prepare sorting data with an invalid image type
137+
sort_data = {
138+
images: [
139+
{ id: @image1.id, type: 'invalid_type', position: 1 }
140+
],
141+
content_type: "Character",
142+
content_id: @character.id
143+
}
144+
145+
# Send the request
146+
post api_v1_gallery_images_sort_path, params: sort_data, as: :json
147+
148+
# Check response - should be unprocessable entity
149+
assert_response :unprocessable_entity
150+
151+
# Position should remain unchanged
152+
@image1.reload
153+
assert_equal 2, @image1.position
154+
end
155+
end
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
require 'test_helper'
2+
3+
class Content::GalleryOrderingTest < ActionDispatch::IntegrationTest
4+
include Devise::Test::IntegrationHelpers
5+
6+
setup do
7+
# Create a user for testing
8+
@user = users(:one)
9+
10+
# Create a character owned by this user
11+
@character = characters(:one)
12+
@character.update(user: @user)
13+
14+
# Create test images with specific positions and creation times
15+
@regular_image1 = ImageUpload.create!(
16+
user: @user,
17+
content_type: "Character",
18+
content_id: @character.id,
19+
privacy: "public",
20+
position: 3,
21+
created_at: 3.days.ago
22+
)
23+
24+
@regular_image2 = ImageUpload.create!(
25+
user: @user,
26+
content_type: "Character",
27+
content_id: @character.id,
28+
privacy: "public",
29+
position: 1,
30+
created_at: 1.day.ago
31+
)
32+
33+
@pinned_image = ImageUpload.create!(
34+
user: @user,
35+
content_type: "Character",
36+
content_id: @character.id,
37+
privacy: "public",
38+
position: 2,
39+
created_at: 2.days.ago,
40+
pinned: true
41+
)
42+
43+
@private_image = ImageUpload.create!(
44+
user: @user,
45+
content_type: "Character",
46+
content_id: @character.id,
47+
privacy: "private",
48+
position: 4,
49+
created_at: 4.days.ago
50+
)
51+
52+
# Create a test BasilCommission
53+
@commission = BasilCommission.new(
54+
user: @user,
55+
entity_type: "Character",
56+
entity_id: @character.id,
57+
position: 5
58+
)
59+
# Mock the saved_at field used in sorting
60+
@commission.instance_variable_set(:@saved_at, 5.days.ago)
61+
def @commission.saved_at
62+
@saved_at
63+
end
64+
@commission.save!
65+
66+
# Create another user for privacy testing
67+
@other_user = users(:two) || User.create!(
68+
email: 'other@example.com',
69+
password: 'password',
70+
password_confirmation: 'password'
71+
)
72+
end
73+
74+
# Tests for content#edit gallery tab
75+
test "content#edit should show images in correct order" do
76+
sign_in @user
77+
get edit_character_path(@character)
78+
79+
assert_response :success
80+
81+
# Look for evidence of correct ordering in the response:
82+
# 1. Pinned image first
83+
# 2. Then by position (1, 2, 3, 4, 5)
84+
85+
# The pinned image should appear first
86+
assert_match /#{@pinned_image.id}.*#{@regular_image2.id}/m, response.body
87+
88+
# Regular images should be ordered by position
89+
assert_match /#{@regular_image2.id}.*#{@regular_image1.id}/m, response.body
90+
91+
# Private images should be visible to the owner
92+
assert @response.body.include?(@private_image.id.to_s), "Private images should be visible to owner"
93+
end
94+
95+
# Tests for content#show header
96+
test "content#show should show images in correct order for owner" do
97+
sign_in @user
98+
get character_path(@character)
99+
100+
assert_response :success
101+
102+
# Owner should see images in the same order as in edit, with pinned first
103+
assert @response.body.include?(@pinned_image.id.to_s), "Pinned image should be visible"
104+
assert @response.body.include?(@private_image.id.to_s), "Private images should be visible to owner"
105+
end
106+
107+
test "content#show should respect privacy for non-owners" do
108+
sign_in @other_user
109+
get character_path(@character)
110+
111+
assert_response :success
112+
113+
# Other users shouldn't see private images
114+
refute @response.body.include?(@private_image.id.to_s), "Private images should not be visible to non-owners"
115+
116+
# But they should see public images
117+
assert @response.body.include?(@pinned_image.id.to_s), "Public images should be visible to non-owners"
118+
end
119+
120+
# Tests for content#gallery view
121+
test "content#gallery should show images in correct order" do
122+
sign_in @user
123+
get gallery_character_path(@character)
124+
125+
assert_response :success
126+
127+
# The pinned image should appear first
128+
assert_match /#{@pinned_image.id}.*#{@regular_image2.id}/m, response.body
129+
130+
# Regular images should be ordered by position
131+
assert_match /#{@regular_image2.id}.*#{@regular_image1.id}/m, response.body
132+
end
133+
134+
test "content#gallery should respect privacy for non-owners" do
135+
sign_in @other_user
136+
get gallery_character_path(@character)
137+
138+
assert_response :success
139+
140+
# Other users shouldn't see private images
141+
refute @response.body.include?(@private_image.id.to_s), "Private images should not be visible to non-owners"
142+
143+
# But they should see public images
144+
assert @response.body.include?(@pinned_image.id.to_s), "Public images should be visible to non-owners"
145+
end
146+
end

test/fixtures/characters.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2+
3+
one:
4+
id: 1
5+
name: Test Character
6+
user_id: 1
7+
privacy: public
8+
page_type: Character
9+
background: A test character for gallery testing
10+
11+
two:
12+
id: 2
13+
name: Another Character
14+
user_id: 2
15+
privacy: public
16+
page_type: Character
17+
background: Another test character

test/fixtures/image_uploads.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2+
3+
regular:
4+
id: 1
5+
user: one
6+
content_type: Character
7+
content_id: 1
8+
privacy: public
9+
position: 1
10+
pinned: false
11+
12+
pinned:
13+
id: 2
14+
user: one
15+
content_type: Character
16+
content_id: 1
17+
privacy: public
18+
position: 2
19+
pinned: true
20+
21+
private:
22+
id: 3
23+
user: one
24+
content_type: Character
25+
content_id: 1
26+
privacy: private
27+
position: 3
28+
pinned: false

test/fixtures/users.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2+
3+
one:
4+
id: 1
5+
email: user.one@example.com
6+
encrypted_password: <%= Devise::Encryptor.digest(User, 'password') %>
7+
upload_bandwidth_kb: 10000
8+
9+
two:
10+
id: 2
11+
email: user.two@example.com
12+
encrypted_password: <%= Devise::Encryptor.digest(User, 'password') %>
13+
upload_bandwidth_kb: 10000

0 commit comments

Comments
 (0)