@@ -7,7 +7,7 @@ class NotificationsControllerTest < ActionController::TestCase
77 sign_in users ( :standard_user )
88
99 [ :json , :html ] . each do |format |
10- try_index ( format : format )
10+ try_notifications ( format : format )
1111
1212 assert_response ( :success )
1313
@@ -27,9 +27,30 @@ class NotificationsControllerTest < ActionController::TestCase
2727 end
2828 end
2929
30+ test ':index should correctly filter notifications' do
31+ sign_in users ( :standard_user )
32+
33+ [ 'any' , 'read' , 'unread' ] . each do |status |
34+ try_notifications ( format : :html , status : status )
35+
36+ assert_response ( :success )
37+
38+ @notifications = assigns ( :notifications )
39+ assert @notifications &.any? , "Expected at least one #{ status } notification"
40+
41+ if status == 'read'
42+ assert @notifications . none? ( &:unread? )
43+ elsif status == 'unread'
44+ assert @notifications . none? ( &:read? )
45+ end
46+ end
47+ end
48+
3049 test 'should mark notification as read' do
3150 sign_in users ( :standard_user )
32- post :read , params : { id : notifications ( :one ) . id , format : :json }
51+
52+ post :read , params : { id : notifications ( :unread ) . id , format : :json }
53+
3354 assert_not_nil assigns ( :notification )
3455 assert_equal true , assigns ( :notification ) . is_read
3556 assert_equal 'success' , JSON . parse ( response . body ) [ 'status' ]
@@ -38,7 +59,9 @@ class NotificationsControllerTest < ActionController::TestCase
3859
3960 test 'should mark all notifications as read' do
4061 sign_in users ( :standard_user )
62+
4163 post :read_all , params : { format : :json }
64+
4265 assert_not_nil assigns ( :notifications )
4366 assigns ( :notifications ) . each do |notification |
4467 assert_equal true , notification . is_read
@@ -49,19 +72,19 @@ class NotificationsControllerTest < ActionController::TestCase
4972
5073 test 'should prevent users marking others notifications read' do
5174 sign_in users ( :editor )
52- post :read , params : { id : notifications ( :one ) . id , format : :json }
75+ post :read , params : { id : notifications ( :unread ) . id , format : :json }
5376 assert_response ( :forbidden )
5477 end
5578
5679 test 'should require authentication to get index' do
5780 sign_out :user
58- get :index , params : { format : :json }
81+ try_notifications
5982 assert_response ( :unauthorized ) # Devise seems to respond 401 for JSON requests.
6083 end
6184
6285 private
6386
64- def try_index ( format : :json )
65- get :index , params : { format : format }
87+ def try_notifications ( ** opts )
88+ get :index , params : { format : :json } . merge ( opts )
6689 end
6790end
0 commit comments