-
Notifications
You must be signed in to change notification settings - Fork 6
feat: web page revalidation #1653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
17fbb72
wip: revalidate
cka-y c83f1c1
fix: web api secrets
cka-y 805434b
fix: web revalidate url
cka-y 5b91646
fix: missing comma
cka-y 9facf2d
fix: missing dependency
cka-y 663d3fa
fix: lint
cka-y 007a2cd
fix: adding missing pckg
cka-y 75658b8
fix: lint
cka-y 18555c0
fix: api call payload
cka-y 7b8ae0e
fix: added revalidate to operations api and removed from transitfeeds…
cka-y 4cb4dc3
Remove TransitFeeds backend sync code
cka-y e87073c
Remove TransitFeeds-related code from web-app
cka-y 7985c45
revert: restore web-app/ and api/ changes
cka-y cf5972c
added missing requirements
cka-y File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,4 +49,5 @@ PyJWT | |
| shapely | ||
| google-cloud-pubsub | ||
| pycountry | ||
| pytz | ||
| pytz | ||
| google-cloud-tasks | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| # | ||
| # MobilityData 2025 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
|
|
||
| import sys | ||
| import unittest | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
|
|
||
| class TestCreateWebRevalidationTask(unittest.TestCase): | ||
| def setUp(self): | ||
| # google-cloud-tasks is not installed in the test environment. | ||
| # Provide a MagicMock so `from google.cloud import tasks_v2` succeeds | ||
| # for tests that proceed past the early-return guards. | ||
| self._mock_tasks_v2 = MagicMock() | ||
| self._sys_modules_patcher = patch.dict(sys.modules, {"google.cloud.tasks_v2": self._mock_tasks_v2}) | ||
| self._sys_modules_patcher.start() | ||
|
|
||
| def tearDown(self): | ||
| self._sys_modules_patcher.stop() | ||
|
|
||
| def test_empty_feed_ids(self): | ||
| """Should return early without creating any tasks.""" | ||
| from shared.common.gcp_utils import create_web_revalidation_task | ||
|
|
||
| # Should not raise | ||
| create_web_revalidation_task([]) | ||
|
|
||
| @patch.dict( | ||
| "os.environ", | ||
| { | ||
| "PROJECT_ID": "test-project", | ||
| "WEB_REVALIDATION_QUEUE": "", | ||
| "GCP_REGION": "us-central1", | ||
| "ENVIRONMENT": "dev", | ||
| }, | ||
| ) | ||
| def test_missing_queue_env_var(self): | ||
| """Should log a warning and return without creating tasks.""" | ||
| from shared.common.gcp_utils import create_web_revalidation_task | ||
|
|
||
| # Should not raise | ||
| create_web_revalidation_task(["mdb-123"]) | ||
|
|
||
| @patch("shared.common.gcp_utils.create_http_task_with_name") | ||
| @patch.dict( | ||
| "os.environ", | ||
| { | ||
| "PROJECT_ID": "test-project", | ||
| "WEB_REVALIDATION_QUEUE": "web-revalidation-queue", | ||
| "GCP_REGION": "us-central1", | ||
| "ENVIRONMENT": "dev", | ||
| "SERVICE_ACCOUNT_EMAIL": "test@test.iam.gserviceaccount.com", | ||
| }, | ||
| ) | ||
| def test_creates_tasks_for_each_feed(self, mock_create_task): | ||
| """Should create one Cloud Task per feed stable ID.""" | ||
| from shared.common.gcp_utils import create_web_revalidation_task | ||
|
|
||
| create_web_revalidation_task(["mdb-100", "mdb-200"]) | ||
|
|
||
| self.assertEqual(mock_create_task.call_count, 2) | ||
|
|
||
| # Verify the task bodies contain the correct feed IDs | ||
| first_call_body = mock_create_task.call_args_list[0] | ||
| second_call_body = mock_create_task.call_args_list[1] | ||
|
|
||
| self.assertIn(b"mdb-100", first_call_body.kwargs.get("body", b"")) | ||
| self.assertIn(b"mdb-200", second_call_body.kwargs.get("body", b"")) | ||
|
|
||
| @patch("shared.common.gcp_utils.create_http_task_with_name") | ||
| @patch.dict( | ||
| "os.environ", | ||
| { | ||
| "PROJECT_ID": "test-project", | ||
| "WEB_REVALIDATION_QUEUE": "web-revalidation-queue", | ||
| "GCP_REGION": "us-central1", | ||
| "ENVIRONMENT": "dev", | ||
| "SERVICE_ACCOUNT_EMAIL": "test@test.iam.gserviceaccount.com", | ||
| }, | ||
| ) | ||
| def test_dedup_task_name_contains_feed_id(self, mock_create_task): | ||
| """Task name should include the feed stable ID for deduplication.""" | ||
| from shared.common.gcp_utils import create_web_revalidation_task | ||
|
|
||
| create_web_revalidation_task(["mdb-42"]) | ||
|
|
||
| self.assertEqual(mock_create_task.call_count, 1) | ||
| task_name = mock_create_task.call_args.kwargs.get("task_name", "") | ||
| self.assertTrue(task_name.startswith("revalidate-mdb-42-")) | ||
|
|
||
| @patch("shared.common.gcp_utils.create_http_task_with_name") | ||
| @patch.dict( | ||
| "os.environ", | ||
| { | ||
| "PROJECT_ID": "test-project", | ||
| "WEB_REVALIDATION_QUEUE": "web-revalidation-queue", | ||
| "GCP_REGION": "us-central1", | ||
| "ENVIRONMENT": "dev", | ||
| "SERVICE_ACCOUNT_EMAIL": "test@test.iam.gserviceaccount.com", | ||
| }, | ||
| ) | ||
| def test_already_exists_is_handled_gracefully(self, mock_create_task): | ||
| """ALREADY_EXISTS errors should be caught and logged, not raised.""" | ||
| mock_create_task.side_effect = Exception("409 ALREADY_EXISTS: task already exists") | ||
| from shared.common.gcp_utils import create_web_revalidation_task | ||
|
|
||
| # Should not raise | ||
| create_web_revalidation_task(["mdb-123"]) | ||
|
|
||
| @patch("shared.common.gcp_utils.create_http_task_with_name") | ||
| @patch.dict( | ||
| "os.environ", | ||
| { | ||
| "PROJECT_ID": "test-project", | ||
| "WEB_REVALIDATION_QUEUE": "web-revalidation-queue", | ||
| "GCP_REGION": "us-central1", | ||
| "ENVIRONMENT": "dev", | ||
| "SERVICE_ACCOUNT_EMAIL": "test@test.iam.gserviceaccount.com", | ||
| }, | ||
| ) | ||
| def test_targets_tasks_executor_url(self, mock_create_task): | ||
| """Tasks should target the tasks_executor Cloud Function URL.""" | ||
| from shared.common.gcp_utils import create_web_revalidation_task | ||
|
|
||
| create_web_revalidation_task(["mdb-1"]) | ||
|
|
||
| url = mock_create_task.call_args.kwargs.get("url", "") | ||
| self.assertIn("tasks_executor-dev", url) | ||
| self.assertIn("us-central1", url) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see this library used in the API. We need it in the operations API requirements.txt.