Page MenuHomePhabricator

Fix server error on /users/ when site has no coordinators
Closed, ResolvedPublic



With a single user signed in and no example data filled, the /users/ page returns a server error because the coordinators user set is empty:


File "/app/TWLight/" in dispatch
  95.         if not self.test_func_coordinator_or_self(request.user):

File "/app/TWLight/" in test_func_coordinator_or_self
  59.         if user in coordinators.user_set.all():

Exception Type: AttributeError at /users/
Exception Value: 'NoneType' object has no attribute 'user_set'

In test_func_coordinator_or_self we should first ensure that there are coordinators before doing if user in coordinators.user_set.all(), by adding if coordinators: to the line above.

This change likely needs to be implemented in CoordinatorOrSelf, CoordinatorsOnly, and PartnerCoordinatorOnly in

It would be useful to add a new test in users/ for this case: One user, no coordinators, and test that the page can load successfully.

Good first bug

This task has been placed in the good first task category. This means it has been scoped and written in a way that makes it simpler for folks who haven’t contributed to the tool’s development or open source software in the past.

If that’s you, welcome! Please feel free to ask questions here about this specific task or the codebase more generally. We’ll be more than happy to help you and clarify the steps needed to complete the task, whether that’s setting up the repository, implementing the necessary changes, or pushing your changes to Github.

If you have experience contributing to this project or similar ones, please consider leaving this one for someone new, and taking a look at the Open Tasks column of the workboard for another task. Also feel free to help out if you see unanswered questions here!

How to contribute

Assign yourself to this task: Click the ‘Add Action’ dropdown menu below and then select Assign / Claim. The box should fill your username in automatically, then click Submit!

To submit your changes, you should fork the repository and create a new branch. After pushing your changes to your Github branch, you can open a pull request. Please link your pull request in a comment here when it has been submitted. Experienced contributors to the project will review your code and either provide feedback or merge it in!

Event Timeline

Samwalton9-WMF renamed this task from Server error on /users/ when site has no coordinators to Fix server error on /users/ when site has no coordinators.Oct 25 2019, 12:31 PM
Samwalton9-WMF updated the task description. (Show Details)

I have changed if user in coordinators.user_set.all(): to if coordinators and user in coordinators.user_set.all(): in PartnerCoordiantorOnly
return user.is_superuser or (coordinators and user in coordinators.user_set.all()) in CoordinatorsOnly
if coordinators and user in coordinators.user_set.all(): it was already there in CoordinatorOrSelf, so no change.

For the test case, I went through users/ but found it intimidating. I have no experience with test cases :\ Please guide me how I can complete this task.

For the test case, I went through users/ but found it intimidating. I have no experience with test cases :\ Please guide me how I can complete this task.

Quite understandable!

We want to test the users profile view, so we'll add a test to the ViewsTestCase test case; it can be called test_user_view_no_coordinators.

The setUp function generates a bunch of users for use across this test case's tests, so if we add a new test it will already have 4 users in the database. editor4 is created as a coordinator, but we don't want any coordinators for this new test, so the first thing we should do in our new test is remove that editor from the coordinator user set:


Next, we can copy the content of test_editor_can_see_own_page - ultimately we want to do the same thing this test does, just without a coordinator existing in the database. This test first generates a request for the user profile page, as if being viewed by user_editor. It then looks at the response for that request and checks that it has a status code of 200, i.e. we successfully loaded the page. Without your code changes, this new test should fail. With it, it should pass!

I created

def test_user_view_no_coordinators(self):
        """Check that users with no coordinators can see their own pages."""
        factory = RequestFactory()
        request = factory.get(self.url1)
        request.user = self.user_editor

        response = views.EditorDetailView.as_view()(request,
        self.assertEqual(response.status_code, 200)

But when I am trying to run this test case without any changes in, it should fail but it passed.

image.png (166×640 px, 17 KB)

Very curious! That suggests to me that we didn't understand the problem correctly. I actually can't reproduce it locally. I wonder if we inadvertently fixed this somewhere else.

Oh, looks like this might have been fixed in

Could you push just the test? It's still useful that we have that test case, even if we don't need direct actual code changes at this time.