|
1 | 1 | """Test titiler.pgstac Mosaic endpoints.""" |
2 | 2 |
|
| 3 | +import datetime |
3 | 4 | import io |
4 | 5 | import json |
5 | 6 | from unittest.mock import patch |
@@ -692,3 +693,147 @@ def test_collections_cql_filter(filter_expr, filter_lang, app): |
692 | 693 | assert len(resp) == 1 |
693 | 694 | assert list(resp[0]) == ["id", "bbox", "assets", "collection"] |
694 | 695 | assert resp[0]["id"] == "20200307aC0853000w361030" |
| 696 | + |
| 697 | + |
| 698 | +def test_datetime_validation(app) -> None: |
| 699 | + """Ensure datetime parameter validation works as expected.""" |
| 700 | + valid_response = app.get( |
| 701 | + f"/collections/{collection_id}/tiles", |
| 702 | + params={ |
| 703 | + "datetime": datetime.datetime.now(tz=datetime.UTC).isoformat(), |
| 704 | + }, |
| 705 | + ) |
| 706 | + assert valid_response.status_code == 200 |
| 707 | + invalid_response = app.get( |
| 708 | + f"/collections/{collection_id}/tiles", |
| 709 | + params={ |
| 710 | + "datetime": "this is not a valid datetime string", |
| 711 | + }, |
| 712 | + ) |
| 713 | + assert invalid_response.status_code == 422 |
| 714 | + |
| 715 | + |
| 716 | +def test_query_validation(app) -> None: |
| 717 | + """Ensure query parameter validation works as expected.""" |
| 718 | + valid_response = app.get( |
| 719 | + f"/collections/{collection_id}/tiles", |
| 720 | + params={ |
| 721 | + "query": json.dumps({"eo:cloud_cover": {"gte": 95}}), |
| 722 | + }, |
| 723 | + ) |
| 724 | + assert valid_response.status_code == 200 |
| 725 | + invalid_response = app.get( |
| 726 | + f"/collections/{collection_id}/tiles", |
| 727 | + params={ |
| 728 | + "query": "this is not a valid query string", |
| 729 | + }, |
| 730 | + ) |
| 731 | + assert invalid_response.status_code == 422 |
| 732 | + |
| 733 | + |
| 734 | +def test_query_json_content(app) -> None: |
| 735 | + """Ensure valid JSON with invalid query content is validated correctly.""" |
| 736 | + invalid_response = app.get( |
| 737 | + f"/collections/{collection_id}/tiles", |
| 738 | + params={ |
| 739 | + "query": json.dumps({"this": "is not a valid query"}), |
| 740 | + }, |
| 741 | + ) |
| 742 | + assert invalid_response.status_code == 422 |
| 743 | + |
| 744 | + |
| 745 | +def test_filter_json_validation(app) -> None: |
| 746 | + """Ensure JSON filter parameter validation works as expected.""" |
| 747 | + valid_response = app.get( |
| 748 | + f"/collections/{collection_id}/tiles", |
| 749 | + params={ |
| 750 | + "filter": json.dumps( |
| 751 | + { |
| 752 | + "op": "lte", |
| 753 | + "args": [ |
| 754 | + { |
| 755 | + "property": "gsd", |
| 756 | + }, |
| 757 | + 10, |
| 758 | + ], |
| 759 | + } |
| 760 | + ), |
| 761 | + "filter-lang": "cql2-json", |
| 762 | + }, |
| 763 | + ) |
| 764 | + assert valid_response.status_code == 200 |
| 765 | + invalid_content_response = app.get( |
| 766 | + f"/collections/{collection_id}/tiles", |
| 767 | + params={ |
| 768 | + "filter": json.dumps({"this": "is invalid content"}), |
| 769 | + }, |
| 770 | + ) |
| 771 | + assert invalid_content_response.status_code == 422 |
| 772 | + invalid_json_response = app.get( |
| 773 | + f"/collections/{collection_id}/tiles", |
| 774 | + params={ |
| 775 | + "filter": "this is not a valid JSON string", |
| 776 | + "filter-lang": "cql2-json", |
| 777 | + }, |
| 778 | + ) |
| 779 | + assert invalid_json_response.status_code == 422 |
| 780 | + |
| 781 | + |
| 782 | +def test_filter_text_validation(app) -> None: |
| 783 | + """Ensure text filter parameter validation works as expected.""" |
| 784 | + valid_response = app.get( |
| 785 | + f"/collections/{collection_id}/tiles", |
| 786 | + params={ |
| 787 | + "filter": "id=irrelevant-value", |
| 788 | + "filter-lang": "cql2-text", |
| 789 | + }, |
| 790 | + ) |
| 791 | + assert valid_response.status_code == 200 |
| 792 | + invalid_response = app.get( |
| 793 | + f"/collections/{collection_id}/tiles", |
| 794 | + params={ |
| 795 | + "filter": "this is not a valid filter string", |
| 796 | + "filter-lang": "cql2-text", |
| 797 | + }, |
| 798 | + ) |
| 799 | + assert invalid_response.status_code == 422 |
| 800 | + |
| 801 | + |
| 802 | +def test_bbox_validation(app) -> None: |
| 803 | + """Ensure bbox parameter validation works as expected.""" |
| 804 | + valid_response_3d = app.get( |
| 805 | + f"/collections/{collection_id}/tiles", |
| 806 | + params={ |
| 807 | + "bbox": "-180,-90,-1,180,90,1", |
| 808 | + }, |
| 809 | + ) |
| 810 | + assert valid_response_3d.status_code == 200 |
| 811 | + |
| 812 | + valid_response_2d = app.get( |
| 813 | + f"/collections/{collection_id}/tiles", |
| 814 | + params={ |
| 815 | + "bbox": "-180,-90,180,90", |
| 816 | + }, |
| 817 | + ) |
| 818 | + assert valid_response_2d.status_code == 200 |
| 819 | + |
| 820 | + invalid_format_1_response = app.get( |
| 821 | + f"/collections/{collection_id}/tiles", |
| 822 | + params={ |
| 823 | + "bbox": "invalid bbox string", |
| 824 | + }, |
| 825 | + ) |
| 826 | + assert invalid_format_1_response.status_code == 422 |
| 827 | + |
| 828 | + invalid_format_2_response = app.get( |
| 829 | + f"/collections/{collection_id}/tiles", |
| 830 | + params={ |
| 831 | + "bbox": "-180,-90,180", |
| 832 | + }, |
| 833 | + ) |
| 834 | + assert invalid_format_2_response.status_code == 422 |
| 835 | + |
| 836 | + invalid_values_response = app.get( |
| 837 | + f"/collections/{collection_id}/tiles", params={"bbox": "180,-90,-180,90"} |
| 838 | + ) |
| 839 | + assert invalid_values_response.status_code == 422 |
0 commit comments