|
2 | 2 | import requests_mock |
3 | 3 | from werkzeug.exceptions import Forbidden |
4 | 4 |
|
5 | | -from app.articles.api import get_content |
| 5 | +from app.articles.api import _get_headers, get_content |
6 | 6 |
|
7 | 7 | gc_articles_api = "articles.alpha.canada.ca/notification-gc-notify" |
8 | 8 | notify_url = f"https://{gc_articles_api}/wp-json/pages" |
@@ -54,3 +54,34 @@ def test_get_content_403(app_, mocker, capsys): |
54 | 54 | get_content("pages", {"slug": "mypage", "lang": "en"}) |
55 | 55 |
|
56 | 56 | assert "403 Forbidden" in str(exception.value) |
| 57 | + |
| 58 | + |
| 59 | +class TestGetHeaders: |
| 60 | + def test_waf_rate_bypass_header_included_when_secret_set(self, app_, mocker): |
| 61 | + with app_.test_request_context(): |
| 62 | + mocker.patch.dict( |
| 63 | + "app.current_app.config", |
| 64 | + values={"GC_ARTICLES_WAF_RATE_BYPASS_SECRET": "some-secret"}, |
| 65 | + ) |
| 66 | + headers = _get_headers() |
| 67 | + assert headers["waf-rate-bypass"] == "some-secret" |
| 68 | + |
| 69 | + def test_waf_rate_bypass_header_not_included_when_secret_not_set(self, app_, mocker): |
| 70 | + with app_.test_request_context(): |
| 71 | + mocker.patch.dict( |
| 72 | + "app.current_app.config", |
| 73 | + values={"GC_ARTICLES_WAF_RATE_BYPASS_SECRET": None}, |
| 74 | + ) |
| 75 | + headers = _get_headers() |
| 76 | + assert "waf-rate-bypass" not in headers |
| 77 | + |
| 78 | + def test_waf_rate_bypass_header_included_alongside_auth_header(self, app_, mocker): |
| 79 | + mocker.patch("app.articles.api.authenticate", return_value="some-token") |
| 80 | + with app_.test_request_context(): |
| 81 | + mocker.patch.dict( |
| 82 | + "app.current_app.config", |
| 83 | + values={"GC_ARTICLES_WAF_RATE_BYPASS_SECRET": "some-secret"}, |
| 84 | + ) |
| 85 | + headers = _get_headers(auth_required=True) |
| 86 | + assert headers["waf-rate-bypass"] == "some-secret" |
| 87 | + assert headers["Authorization"] == "Bearer some-token" |
0 commit comments