Page MenuHomePhabricator

Update AuthorizedUsers API to only list recently logged in Bundle users
Closed, ResolvedPublic

Description

Task
If eligible, users receive a Library Bundle authorization in the Library Card platform. We then use this list of users for the Wikilink tool to determine editors who had access to content when they added a link on Wikimedia projects. This works well for approval-based partners because if a user is applying for access to a resource, we can be reasonably sure any subsequent citations they add were a result of that access. We can't be so sure for the Library Bundle because users are granted automatic access and to a wide range of publishers. We don't want to be inadvertently claiming that users are adding/removing citations to these Bundle publishers when we know that they haven't been active on the Library Card platform.

To solve this, we can add some additional logic to the AuthorizedUsers API view (https://github.com/WikipediaLibrary/TWLight/blob/master/TWLight/users/views.py#L675).

We should add something like the following code after we retrieve valid partner authorizations (https://github.com/WikipediaLibrary/TWLight/blob/master/TWLight/users/views.py#L695):

if partner.authorization_method = partner.BUNDLE:
    valid_partner_auths = valid_partner_auths.filter(
        user_last_login__gt=datetime.today() - timedelta(weeks=2)
    )

Where datetime and timedelta are imported from datetime. This code hasn't been tested, and is provided only as a suggestion, so you may need to alter it.

We should also add a new test (https://github.com/WikipediaLibrary/TWLight/blob/master/TWLight/tests.py#L1114) which verifies this behaviour, and ensure that previous tests still work as expected.

Good first task

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, and include the Phabricator task number in the pull request. 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 Expire Library Bundle authorizations after some time to Update AuthorizedUsers API to only list recently logged in Bundle users.Sep 15 2020, 10:58 AM
Samwalton9-WMF updated the task description. (Show Details)

Hi! I am a new contributor to Wikimedia.
Can I take up this task?

@ShreyTripathi You're very welcome to claim this task - let me know if you have any questions :)

@Samwalton9 Sorry for the novice question, but I am unable to create a superuser for the example data. Can you please help me out?
Also, this is the place for further communication, right?

@ShreyTripathi To use the example data, first log in so that there is a single account registered in the tool. The example data script will then make that account a superuser. And yes, this is the best place :)

@Samwalton9 I have created a pull request at https://github.com/WikipediaLibrary/TWLight/pull/534.
I have added the relevant code, but one of the existing tests is failing. I have described this in more detail over there. Please check it out and suggest the relevant changes.

Great! The test that is failing is this one (https://github.com/WikipediaLibrary/TWLight/blob/master/TWLight/tests.py#L1187) which checks that for a Bundle partner, the correct user list is returned. Since we changed the response for Bundle partners, it is now failing. This didnt occur to me when writing the task description!

I think we should do two things: Update this test, and add a new one.

To update this test (one that passes) we should update the test user's last login data. On line 1199 we could update self.editor1.user.last_login to today's date. That should make the test pass.

To add a new test, we would want to copy this test entirely, but change last_login to 3 weeks ago.

Let me know if you have any questions as you give that a go :)

@Samwalton9 I tried updating self.editor1.user.last_login to today's date, but that still fails the test. Lines 1195-1204 in tests.py now look like this:

bundle_partner_1 = PartnerFactory(authorization_method=Partner.BUNDLE)
bundle_partner_2 = PartnerFactory(authorization_method=Partner.BUNDLE)

self.editor1.wp_bundle_eligible = True
self.editor1.user.last_login = pytz.utc.localize(datetime.datetime.today())
self.editor1.save()
self.editor1.update_bundle_authorization()

# Verify we created the bundle auth as expected
self.assertEqual(get_all_bundle_authorizations().count(), 1)

I tried finding why the test is failing and realized that valid_partner_auths on line 696 in views.py shows the correct query set before going into the if-condition I had added in my pull request, but it shows an empty query set after passing through the if-condition. This means that user.last_login is not satisfying the condition for last_login added to it in the test.
I am really confused as to why this is happening, even though everything seems to be correct. Please help me out here.

Never mind, I found a fix to the issue I was facing! The problem was that after setting self.editor.user.last_login to today's date, self.editor and self.editor.user needed to be saved separately. So I had to add an extra line self.editor1.user.save() to the test. This works (maybe) because editor.user is an entirely different object (please correct me here if I am wrong).
In addition, I have also added another test to check that the users who have not logged in the past two weeks are not listed.
Please review the changes in the commit to the pull request I created, and/or suggest relevant changes.

Great job on finding the fix! That issue has tripped me up in the past too :)