mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-15 23:57:48 +00:00
Keep only used api/taxons index action, delete all others as not required right now
This commit is contained in:
@@ -25,51 +25,12 @@ module Spree
|
||||
show
|
||||
end
|
||||
|
||||
def create
|
||||
authorize! :create, Taxon
|
||||
@taxon = Taxon.new(params[:taxon])
|
||||
@taxon.taxonomy_id = params[:taxonomy_id]
|
||||
taxonomy = Taxonomy.find_by_id(params[:taxonomy_id])
|
||||
|
||||
if taxonomy.nil?
|
||||
@taxon.errors[:taxonomy_id] = I18n.t(:invalid_taxonomy_id, scope: 'spree.api')
|
||||
invalid_resource!(@taxon) && return
|
||||
end
|
||||
|
||||
@taxon.parent_id = taxonomy.root.id unless params[:taxon][:parent_id]
|
||||
|
||||
if @taxon.save
|
||||
respond_with(@taxon, status: 201, default_template: :show)
|
||||
else
|
||||
invalid_resource!(@taxon)
|
||||
end
|
||||
end
|
||||
|
||||
def update
|
||||
authorize! :update, Taxon
|
||||
if taxon.update_attributes(params[:taxon])
|
||||
respond_with(taxon, status: 200, default_template: :show)
|
||||
else
|
||||
invalid_resource!(taxon)
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
authorize! :delete, Taxon
|
||||
taxon.destroy
|
||||
respond_with(taxon, status: 204)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def taxonomy
|
||||
return if params[:taxonomy_id].blank?
|
||||
@taxonomy ||= Taxonomy.find(params[:taxonomy_id])
|
||||
end
|
||||
|
||||
def taxon
|
||||
@taxon ||= taxonomy.taxons.find(params[:id])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -64,72 +64,6 @@ module Spree
|
||||
response["attr"].should eq("name" => taxon2.name, "id" => taxon2.id)
|
||||
response["state"].should eq("closed")
|
||||
end
|
||||
|
||||
it "can learn how to create a new taxon" do
|
||||
api_get :new, taxonomy_id: taxonomy.id
|
||||
expect(json_response["attributes"]).to eq(attributes.map(&:to_s))
|
||||
required_attributes = json_response["required_attributes"]
|
||||
expect(required_attributes).to include("name")
|
||||
end
|
||||
|
||||
it "cannot create a new taxon if not an admin" do
|
||||
api_post :create, taxonomy_id: taxonomy.id, taxon: { name: "Location" }
|
||||
assert_unauthorized!
|
||||
end
|
||||
|
||||
it "cannot update a taxon" do
|
||||
api_put :update, taxonomy_id: taxonomy.id,
|
||||
id: taxon.id,
|
||||
taxon: { name: "I hacked your store!" }
|
||||
assert_unauthorized!
|
||||
end
|
||||
|
||||
it "cannot delete a taxon" do
|
||||
api_delete :destroy, taxonomy_id: taxonomy.id, id: taxon.id
|
||||
assert_unauthorized!
|
||||
end
|
||||
end
|
||||
|
||||
context "as an admin" do
|
||||
let(:current_api_user) { build(:admin_user) }
|
||||
|
||||
it "can create" do
|
||||
api_post :create, taxonomy_id: taxonomy.id, taxon: { name: "Colors" }
|
||||
|
||||
expect(attributes.all? { |a| json_response.include? a }).to be true
|
||||
expect(response.status).to eq(201)
|
||||
|
||||
expect(taxonomy.reload.root.children.count).to eq 2
|
||||
|
||||
expect(Spree::Taxon.last.parent_id).to eq taxonomy.root.id
|
||||
expect(Spree::Taxon.last.taxonomy_id).to eq taxonomy.id
|
||||
end
|
||||
|
||||
it "cannot create a new taxon with invalid attributes" do
|
||||
api_post :create, taxonomy_id: taxonomy.id, taxon: {}
|
||||
expect(response.status).to eq(422)
|
||||
expect(json_response["error"]).to eq("Invalid resource. Please fix errors and try again.")
|
||||
errors = json_response["errors"]
|
||||
|
||||
expect(taxonomy.reload.root.children.count).to eq 1
|
||||
end
|
||||
|
||||
it "cannot create a new taxon with invalid taxonomy_id" do
|
||||
api_post :create, taxonomy_id: 1000, taxon: { name: "Colors" }
|
||||
expect(response.status).to eq(422)
|
||||
expect(json_response["error"]).to eq("Invalid resource. Please fix errors and try again.")
|
||||
|
||||
errors = json_response["errors"]
|
||||
expect(errors["taxonomy_id"]).not_to be_nil
|
||||
expect(errors["taxonomy_id"].first).to eq "Invalid taxonomy id."
|
||||
|
||||
expect(taxonomy.reload.root.children.count).to eq 1
|
||||
end
|
||||
|
||||
it "can destroy" do
|
||||
api_delete :destroy, taxonomy_id: taxonomy.id, id: taxon2.id
|
||||
expect(response.status).to eq(204)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user