use super::*;
use crate::client::test_support::serve_once;
const ANONYMOUS_ACCOUNT: &str = r#"
<div>
<label>
<input type="radio"
name="display_name"
onchange="anon(true)"
value="anonymous">
<span>(anonymous user #123456)</span>
</label>
</div>
"#;
fn account_id(body: &str) -> Option<u64> {
parse_account_id(&Html::parse_document(body))
}
#[test]
fn creates_sensitive_session_cookie() {
let cookie = session_cookie("abc123").unwrap();
assert_eq!(cookie.to_str().unwrap(), "session=abc123");
assert!(cookie.is_sensitive());
}
#[test]
fn rejects_invalid_session_tokens() {
for token in ["", "invalid token", "abc\n123", "abc;other=123", "abc,123"] {
assert!(matches!(
session_cookie(token),
Err(ClientError::InvalidSession)
));
}
}
#[test]
fn accepts_successful_response() {
assert!(validate_status(StatusCode::OK).is_ok());
}
#[test]
fn rejects_unauthorized_response() {
assert!(matches!(
validate_status(StatusCode::BAD_REQUEST),
Err(ClientError::Unauthorized)
));
}
#[test]
fn rejects_rate_limited_response() {
assert!(matches!(
validate_status(StatusCode::TOO_MANY_REQUESTS),
Err(ClientError::RateLimited)
));
}
#[test]
fn rejects_redirects() {
assert!(matches!(
validate_status(StatusCode::FOUND),
Err(ClientError::UnexpectedStatus(_))
));
}
#[test]
fn accepts_plain_text_response() {
for value in ["text/plain", "text/plain; charset=utf-8"] {
assert!(validate_content_type(Some(value)).is_ok());
}
}
#[test]
fn rejects_html_response() {
assert!(matches!(
validate_content_type(Some("text/html")),
Err(ClientError::InvalidResponse)
));
}
#[test]
fn rejects_missing_content_type() {
assert!(matches!(
validate_content_type(None),
Err(ClientError::InvalidResponse)
));
}
#[test]
fn detects_logout_link() {
for path in [
"/auth/logout",
"/2025/auth/logout",
"/2026/auth/logout",
"/2027/auth/logout",
] {
assert!(has_logout_link(&Html::parse_document(&format!(
r#"<a href="{path}">Log Out</a>"#
))));
}
}
#[test]
fn rejects_missing_logout_link() {
for body in [
"",
r#"<a href="/auth/login">Log In</a>"#,
r#"<a href="/invalid/auth/logout">Log Out</a>"#,
r#"<a href="https://example.com/auth/logout">Log Out</a>"#,
] {
assert!(!has_logout_link(&Html::parse_document(body)));
}
}
#[test]
fn ignores_logout_link_inside_comment() {
let body = r#"
<!-- <a href="/auth/logout">Log Out</a> -->
"#;
assert!(!has_logout_link(&Html::parse_document(body)));
}
#[test]
fn extracts_account_identity() {
assert_eq!(account_id(ANONYMOUS_ACCOUNT), Some(123_456),);
}
#[test]
fn rejects_missing_account_identity() {
assert_eq!(account_id("<html></html>"), None);
}
#[test]
fn rejects_invalid_account_identity() {
for value in ["", "abc", "0", "../123", "-123", "18446744073709551616"] {
let body = ANONYMOUS_ACCOUNT.replace("123456", value);
assert_eq!(account_id(&body), None);
}
}
#[test]
fn rejects_ambiguous_account_identity() {
let body = format!(
"{ANONYMOUS_ACCOUNT}{}",
ANONYMOUS_ACCOUNT.replace("123456", "456"),
);
assert_eq!(account_id(&body), None);
}
#[test]
fn rejects_duplicate_anonymous_options() {
let body = format!("{ANONYMOUS_ACCOUNT}{ANONYMOUS_ACCOUNT}");
assert_eq!(account_id(&body), None);
}
#[test]
fn ignores_unrelated_account_identity_text() {
let body = format!(
r"
<header>
<span>(anonymous user #999)</span>
</header>
{ANONYMOUS_ACCOUNT}
<div>(anonymous user #888)</div>
"
);
assert_eq!(account_id(&body), Some(123_456));
}
#[test]
fn rejects_unrelated_account_identity_text() {
let body = r"
<span>(anonymous user #999)</span>
";
assert_eq!(account_id(body), None);
}
#[test]
fn rejects_identity_from_different_display_option() {
let body = ANONYMOUS_ACCOUNT.replace(r#"value="anonymous""#, r#"value="custom""#);
assert_eq!(account_id(&body), None);
}
#[test]
fn accepts_reordered_attributes() {
let body = r#"
<label>
<input value="anonymous"
checked
name="display_name"
type="radio">
<span>
(anonymous user #123456)
</span>
</label>
"#;
assert_eq!(account_id(body), Some(123_456));
}
#[test]
fn downloads_input_with_expected_headers() {
let (url, server) = serve_once(
"200 OK",
"Content-Type: text/plain\r\n",
b"synthetic-input\n",
);
let client = AocHttpClient::for_test(&url).unwrap();
let input = client.fetch_input(2025, 1, "test-session").unwrap();
assert_eq!(input, "synthetic-input\n");
let request = server.join().unwrap();
assert_eq!(
request.lines().next(),
Some("GET /2025/day/1/input HTTP/1.1"),
);
assert!(
request
.lines()
.any(|line| { line.eq_ignore_ascii_case("cookie: session=test-session",) })
);
assert!(
request
.lines()
.any(|line| { line.eq_ignore_ascii_case(&format!("user-agent: {USER_AGENT}"),) })
);
}
#[test]
fn rejects_http_error_responses() {
for (status, expected) in [
("403 Forbidden", ClientError::Unauthorized),
("404 Not Found", ClientError::InputUnavailable),
("429 Too Many Requests", ClientError::RateLimited),
] {
let (url, server) = serve_once(status, "Content-Type: text/plain\r\n", b"error");
let client = AocHttpClient::for_test(&url).unwrap();
let error = client.fetch_input(2025, 1, "test-session").unwrap_err();
assert_eq!(
std::mem::discriminant(&error),
std::mem::discriminant(&expected),
);
server.join().unwrap();
}
}
#[test]
fn rejects_invalid_input_responses() {
let cases: [(&str, &[u8]); 4] = [
("text/html", b"<html>Login required</html>"),
("text/plain", b""),
("text/plain", b" \n"),
("text/plain", &[0xff]),
];
for (content_type, body) in cases {
let headers = match content_type {
"text/html" => "Content-Type: text/html\r\n",
_ => "Content-Type: text/plain\r\n",
};
let (url, server) = serve_once("200 OK", headers, body);
let client = AocHttpClient::for_test(&url).unwrap();
assert!(matches!(
client.fetch_input(2025, 1, "test-session"),
Err(ClientError::InvalidResponse)
));
server.join().unwrap();
}
}
#[test]
fn does_not_follow_input_redirects() {
let (url, server) = serve_once("302 Found", "Location: /redirected\r\n", b"");
let client = AocHttpClient::for_test(&url).unwrap();
assert!(matches!(
client.fetch_input(2025, 1, "test-session"),
Err(ClientError::UnexpectedStatus(StatusCode::FOUND))
));
server.join().unwrap();
}
#[test]
fn verifies_authenticated_session() {
let (url, server) = serve_once(
"200 OK",
"Content-Type: text/html\r\n",
br#"
<html>
<a href="/2026/auth/logout">Log Out</a>
<div>
<label>
<input type="radio"
name="display_name"
onchange="anon(true)"
value="anonymous">
<span>(anonymous user #123456)</span>
</label>
</div>
</html>
"#,
);
let client = AocHttpClient::for_test(&url).unwrap();
assert_eq!(
client.verify_session_response("test-session").unwrap(),
123_456,
);
let request = server.join().unwrap();
assert_eq!(request.lines().next(), Some("GET /settings HTTP/1.1"),);
}
#[test]
fn rejects_expired_session_redirect() {
let (url, server) = serve_once("302 Found", "Location: /auth/login\r\n", b"");
let client = AocHttpClient::for_test(&url).unwrap();
assert!(matches!(
client.verify_session_response("test-session"),
Err(ClientError::Unauthorized)
));
server.join().unwrap();
}
#[test]
fn reports_session_verification_rate_limit() {
let (url, server) = serve_once(
"429 Too Many Requests",
"Content-Type: text/plain\r\n",
b"rate-limited",
);
let client = AocHttpClient::for_test(&url).unwrap();
assert!(matches!(
client.verify_session_response("test-session"),
Err(ClientError::RateLimited)
));
server.join().unwrap();
}
#[test]
fn rejects_unauthenticated_settings_page() {
let (url, server) = serve_once(
"200 OK",
"Content-Type: text/html\r\n",
br#"
<html>
<a href="/auth/login">Log In</a>
</html>
"#,
);
let client = AocHttpClient::for_test(&url).unwrap();
assert!(matches!(
client.verify_session_response("test-session"),
Err(ClientError::InvalidResponse)
));
server.join().unwrap();
}
#[test]
fn rejects_settings_without_account_identity() {
let (url, server) = serve_once(
"200 OK",
"Content-Type: text/html\r\n",
br#"
<html>
<a href="/auth/logout">Log Out</a>
</html>
"#,
);
let client = AocHttpClient::for_test(&url).unwrap();
assert!(matches!(
client.verify_session_response("test-session"),
Err(ClientError::AccountIdentityUnavailable)
));
server.join().unwrap();
}
#[test]
fn rejects_settings_with_invalid_content_type() {
for headers in [
"",
"Content-Type: text/plain\r\n",
"Content-Type: application/json\r\n",
] {
let (url, server) = serve_once(
"200 OK",
headers,
br#"
<a href="/auth/logout">Log Out</a>
<label>(anonymous user #123)</label>
"#,
);
let client = AocHttpClient::for_test(&url).unwrap();
assert!(matches!(
client.verify_session_response("test-session"),
Err(ClientError::InvalidResponse)
));
server.join().unwrap();
}
}
#[test]
fn rejects_settings_with_invalid_utf8() {
let (url, server) = serve_once("200 OK", "Content-Type: text/html\r\n", &[0xff]);
let client = AocHttpClient::for_test(&url).unwrap();
assert!(matches!(
client.verify_session_response("test-session"),
Err(ClientError::InvalidResponse)
));
server.join().unwrap();
}
#[test]
fn session_verification_uses_shared_request_lock() {
let directory = tempfile::tempdir().unwrap();
let (url, server) = serve_once(
"200 OK",
"Content-Type: text/html\r\n",
br#"
<a href="/auth/logout">Log Out</a>
<label>
<input type="radio"
name="display_name"
value="anonymous">
<span>(anonymous user #123456)</span>
</label>
"#,
);
let client = AocHttpClient::for_test(&url).unwrap();
assert_eq!(
client
.verify_session_at(directory.path(), "test-session",)
.unwrap(),
123_456,
);
assert!(directory.path().join("download.lock").is_file());
server.join().unwrap();
}