Skip to content
14 changes: 13 additions & 1 deletion test/controllers/tags_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,21 @@ class TagsControllerTest < ActionController::TestCase

users.each do |user|
sign_in(user)
try_create_tag(category, tag_set, name: "test-tag-#{user.id}")

draft_path = create_tag_path(id: category.id)
tag_name = "test-tag-#{user.id}"

@controller.stub('params', { tag_name: tag_name }) do
@controller.do_save_draft(user, draft_path)
end
Comment on lines +118 to +120
Copy link
Member Author

@Oaphi Oaphi Oct 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an unfortunate mess, but doing this "the proper way" would require quite a bit of work that's outside of the scope of the PR (although I am not against making these changes if necessary): move all this to integration tests, move save_draft and delete_draft to concern's actions, etc.


try_create_tag(category, tag_set, name: tag_name)

if !@controller.helpers.user_signed_in?
assert_redirected_to_sign_in
elsif user.can_edit_tags?
assert_response(:found, "Expected user '#{user.name}' to be able to create tags")
assert_draft_deleted(user, draft_path, :tag_name)
else
assert_response(:not_found, "Expected user '#{user.name}' to not be able to create tags")
end
Expand Down Expand Up @@ -276,6 +285,9 @@ class TagsControllerTest < ActionController::TestCase
# @param category [Category] category to create the tag in
# @param tag_set [TagSet] tag set the tag belongs to
def try_create_tag(tag_set, category, **opts)
# this is needed for draft deletion until we switch from request.referer
@request.set_header('HTTP_REFERER', create_tag_path(id: category.id))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this is only here because we rely on request.referer when creating and deleting drafts. I'd like to work on switching from that approach separately, but open to doing it "while we are at it" too.


post :create, params: {
id: category.id,
tag: {
Expand Down
10 changes: 10 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ def copy_abilities(community_id)
end
end

def assert_draft_deleted(user, path, *fields)
base_key = "saved_post.#{user.id}.#{path}"

fields.each do |key|
key_name = DraftManagement::NESTED_DRAFTABLE_FIELDS.include?(key) ? base_key : "#{base_key}.#{key}"
assert_not(RequestContext.redis.exists?(key_name),
"Expected '#{key_name}' draft to be deleted")
end
end

def assert_valid_json_response
assert_nothing_raised do
parsed = JSON.parse(response.body)
Expand Down