this repo has no description
1mod common; 2use common::*; 3use reqwest::StatusCode; 4use serde_json::{json, Value}; 5use wiremock::{MockServer, Mock, ResponseTemplate}; 6use wiremock::matchers::{method, path}; 7 8// #[tokio::test] 9// async fn test_resolve_handle() { 10// let client = client(); 11// let params = [ 12// ("handle", "bsky.app"), 13// ]; 14// let res = client.get(format!("{}/xrpc/com.atproto.identity.resolveHandle", base_url().await)) 15// .query(&params) 16// .send() 17// .await 18// .expect("Failed to send request"); 19// 20// assert_eq!(res.status(), StatusCode::OK); 21// } 22 23#[tokio::test] 24async fn test_well_known_did() { 25 let client = client(); 26 let res = client.get(format!("{}/.well-known/did.json", base_url().await)) 27 .send() 28 .await 29 .expect("Failed to send request"); 30 31 assert_eq!(res.status(), StatusCode::OK); 32 let body: Value = res.json().await.expect("Response was not valid JSON"); 33 assert!(body["id"].as_str().unwrap().starts_with("did:web:")); 34 assert_eq!(body["service"][0]["type"], "AtprotoPersonalDataServer"); 35} 36 37#[tokio::test] 38async fn test_create_did_web_account_and_resolve() { 39 let client = client(); 40 41 let mock_server = MockServer::start().await; 42 let mock_uri = mock_server.uri(); 43 let mock_addr = mock_uri.trim_start_matches("http://"); 44 45 let did = format!("did:web:{}", mock_addr.replace(":", "%3A")); 46 47 let handle = format!("webuser_{}", uuid::Uuid::new_v4()); 48 49 let pds_endpoint = "https://localhost"; 50 51 let did_doc = json!({ 52 "@context": ["https://www.w3.org/ns/did/v1"], 53 "id": did, 54 "service": [{ 55 "id": "#atproto_pds", 56 "type": "AtprotoPersonalDataServer", 57 "serviceEndpoint": pds_endpoint 58 }] 59 }); 60 61 Mock::given(method("GET")) 62 .and(path("/.well-known/did.json")) 63 .respond_with(ResponseTemplate::new(200).set_body_json(did_doc)) 64 .mount(&mock_server) 65 .await; 66 67 let payload = json!({ 68 "handle": handle, 69 "email": format!("{}@example.com", handle), 70 "password": "password", 71 "did": did 72 }); 73 74 let res = client.post(format!("{}/xrpc/com.atproto.server.createAccount", base_url().await)) 75 .json(&payload) 76 .send() 77 .await 78 .expect("Failed to send request"); 79 80 if res.status() != StatusCode::OK { 81 let status = res.status(); 82 let body: Value = res.json().await.unwrap_or(json!({"error": "could not parse body"})); 83 panic!("createAccount failed with status {}: {:?}", status, body); 84 } 85 let body: Value = res.json().await.expect("createAccount response was not JSON"); 86 assert_eq!(body["did"], did); 87 88 let res = client.get(format!("{}/u/{}/did.json", base_url().await, handle)) 89 .send() 90 .await 91 .expect("Failed to fetch DID doc"); 92 93 assert_eq!(res.status(), StatusCode::OK); 94 let doc: Value = res.json().await.expect("DID doc was not JSON"); 95 96 assert_eq!(doc["id"], did); 97 assert_eq!(doc["alsoKnownAs"][0], format!("at://{}", handle)); 98 assert_eq!(doc["verificationMethod"][0]["controller"], did); 99 assert!(doc["verificationMethod"][0]["publicKeyJwk"].is_object()); 100} 101 102#[tokio::test] 103async fn test_create_account_duplicate_handle() { 104 let client = client(); 105 let handle = format!("dupe_{}", uuid::Uuid::new_v4()); 106 let email = format!("{}@example.com", handle); 107 108 let payload = json!({ 109 "handle": handle, 110 "email": email, 111 "password": "password" 112 }); 113 114 let res = client.post(format!("{}/xrpc/com.atproto.server.createAccount", base_url().await)) 115 .json(&payload) 116 .send() 117 .await 118 .expect("Failed to send request"); 119 assert_eq!(res.status(), StatusCode::OK); 120 121 let res = client.post(format!("{}/xrpc/com.atproto.server.createAccount", base_url().await)) 122 .json(&payload) 123 .send() 124 .await 125 .expect("Failed to send request"); 126 127 assert_eq!(res.status(), StatusCode::BAD_REQUEST); 128 let body: Value = res.json().await.expect("Response was not JSON"); 129 assert_eq!(body["error"], "HandleTaken"); 130} 131 132#[tokio::test] 133async fn test_did_web_lifecycle() { 134 let client = client(); 135 let handle = format!("lifecycle_{}", uuid::Uuid::new_v4()); 136 let did = format!("did:web:localhost:u:{}", handle); 137 let email = format!("{}@test.com", handle); 138 139 let create_payload = json!({ 140 "handle": handle, 141 "email": email, 142 "password": "password", 143 "did": did 144 }); 145 146 let res = client.post(format!("{}/xrpc/com.atproto.server.createAccount", base_url().await)) 147 .json(&create_payload) 148 .send() 149 .await 150 .expect("Failed createAccount"); 151 152 if res.status() != StatusCode::OK { 153 let body: Value = res.json().await.unwrap(); 154 println!("createAccount failed: {:?}", body); 155 panic!("createAccount returned non-200"); 156 } 157 assert_eq!(res.status(), StatusCode::OK); 158 let create_body: Value = res.json().await.expect("Not JSON"); 159 assert_eq!(create_body["did"], did); 160 161 let login_payload = json!({ 162 "identifier": handle, 163 "password": "password" 164 }); 165 let res = client.post(format!("{}/xrpc/com.atproto.server.createSession", base_url().await)) 166 .json(&login_payload) 167 .send() 168 .await 169 .expect("Failed createSession"); 170 171 assert_eq!(res.status(), StatusCode::OK); 172 let session_body: Value = res.json().await.expect("Not JSON"); 173 let _jwt = session_body["accessJwt"].as_str().unwrap(); 174 175 /* 176 let profile_payload = json!({ 177 "repo": did, 178 "collection": "app.bsky.actor.profile", 179 "rkey": "self", 180 "record": { 181 "$type": "app.bsky.actor.profile", 182 "displayName": "DID Web User", 183 "description": "Testing lifecycle" 184 } 185 }); 186 187 let res = client.post(format!("{}/xrpc/com.atproto.repo.putRecord", base_url().await)) 188 .bearer_auth(_jwt) 189 .json(&profile_payload) 190 .send() 191 .await 192 .expect("Failed putRecord"); 193 194 if res.status() != StatusCode::OK { 195 let body: Value = res.json().await.unwrap(); 196 println!("putRecord failed: {:?}", body); 197 panic!("putRecord returned non-200"); 198 } 199 assert_eq!(res.status(), StatusCode::OK); 200 201 let res = client.get(format!("{}/xrpc/com.atproto.repo.getRecord", base_url().await)) 202 .query(&[ 203 ("repo", &handle), 204 ("collection", &"app.bsky.actor.profile".to_string()), 205 ("rkey", &"self".to_string()) 206 ]) 207 .send() 208 .await 209 .expect("Failed getRecord"); 210 211 if res.status() != StatusCode::OK { 212 let body: Value = res.json().await.unwrap(); 213 println!("getRecord failed: {:?}", body); 214 panic!("getRecord returned non-200"); 215 } 216 let record_body: Value = res.json().await.expect("Not JSON"); 217 assert_eq!(record_body["value"]["displayName"], "DID Web User"); 218 */ 219}