Skip to content

Commit

Permalink
Remove references to Tag where possible
Browse files Browse the repository at this point in the history
Tag is based upon classifications, so just use those

Mostly replaced the Tag.find() with Classification.find_by(:tag_id)
  • Loading branch information
kbrock committed Jan 31, 2019
1 parent 7fc8c01 commit d359a07
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 30 deletions.
2 changes: 1 addition & 1 deletion spec/requests/categories_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
classification = FactoryBot.create(:classification_tag)
category = FactoryBot.create(:category, :children => [classification])
tag = classification.tag
Tag.create(:name => "some_other_tag")
FactoryBot.create(:classification, :name => "some_other_tag")
api_basic_authorize

get(api_category_tags_url(nil, category))
Expand Down
4 changes: 2 additions & 2 deletions spec/requests/querying_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -650,8 +650,8 @@ def create_vms_by_name(names)
end

it "can do fuzzy matching on strings with forward slashes" do
tag_1 = FactoryBot.create(:tag, :name => "/managed/foo")
_tag_2 = FactoryBot.create(:tag, :name => "/managed/bar")
tag_1 = FactoryBot.create(:classification, :name => "foo").tag
_tag_2 = FactoryBot.create(:classification, :name => "bar")
api_basic_authorize collection_action_identifier(:tags, :read, :get)

get(api_tags_url, :params => { :filter => ["name='*/foo'"] })
Expand Down
55 changes: 28 additions & 27 deletions spec/requests/tags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

get api_tags_url

expect_query_result(:tags, Tag.count)
expect_query_result(:tags, Classification.count)
end

context "with an appropriate role" do
Expand All @@ -26,10 +26,10 @@
category = FactoryBot.create(:classification)
options = {:name => "test_tag", :description => "Test Tag", :category => {:href => api_category_url(nil, category)}}

expect { post api_tags_url, :params => options }.to change(Tag, :count).by(1)
expect { post api_tags_url, :params => options }.to change(Classification, :count).by(1)

result = response.parsed_body["results"].first
tag = Tag.find(result["id"])
tag = Classification.find_by!(:tag_id => result["id"]).tag
tag_category = tag.category
expect(tag_category).to eq(category)
expect(result["href"]).to include(api_tag_url(nil, tag))
Expand All @@ -42,9 +42,9 @@

expect do
post api_tags_url, :params => { :name => "test_tag", :description => "Test Tag", :category => {:id => category.id} }
end.to change(Tag, :count).by(1)
end.to change(Classification, :count).by(1)

tag = Tag.find(response.parsed_body["results"].first["id"])
tag = Classification.find_by!(:tag_id => response.parsed_body["results"].first["id"]).tag
tag_category = tag.category
expect(tag_category).to eq(category)

Expand All @@ -57,9 +57,9 @@

expect do
post api_tags_url, :params => { :name => "test_tag", :description => "Test Tag", :category => {:name => category.name} }
end.to change(Tag, :count).by(1)
end.to change(Classification, :count).by(1)

tag = Tag.find(response.parsed_body["results"].first["id"])
tag = Classification.find_by!(:tag_id => response.parsed_body["results"].first["id"]).tag
tag_category = tag.category
expect(tag_category).to eq(category)

Expand All @@ -72,8 +72,8 @@

expect do
post(api_category_tags_url(nil, category), :params => { :name => "test_tag", :description => "Test Tag" })
end.to change(Tag, :count).by(1)
tag = Tag.find(response.parsed_body["results"].first["id"])
end.to change(Classification, :count).by(1)
tag = Classification.find_by!(:tag_id => response.parsed_body["results"].first["id"]).tag
tag_category = tag.category
expect(tag_category).to eq(category)

Expand Down Expand Up @@ -108,8 +108,8 @@
tag = classification.tag

expect do
post api_tag_url(nil, tag), :params => gen_request(:edit, :description => "New Description")
end.to change { tag.reload.classification.description }.to("New Description")
post api_tag_url(nil, classification.tag_id), :params => gen_request(:edit, :description => "New Description")
end.to change { classification.reload.description }.to("New Description")

expect(response).to have_http_status(:ok)
end
Expand All @@ -119,7 +119,7 @@
classification = FactoryBot.create(:classification_tag)
tag = classification.tag

expect { post api_tag_url(nil, tag), :params => { :action => :delete } }.to change(Tag, :count).by(-1)
expect { post api_tag_url(nil, tag), :params => { :action => :delete } }.to change(Classification, :count).by(-1)
expect { classification.reload }.to raise_error(ActiveRecord::RecordNotFound)
expect(response).to have_http_status(:ok)
end
Expand All @@ -129,7 +129,7 @@
classification = FactoryBot.create(:classification_tag)
tag = classification.tag

expect { delete api_tag_url(nil, tag) }.to change(Tag, :count).by(-1)
expect { delete api_tag_url(nil, tag) }.to change(Classification, :count).by(-1)
expect { classification.reload }.to raise_error(ActiveRecord::RecordNotFound)
expect(response).to have_http_status(:no_content)
end
Expand Down Expand Up @@ -166,7 +166,7 @@

expect do
post(api_category_tags_url(nil, category), :params => gen_request(:delete, [{:id => tag1.id}, {:id => tag2.id}]))
end.to change(Tag, :count).by(-2)
end.to change(Classification, :count).by(-2)
expect { classification1.reload }.to raise_error(ActiveRecord::RecordNotFound)
expect { classification2.reload }.to raise_error(ActiveRecord::RecordNotFound)
expect_result_to_match_hash(
Expand All @@ -190,7 +190,7 @@

expect do
post(api_category_tags_url(nil, category), :params => body)
end.to change(Tag, :count).by(-2)
end.to change(Classification, :count).by(-2)
expect { classification1.reload }.to raise_error(ActiveRecord::RecordNotFound)
expect { classification2.reload }.to raise_error(ActiveRecord::RecordNotFound)
expect_result_to_match_hash(
Expand All @@ -210,36 +210,37 @@

expect do
post api_tags_url, :params => { :name => "test_tag", :description => "Test Tag" }
end.not_to change(Tag, :count)
end.not_to change(Classification, :count)

expect(response).to have_http_status(:forbidden)
end

it "cannot update a tag" do
api_basic_authorize
tag = Tag.create(:name => "Old name")
classification = Classification.is_category.create(:name => "test_tag", :description => "Test Tag")
tag = classification.tag

expect do
post api_tag_url(nil, tag), :params => gen_request(:edit, :name => "New name")
end.not_to change { tag.reload.name }
post api_tag_url(nil, tag), :params => gen_request(:edit, :name => "new_name")
end.not_to change { classification.reload.name }

expect(response).to have_http_status(:forbidden)
end

it "cannot delete a tag through POST" do
api_basic_authorize
tag = Tag.create(:name => "Test tag")
tag = Classification.is_category.create(:name => "test_tag", :description => "Test Tag").tag

expect { post api_tag_url(nil, tag), :params => { :action => :delete } }.not_to change(Tag, :count)
expect { post api_tag_url(nil, tag), :params => { :action => :delete } }.not_to change(Classification, :count)

expect(response).to have_http_status(:forbidden)
end

it "cannot delete a tag through DELETE" do
api_basic_authorize
tag = Tag.create(:name => "Test tag")
tag = Classification.is_category.create(:name => "test_tag", :description => "Test Tag").tag

expect { delete api_tag_url(nil, tag) }.not_to change(Tag, :count)
expect { delete api_tag_url(nil, tag) }.not_to change(Classification, :count)

expect(response).to have_http_status(:forbidden)
end
Expand All @@ -258,14 +259,14 @@

get api_tags_url, :params => { :expand => "resources" }

expect_query_result(:tags, Tag.count, Tag.count)
expect_query_result(:tags, Classification.count, Classification.count)
expect_result_resources_to_include_keys("resources", %w(id name))
end

it "query tag details with multiple virtual attributes" do
api_basic_authorize action_identifier(:tags, :read, :resource_actions, :get)

tag = Tag.last
tag = Classification.is_entry.last.tag
attr_list = "category.name,category.description,classification.name,classification.description"
get api_tag_url(nil, tag), :params => { :attributes => attr_list }

Expand All @@ -281,7 +282,7 @@
it "query tag details with categorization" do
api_basic_authorize action_identifier(:tags, :read, :resource_actions, :get)

tag = Tag.last
tag = Classification.is_entry.last.tag
get api_tag_url(nil, tag), :params => { :attributes => "categorization" }

expect_single_resource_query(
Expand All @@ -302,7 +303,7 @@

get api_tags_url, :params => { :expand => "resources", :attributes => "categorization" }

expect_query_result(:tags, Tag.count, Tag.count)
expect_query_result(:tags, Classification.count, Classification.count)
expect_result_resources_to_include_keys("resources", %w(id name categorization))
end
end
Expand Down

0 comments on commit d359a07

Please sign in to comment.