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