Commit bd5102a8 authored by charlie-ablett's avatar charlie-ablett
Browse files

Favourite recipes

parent 1ed50e91
module Api
module V1
class RecipesController < ApplicationController
before_action :authenticate_account!, only: [:create, :update, :destroy]
before_action :authenticate_account!, only: [:create, :update, :destroy, :favourite, :unfavourite]
before_action :authenticate!, only: [:index, :show, :execute]
respond_to :json
......@@ -33,6 +33,18 @@ module Api
render_error(e)
end
def favourite
render json: recipe.mark_as_favourite!(current_entity.account)
rescue => e
render_error(e)
end
def unfavourite
render json: recipe.unmark_as_favourite!(current_entity.account)
rescue => e
render_error(e)
end
def index
render json: recipes, scope: current_entity, scope_name: :current_entity
end
......
......@@ -137,6 +137,17 @@ class Recipe < ApplicationRecord
recipe_favourites.map(&:account_id).include?(account.id)
end
def mark_as_favourite!(account)
favourite = recipe_favourites.new(account: account)
favourite.save
end
def unmark_as_favourite!(account)
favourite = recipe_favourites.where(account: account).first
favourite.destroy if favourite
true
end
private
def generate_steps_with_positions(recipe_step_data)
......
......@@ -38,6 +38,8 @@ Rails.application.routes.draw do
resources :recipes, only: [:index, :show, :create, :update, :destroy] do
member do
post 'execute'
get 'favourite'
get 'unfavourite'
end
end
......
......@@ -752,13 +752,193 @@ RSpec.describe Api::V1::RecipesController do
end
end
describe "GET favourite" do
let!(:recipe) { create(:recipe, account: account) }
context 'with a valid token' do
context 'if recipe exists' do
context 'and it belongs to the user' do
before do
request_with_auth(account.new_jwt) do
perform_favourite_request({id: recipe.id})
end
end
specify do
expect(response.status).to eq 200
end
it 'is favourited' do
expect(recipe.reload.favourited_by?(account)).to be_truthy
end
end
context 'and it belongs to a different user' do
context 'and it is public' do
let!(:other_recipe) { create(:recipe, account: other_account, public: true) }
before do
request_with_auth(account.new_jwt) do
perform_favourite_request({id: other_recipe.id})
end
end
specify do
expect(response.status).to eq 200
end
it 'is favourited' do
expect(other_recipe.reload.favourited_by?(account)).to be_truthy
end
end
context 'and it is private' do
let!(:other_recipe) { create(:recipe, account: other_account, public: false) }
before do
request_with_auth(account.new_jwt) do
perform_favourite_request({id: other_recipe.id})
end
end
specify do
expect(response.status).to eq 404
end
it 'is not favourited' do
expect(other_recipe.reload.favourited_by?(account)).to be_falsey
end
end
end
end
context 'if recipe does not exist' do
specify do
request_with_auth(account.new_jwt) do
perform_favourite_request({id: "LOL"})
end
expect(response.status).to eq 404
expect(assigns[:recipe]).to be_nil
end
end
end
context 'if no valid token is supplied' do
it "does not return anything" do
request_with_auth do
perform_favourite_request({id: recipe.id})
end
expect(response.status).to eq 401
expect(assigns[:recipe]).to be_nil
end
end
end
describe "GET unfavourite" do
let!(:recipe) { create(:recipe, account: account) }
before do
create(:recipe_favourite, recipe: recipe, account: account)
end
context 'with a valid token' do
context 'if recipe exists' do
context 'and it belongs to the user' do
before do
request_with_auth(account.new_jwt) do
perform_unfavourite_request({id: recipe.id})
end
end
specify do
expect(response.status).to eq 200
end
it 'is favourited' do
expect(recipe.reload.favourited_by?(account)).to be_falsey
end
end
context 'and it belongs to a different user' do
context 'and it is public' do
let!(:other_recipe) { create(:recipe, account: other_account, public: true) }
before do
request_with_auth(account.new_jwt) do
perform_unfavourite_request({id: other_recipe.id})
end
end
specify do
expect(response.status).to eq 200
end
it 'is favourited' do
expect(other_recipe.reload.favourited_by?(account)).to be_falsey
end
end
context 'and it is private' do
let!(:other_recipe) { create(:recipe, account: other_account, public: false) }
before do
request_with_auth(account.new_jwt) do
perform_unfavourite_request({id: other_recipe.id})
end
end
specify do
expect(response.status).to eq 404
end
it 'is not favourited' do
expect(other_recipe.reload.favourited_by?(account)).to be_falsey
end
end
end
end
context 'if recipe does not exist' do
specify do
request_with_auth(account.new_jwt) do
perform_unfavourite_request({id: "LOL"})
end
expect(response.status).to eq 404
expect(assigns[:recipe]).to be_nil
end
end
end
context 'if no valid token is supplied' do
it "does not return anything" do
request_with_auth do
perform_unfavourite_request({id: recipe.id})
end
expect(response.status).to eq 401
expect(assigns[:recipe]).to be_nil
end
end
end
# request.headers.merge!(auth_headers)
# this is special for controller tests - you can't just merge them in manually for some reason
# this is special for controller tests - you can't just merge them in manually
def perform_execute_request(data = {})
execute_recipe(version, data)
end
def perform_favourite_request(data = {})
favourite_recipe(version, data)
end
def perform_unfavourite_request(data = {})
unfavourite_recipe(version, data)
end
def perform_create_request(data = {})
post_create_request(version, data)
end
......
......@@ -183,46 +183,95 @@ RSpec.describe Recipe, type: :model do
end
end
describe '#favourited_by?' do
describe 'favouriting' do
subject { create(:recipe) }
let(:other_recipe) { create(:recipe) }
context 'that recipe has not been favourited' do
specify do
expect(subject.favourited_by?(account)).to be_falsey
describe 'mark as favourite' do
context 'recipe has already been favourited by that account' do
before do
create(:recipe_favourite, recipe: subject, account: account)
end
specify do
subject.mark_as_favourite!(account)
expect(subject.favourited_by?(account)).to be_truthy
end
end
context 'recipe has not been favourited by that account' do
it 'recipe is still favourited' do
subject.mark_as_favourite!(account)
expect(subject.favourited_by?(account)).to be_truthy
end
end
end
context 'that recipe has been favourited by another user only' do
before do
create(:recipe_favourite, recipe: subject, account: other_account)
describe 'unmark as favourite' do
context 'recipe has been favourited by that account' do
before do
create(:recipe_favourite, recipe: subject, account: account)
end
specify do
subject.unmark_as_favourite!(account)
expect(subject.favourited_by?(account)).to be_falsey
end
end
specify do
expect(subject.favourited_by?(account)).to be_falsey
context 'recipe has not been favourited by that account' do
it 'recipe is not favourited' do
subject.unmark_as_favourite!(account)
expect(subject.favourited_by?(account)).to be_falsey
end
end
end
context 'a different recipe has been favourited by that user' do
before do
create(:recipe_favourite, recipe: other_recipe, account: account)
describe '#favourited_by?' do
context 'that recipe has not been favourited' do
specify do
expect(subject.favourited_by?(account)).to be_falsey
end
end
specify do
expect(subject.favourited_by?(account)).to be_falsey
context 'that recipe has been favourited by another account only' do
before do
create(:recipe_favourite, recipe: subject, account: other_account)
end
specify do
expect(subject.favourited_by?(account)).to be_falsey
end
end
end
context 'that recipe has been favourited by that user' do
before do
create(:recipe_favourite, recipe: subject, account: account)
context 'a different recipe has been favourited by that account' do
before do
create(:recipe_favourite, recipe: other_recipe, account: account)
end
specify do
expect(subject.favourited_by?(account)).to be_falsey
end
end
specify do
expect(subject.favourited_by?(account)).to be_truthy
context 'that recipe has been favourited by that account' do
before do
create(:recipe_favourite, recipe: subject, account: account)
end
specify do
expect(subject.favourited_by?(account)).to be_truthy
end
end
end
end
end
\ No newline at end of file
......@@ -40,6 +40,14 @@ def retry_execution(version, data = {}.to_json)
get :retry, params: data, headers: {'Content-Type' => "application/json", 'Accept' => "application/vnd.ink.#{version}" }
end
def favourite_recipe(version, data = {}.to_json)
get :favourite, params: data, headers: {'Content-Type' => "application/json", 'Accept' => "application/vnd.ink.#{version}" }
end
def unfavourite_recipe(version, data = {}.to_json)
get :unfavourite, params: data, headers: {'Content-Type' => "application/json", 'Accept' => "application/vnd.ink.#{version}" }
end
# process_chain / process_stepcontroller requests
def download_input_file(version, data = {}.to_json)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment