Skip to content

Commit

Permalink
aeweb schema is more strict (additionalProperties: false)
Browse files Browse the repository at this point in the history
use subschemas
add a few tests
  • Loading branch information
bchamagne authored and Neylix committed Jul 21, 2023
1 parent 23b2ffe commit 905c510
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 4 deletions.
20 changes: 16 additions & 4 deletions priv/json-schemas/aeweb.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,25 @@
"$schema": "http://json-schema.org/draft-07/schema#",
"oneOf": [
{
"$ref": "#/$defs/dataTx"
},
{
"$ref": "#/$defs/unpublishedReferenceTx"
},
{
"$ref": "#/$defs/publishedReferenceTx"
}
],
"$defs": {
"dataTx": {
"type": "object",
"description": "Data tx",
"minProperties": 1,
"additionalProperties": {
"type": "string"
}
},
{
"unpublishedReferenceTx": {
"type": "object",
"description": "Reference tx of an unpublished website",
"properties": {
Expand All @@ -31,7 +42,7 @@
],
"additionalProperties": false
},
{
"publishedReferenceTx": {
"type": "object",
"description": "Reference tx of a published website",
"properties": {
Expand Down Expand Up @@ -95,7 +106,8 @@
"required": [
"aewebVersion",
"metaData"
]
],
"additionalProperties": false
}
]
}
}
91 changes: 91 additions & 0 deletions test/archethic/mining/pending_transaction_validation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,45 @@ defmodule Archethic.Mining.PendingTransactionValidationTest do
assert :ok = PendingTransactionValidation.validate(tx, DateTime.utc_now())
end

test "should return :ok when we deploy a aeweb ref transaction with publicationStatus" do
tx =
Transaction.new(:hosting, %TransactionData{
content:
Jason.encode!(%{
"aewebVersion" => 1,
"publicationStatus" => "PUBLISHED",
"metaData" => %{
"index.html" => %{
"encoding" => "gzip",
"hash" => "abcd123",
"size" => 144,
"addresses" => [
Crypto.derive_keypair("seed", 0)
|> elem(0)
|> Crypto.derive_address()
|> Base.encode16()
]
}
}
})
})

assert :ok = PendingTransactionValidation.validate(tx, DateTime.utc_now())
end

test "should return :ok when we deploy a aeweb ref transaction (unpublished)" do
tx =
Transaction.new(:hosting, %TransactionData{
content:
Jason.encode!(%{
"aewebVersion" => 1,
"publicationStatus" => "UNPUBLISHED"
})
})

assert :ok = PendingTransactionValidation.validate(tx, DateTime.utc_now())
end

test "should return :ok when we deploy a aeweb file transaction" do
tx =
Transaction.new(:hosting, %TransactionData{
Expand Down Expand Up @@ -320,6 +359,58 @@ defmodule Archethic.Mining.PendingTransactionValidationTest do

assert {:error, _reason} = PendingTransactionValidation.validate(tx, DateTime.utc_now())
end

test "should return :error when we deploy a wrong aeweb ref transaction (unpublished)" do
tx =
Transaction.new(:hosting, %TransactionData{
content:
Jason.encode!(%{
"aewebVersion" => 1,
"publicationStatus" => "UNPUBLISHED",
"metaData" => %{
"index.html" => %{
"encoding" => "gzip",
"hash" => "abcd123",
"size" => 144,
"addresses" => [
Crypto.derive_keypair("seed", 0)
|> elem(0)
|> Crypto.derive_address()
|> Base.encode16()
]
}
}
})
})

assert {:error, _error} = PendingTransactionValidation.validate(tx, DateTime.utc_now())
end

test "should return :error when it does not respect the schema" do
tx =
Transaction.new(:hosting, %TransactionData{
content:
Jason.encode!(%{
"aewebVersion" => 1,
"hello" => "world",
"metaData" => %{
"index.html" => %{
"encoding" => "gzip",
"hash" => "abcd123",
"size" => 144,
"addresses" => [
Crypto.derive_keypair("seed", 0)
|> elem(0)
|> Crypto.derive_address()
|> Base.encode16()
]
}
}
})
})

assert {:error, _reason} = PendingTransactionValidation.validate(tx, DateTime.utc_now())
end
end

describe "Node" do
Expand Down

0 comments on commit 905c510

Please sign in to comment.