@@ -9,14 +9,16 @@ module Test.Cardano.Metadata.Validation
99 ) where
1010
1111import qualified Data.Aeson as Aeson
12+ import qualified Data.Aeson.Encode.Pretty as Aeson
13+ import Data.Aeson.QQ
14+ ( aesonQQ )
15+ import qualified Data.ByteString.Lazy as BSL
1216import Data.Foldable
1317 ( forM_ , traverse_ )
1418import Data.Int
1519 ( Int32 , Int64 , Int8 )
1620import qualified Data.List.NonEmpty as NE
1721import qualified Data.Map.Strict as M
18- import Data.Maybe
19- ( fromJust )
2022import qualified Data.Text as T
2123import Data.Validation
2224 ( Validation (Failure ) )
@@ -59,6 +61,7 @@ import Cardano.Metadata.Types.Common
5961 , seqSucc
6062 , seqZero
6163 , unSequenceNumber
64+ , unSubject
6265 )
6366import Cardano.Metadata.Validation.Rules
6467 ( ValidationError (ErrorMetadataFileBaseNameLengthBounds , ErrorMetadataFileExpectedExtension , ErrorMetadataFileNameDoesntMatchSubject , ErrorMetadataFileTooBig , ErrorMetadataPropertySequenceNumberMustBeLarger )
@@ -78,6 +81,7 @@ import Cardano.Metadata.Validation.Types
7881 , Metadata (Metadata )
7982 , invalid
8083 , metaAttestedProperties
84+ , metaSubject
8185 , metaVerifiableProperties
8286 , onMatchingAttestedProperties
8387 , valid
@@ -102,6 +106,21 @@ tests = testGroup "Validation tests"
102106 ]
103107 ]
104108
109+ -- | From a JSON value, parse some Metadata and use the length of the
110+ -- pretty encoded JSON value and parsed Subject to make a well-formed
111+ -- File.
112+ asMetadataFile :: Aeson. Value -> File Metadata
113+ asMetadataFile json =
114+ let
115+ -- Prettily print the JSON to get a realistic and consistent file size
116+ fileSize = BSL. length (Aeson. encodePretty json)
117+ metadata = case Aeson. fromJSON json of
118+ Aeson. Error err -> error err
119+ Aeson. Success a -> a
120+ fileName = unSubject (metaSubject metadata) <> " .json"
121+ in
122+ File metadata (fromIntegral fileSize) (T. unpack fileName)
123+
105124prop_rules_isJSONFile :: H. Property
106125prop_rules_isJSONFile = property $ do
107126 -- All files with the ".json" extension should pass
@@ -241,8 +260,7 @@ prop_rules_subjectMatchesFileName = property $ do
241260unit_sequence_number_of_unknown_property_validated :: Assertion
242261unit_sequence_number_of_unknown_property_validated = do
243262 let
244- before :: Metadata
245- before = fromJust $ Aeson. decode' [r |
263+ before = [aesonQQ |
246264 {
247265 "subject": "1234",
248266 "prop": {
@@ -251,8 +269,7 @@ unit_sequence_number_of_unknown_property_validated = do
251269 }
252270 }
253271 |]
254- after :: Metadata
255- after = fromJust $ Aeson. decode' [r |
272+ after = [aesonQQ |
256273 {
257274 "subject": "1234",
258275 "prop": {
@@ -261,7 +278,7 @@ unit_sequence_number_of_unknown_property_validated = do
261278 }
262279 }
263280 |]
264- diff = Changed (File before undefined undefined ) (File after undefined undefined )
281+ diff = Changed (asMetadataFile before) (asMetadataFile after)
265282
266283 defaultRules `apply` diff
267284 @?= (Failure (ErrorMetadataPropertySequenceNumberMustBeLarger (AttestedProperty {attestedValue = Aeson. String " hello" , attestedSignatures = [] , attestedSequenceNumber = seqFromNatural 0 }) (AttestedProperty {attestedValue = Aeson. String " goodbye" , attestedSignatures = [] , attestedSequenceNumber = seqFromNatural 0 }) (seqFromNatural 0 ) (seqFromNatural 0 ) NE. :| [] ) :: Validation (NE. NonEmpty (ValidationError () )) () )
0 commit comments