Make API likes controller match specs
This commit is contained in:
parent
39265ab9b5
commit
348790292b
5 changed files with 74 additions and 66 deletions
|
|
@ -4,7 +4,7 @@ module Api
|
||||||
module V1
|
module V1
|
||||||
class LikesController < Api::V1::BaseController
|
class LikesController < Api::V1::BaseController
|
||||||
before_action only: %i[create destroy] do
|
before_action only: %i[create destroy] do
|
||||||
require_access_token %w[read write]
|
require_access_token %w[write]
|
||||||
end
|
end
|
||||||
|
|
||||||
rescue_from ActiveRecord::RecordNotFound do
|
rescue_from ActiveRecord::RecordNotFound do
|
||||||
|
|
@ -16,33 +16,17 @@ module Api
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@like = current_user.like!(target) if target
|
like_service.create(params[:post_id])
|
||||||
if @like
|
|
||||||
render json: @like.as_api_response(:backbone), status: 201
|
|
||||||
else
|
|
||||||
render nothing: true, status: 422
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def destroy
|
|
||||||
@like = Like.find_by!(id: params[:id], author_id: current_user.person.id)
|
|
||||||
current_user.retract(@like)
|
|
||||||
head :no_content, status: 204
|
head :no_content, status: 204
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
def destroy
|
||||||
|
like_service.unlike_post(params[:post_id])
|
||||||
|
head :no_content, status: 204
|
||||||
|
end
|
||||||
|
|
||||||
def target
|
def like_service
|
||||||
@target ||= if params[:post_id]
|
@like_service ||= LikeService.new(current_user)
|
||||||
current_user.find_visible_shareable_by_id(Post, params[:post_id]).tap do |post|
|
|
||||||
raise(ActiveRecord::RecordNotFound.new) unless post
|
|
||||||
end
|
|
||||||
else
|
|
||||||
Comment.find(params[:comment_id]).tap do |comment|
|
|
||||||
shareable = current_user.find_visible_shareable_by_id(Post, comment.commentable_id)
|
|
||||||
raise(ActiveRecord::RecordNotFound.new) unless shareable
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,17 @@ class LikeService
|
||||||
user ? likes.order("author_id = #{user.person.id} DESC") : likes
|
user ? likes.order("author_id = #{user.person.id} DESC") : likes
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def unlike_post(post_id)
|
||||||
|
likes = post_service.find!(post_id).likes
|
||||||
|
likes = likes.order("author_id = #{user.person.id} DESC")
|
||||||
|
if likes.empty? && user.owns?(likes[0])
|
||||||
|
user.retract(likes[0])
|
||||||
|
true
|
||||||
|
else
|
||||||
|
false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
attr_reader :user
|
attr_reader :user
|
||||||
|
|
|
||||||
|
|
@ -225,7 +225,7 @@ Rails.application.routes.draw do
|
||||||
match "user", to: "users#show", via: %i[get post]
|
match "user", to: "users#show", via: %i[get post]
|
||||||
resources :posts, only: %i[show create destroy] do
|
resources :posts, only: %i[show create destroy] do
|
||||||
resources :comments, only: %i[create destroy]
|
resources :comments, only: %i[create destroy]
|
||||||
resources :likes, only: %i[create destroy]
|
resource :likes, only: %i[create destroy]
|
||||||
end
|
end
|
||||||
resources :conversations, only: %i[show index create destroy] do
|
resources :conversations, only: %i[show index create destroy] do
|
||||||
delete "visibility" => "conversation_visibilities#destroy"
|
delete "visibility" => "conversation_visibilities#destroy"
|
||||||
|
|
|
||||||
|
|
@ -15,61 +15,63 @@ describe Api::V1::LikesController do
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#show" do
|
describe "#create" do
|
||||||
it "returns the likes for a given post" do
|
context "with right post id" do
|
||||||
get(
|
it "succeeeds in liking post" do
|
||||||
api_v1_post_likes_path(post_id: @status.id),
|
post(
|
||||||
|
api_v1_post_likes_path(post_id: @status.guid),
|
||||||
params: {access_token: access_token}
|
params: {access_token: access_token}
|
||||||
)
|
)
|
||||||
|
expect(response.status).to eq(204)
|
||||||
|
likes = like_service.find_for_post(@status.guid)
|
||||||
|
expect(likes.length).to eq(1)
|
||||||
|
expect(likes[0].author.id).to eq(auth.user.person.id)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "fails on random post id" do
|
context "with wrong post id" do
|
||||||
get(
|
it "fails at liking post" do
|
||||||
api_v1_post_likes_path(post_id: @status.id),
|
post(
|
||||||
|
api_v1_post_likes_path(post_id: 99_999_999),
|
||||||
params: {access_token: access_token}
|
params: {access_token: access_token}
|
||||||
)
|
)
|
||||||
|
expect(response.status).to eq(404)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#create" do
|
describe "#create" do
|
||||||
it "returns the expected author" do
|
|
||||||
post(
|
|
||||||
api_v1_post_likes_path(post_id: @status.id),
|
|
||||||
params: {access_token: access_token}
|
|
||||||
)
|
|
||||||
json = JSON.parse(response.body)
|
|
||||||
expect(json["author"]["id"]).to eq(auth.user.person.id)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "fails on random post id" do
|
|
||||||
post api_v0_post_likes_path(post_id: 99_999_999), params: {access_token: access_token}
|
|
||||||
expect(response.body).to eq("Post or like not found")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "#delete" do
|
|
||||||
before do
|
before do
|
||||||
post(
|
post(
|
||||||
api_v1_post_likes_path(post_id: @status.id),
|
api_v1_post_likes_path(post_id: @status.guid),
|
||||||
params: {access_token: access_token}
|
params: {access_token: access_token}
|
||||||
)
|
)
|
||||||
@like_id = JSON.parse(response.body)["id"]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "succeeds" do
|
context "with right post id" do
|
||||||
|
it "succeeds at unliking post" do
|
||||||
delete(
|
delete(
|
||||||
api_v1_post_like_path(post_id: @status.id, id: @like_id),
|
api_v1_post_likes_path(post_id: @status.guid),
|
||||||
params: {access_token: access_token}
|
params: {access_token: access_token}
|
||||||
)
|
)
|
||||||
expect(response).to be_success
|
expect(response.status).to eq(204)
|
||||||
|
likes = like_service.find_for_post(@status.guid)
|
||||||
|
expect(likes.length).to eq(0)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "fails on random like id" do
|
context "with wrong post id" do
|
||||||
|
it "fails at unliking post" do
|
||||||
delete(
|
delete(
|
||||||
api_v1_post_like_path(post_id: @status.id, id: 99_999_999),
|
api_v1_post_likes_path(post_id: 99_999_999),
|
||||||
params: {access_token: access_token}
|
params: {access_token: access_token}
|
||||||
)
|
)
|
||||||
expect(response.body).to eq("Post or like not found")
|
expect(response.status).to eq(404)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def like_service
|
||||||
|
LikeService.new(auth.user)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -120,4 +120,15 @@ describe LikeService do
|
||||||
expect(LikeService.new.find_for_post(post.id)).to match_array(likes)
|
expect(LikeService.new.find_for_post(post.id)).to match_array(likes)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "#unlike_post" do
|
||||||
|
before do
|
||||||
|
LikeService.new(alice).create(post.id)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "removes the like to the post" do
|
||||||
|
LikeService.new(alice).unlike_post(post.id)
|
||||||
|
expect(post.likes.length).to eq(0)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue