From b8739d52ea3306092dd8189a8206ab0ac062f44e Mon Sep 17 00:00:00 2001 From: Vincent Emonet Date: Mon, 27 Nov 2023 08:53:45 +0100 Subject: [PATCH] add test, improve docs --- lib/docs/assets/turtle.js | 2 +- lib/docs/introduction.md | 13 +++++++++---- lib/docs/use_python.md | 4 ---- lib/src/nanopub.rs | 9 ++++++++- lib/src/profile.rs | 12 ++++++------ lib/src/publish.rs | 5 +---- lib/tests/nanopub_test.rs | 14 ++++++++++++-- 7 files changed, 37 insertions(+), 22 deletions(-) diff --git a/lib/docs/assets/turtle.js b/lib/docs/assets/turtle.js index eb2e5f7..42272ed 100644 --- a/lib/docs/assets/turtle.js +++ b/lib/docs/assets/turtle.js @@ -9,7 +9,7 @@ var module = module ? module : {}; // shim for browser use function hljsDefineTurtle(hljs) { // export default function (hljs) { var KEYWORDS = { - keyword: "BASE|2 PREFIX|5 @base|10 @prefix|10", + keyword: "BASE|2 PREFIX|5 GRAPH|3 @base|10 @prefix|10", literal: "true false", built_in: "a", }; diff --git a/lib/docs/introduction.md b/lib/docs/introduction.md index 09d7989..a563fe1 100644 --- a/lib/docs/introduction.md +++ b/lib/docs/introduction.md @@ -6,7 +6,10 @@ This project aims to provide a comprehensive cross-platform toolkit to sign, publish, and check **[Nanopublications](https://nanopub.net)**. -Sign and publish providing a private RSA key string, or a `profile.yml` file. Check the validity of signed or unsigned Nanopublications. +It enables developers to: + +- Sign and publish nanopubs using a RSA private key, with support for configuration through a `profile.yml` file. +- Check the validity of signed or unsigned nanopubs. It is packaged to be used easily through various popular interfaces: @@ -28,9 +31,11 @@ The library automatically handles most RDF serializations supporting quads for t - Nquads - JSON-LD -When signing a nanopub, some metadata in the pubinfo graph are created automatically if they are not already set in the RDF provided: +When signing a nanopub, some metadata are created automatically in the pubinfo graph if they are not already set in the provided RDF: + +- Date and time of the nanopub creation is added using `dct:created` +- ORCID of the creator is added using `dct:creator` if an ORCID was provided in the profile used to sign the nanopub (we also check if the ORCID has been set with `prov:wasAttributedTo` or `pav:createdBy`) -- Date and time of the Nanopublication creation using `dct:created`. -- ORCID of the creator using `dct:creator`, if an ORCID was provided in the profile used to sign the Nanopublication (we also check if the ORCID has been set with `prov:wasAttributedTo`, or `pav:createdBy`) +Checkout the page most adapted to your use-case to get started. > 💡 If you are facing any problem, or have ideas to help improve this project, please [create an issue](https://github.com/vemonet/nanopub-rs/issues) on GitHub. diff --git a/lib/docs/use_python.md b/lib/docs/use_python.md index 1f0ded5..28fd8ba 100644 --- a/lib/docs/use_python.md +++ b/lib/docs/use_python.md @@ -4,10 +4,6 @@ You can easily publish Nanopubs from Python. -```admonish warning title="Early stage" -This component is in a really early stage, and the below documentation might not be right. -``` - ## 📥️ Install Install the `pip` package: diff --git a/lib/src/nanopub.rs b/lib/src/nanopub.rs index 458f57e..ff4add1 100644 --- a/lib/src/nanopub.rs +++ b/lib/src/nanopub.rs @@ -52,7 +52,7 @@ pub struct Nanopub { pub orcid: String, pub published: bool, // pub info: NpInfo, - // dataset: LightDataset, + // pub dataset: LightDataset, } impl fmt::Display for Nanopub { @@ -68,6 +68,13 @@ impl fmt::Display for Nanopub { } impl Nanopub { + // // TODO: change approach to use Nanopub::new(rdf).sign()? + // pub fn new(rdf: T) -> Result { + // let mut dataset = rdf.get_dataset()?; + // let np_info = extract_np_info(&dataset, false)?; + // // Ok(Self { info: np_info, dataset, published: false }) + // } + /// Fetch a Nanopub given its URI. /// /// # Arguments diff --git a/lib/src/profile.rs b/lib/src/profile.rs index 50ecf66..7059ff4 100644 --- a/lib/src/profile.rs +++ b/lib/src/profile.rs @@ -138,17 +138,17 @@ pub fn gen_keys() -> Result<(String, String), NpError> { // Because of wasm we can't use the rand crate struct WasmRng; impl RngCore for WasmRng { - fn next_u32(&mut self) -> u32 { - impls::next_u32_via_fill(self) - } - fn next_u64(&mut self) -> u64 { - impls::next_u64_via_fill(self) - } fn fill_bytes(&mut self, dest: &mut [u8]) { getrandom(dest).expect("Error generating random bytes"); } fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> { getrandom(dest).map_err(rand_core::Error::new) } + fn next_u32(&mut self) -> u32 { + impls::next_u32_via_fill(self) + } + fn next_u64(&mut self) -> u64 { + impls::next_u64_via_fill(self) + } } impl CryptoRng for WasmRng {} diff --git a/lib/src/publish.rs b/lib/src/publish.rs index 82d46b8..5b27286 100644 --- a/lib/src/publish.rs +++ b/lib/src/publish.rs @@ -15,10 +15,7 @@ pub async fn publish_np(server: &str, np: &str) -> Result { reqwest::StatusCode::CREATED => Ok(true), _ => { // Get the error message from the response body - let error_msg = res - .text() - .await - .unwrap_or_else(|_| "Unknown error while publishing the nanopub".to_string()); + let error_msg = res.text().await?; Err(NpError(error_msg)) } } diff --git a/lib/tests/nanopub_test.rs b/lib/tests/nanopub_test.rs index ad43bc2..c9ec4f0 100644 --- a/lib/tests/nanopub_test.rs +++ b/lib/tests/nanopub_test.rs @@ -1,7 +1,9 @@ use nanopub::{ + constants::TEST_SERVER, extract::extract_np_info, get_np_server, profile::gen_keys, + publish::publish_np, sign::normalize_dataset, utils::{ns, parse_rdf}, Nanopub, NpProfile, @@ -50,7 +52,8 @@ fn sign_nanopub_blank() -> Result<(), Box> { &get_test_key(), None, )?; - println!("{}", profile); // required for coverage + println!("{}", profile); // cov + let _pubkey = profile.get_public_key(); // cov let np = Nanopub::sign(&np_rdf, &profile)?; assert!(!np.published); Ok(()) @@ -60,7 +63,7 @@ fn sign_nanopub_blank() -> Result<(), Box> { fn check_valid_unsigned() -> Result<(), Box> { let np_rdf = fs::read_to_string("./tests/resources/simple1-rsa.trig")?; let np = Nanopub::check(&np_rdf); - assert!(!np.is_err()); + assert!(np.is_ok()); Ok(()) } @@ -181,3 +184,10 @@ fn test_gen_keys() -> Result<(), Box> { assert!(privkey.len() > 10); Ok(()) } + +#[tokio::test] +async fn unit_publish_np_fail() -> Result<(), Box> { + let res = publish_np(TEST_SERVER, "wrong").await; + assert!(res.is_err()); + Ok(()) +}