@@ -5,22 +5,56 @@ class FlagsControllerTest < ActionController::TestCase
55
66 test 'should create new post flag' do
77 sign_in users ( :standard_user )
8- post :new , params : { reason : 'ABCDEF GHIJKL MNOPQR STUVWX YZ' , post_id : posts ( :answer_two ) . id , post_type : 'Post' }
98
10- assert_not_nil assigns ( :flag )
11- assert_not_nil assigns ( :flag ) . post
12- assert_equal 'success' , JSON . parse ( response . body ) [ 'status' ]
9+ try_create_flag ( posts ( :answer_two ) , reason : 'testing post flags' )
10+
1311 assert_response ( :created )
12+
13+ @flag = assigns ( :flag )
14+ assert_not_nil @flag
15+ assert_not_nil @flag . post
16+ assert_equal 'success' , JSON . parse ( response . body ) [ 'status' ]
17+ assert_equal I18n . t ( 'flags.success.create_generic' ) , JSON . parse ( response . body ) [ 'message' ]
1418 end
1519
1620 test 'should create new comment flag' do
1721 sign_in users ( :standard_user )
18- post :new , params : { reason : 'ABCDEF GHIJKL MNOPQR STUVWX YZ' , post_id : comments ( :one ) . id , post_type : 'Comment' }
1922
20- assert_not_nil assigns ( :flag )
21- assert_not_nil assigns ( :flag ) . post
22- assert_equal 'success' , JSON . parse ( response . body ) [ 'status' ]
23+ try_create_flag ( comments ( :one ) , reason : 'testing comment flags' )
24+
2325 assert_response ( :created )
26+
27+ @flag = assigns ( :flag )
28+ assert_not_nil @flag
29+ assert_not_nil @flag . post
30+ assert_equal 'success' , JSON . parse ( response . body ) [ 'status' ]
31+ end
32+
33+ test 'should fail to create invalid flags' do
34+ sign_in users ( :standard_user )
35+
36+ try_create_flag ( posts ( :question_one ) , reason : 'a' * 1001 )
37+
38+ assert_response ( :bad_request )
39+ assert_equal 'failed' , JSON . parse ( response . body ) [ 'status' ]
40+ assert_equal I18n . t ( 'flags.errors.create_generic' ) , JSON . parse ( response . body ) [ 'message' ]
41+ end
42+
43+ test 'should fail to create flags for rate-limited users' do
44+ {
45+ RL_NewUserFlags : users ( :basic_user ) ,
46+ RL_Flags : users ( :standard_user )
47+ } . each_pair do |setting , user |
48+ sign_in ( user )
49+
50+ SiteSetting [ setting ] = 0
51+
52+ try_create_flag ( posts ( :question_one ) , reason : 'testing flag rate limits' )
53+
54+ assert_response ( :forbidden )
55+ assert_equal 'failed' , JSON . parse ( response . body ) [ 'status' ]
56+ assert_equal I18n . t ( 'flags.errors.rate_limited' , count : 0 ) , JSON . parse ( response . body ) [ 'message' ]
57+ end
2458 end
2559
2660 test 'should retrieve flag queue' do
@@ -120,6 +154,11 @@ class FlagsControllerTest < ActionController::TestCase
120154
121155 private
122156
157+ def try_create_flag ( target , **opts )
158+ post :new , params : { post_id : target . id ,
159+ post_type : target . is_a? ( Post ) ? 'Post' : 'Comment' } . merge ( opts )
160+ end
161+
123162 def try_resolve_flag ( flag , result : nil , message : nil )
124163 post :resolve , params : { id : flag . id , result : result , message : message }
125164 end
0 commit comments