-
Notifications
You must be signed in to change notification settings - Fork 925
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add create note subscription api endpoint
- Loading branch information
1 parent
6042632
commit 3ecf794
Showing
4 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module Api | ||
class NoteSubscriptionsController < ApiController | ||
before_action :check_api_writable | ||
before_action :authorize | ||
|
||
authorize_resource | ||
|
||
def create | ||
note_id = params[:note_id].to_i | ||
note = Note.find(note_id) | ||
note.subscribers << current_user | ||
rescue ActiveRecord::RecordNotUnique | ||
head :conflict | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
test/controllers/api/note_subscriptions_controller_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
require "test_helper" | ||
|
||
module Api | ||
class NoteSubscriptionsControllerTest < ActionDispatch::IntegrationTest | ||
def test_routes | ||
assert_routing( | ||
{ :path => "/api/0.6/notes/1/subscription", :method => :post }, | ||
{ :controller => "api/note_subscriptions", :action => "create", :note_id => "1" } | ||
) | ||
end | ||
|
||
def test_create | ||
user = create(:user) | ||
auth_header = bearer_authorization_header user | ||
note = create(:note_with_comments) | ||
assert_empty note.subscribers | ||
|
||
assert_difference "NoteSubscription.count", 1 do | ||
assert_difference "note.subscribers.count", 1 do | ||
post api_note_subscription_path(note), :headers => auth_header | ||
assert_response :success | ||
end | ||
end | ||
assert_response :success | ||
assert_equal user, note.subscribers.last | ||
end | ||
|
||
def test_create_fail_anonymous | ||
note = create(:note_with_comments) | ||
|
||
assert_no_difference "NoteSubscription.count" do | ||
assert_no_difference "note.subscribers.count" do | ||
post api_note_subscription_path(note) | ||
assert_response :unauthorized | ||
end | ||
end | ||
end | ||
|
||
def test_create_fail_no_scope | ||
user = create(:user) | ||
auth_header = bearer_authorization_header user, :scopes => %w[read_prefs] | ||
note = create(:note_with_comments) | ||
|
||
assert_no_difference "NoteSubscription.count" do | ||
assert_no_difference "note.subscribers.count" do | ||
post api_note_subscription_path(note), :headers => auth_header | ||
assert_response :forbidden | ||
end | ||
end | ||
end | ||
|
||
def test_create_fail_note_not_found | ||
user = create(:user) | ||
auth_header = bearer_authorization_header user | ||
|
||
assert_no_difference "NoteSubscription.count" do | ||
post api_note_subscription_path(999111), :headers => auth_header | ||
assert_response :not_found | ||
end | ||
end | ||
|
||
def test_create_fail_already_subscribed | ||
user = create(:user) | ||
auth_header = bearer_authorization_header user | ||
note = create(:note_with_comments) | ||
create(:note_subscription, :user => user, :note => note) | ||
|
||
assert_no_difference "NoteSubscription.count" do | ||
assert_no_difference "note.subscribers.count" do | ||
post api_note_subscription_path(note), :headers => auth_header | ||
assert_response :conflict | ||
end | ||
end | ||
end | ||
end | ||
end |