recaptcha_verify/
lib.rs

1mod ent;
2mod v3;
3
4#[allow(deprecated)]
5pub use v3::verify;
6pub use v3::verify_v3;
7pub use v3::RecaptchaError;
8
9pub use ent::verify_enterprise;
10pub use ent::verify_enterprise_detailed;
11pub use ent::RecaptchaEntError;
12pub use ent::RecaptchaEntResult;
13
14#[cfg(test)]
15mod tests {
16    use super::*;
17    use std::net::{IpAddr, Ipv4Addr};
18
19    /// Check https://developers.google.com/recaptcha/docs/faq#id-like-to-run-automated-tests-with-recaptcha.-what-should-i-do
20    const GOOGLE_MOCK_SECRET: &str = "6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe";
21
22    const UNKNOWN_ERROR: &str = "trololo-detected";
23
24    #[tokio::test]
25    #[allow(deprecated)]
26    async fn it_works_v3() {
27        assert!(matches!(
28            "missing-input-secret".to_string().try_into(),
29            Ok(RecaptchaError::MissingInputSecret)
30        ));
31        assert!(matches!(
32            "invalid-input-secret".to_string().try_into(),
33            Ok(RecaptchaError::InvalidInputSecret)
34        ));
35        assert!(matches!(
36            "missing-input-response".to_string().try_into(),
37            Ok(RecaptchaError::MissingInputResponse)
38        ));
39        assert!(matches!(
40            "invalid-input-response".to_string().try_into(),
41            Ok(RecaptchaError::InvalidInputResponse)
42        ));
43        assert!(matches!(
44            "bad-request".to_string().try_into(),
45            Ok(RecaptchaError::BadRequest)
46        ));
47        assert!(matches!(
48            "timeout-or-duplicate".to_string().try_into(),
49            Ok(RecaptchaError::TimeoutOrDuplicate)
50        ));
51
52        assert!(
53            matches!(UNKNOWN_ERROR.to_string().try_into(), Ok(RecaptchaError::Unknown(Some(s))) if s == UNKNOWN_ERROR )
54        );
55
56        //
57
58        let res: Result<(), RecaptchaError> = verify("test", "test", None).await;
59
60        assert!(matches!(res, Err(RecaptchaError::InvalidInputResponse)));
61
62        //
63
64        let localhost_v4 = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
65        let res: Result<(), RecaptchaError> =
66            verify(GOOGLE_MOCK_SECRET, "test", Some(&localhost_v4)).await;
67
68        assert!(matches!(res, Ok(())));
69
70        //
71
72        let res: Result<(), RecaptchaError> = verify_v3("test", "test", None).await;
73
74        assert!(matches!(res, Err(RecaptchaError::InvalidInputResponse)));
75
76        //
77
78        let localhost_v4 = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
79        let res: Result<(), RecaptchaError> =
80            verify_v3(GOOGLE_MOCK_SECRET, "test", Some(&localhost_v4)).await;
81
82        assert!(matches!(res, Ok(())));
83    }
84
85    #[tokio::test]
86    #[allow(deprecated)]
87    async fn it_works_ent() {
88        let result =
89            verify_enterprise_detailed("project", "api_key", "site_key", "token", Some("login"))
90                .await;
91
92        assert!(
93            matches!(result, Err(RecaptchaEntError::ApiError(api_error)) if api_error.error.code >= 400)
94        );
95
96        let result =
97            verify_enterprise("project", "api_key", "site_key", "token", Some("test")).await;
98
99        assert!(
100            matches!(result, Err(RecaptchaEntError::ApiError(api_error)) if api_error.error.code >= 400)
101        );
102    }
103}