Skip to content

Conversation

@ReverM
Copy link
Contributor

@ReverM ReverM commented Dec 2, 2025

Issue

https://otwarchive.atlassian.net/browse/AO3-6944

Purpose

This fixes a 500 error bug when sorting by prompter with anonymous prompts.

Credit

Danaël / Rever ( they / he )

Copy link
Member

@brianjaustin brianjaustin left a comment

Choose a reason for hiding this comment

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

Could you add a test for the bug behaviour (one that fails without your fix and passes with it)? Thanks!

Caveat for the 2 suggestions below: I have not had a chance to fully test them both, so it's possible they don't work or need some tweaks

@ReverM
Copy link
Contributor Author

ReverM commented Dec 4, 2025

Could you add a test for the bug behaviour (one that fails without your fix and passes with it)? Thanks!

Caveat for the 2 suggestions below: I have not had a chance to fully test them both, so it's possible they don't work or need some tweaks

I wasn't sure how to implement test for this given the replication method. Is there a good way to emulate multiple users or force parameters?

Update: a test has been added. I added 2 new step definitions

@ReverM ReverM requested a review from brianjaustin December 4, 2025 18:42
Copy link
Member

@brianjaustin brianjaustin left a comment

Choose a reason for hiding this comment

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

Thanks for the initial work on the test! We try to avoid "leaking" implementation details like the URL parameter in cucumber, though, so this is a better fit for a controller test, where it's easy to specify arbitrary parameters. (Aside: Rails does not encourage controller tests anymore, but we still use them, and for targetted scenarios like this, we still find them to be the best option.)

A couple example tests that specify parameters: https://github.com/otwcode/otwarchive/blob/a5c1f042d83f9a1caacbe8824d9e3d0cef58cec3/spec/controllers/works/works_controller_spec.rb

As there is no spec for this controller, please feel free to create one at spec/controllers/challenge_requests_controller_spec.rb and let us know if you have further questions!

@ReverM
Copy link
Contributor Author

ReverM commented Dec 7, 2025

Thanks for the initial work on the test! We try to avoid "leaking" implementation details like the URL parameter in cucumber, though, so this is a better fit for a controller test, where it's easy to specify arbitrary parameters. (Aside: Rails does not encourage controller tests anymore, but we still use them, and for targetted scenarios like this, we still find them to be the best option.)

A couple example tests that specify parameters: a5c1f04/spec/controllers/works/works_controller_spec.rb

As there is no spec for this controller, please feel free to create one at spec/controllers/challenge_requests_controller_spec.rb and let us know if you have further questions!

I tried doing it via spec at first but got confused since the create is not defined in challenge_request_controller.rb. It seems like I'd need to pass through FactoryBot.create in order to populate the prompts to get the requested setting. That's the main reason I went towards cucumber, but I will change it to a spec test.

My only question would be if FactoryBot.create is the correct way to populate the prompts in order to test the behaviour?

@brianjaustin
Copy link
Member

Yes, factorybot would be the correct way. Important note, however: Factorybot is automatically included in spec files, so you can just use create without the whole FactoryBot. at the beginning

@ReverM
Copy link
Contributor Author

ReverM commented Dec 8, 2025

I am unsure if this replicate the situation I want to test (i.e I'm unsure if the request is getting correctly created), but other than that, I have added the test in spec form.

@ReverM ReverM requested a review from brianjaustin December 8, 2025 02:20
Copy link
Member

@brianjaustin brianjaustin left a comment

Choose a reason for hiding this comment

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

Thank you for the test! For this sort of thing, the benefit of it being a bug is you can run the test against master and see that it doesn't pass. Unfortunately, in this case, the test does pass, so I think a bit more will be needed. To help with that, you can put binding.pry in any line of the controller or test to create a breakpoint, which will make iterating to set up exactly the right state a bit easier

include RedirectExpectationHelper

describe "index" do
it "should not throw a 500 error if sorting by prompter with an anonymous prompt" do
Copy link
Member

Choose a reason for hiding this comment

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

Based on https://github.com/otwcode/otwarchive/wiki/Automated-Testing#user-content-Our_RSpec_Standards, we would prefer something like

Suggested change
it "should not throw a 500 error if sorting by prompter with an anonymous prompt" do
it "does not throw a 500 error if sorting by prompter with an anonymous prompt" do

It might also be nice to use a context block for the background information like so

    context "when there are anonymous prompts" do
        it "does not throw an error when sorting by prompter" do
            # ...
        end
    end

@ReverM
Copy link
Contributor Author

ReverM commented Dec 14, 2025

Thank you for the test! For this sort of thing, the benefit of it being a bug is you can run the test against master and see that it doesn't pass. Unfortunately, in this case, the test does pass, so I think a bit more will be needed. To help with that, you can put binding.pry in any line of the controller or test to create a breakpoint, which will make iterating to set up exactly the right state a bit easier

I have successfully changed the test so the correct state is reached. However, I cannot get it to fail even on master (I have confirmed that there is an anonymous prompt in the collection, and that the sort column is prompter). It seems that on master @requests is a WillPaginate::ActiveRecord instance, while on my fix it's the actual request. Could it be possible that spec tests do not go all the way through with respect to pagination?

@brianjaustin
Copy link
Member

brianjaustin commented Dec 15, 2025

Ah yes, there's a bit extra to force rendering in controller tests. You can use the render_views declaration within the test: https://rspec.info/features/7-0/rspec-rails/controller-specs/render-views/. I tried running the test against master with that, and it fails like I would expect

@ReverM
Copy link
Contributor Author

ReverM commented Dec 15, 2025

Ah yes, there's a bit extra to force rendering in controller tests. You can use the render_views declaration within the test: rspec.info/features/7-0/rspec-rails/controller-specs/render-views. I tried running the test against master with that, and it fails like I would expect

That was it! Thank you!

Test has been updated as requested (oops there might be a run indexer job needed)

Copy link
Member

@brianjaustin brianjaustin left a comment

Choose a reason for hiding this comment

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

The first suggestion I've added should be enough to run the test fully; we have a hook to make rspec tests that require Elasticsearch easier

Co-authored-by: Brian Austin <13002992+brianjaustin@users.noreply.github.com>
@brianjaustin
Copy link
Member

It looks like more indexes are going to be needed, sorry 😞 You can see a list of the available hooks in https://github.com/otwcode/otwarchive/blob/master/spec/spec_helper.rb#L89 to tinker with -- I believe they should be stackable/combinable

@ReverM
Copy link
Contributor Author

ReverM commented Dec 15, 2025

It looks like more indexes are going to be needed, sorry 😞 You can see a list of the available hooks in master/spec/spec_helper.rb#L89 to tinker with -- I believe they should be stackable/combinable

I do find it somewhat curious how the test work just fine when ran locally. What should be the best way for me to test that the test is going to work on master? I usually always have a terminal open to run elasticsearch. should I not do that?

@brianjaustin
Copy link
Member

Locally, Elasticsearch is already configured, so you would need to delete the indexes to replicate how the environment looks in CI. I can't test this at the moment myself, but you should be able do to something like

Indexer.all.each(&:delete)

to force that locally. (If not, calling delete for each indexer -- like CollectionIndexer.delete -- should work)

@ReverM
Copy link
Contributor Author

ReverM commented Dec 17, 2025

I can't seem to be able to replicate the test configuration, will undraft once I find the correct hook to add to make the test work

@ReverM
Copy link
Contributor Author

ReverM commented Dec 17, 2025

Locally, Elasticsearch is already configured, so you would need to delete the indexes to replicate how the environment looks in CI. I can't test this at the moment myself, but you should be able do to something like

Indexer.all.each(&:delete)

to force that locally. (If not, calling delete for each indexer -- like CollectionIndexer.delete -- should work)

This seem to not be sufficient to make the test fail locally 😔

@ReverM ReverM marked this pull request as ready for review December 18, 2025 02:15
@ReverM ReverM requested a review from brianjaustin December 18, 2025 02:15
Copy link
Member

@brianjaustin brianjaustin left a comment

Choose a reason for hiding this comment

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

Thank you, and nicely done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants