Skip to content

Commit

Permalink
fix(cat-gateway): Fix document PUT endpoint (#1788)
Browse files Browse the repository at this point in the history
* fix put endpoint, handle properly error

* cleanup dead_code lint

* add comment

* wip

* fix spelling

* fix

---------

Co-authored-by: bkioshn <35752733+bkioshn@users.noreply.github.com>
  • Loading branch information
Mr-Leshiy and bkioshn authored Feb 5, 2025
1 parent 9f55fce commit 2784783
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 19 deletions.
16 changes: 6 additions & 10 deletions catalyst-gateway/bin/src/db/event/signed_docs/full_signed_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ pub(crate) const SELECT_SIGNED_DOCS_TEMPLATE: JinjaTemplateSource = JinjaTemplat
source: include_str!("./sql/select_signed_documents.sql.jinja"),
};

/// `FullSignedDoc::store` method error.
#[derive(thiserror::Error, Debug)]
#[error("Document with the same `id` and `ver` already exists")]
pub(crate) struct StoreError;

/// Full signed doc event db struct
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct FullSignedDoc {
Expand Down Expand Up @@ -44,12 +49,6 @@ impl FullSignedDoc {
self.body.ver()
}

/// Returns the document author.
#[allow(dead_code)]
pub(crate) fn authors(&self) -> &Vec<String> {
self.body.authors()
}

/// Returns the document metadata.
#[allow(dead_code)]
pub(crate) fn metadata(&self) -> Option<&serde_json::Value> {
Expand Down Expand Up @@ -87,10 +86,7 @@ impl FullSignedDoc {
pub(crate) async fn store(&self) -> anyhow::Result<bool> {
match Self::retrieve(self.id(), Some(self.ver())).await {
Ok(res_doc) => {
anyhow::ensure!(
&res_doc == self,
"Document with the same `id` and `ver` already exists"
);
anyhow::ensure!(&res_doc == self, StoreError);
Ok(false)
},
Err(err) if err.is::<NotFoundError>() => {
Expand Down
2 changes: 1 addition & 1 deletion catalyst-gateway/bin/src/db/event/signed_docs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod signed_doc_body;
mod tests;

pub(crate) use doc_ref::DocumentRef;
pub(crate) use full_signed_doc::{FullSignedDoc, SELECT_SIGNED_DOCS_TEMPLATE};
pub(crate) use full_signed_doc::{FullSignedDoc, StoreError, SELECT_SIGNED_DOCS_TEMPLATE};
pub(crate) use query_filter::DocsQueryFilter;
pub(crate) use signed_doc_body::{
SignedDocBody, FILTERED_COUNT_SIGNED_DOCS_TEMPLATE, FILTERED_SELECT_SIGNED_DOCS_TEMPLATE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ impl SignedDocBody {
&self.doc_type
}

/// Returns the document authors.
pub(crate) fn authors(&self) -> &Vec<String> {
&self.authors
}

/// Returns the document metadata.
pub(crate) fn metadata(&self) -> Option<&serde_json::Value> {
self.metadata.as_ref()
Expand Down Expand Up @@ -87,7 +82,6 @@ impl SignedDocBody {
}

/// Loads a async stream of `SignedDocBody` from the event db.
#[allow(dead_code)]
pub(crate) async fn retrieve(
conditions: &DocsQueryFilter, query_limits: &QueryLimits,
) -> anyhow::Result<impl Stream<Item = anyhow::Result<Self>>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use catalyst_signed_doc::CatalystSignedDocument;
use poem_openapi::{payload::Json, ApiResponse};

use crate::{
db::event::signed_docs::{FullSignedDoc, SignedDocBody},
db::event::signed_docs::{FullSignedDoc, SignedDocBody, StoreError},
service::common::responses::WithErrorResponses,
};

Expand Down Expand Up @@ -91,6 +91,13 @@ pub(crate) async fn endpoint(doc_bytes: Vec<u8>) -> AllResponses {
{
Ok(true) => Responses::Created.into(),
Ok(false) => Responses::NoContent.into(),
Err(err) if err.is::<StoreError>() => {
Responses::BadRequest(Json(PutDocumentBadRequest::new(
"Document with the same `id` and `ver` already exists",
None,
)))
.into()
},
Err(err) => AllResponses::handle_error(&err),
}
},
Expand Down
37 changes: 36 additions & 1 deletion catalyst-gateway/tests/api_tests/api_tests/signed_document.hurl
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# document data generated using this cli tool https://github.com/input-output-hk/catalyst-libs/tree/main/rust/signed_doc

PUT http://localhost:3030/api/draft/document
# cspell: disable
Authorization: Bearer catv1.UJm5ZNT1n7l3_h3c3VXp1R9QAZStRmrxdtYwTrdsxKWIF1hAi3mqbz6dPNiICQCkoXWJs8KCpcaPuE7LE5Iu9su0ZweK_0Qr9KhBNNHrDMCh79-fruK7WyNPYNc6FrjwTPaIAQ
# cspell: enable
Content-Type: application/cbor
hex, 84585fa6012703183270436f6e74656e742d456e636f64696e676262726474797065d82550913c9265f9f944fcb3cf9d9516ae9baf626964d8255001946ea1818a7e0eb6b16169f02ffd4e63766572d8255001946ea1818a7e0eb6b16169f02ffd4ea0581c8b0b807b22616765223a32392c226e616d65223a22416c6578227d0380;
HTTP *
Expand All @@ -7,15 +12,45 @@ status >= 201
status <= 204

PUT http://localhost:3030/api/draft/document
# cspell: disable
Authorization: Bearer catv1.UJm5ZNT1n7l3_h3c3VXp1R9QAZStRmrxdtYwTrdsxKWIF1hAi3mqbz6dPNiICQCkoXWJs8KCpcaPuE7LE5Iu9su0ZweK_0Qr9KhBNNHrDMCh79-fruK7WyNPYNc6FrjwTPaIAQ
# cspell: enable
Content-Type: application/cbor
hex, 84585fa6012703183270436f6e74656e742d456e636f64696e676262726474797065d82550913c9265f9f944fcb3cf9d9516ae9baf626964d8255001946ea1818a7e0eb6b16169f02ffd4e63766572d8255001946ea1818a7e0eb6b16169f02ffd4ea0581c8b0b807b22616765223a32392c226e616d65223a22416c6578227d0380;
HTTP 204

GET http://localhost:3030/api/draft/document/01946ea1-818a-7e0e-b6b1-6169f02ffd4e
# cspell: disable
Authorization: Bearer catv1.UJm5ZNT1n7l3_h3c3VXp1R9QAZStRmrxdtYwTrdsxKWIF1hAi3mqbz6dPNiICQCkoXWJs8KCpcaPuE7LE5Iu9su0ZweK_0Qr9KhBNNHrDMCh79-fruK7WyNPYNc6FrjwTPaIAQ
# cspell: enable
HTTP 200

POST http://localhost:3030/api/draft/document/index
# cspell: disable
Authorization: Bearer catv1.UJm5ZNT1n7l3_h3c3VXp1R9QAZStRmrxdtYwTrdsxKWIF1hAi3mqbz6dPNiICQCkoXWJs8KCpcaPuE7LE5Iu9su0ZweK_0Qr9KhBNNHrDMCh79-fruK7WyNPYNc6FrjwTPaIAQ
# cspell: enable
{
"id": { "eq": "01946ea1-818a-7e0e-b6b1-6169f02ffd4e" }
}
HTTP 200
HTTP 200

# try to publish document with the same id, but with the different content
PUT http://localhost:3030/api/draft/document
# cspell: disable
Authorization: Bearer catv1.UJm5ZNT1n7l3_h3c3VXp1R9QAZStRmrxdtYwTrdsxKWIF1hAi3mqbz6dPNiICQCkoXWJs8KCpcaPuE7LE5Iu9su0ZweK_0Qr9KhBNNHrDMCh79-fruK7WyNPYNc6FrjwTPaIAQ
# cspell: enable
Content-Type: application/cbor
hex, 84585fa6012703183270436f6e74656e742d456e636f64696e676262726474797065d82550913c9265f9f944fcb3cf9d9516ae9baf626964d8255001946ea1818a7e0eb6b16169f02ffd4e63766572d8255001946ea1818a7e0eb6b16169f02ffd4ea0581c8b0b807b22616765223a33302c226e616d65223a22416c6578227d0380;
HTTP 400

# publish document with the same id, but with different version
PUT http://localhost:3030/api/draft/document
# cspell: disable
Authorization: Bearer catv1.UJm5ZNT1n7l3_h3c3VXp1R9QAZStRmrxdtYwTrdsxKWIF1hAi3mqbz6dPNiICQCkoXWJs8KCpcaPuE7LE5Iu9su0ZweK_0Qr9KhBNNHrDMCh79-fruK7WyNPYNc6FrjwTPaIAQ
# cspell: enable
Content-Type: application/cbor
hex, 84585fa6012703183270436f6e74656e742d456e636f64696e676262726474797065d82550913c9265f9f944fcb3cf9d9516ae9baf626964d8255001946ea1818a7e0eb6b16169f02ffd4e63766572d825500194d649960076639ebc1613291cf25fa0581c8b0b807b22616765223a32392c226e616d65223a22416c6578227d0380;
HTTP *
[Asserts]
status >= 201
status <= 204

0 comments on commit 2784783

Please sign in to comment.