|
14 | 14 | verify_api_key, |
15 | 15 | verify_jwt_credentials, |
16 | 16 | verify_telegram_auth_key, |
| 17 | + verify_whatsapp_signature, |
| 18 | + verify_whatsapp_webhook_challenge, |
17 | 19 | ) |
18 | 20 | from util.config import config |
19 | 21 |
|
@@ -138,3 +140,74 @@ def test_get_chat_type_from_jwt_missing_platform(self): |
138 | 140 | claims = {"sub": "user-123"} |
139 | 141 | chat_type = get_chat_type_from_jwt(claims) |
140 | 142 | self.assertIsNone(chat_type) |
| 143 | + |
| 144 | + @patch("api.auth.config") |
| 145 | + def test_whatsapp_webhook_challenge_success(self, mock_config: MagicMock): |
| 146 | + mock_config.whatsapp_must_auth = True |
| 147 | + mock_config.whatsapp_auth_key = SecretStr("test-token") |
| 148 | + challenge = verify_whatsapp_webhook_challenge("subscribe", "test-challenge", "test-token") |
| 149 | + self.assertEqual(challenge, "test-challenge") |
| 150 | + |
| 151 | + @patch("api.auth.config") |
| 152 | + def test_whatsapp_webhook_challenge_invalid_token(self, mock_config: MagicMock): |
| 153 | + mock_config.whatsapp_must_auth = True |
| 154 | + mock_config.whatsapp_auth_key = SecretStr("correct-token") |
| 155 | + with self.assertRaises(HTTPException) as context: |
| 156 | + verify_whatsapp_webhook_challenge("subscribe", "test-challenge", "wrong-token") |
| 157 | + self.assertEqual(context.exception.status_code, HTTP_403_FORBIDDEN) |
| 158 | + self.assertEqual(context.exception.detail, "Webhook verification failed") |
| 159 | + |
| 160 | + @patch("api.auth.config") |
| 161 | + def test_whatsapp_webhook_challenge_invalid_mode(self, mock_config: MagicMock): |
| 162 | + mock_config.whatsapp_must_auth = True |
| 163 | + mock_config.whatsapp_auth_key = SecretStr("test-token") |
| 164 | + with self.assertRaises(HTTPException) as context: |
| 165 | + verify_whatsapp_webhook_challenge("unsubscribe", "test-challenge", "test-token") |
| 166 | + self.assertEqual(context.exception.status_code, HTTP_403_FORBIDDEN) |
| 167 | + self.assertEqual(context.exception.detail, "Webhook verification failed") |
| 168 | + |
| 169 | + def test_whatsapp_webhook_challenge_auth_disabled(self): |
| 170 | + challenge = verify_whatsapp_webhook_challenge("subscribe", "test-challenge", "any-token") |
| 171 | + self.assertEqual(challenge, "test-challenge") |
| 172 | + |
| 173 | + @patch("api.auth.config") |
| 174 | + def test_whatsapp_signature_verification_success(self, mock_config: MagicMock): |
| 175 | + import hashlib |
| 176 | + import hmac |
| 177 | + mock_config.whatsapp_must_auth = True |
| 178 | + mock_config.whatsapp_app_secret = SecretStr("test-secret") |
| 179 | + payload = b'{"test": "data"}' |
| 180 | + signature = hmac.new(b"test-secret", payload, hashlib.sha256).hexdigest() |
| 181 | + verify_whatsapp_signature(payload, f"sha256={signature}") |
| 182 | + |
| 183 | + @patch("api.auth.config") |
| 184 | + def test_whatsapp_signature_verification_invalid_signature(self, mock_config: MagicMock): |
| 185 | + mock_config.whatsapp_must_auth = True |
| 186 | + mock_config.whatsapp_app_secret = SecretStr("test-secret") |
| 187 | + payload = b'{"test": "data"}' |
| 188 | + with self.assertRaises(HTTPException) as context: |
| 189 | + verify_whatsapp_signature(payload, "sha256=wrong-signature") |
| 190 | + self.assertEqual(context.exception.status_code, HTTP_403_FORBIDDEN) |
| 191 | + self.assertEqual(context.exception.detail, "Invalid signature") |
| 192 | + |
| 193 | + @patch("api.auth.config") |
| 194 | + def test_whatsapp_signature_verification_missing_header(self, mock_config: MagicMock): |
| 195 | + mock_config.whatsapp_must_auth = True |
| 196 | + payload = b'{"test": "data"}' |
| 197 | + with self.assertRaises(HTTPException) as context: |
| 198 | + verify_whatsapp_signature(payload, None) |
| 199 | + self.assertEqual(context.exception.status_code, HTTP_403_FORBIDDEN) |
| 200 | + self.assertEqual(context.exception.detail, "Missing signature header") |
| 201 | + |
| 202 | + @patch("api.auth.config") |
| 203 | + def test_whatsapp_signature_verification_invalid_format(self, mock_config: MagicMock): |
| 204 | + mock_config.whatsapp_must_auth = True |
| 205 | + payload = b'{"test": "data"}' |
| 206 | + with self.assertRaises(HTTPException) as context: |
| 207 | + verify_whatsapp_signature(payload, "invalid-format") |
| 208 | + self.assertEqual(context.exception.status_code, HTTP_403_FORBIDDEN) |
| 209 | + self.assertEqual(context.exception.detail, "Invalid signature format") |
| 210 | + |
| 211 | + def test_whatsapp_signature_verification_auth_disabled(self): |
| 212 | + payload = b'{"test": "data"}' |
| 213 | + verify_whatsapp_signature(payload, None) |
0 commit comments