DemocracyClub / dc_django_utils

Shared helpers and utility functions for DC websites
MIT License
0 stars 2 forks source link

Fix bug where dc_form render called with a string #16

Closed michaeljcollinsuk closed 3 years ago

michaeljcollinsuk commented 3 years ago

I still think it would be worth fixing this further by not having the 500 page extend from the projects base.html, as it was the attempt to render the mailing form on a 500 page that is the root cause of the error. However I'm not sure how best to go about this right now, for in the meantime this will resolve the error AttributeError: 'str' object has no attribute 'visible_fields' that was occurring in projects and suppressing the real error that was occurring from being raised

symroe commented 3 years ago

This looks about right, but I wonder if it's possible to write a test for it at all? And verify that the error is actually shown? No worries if it's too hard to do!

michaeljcollinsuk commented 3 years ago

This looks about right, but I wonder if it's possible to write a test for it at all? And verify that the error is actually shown? No worries if it's too hard to do!

I can add a unit test to check that render isn't called if it gets passed a non-form object... but something closer to a integration test that replicates the issue that was occurring struggling a bit to think of how to do.

I know its not an actual test, but I did test it locally by making this change, then removing the EE_BASE setting from WCIVF (which had caused me to come across this again yesterday), and running the test to see how it fails. And instead of getting the AttributeError: 'str' object has no attribute 'visible_fields' error, I now got the actual exception due to the missing setting that was triggering the 500 for example:

  File "/Users/michaelcollins/code/dc/WhoCanIVoteFor/env/lib/python3.6/site-packages/vcr/stubs/__init__.py", line 232, in getresponse
    cassette=self.cassette, failed_request=self._vcr_request
vcr.errors.CannotOverwriteExistingCassetteException: Can't overwrite existing cassette ('fixtures/vcr_cassettes/test_postcode_view.yaml') in your current record mode (<RecordMode.ONCE: 'once'>).
No match for the request (<Request (GET) http://localhost:8000/api/elections?postcode=EC1A%204EU&current=1>) was found.
Found 1 similar requests with 3 different matcher(s) :

1 - (<Request (GET) https://elections.democracyclub.org.uk/api/elections?postcode=EC1A%204EU&current=1>).
Matchers succeeded : ['method', 'path', 'query']
Matchers failed :
scheme - assertion failure :
http != https
host - assertion failure :
localhost != elections.democracyclub.org.uk
port - assertion failure :
8000 != 443
========================================================================================= short test summary info ==========================================================================================
FAILED wcivf/apps/api/tests/test_api.py::TestAPISearchViews::test_candidates_for_postcode_view - vcr.errors.CannotOverwriteExistingCassetteException: Can't overwrite existing cassette ('fixtures/vcr_ca...