def test_sso_sync(enable_sso, user_client, user, requests_mock):
requests_mock.post(
f"{django_settings.SSO_HOST}/user_status",
json=user_status_json(user, projects=["user_project", "another_project"]),
)
user.fairdata_username = user.username # make user a fairdata user
# Recent sync, should not sync user on next request
user.synced = timezone.now()
user.save()
res = user_client.get(reverse("user"))
assert res.status_code == 200
assert res.data["csc_projects"] == []
# No previous sync, should sync user
user.refresh_from_db()
user.synced = None
user.save()
res = user_client.get(reverse("user"))
assert res.status_code == 200
assert res.data["csc_projects"] == ["user_project", "another_project"]
requests_mock.post(
f"{django_settings.SSO_HOST}/user_status",
json=user_status_json(user, projects=["not_yet_synced_project"]),
)
# Long time since sync, should sync user
user.refresh_from_db()
user.synced = timezone.now() - timezone.timedelta(days=28)
user.save()
res = user_client.get(reverse("user"))
assert res.status_code == 200
assert res.data["csc_projects"] == ["not_yet_synced_project"]