1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
use sts::http::Server;
fn main() {
let bind_addr: String = match std::env::var("BIND_ADDR") {
Ok(val) => val,
Err(_) => String::from("127.0.0.1:7878"),
};
let server = Server::new(bind_addr);
server.start();
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashMap;
#[test]
fn test_oauth_server_creation() {
let server = sts::http::Server::new("127.0.0.1:0".to_string());
// If we get here without panicking, the server was created successfully
assert!(true);
}
#[test]
fn test_authorization_code_generation() {
let config = sts::Config::from_env();
let oauth_server = sts::OAuthServer::new(&config);
let mut params = HashMap::new();
params.insert("client_id".to_string(), "test_client".to_string());
params.insert(
"redirect_uri".to_string(),
"http://localhost:3000/callback".to_string(),
);
params.insert("response_type".to_string(), "code".to_string());
params.insert("state".to_string(), "test_state".to_string());
let result = oauth_server.handle_authorize(¶ms);
assert!(result.is_ok());
let redirect_url = result.unwrap();
assert!(redirect_url.contains("code="));
assert!(redirect_url.contains("state=test_state"));
}
#[test]
fn test_missing_client_id() {
let config = sts::Config::from_env();
let oauth_server = sts::OAuthServer::new(&config);
let mut params = HashMap::new();
params.insert(
"redirect_uri".to_string(),
"http://localhost:3000/callback".to_string(),
);
params.insert("response_type".to_string(), "code".to_string());
let result = oauth_server.handle_authorize(¶ms);
assert!(result.is_err());
assert!(result.unwrap_err().contains("invalid_request"));
}
#[test]
fn test_unsupported_response_type() {
let config = sts::Config::from_env();
let oauth_server = sts::OAuthServer::new(&config);
let mut params = HashMap::new();
params.insert("client_id".to_string(), "test_client".to_string());
params.insert(
"redirect_uri".to_string(),
"http://localhost:3000/callback".to_string(),
);
params.insert("response_type".to_string(), "token".to_string());
let result = oauth_server.handle_authorize(¶ms);
assert!(result.is_err());
assert!(result.unwrap_err().contains("unsupported_response_type"));
}
}
|