|
| 1 | +use super::DNA_HASH; |
1 | 2 | use crate::config::{AllowedFns, Configuration};
|
2 | 3 | use crate::test::data::new_test_app_info;
|
3 | 4 | use crate::test::router::TestRouter;
|
4 | 5 | use crate::{MockAdminCall, MockAppCall};
|
5 |
| -use holochain_client::ExternIO; |
| 6 | +use holochain_client::{ConductorApiError, ExternIO}; |
| 7 | +use holochain_conductor_api::ExternalApiWireError; |
6 | 8 | use holochain_types::prelude::DnaHash;
|
7 | 9 | use reqwest::StatusCode;
|
8 | 10 | use std::collections::HashMap;
|
9 | 11 | use std::net::{Ipv4Addr, SocketAddr};
|
10 | 12 | use std::sync::Arc;
|
11 | 13 |
|
12 |
| -#[tokio::test(flavor = "multi_thread")] |
13 |
| -async fn happy_zome_call() { |
14 |
| - let app_id = "tapp"; |
15 |
| - let dna_hash = DnaHash::from_raw_32(vec![1; 32]); |
| 14 | +const APP_ID: &str = "tapp"; |
16 | 15 |
|
| 16 | +fn create_test_router(app_call: MockAppCall) -> TestRouter { |
17 | 17 | let mut allowed_fns = HashMap::new();
|
18 |
| - allowed_fns.insert(app_id.into(), AllowedFns::All); |
| 18 | + allowed_fns.insert(APP_ID.into(), AllowedFns::All); |
19 | 19 | let config = Configuration::try_new(
|
20 | 20 | SocketAddr::new(Ipv4Addr::LOCALHOST.into(), 8888),
|
21 | 21 | "1024",
|
22 |
| - app_id, |
| 22 | + APP_ID, |
23 | 23 | allowed_fns,
|
24 | 24 | "",
|
25 | 25 | "",
|
26 | 26 | )
|
27 | 27 | .unwrap();
|
28 | 28 |
|
29 | 29 | let mut admin_call = MockAdminCall::new();
|
30 |
| - let dna_hash2 = dna_hash.clone(); |
31 | 30 | admin_call.expect_list_apps().returning(move |_| {
|
32 |
| - let dna_hash = dna_hash2.clone(); |
33 | 31 | Box::pin(async move {
|
34 |
| - let app_info = new_test_app_info(app_id, dna_hash); |
| 32 | + let app_info = new_test_app_info(APP_ID, DnaHash::from_raw_32(vec![1; 32])); |
35 | 33 | Ok(vec![app_info])
|
36 | 34 | })
|
37 | 35 | });
|
38 | 36 | let admin_call = Arc::new(admin_call);
|
| 37 | + let app_call = Arc::new(app_call); |
| 38 | + TestRouter::new_with_config_and_interfaces(config, admin_call, app_call) |
| 39 | +} |
| 40 | + |
| 41 | +#[tokio::test] |
| 42 | +async fn happy_zome_call() { |
39 | 43 | let mut app_call = MockAppCall::new();
|
40 | 44 | app_call
|
41 | 45 | .expect_handle_zome_call()
|
42 | 46 | .returning(|_, _, _, _, _| {
|
43 | 47 | Box::pin(async move { Ok(ExternIO::encode("return_value").unwrap()) })
|
44 | 48 | });
|
45 |
| - let app_call = Arc::new(app_call); |
46 |
| - let router = TestRouter::new_with_config_and_interfaces(config, admin_call, app_call); |
| 49 | + let router = create_test_router(app_call); |
47 | 50 | let (status_code, body) = router
|
48 |
| - .request(&format!("/{dna_hash}/{app_id}/coordinator/fn_name")) |
| 51 | + .request(&format!("/{DNA_HASH}/{APP_ID}/coordinator/fn_name")) |
49 | 52 | .await;
|
50 | 53 | assert_eq!(status_code, StatusCode::OK);
|
51 | 54 | assert_eq!(body, r#""return_value""#);
|
52 | 55 | }
|
| 56 | + |
| 57 | +#[tokio::test] |
| 58 | +async fn app_not_found() { |
| 59 | + let mut app_call = MockAppCall::new(); |
| 60 | + app_call |
| 61 | + .expect_handle_zome_call() |
| 62 | + .returning(|_, _, _, _, _| { |
| 63 | + Box::pin(async move { |
| 64 | + Err(crate::HcHttpGatewayError::HolochainError( |
| 65 | + ConductorApiError::AppNotFound, |
| 66 | + )) |
| 67 | + }) |
| 68 | + }); |
| 69 | + let router = create_test_router(app_call); |
| 70 | + let (status_code, body) = router |
| 71 | + .request(&format!("/{DNA_HASH}/{APP_ID}/coordinator/fn_name")) |
| 72 | + .await; |
| 73 | + // The app must have been found earlier when looking it up for the call, |
| 74 | + // so this must have been an internal error of some kind. |
| 75 | + assert_eq!(status_code, StatusCode::INTERNAL_SERVER_ERROR); |
| 76 | + assert_eq!(body, r#"{"error":"Something went wrong"}"#); |
| 77 | +} |
| 78 | + |
| 79 | +#[tokio::test] |
| 80 | +async fn cell_not_found() { |
| 81 | + let mut app_call = MockAppCall::new(); |
| 82 | + app_call |
| 83 | + .expect_handle_zome_call() |
| 84 | + .returning(|_, _, _, _, _| { |
| 85 | + Box::pin(async move { |
| 86 | + Err(crate::HcHttpGatewayError::HolochainError( |
| 87 | + ConductorApiError::CellNotFound, |
| 88 | + )) |
| 89 | + }) |
| 90 | + }); |
| 91 | + let router = create_test_router(app_call); |
| 92 | + let (status_code, body) = router |
| 93 | + .request(&format!("/{DNA_HASH}/{APP_ID}/coordinator/fn_name")) |
| 94 | + .await; |
| 95 | + assert_eq!(status_code, StatusCode::INTERNAL_SERVER_ERROR); |
| 96 | + assert_eq!(body, r#"{"error":"Something went wrong"}"#); |
| 97 | +} |
| 98 | + |
| 99 | +#[tokio::test] |
| 100 | +async fn external_api_wire_error() { |
| 101 | + let mut app_call = MockAppCall::new(); |
| 102 | + app_call |
| 103 | + .expect_handle_zome_call() |
| 104 | + .returning(|_, _, _, _, _| { |
| 105 | + Box::pin(async move { |
| 106 | + Err(crate::HcHttpGatewayError::HolochainError( |
| 107 | + ConductorApiError::ExternalApiWireError( |
| 108 | + ExternalApiWireError::ZomeCallUnauthorized("unauthorized".to_string()), |
| 109 | + ), |
| 110 | + )) |
| 111 | + }) |
| 112 | + }); |
| 113 | + let router = create_test_router(app_call); |
| 114 | + let (status_code, body) = router |
| 115 | + .request(&format!("/{DNA_HASH}/{APP_ID}/coordinator/fn_name")) |
| 116 | + .await; |
| 117 | + assert_eq!(status_code, StatusCode::INTERNAL_SERVER_ERROR); |
| 118 | + assert_eq!(body, r#"{"error":"Something went wrong"}"#); |
| 119 | +} |
| 120 | + |
| 121 | +#[tokio::test] |
| 122 | +async fn fresh_nonce_error() { |
| 123 | + let mut app_call = MockAppCall::new(); |
| 124 | + app_call |
| 125 | + .expect_handle_zome_call() |
| 126 | + .returning(|_, _, _, _, _| { |
| 127 | + Box::pin(async move { |
| 128 | + Err(crate::HcHttpGatewayError::HolochainError( |
| 129 | + ConductorApiError::FreshNonceError("nonce_kaputt".into()), |
| 130 | + )) |
| 131 | + }) |
| 132 | + }); |
| 133 | + let router = create_test_router(app_call); |
| 134 | + let (status_code, body) = router |
| 135 | + .request(&format!("/{DNA_HASH}/{APP_ID}/coordinator/fn_name")) |
| 136 | + .await; |
| 137 | + assert_eq!(status_code, StatusCode::INTERNAL_SERVER_ERROR); |
| 138 | + assert_eq!(body, r#"{"error":"Something went wrong"}"#); |
| 139 | +} |
| 140 | + |
| 141 | +#[tokio::test] |
| 142 | +async fn io_error() { |
| 143 | + let mut app_call = MockAppCall::new(); |
| 144 | + app_call |
| 145 | + .expect_handle_zome_call() |
| 146 | + .returning(|_, _, _, _, _| { |
| 147 | + Box::pin(async move { |
| 148 | + Err(crate::HcHttpGatewayError::HolochainError( |
| 149 | + ConductorApiError::IoError(std::io::Error::other("ssd not found")), |
| 150 | + )) |
| 151 | + }) |
| 152 | + }); |
| 153 | + let router = create_test_router(app_call); |
| 154 | + let (status_code, body) = router |
| 155 | + .request(&format!("/{DNA_HASH}/{APP_ID}/coordinator/fn_name")) |
| 156 | + .await; |
| 157 | + assert_eq!(status_code, StatusCode::INTERNAL_SERVER_ERROR); |
| 158 | + assert_eq!(body, r#"{"error":"Something went wrong"}"#); |
| 159 | +} |
| 160 | + |
| 161 | +#[tokio::test] |
| 162 | +async fn sign_zome_call_error() { |
| 163 | + let mut app_call = MockAppCall::new(); |
| 164 | + app_call |
| 165 | + .expect_handle_zome_call() |
| 166 | + .returning(|_, _, _, _, _| { |
| 167 | + Box::pin(async move { |
| 168 | + Err(crate::HcHttpGatewayError::HolochainError( |
| 169 | + ConductorApiError::SignZomeCallError("unsigned".to_string()), |
| 170 | + )) |
| 171 | + }) |
| 172 | + }); |
| 173 | + let router = create_test_router(app_call); |
| 174 | + let (status_code, body) = router |
| 175 | + .request(&format!("/{DNA_HASH}/{APP_ID}/coordinator/fn_name")) |
| 176 | + .await; |
| 177 | + assert_eq!(status_code, StatusCode::INTERNAL_SERVER_ERROR); |
| 178 | + assert_eq!(body, r#"{"error":"Something went wrong"}"#); |
| 179 | +} |
| 180 | + |
| 181 | +#[tokio::test] |
| 182 | +async fn websocket_error() { |
| 183 | + let mut app_call = MockAppCall::new(); |
| 184 | + app_call |
| 185 | + .expect_handle_zome_call() |
| 186 | + .returning(|_, _, _, _, _| { |
| 187 | + Box::pin(async move { |
| 188 | + Err(crate::HcHttpGatewayError::HolochainError( |
| 189 | + ConductorApiError::WebsocketError( |
| 190 | + // WebsocketError is not exposed. |
| 191 | + std::io::Error::other("websocket closed").into(), |
| 192 | + ), |
| 193 | + )) |
| 194 | + }) |
| 195 | + }); |
| 196 | + let router = create_test_router(app_call); |
| 197 | + let (status_code, body) = router |
| 198 | + .request(&format!("/{DNA_HASH}/{APP_ID}/coordinator/fn_name")) |
| 199 | + .await; |
| 200 | + assert_eq!(status_code, StatusCode::INTERNAL_SERVER_ERROR); |
| 201 | + assert_eq!(body, r#"{"error":"Something went wrong"}"#); |
| 202 | +} |
0 commit comments