-
Notifications
You must be signed in to change notification settings - Fork 481
/
test_public_thumbnails.rb
196 lines (162 loc) · 5.96 KB
/
test_public_thumbnails.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
require_relative '../../files_api_test_base' # Must be required first to establish load paths
require_relative '../../files_api_test_helper'
require 'cdo/aws/s3'
class PublicThumbnailsTest < FilesApiTestBase
def setup
@thumbnail_filename = '.metadata/thumbnail.png'
@thumbnail_body = 'stub-thumbnail-body'
AWS::S3.create_client
Aws::S3::Client.expects(:new).never
end
#Disabled while we gather Azure moderation stats. (PR 24626)'
def test_adult_thumbnail
skip
ImageModeration.stubs(:rate_image).once.returns :adult
with_project_type('applab') do |channel_id|
get "/v3/files-public/#{channel_id}/#{@thumbnail_filename}"
# Responds with a 404, like we do for flagged content
assert not_found?
# Includes content rating metadata in the response that the client can read
assert_equal 'adult', last_response['x-cdo-content-rating']
# Response is cached for an hour
assert_equal 'public, max-age=3600, s-maxage=1800', last_response['Cache-Control']
# Flags the project as abusive.
get "/v3/channels/#{channel_id}/abuse"
assert successful?
assert_equal 15, JSON.parse(last_response.body)['abuse_score']
# Flags the thumbnail as abusive
thumbnail = FileBucket.new.get(channel_id, @thumbnail_filename)
metadata = thumbnail[:metadata]
thumbnail_abuse = [metadata['abuse_score'].to_i, metadata['abuse-score'].to_i].max
assert_equal 15, thumbnail_abuse
end
end
#Disabled while we gather Azure moderation stats. (PR 24626)'
def test_racy_thumbnail
skip
ImageModeration.stubs(:rate_image).once.returns :racy
with_project_type('applab') do |channel_id|
get "/v3/files-public/#{channel_id}/#{@thumbnail_filename}"
# Responds with a 404, like we do for flagged content
assert not_found?
# Includes content rating metadata in the response that the client can read
assert_equal 'racy', last_response['x-cdo-content-rating']
# Response is cached for an hour
assert_equal 'public, max-age=3600, s-maxage=1800', last_response['Cache-Control']
# Flags the project as abusive.
get "/v3/channels/#{channel_id}/abuse"
assert successful?
assert_equal 15, JSON.parse(last_response.body)['abuse_score']
# Flags the thumbnail as abusive
thumbnail = FileBucket.new.get(channel_id, @thumbnail_filename)
metadata = thumbnail[:metadata]
thumbnail_abuse = [metadata['abuse_score'].to_i, metadata['abuse-score'].to_i].max
assert_equal 15, thumbnail_abuse
end
end
def test_everyone_thumbnail
ImageModeration.stubs(:rate_image).once.returns(:everyone).with do |file, _, _|
# Real ImageModeration reads the IO stream in order to do its work.
# As a result, the caller needs to rewind the stream before using it as
# the response body.
# Here, I'm ensuring our stubbed ImageModeration simulates that behavior.
file.read
# Return true so this expectation matches any arguments.
true
end
with_project_type('applab') do |channel_id|
get "/v3/files-public/#{channel_id}/#{@thumbnail_filename}"
assert successful?
assert_equal 'public, max-age=3600, s-maxage=1800', last_response['Cache-Control']
assert_equal @thumbnail_body, last_response.body
# Does not flag the project as abusive
get "/v3/channels/#{channel_id}/abuse"
assert successful?
assert_equal 0, JSON.parse(last_response.body)['abuse_score']
# Does not flag the thumbnail as abusive
thumbnail = FileBucket.new.get(channel_id, @thumbnail_filename)
metadata = thumbnail[:metadata]
thumbnail_abuse = [metadata['abuse_score'].to_i, metadata['abuse-score'].to_i].max
assert_equal 0, thumbnail_abuse
end
end
def test_bad_channel_thumbnail
ImageModeration.expects(:rate_image).never
get "/v3/files-public/undefined/.metadata/thumbnail.png"
assert not_found?
end
def test_moderates_applab
assert_moderates_project_type 'applab'
end
def test_moderates_gamelab
assert_moderates_project_type 'gamelab'
end
def test_no_moderation_for_other_types
%w(
artist
weblab
frozen
playlab
flappy
gumball
iceage
infinity
minecraft_adventurer
minecraft_designer
minecraft_hero
starwars
starwarsblocks
starwarsblocks_hour
bounce
sports
basketball
artist_k1
playlab_k1
).each do |type|
refute_moderates_project_type type
end
end
private
def assert_moderates_project_type(project_type)
ImageModeration.expects(:rate_image).once.returns :everyone
with_project_type project_type do |channel_id|
get "/v3/files-public/#{channel_id}/#{@thumbnail_filename}"
assert successful?
end
end
def refute_moderates_project_type(project_type)
ImageModeration.expects(:rate_image).never
with_project_type project_type do |channel_id|
get "/v3/files-public/#{channel_id}/#{@thumbnail_filename}"
assert successful?
end
end
# Creates a channel of the given type, with a thumbnail populated.
# Yields the channel id to the provided block.
# Performs cleanup of the thumbnail and channel when the block ends.
def with_project_type(project_type)
# Setup
channel_id = create_channel(projectType: project_type)
create_thumbnail(channel_id)
# Test
yield channel_id
ensure
delete_thumbnail(channel_id)
# Require that tests delete the assets they upload
get "v3/files/#{channel_id}"
expected_empty_files = {
'files' => [],
'filesVersionId' => ''
}
assert_equal(expected_empty_files, JSON.parse(last_response.body))
delete_channel(channel_id)
end
def create_thumbnail(channel_id)
put "/v3/files/#{channel_id}/#{@thumbnail_filename}", @thumbnail_body, {}
assert successful?
end
def delete_thumbnail(channel_id)
delete "/v3/files/#{channel_id}/#{@thumbnail_filename}"
assert successful?
end
end