]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/note_subscriptions_controller_test.rb
Add frozen_string_literal comments to ruby files
[rails.git] / test / controllers / api / note_subscriptions_controller_test.rb
1 # frozen_string_literal: true
2
3 require "test_helper"
4
5 module Api
6   class NoteSubscriptionsControllerTest < ActionDispatch::IntegrationTest
7     def test_routes
8       assert_routing(
9         { :path => "/api/0.6/notes/1/subscription", :method => :post },
10         { :controller => "api/note_subscriptions", :action => "create", :note_id => "1" }
11       )
12       assert_routing(
13         { :path => "/api/0.6/notes/1/subscription", :method => :delete },
14         { :controller => "api/note_subscriptions", :action => "destroy", :note_id => "1" }
15       )
16     end
17
18     def test_create
19       user = create(:user)
20       auth_header = bearer_authorization_header user
21       note = create(:note_with_comments)
22       assert_empty note.subscribers
23
24       assert_difference "NoteSubscription.count", 1 do
25         assert_difference "note.subscribers.count", 1 do
26           post api_note_subscription_path(note), :headers => auth_header
27           assert_response :success
28         end
29       end
30       assert_equal user, note.subscribers.last
31     end
32
33     def test_create_fail_anonymous
34       note = create(:note_with_comments)
35
36       assert_no_difference "NoteSubscription.count" do
37         assert_no_difference "note.subscribers.count" do
38           post api_note_subscription_path(note)
39           assert_response :unauthorized
40         end
41       end
42     end
43
44     def test_create_fail_no_scope
45       user = create(:user)
46       auth_header = bearer_authorization_header user, :scopes => %w[read_prefs]
47       note = create(:note_with_comments)
48
49       assert_no_difference "NoteSubscription.count" do
50         assert_no_difference "note.subscribers.count" do
51           post api_note_subscription_path(note), :headers => auth_header
52           assert_response :forbidden
53         end
54       end
55     end
56
57     def test_create_fail_note_not_found
58       user = create(:user)
59       auth_header = bearer_authorization_header user
60
61       assert_no_difference "NoteSubscription.count" do
62         post api_note_subscription_path(999111), :headers => auth_header
63         assert_response :not_found
64       end
65       assert_match "not found", @response.body
66     end
67
68     def test_create_fail_already_subscribed
69       user = create(:user)
70       auth_header = bearer_authorization_header user
71       note = create(:note_with_comments)
72       create(:note_subscription, :user => user, :note => note)
73
74       assert_no_difference "NoteSubscription.count" do
75         assert_no_difference "note.subscribers.count" do
76           post api_note_subscription_path(note), :headers => auth_header
77           assert_response :conflict
78         end
79       end
80       assert_match "already subscribed", @response.body
81     end
82
83     def test_destroy
84       user = create(:user)
85       auth_header = bearer_authorization_header user
86       other_user = create(:user)
87       note = create(:note_with_comments)
88       other_note = create(:note_with_comments)
89       create(:note_subscription, :user => user, :note => note)
90       create(:note_subscription, :user => other_user, :note => note)
91       create(:note_subscription, :user => user, :note => other_note)
92
93       assert_difference "NoteSubscription.count", -1 do
94         assert_difference "note.subscribers.count", -1 do
95           delete api_note_subscription_path(note), :headers => auth_header
96           assert_response :success
97         end
98       end
99       note.reload
100       assert_equal [other_user], note.subscribers
101       assert_equal [user], other_note.subscribers
102     end
103
104     def test_destroy_fail_anonymous
105       note = create(:note_with_comments)
106
107       delete api_note_subscription_path(note)
108       assert_response :unauthorized
109     end
110
111     def test_destroy_fail_no_scope
112       user = create(:user)
113       auth_header = bearer_authorization_header user, :scopes => %w[read_prefs]
114       note = create(:note_with_comments)
115       create(:note_subscription, :user => user, :note => note)
116
117       assert_no_difference "NoteSubscription.count" do
118         assert_no_difference "note.subscribers.count" do
119           delete api_note_subscription_path(note), :headers => auth_header
120           assert_response :forbidden
121         end
122       end
123     end
124
125     def test_destroy_fail_note_not_found
126       user = create(:user)
127       auth_header = bearer_authorization_header user
128
129       delete api_note_subscription_path(999111), :headers => auth_header
130       assert_response :not_found
131       assert_match "not found", @response.body
132     end
133
134     def test_destroy_fail_not_subscribed
135       user = create(:user)
136       auth_header = bearer_authorization_header user
137       note = create(:note_with_comments)
138
139       delete api_note_subscription_path(note), :headers => auth_header
140       assert_response :not_found
141       assert_match "not subscribed", @response.body
142     end
143   end
144 end