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

finish remaining preset actions and tests

parent 76f570de
......@@ -7,8 +7,15 @@ module Api
respond_to :json
def create
new_recipe_step_preset.save!
render json: new_recipe_step_preset, scope: current_entity, scope_name: :current_entity
if new_recipe_step_preset.recipe_step.recipe.available_to_account?(current_entity.account)
new_recipe_step_preset.execution_parameters = params[:recipe_step_preset][:execution_parameters]
# not being picked up automatically?
new_recipe_step_preset.save!
render json: new_recipe_step_preset, scope: current_entity, scope_name: :current_entity
else
render json: "Recipe not available", status: 404
return
end
rescue => e
ap e.message
ap e.backtrace
......@@ -16,11 +23,21 @@ module Api
end
def index
render json: recipes, scope: current_entity, scope_name: :current_entity
recipe_step = RecipeStep.find(params[:recipe_step_id])
recipe = Recipe.find(recipe_step.recipe_id)
if recipe.available_to_account?(current_entity.account)
render json: recipe_step_presets, scope: current_entity, scope_name: :current_entity
else
render json: "Recipe not available", status: 404 and return
end
end
def show
render json: recipe, scope: current_entity, scope_name: :current_entity
if recipe_step_preset.recipe_step.recipe.available_to_account?(current_entity.account)
render json: recipe_step_preset, scope: current_entity, scope_name: :current_entity
else
render json: "Recipe not available", status: 404
end
rescue => e
ap e.message
ap e.backtrace
......@@ -28,8 +45,13 @@ module Api
end
def update
recipe.update!(update_recipe_params)
render json: recipe, root: false, scope: current_entity, scope_name: :current_entity
if recipe_step_preset.recipe_step.recipe.available_to_account?(current_entity.account)
recipe_step_preset.execution_parameters = params[:recipe_step_preset][:execution_parameters]
recipe_step_preset.update!(recipe_step_preset_params)
render json: recipe_step_preset, root: false, scope: current_entity, scope_name: :current_entity
else
render json: "Recipe not available", status: 404 and return
end
rescue => e
ap e.message
ap e.backtrace
......@@ -37,33 +59,29 @@ module Api
end
def destroy
recipe.attempt_to_destroy!(current_entity)
render json: recipe, root: false, scope: current_entity, scope_name: :current_entity
recipe_step_preset.destroy
render json: recipe_step_preset, root: false, scope: current_entity, scope_name: :current_entity
rescue => e
render_error(e)
end
private
def recipe_params
params.require(:recipe).permit(:name, :description, :active, :public)
def recipe_step_preset_params
params.require(:recipe_step_preset).permit(:name, :description, :execution_parameters, :recipe_step_id)
end
def update_recipe_params
params.require(:recipe).permit(:name, :description, :active, :public)
def recipe_step_preset
@recipe_step_preset ||= current_entity.account.recipe_step_presets.find(params[:id])
end
def recipe_step_preset
@recipe_step_preset ||= RecipeStepPreset.find(params[:id])
def recipe_step_presets
@recipe_step_presets ||= current_entity.account.recipe_step_presets.where(recipe_step_id: params[:recipe_step_id])
end
def new_recipe_step_preset
@new_recipe_step_preset ||= current_entity.account.recipes.new(recipe_params)
@new_recipe_step_preset ||= current_entity.account.recipe_step_presets.new(recipe_step_preset_params)
end
# def recipes
# @recipes ||= Recipe.includes(:recipe_steps, process_chains: :process_steps).available_to_account(current_entity.account.id, current_entity.admin?)
# end
end
end
end
\ No newline at end of file
......@@ -153,6 +153,12 @@ class Recipe < ApplicationRecord
end
end
def available_to_account?(target_account)
return true if public? || account == target_account
return false if !active? && account != target_account
false
end
private
def generate_steps_with_positions(recipe_step_data)
......
......@@ -66,6 +66,11 @@ Rails.application.routes.draw do
end
end
resources :recipe_steps, only: [] do #ha
resources :recipe_step_presets, only: [:index]
end
resources :recipe_step_presets, only: [:create, :update, :show, :destroy]
end
# scope module: :v2, constraints: ApiConstraints.new(version: 2, default: true) do
......
require 'rails_helper'
require_relative 'version'
RSpec.describe Api::V1::RecipeStepPresetsController do
let!(:account) { create(:account) }
let!(:other_account) { create(:account) }
let!(:recipe) { create(:recipe, account: account, public: true) }
let!(:recipe_step) { recipe.recipe_steps.first }
let!(:new_name) { "marvellous preset" }
let!(:new_description) { "absolutely magical" }
let!(:new_execution_parameters) { { "a" => 'b', "c" => 'd'} }
describe "PUT update" do
let(:preset_params) {
{
recipe_step_preset: {
name: new_name,
description: new_description,
execution_parameters: new_execution_parameters
},
id: recipe_step_preset.id
}
}
let!(:recipe_step_preset) { create(:recipe_step_preset, account: account, recipe_step: recipe_step) }
context 'with correct info supplied' do
context 'belonging to that account' do
it 'updates the values' do
request_with_auth(account.new_jwt) do
perform_update_request(preset_params)
end
recipe_step_preset.reload
expect(response.status).to eq 200
expect(recipe_step_preset.name).to eq new_name
expect(recipe_step_preset.description).to eq new_description
expect(recipe_step_preset.execution_parameters).to eq new_execution_parameters
end
end
context "another account's preset" do
before do
recipe_step_preset.update_attribute(:account_id, other_account.id)
end
it 'does not allow it' do
request_with_auth(account.new_jwt) do
perform_update_request(preset_params)
end
expect(response.status).to eq 404
end
end
context "on another account's preset" do
before do
recipe_step_preset.update_attribute(:account_id, other_account.id)
end
it 'does not allow it' do
request_with_auth(account.new_jwt) do
perform_update_request(preset_params)
end
expect(response.status).to eq 404
end
end
end
describe 'with no token' do
specify do
request_with_auth do
perform_update_request(preset_params)
end
expect(response.status).to eq 401
expect(assigns[:new_recipe_step_preset]).to be_nil
end
end
end
describe "POST create" do
let(:preset_params) {
{
recipe_step_preset: {
name: new_name,
description: new_description,
recipe_step_id: recipe_step.id,
execution_parameters: new_execution_parameters
}
}
}
context 'if a valid token is supplied' do
context 'if the recipe step preset is valid' do
context "on another account's private recipe" do
before do
recipe.update_attribute(:account_id, other_account.id)
recipe.update_attribute(:public, false)
end
it 'does not allow it' do
request_with_auth(account.new_jwt) do
perform_create_request(preset_params)
end
expect(response.status).to eq 404
end
end
context 'and they are valid' do
it "creates the preset" do
request_with_auth(account.new_jwt) do
perform_create_request(preset_params)
end
expect(response.status).to eq 200
new_preset = assigns[:new_recipe_step_preset]
expect(new_preset).to be_a RecipeStepPreset
expect(new_preset.name).to eq new_name
expect(new_preset.description).to eq new_description
expect(new_preset.account).to eq account
expect(new_preset.execution_parameters).to eq new_execution_parameters
end
end
end
context 'if the recipe is invalid' do
context 'if the recipe is missing a field' do
let(:invalid_preset_params) {
{
recipe_step_preset: {
description: new_description,
recipe_step_id: recipe_step.id,
execution_parameters: new_execution_parameters
}
}
}
it "is not successful" do
request_with_auth(account.new_jwt) do
perform_create_request(invalid_preset_params)
end
expect(response.status).to eq 422
expect(body_as_json['errors']).to eq ["Validation failed: Name can't be blank"]
end
end
end
end
context 'if no valid token is supplied' do
let(:preset_params) {
{
recipe_step_preset: {
name: new_name,
description: new_description,
execution_parameters: new_execution_parameters
}
}
}
it "does not assign anything" do
request_with_auth do
perform_create_request(preset_params)
end
expect(response.status).to eq 401
expect(assigns[:new_recipe_step_preset]).to be_nil
end
end
end
describe "GET index" do
context 'if a valid token is supplied' do
context 'there are no presets' do
it 'returns none' do
request_with_auth(account.new_jwt) do
perform_index_request(recipe_step_id: recipe_step.id)
end
expect(response.status).to eq 200
expect(assigns[:recipe_step_presets]).to eq []
end
end
context 'there are presets' do
context "for their own recipe" do
let!(:recipe_1) { create(:recipe, name: "recipe1", account: account) }
let!(:preset_1) { create(:recipe_step_preset, recipe_step: recipe_1.recipe_steps.first, account: account) }
let!(:preset_2) { create(:recipe_step_preset, recipe_step: recipe_1.recipe_steps.first, account: other_account) }
specify do
request_with_auth(account.new_jwt) do
perform_index_request(recipe_step_id: recipe_1.recipe_steps.first.id)
end
expect(response.status).to eq 200
expect(assigns[:recipe_step_presets].to_a).to eq [preset_1]
end
end
context 'for an inactive recipe' do
let!(:recipe_2) { create(:recipe, name: "recipe2", account: account, active: false) }
let!(:preset_3) { create(:recipe_step_preset, recipe_step: recipe_2.recipe_steps.first, account: account) }
let!(:preset_4) { create(:recipe_step_preset, recipe_step: recipe_2.recipe_steps.first, account: other_account) }
context 'belongs to another account' do
before { recipe_2.update_attribute(:account_id, other_account.id) }
specify do
request_with_auth(account.new_jwt) do
perform_index_request(recipe_step_id: recipe_2.recipe_steps.first.id)
end
expect(response.status).to eq 404
expect(assigns[:recipe_step_presets]).to be_nil
end
end
context 'belongs to that account' do
specify do
request_with_auth(account.new_jwt) do
perform_index_request(recipe_step_id: recipe_2.recipe_steps.first.id)
end
expect(response.status).to eq 200
expect(assigns[:recipe_step_presets].to_a).to eq [preset_3]
end
end
end
context 'for an private recipe belonging to someone else' do
let!(:recipe_3) { create(:recipe, name: "recipe3", account: other_account, public: false) }
let!(:preset_5) { create(:recipe_step_preset, recipe_step: recipe_3.recipe_steps.first, account: account) }
let!(:preset_6) { create(:recipe_step_preset, recipe_step: recipe_3.recipe_steps.first, account: other_account) }
specify do
request_with_auth(account.new_jwt) do
perform_index_request(recipe_step_id: recipe_3.recipe_steps.first.id)
end
expect(response.status).to eq 404
expect(assigns[:recipe_step_presets]).to be_nil
end
end
context 'for a public recipe belonging to someone else' do
let!(:recipe_4) { create(:recipe, name: "recipe4", account: other_account, public: true) }
let!(:preset_7) { create(:recipe_step_preset, recipe_step: recipe_4.recipe_steps.first, account: account) }
let!(:preset_8) { create(:recipe_step_preset, recipe_step: recipe_4.recipe_steps.first, account: other_account) }
specify do
request_with_auth(account.new_jwt) do
perform_index_request(recipe_step_id: recipe_4.recipe_steps.first.id)
end
expect(response.status).to eq 200
expect(assigns[:recipe_step_presets]).to eq [preset_7]
end
end
end
end
context 'if no valid token is supplied' do
it "does not assign anything" do
request_with_auth do
perform_index_request({recipe_step_id: recipe.recipe_steps.first.id})
end
expect(response.status).to eq 401
expect(assigns[:recipe_step_presets]).to be_nil
end
end
end
describe "GET show" do
let!(:recipe_step_preset) { create(:recipe_step_preset, account: account, recipe_step: recipe_step) }
context 'if a valid token is supplied' do
context 'if the preset does not exist' do
it "returns an error" do
request_with_auth(account.new_jwt) do
perform_show_request({id: "nonsense"})
end
expect(response.status).to eq 404
expect(assigns[:recipe_step_preset]).to be_nil
end
end
context 'the preset exists' do
context 'the recipe belongs to the account' do
it "finds the preset" do
request_with_auth(account.new_jwt) do
perform_show_request({id: recipe_step_preset.id})
end
expect(response.status).to eq 200
expect(assigns[:recipe_step_preset]).to eq recipe_step_preset
end
end
context 'the recipe belongs to another account and is available' do
let!(:preset2) { create(:recipe_step_preset, account: other_account, recipe_step: recipe.recipe_steps.first) }
it "does not find the preset" do
request_with_auth(account.new_jwt) do
perform_show_request({id: preset2.id})
end
expect(response.status).to eq 404
expect(assigns[:recipe_step_preset]).to be_nil
end
end
context 'for an inactive recipe' do
let!(:recipe1) { create(:recipe, account: account, active: false) }
let!(:preset1) { create(:recipe_step_preset, recipe_step: recipe1.recipe_steps.first, account: account) }
context 'belongs to that account' do
specify do
request_with_auth(account.new_jwt) do
perform_show_request(id: preset1.id)
end
expect(response.status).to eq 200
expect(assigns[:recipe_step_preset]).to eq preset1
end
end
context 'belongs to another account' do
before { recipe1.update_attribute(:account_id, other_account.id) }
specify do
request_with_auth(account.new_jwt) do
perform_show_request(id: preset1.id)
end
expect(response.status).to eq 404
end
end
end
context 'for a private recipe belonging to someone else' do
let!(:recipe1) { create(:recipe, name: "recipe3", account: other_account, public: false) }
let!(:preset1) { create(:recipe_step_preset, recipe_step: recipe1.recipe_steps.first, account: account) }
specify do
request_with_auth(account.new_jwt) do
perform_show_request(id: preset1.id)
end
expect(response.status).to eq 404
end
end
context 'for a public recipe belonging to someone else' do
let!(:recipe1) { create(:recipe, name: "recipe4", account: other_account, public: true) }
let!(:preset1) { create(:recipe_step_preset, recipe_step: recipe1.recipe_steps.first, account: account) }
specify do
request_with_auth(account.new_jwt) do
perform_show_request(id: preset1.id)
end
expect(response.status).to eq 200
expect(assigns[:recipe_step_preset]).to eq preset1
end
end
end
end
context 'if no valid token is supplied' do
it "does not return anything" do
request_with_auth do
perform_show_request({id: recipe_step_preset.id})
end
expect(response.status).to eq 401
expect(assigns[:recipe_step_preset]).to be_nil
end
end
end
describe "DELETE destroy" do
context 'if a valid token is supplied' do
context 'if the preset does not exist' do
it "returns an error" do
request_with_auth(account.new_jwt) do
perform_destroy_request({id: "nonsense"})
end
expect(response.status).to eq 404
expect(assigns[:recipe_step_preset]).to be_nil
end
end
context 'the preset exists' do
let!(:recipe_step_preset) { create(:recipe_step_preset, account: account, recipe_step: recipe_step) }
context 'the preset belongs to the account' do
it "allows it to be destroyed" do
request_with_auth(account.new_jwt) do
perform_destroy_request({id: recipe_step_preset.id})
end
expect(response.status).to eq 200
expect(assigns[:recipe_step_preset]).to eq recipe_step_preset
end
end
context 'the preset belongs to another account' do
before { recipe_step_preset.update_attribute(:account_id, other_account.id) }
it "allows it to be destroyed" do
request_with_auth(account.new_jwt) do
perform_destroy_request(id: recipe_step_preset.id)
end
expect(response.status).to eq 404
expect(assigns[:recipe_step_preset]).to eq nil
end
end
end
end
context 'if no valid token is supplied' do
it "does not return anything" do
request_with_auth do
perform_destroy_request({id: "rubbish"})
end
expect(response.status).to eq 401
expect(assigns[:recipe_step_preset]).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
def perform_create_request(data = {})
post_create_request(version, data)
end
def perform_update_request(data)
put_update_request(version, data)
end
def perform_index_request(data = {})
get_index_request(version, data)
end
def perform_show_request(data = {})
get_show_request(version, data)
end
def perform_destroy_request(data = {})
delete_destroy_request(version, data)