]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/note_subscriptions_controller_test.rb
45aa33ad16ff36c3e89150f0fee38af93ed217af
[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     end
11
12     def test_create
13       user = create(:user)
14       auth_header = bearer_authorization_header user
15       note = create(:note_with_comments)
16       assert_empty note.subscribers
17
18       assert_difference "NoteSubscription.count", 1 do
19         assert_difference "note.subscribers.count", 1 do
20           post api_note_subscription_path(note), :headers => auth_header
21           assert_response :success
22         end
23       end
24       assert_equal user, note.subscribers.last
25     end
26
27     def test_create_fail_anonymous
28       note = create(:note_with_comments)
29
30       assert_no_difference "NoteSubscription.count" do
31         assert_no_difference "note.subscribers.count" do
32           post api_note_subscription_path(note)
33           assert_response :unauthorized
34         end
35       end
36     end
37
38     def test_create_fail_no_scope
39       user = create(:user)
40       auth_header = bearer_authorization_header user, :scopes => %w[read_prefs]
41       note = create(:note_with_comments)
42
43       assert_no_difference "NoteSubscription.count" do
44         assert_no_difference "note.subscribers.count" do
45           post api_note_subscription_path(note), :headers => auth_header
46           assert_response :forbidden
47         end
48       end
49     end
50
51     def test_create_fail_note_not_found
52       user = create(:user)
53       auth_header = bearer_authorization_header user
54
55       assert_no_difference "NoteSubscription.count" do
56         post api_note_subscription_path(999111), :headers => auth_header
57         assert_response :not_found
58       end
59       assert_match "not found", @response.body
60     end
61
62     def test_create_fail_already_subscribed
63       user = create(:user)
64       auth_header = bearer_authorization_header user
65       note = create(:note_with_comments)
66       create(:note_subscription, :user => user, :note => note)
67
68       assert_no_difference "NoteSubscription.count" do
69         assert_no_difference "note.subscribers.count" do
70           post api_note_subscription_path(note), :headers => auth_header
71           assert_response :conflict
72         end
73       end
74       assert_match "already subscribed", @response.body
75     end
76   end
77 end