|
3 | 3 | from openfeature.contrib.provider.ofrep import OFREPProvider |
4 | 4 | from openfeature.evaluation_context import EvaluationContext |
5 | 5 | from openfeature.exception import ( |
| 6 | + ErrorCode, |
6 | 7 | FlagNotFoundError, |
7 | 8 | GeneralError, |
8 | 9 | InvalidContextError, |
| 10 | + OpenFeatureError, |
9 | 11 | ParseError, |
10 | 12 | TypeMismatchError, |
11 | 13 | ) |
@@ -83,6 +85,61 @@ def test_provider_flag_not_found(ofrep_provider, requests_mock): |
83 | 85 | ofrep_provider.resolve_boolean_details("flag_key", False) |
84 | 86 |
|
85 | 87 |
|
| 88 | +def test_provider_unauthorized(ofrep_provider, requests_mock): |
| 89 | + requests_mock.post( |
| 90 | + "http://localhost:8080/ofrep/v1/evaluate/flags/flag_key", |
| 91 | + status_code=401, |
| 92 | + text="unauthorized", |
| 93 | + ) |
| 94 | + |
| 95 | + with pytest.raises(OpenFeatureError) as exc_info: |
| 96 | + ofrep_provider.resolve_boolean_details("flag_key", False) |
| 97 | + |
| 98 | + assert exc_info.value.error_code == ErrorCode.GENERAL |
| 99 | + assert exc_info.value.error_message == "unauthorized" |
| 100 | + |
| 101 | + |
| 102 | +def test_provider_forbidden(ofrep_provider, requests_mock): |
| 103 | + requests_mock.post( |
| 104 | + "http://localhost:8080/ofrep/v1/evaluate/flags/flag_key", |
| 105 | + status_code=403, |
| 106 | + text="forbidden", |
| 107 | + ) |
| 108 | + |
| 109 | + with pytest.raises(OpenFeatureError) as exc_info: |
| 110 | + ofrep_provider.resolve_boolean_details("flag_key", False) |
| 111 | + |
| 112 | + assert exc_info.value.error_code == ErrorCode.GENERAL |
| 113 | + assert exc_info.value.error_message == "forbidden" |
| 114 | + |
| 115 | + |
| 116 | +def test_provider_flag_not_found_invalid_json(ofrep_provider, requests_mock): |
| 117 | + """Test 404 with non-JSON response falls back to response text for error details""" |
| 118 | + requests_mock.post( |
| 119 | + "http://localhost:8080/ofrep/v1/evaluate/flags/flag_key", |
| 120 | + status_code=404, |
| 121 | + text="Flag not found - plain text response", |
| 122 | + ) |
| 123 | + |
| 124 | + with pytest.raises(FlagNotFoundError, match="Flag not found - plain text response"): |
| 125 | + ofrep_provider.resolve_boolean_details("flag_key", False) |
| 126 | + |
| 127 | + |
| 128 | +def test_provider_server_error(ofrep_provider, requests_mock): |
| 129 | + """Test generic OpenFeatureError for status codes > 400 (e.g. 500, 502)""" |
| 130 | + requests_mock.post( |
| 131 | + "http://localhost:8080/ofrep/v1/evaluate/flags/flag_key", |
| 132 | + status_code=500, |
| 133 | + text="Internal Server Error", |
| 134 | + ) |
| 135 | + |
| 136 | + with pytest.raises(OpenFeatureError) as exc_info: |
| 137 | + ofrep_provider.resolve_boolean_details("flag_key", False) |
| 138 | + |
| 139 | + assert exc_info.value.error_code == ErrorCode.GENERAL |
| 140 | + assert exc_info.value.error_message == "Internal Server Error" |
| 141 | + |
| 142 | + |
86 | 143 | def test_provider_invalid_context(ofrep_provider, requests_mock): |
87 | 144 | requests_mock.post( |
88 | 145 | "http://localhost:8080/ofrep/v1/evaluate/flags/flag_key", |
|
0 commit comments